Skip to content
Snippets Groups Projects
Commit 4884ca0e authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Check tiles are correctly ordered

parent e5ac2049
No related branches found
No related tags found
1 merge request!14Resolve "Add check tiles ordering feature"
Pipeline #1249 passed
org.gradle.jvmargs=-Xmx1536M org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true android.useAndroidX=true
android.enableJetifier=true android.enableJetifier=true
app.versionName=0.0.10 app.versionName=0.0.11
app.versionCode=10 app.versionCode=11
...@@ -6,8 +6,8 @@ import '../provider/data.dart'; ...@@ -6,8 +6,8 @@ import '../provider/data.dart';
class Tile { class Tile {
final Image image; final Image image;
final double size; final double size;
final int currentCol; int currentCol;
final int currentRow; int currentRow;
final int originalCol; final int originalCol;
final int originalRow; final int originalRow;
...@@ -72,7 +72,6 @@ class Tile { ...@@ -72,7 +72,6 @@ class Tile {
); );
}, },
onAccept: (List<int> data) { onAccept: (List<int> data) {
print(data.toString());
myProvider.swapTiles( myProvider.swapTiles(
[this.currentCol, this.currentRow], [this.currentCol, this.currentRow],
data data
......
...@@ -58,6 +58,14 @@ class Data extends ChangeNotifier { ...@@ -58,6 +58,14 @@ class Data extends ChangeNotifier {
_tiles[indexTile1] = _tiles[indexTile2]; _tiles[indexTile1] = _tiles[indexTile2];
_tiles[indexTile2] = swap; _tiles[indexTile2] = swap;
int swapCol = _tiles[indexTile1].currentCol;
_tiles[indexTile1].currentCol = _tiles[indexTile2].currentCol;
_tiles[indexTile2].currentCol = swapCol;
int swapRow = _tiles[indexTile1].currentRow;
_tiles[indexTile1].currentRow = _tiles[indexTile2].currentRow;
_tiles[indexTile2].currentRow = swapRow;
notifyListeners(); notifyListeners();
} }
......
import 'dart:typed_data'; import 'dart:typed_data';
import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle; import 'package:flutter/services.dart' show rootBundle;
...@@ -143,6 +144,30 @@ class Home extends StatelessWidget { ...@@ -143,6 +144,30 @@ class Home extends StatelessWidget {
); );
} }
List<Tile> shuffleTiles(List<Tile> tiles) {
var random = new Random();
int tilesCount = tiles.length;
for (int i = 0; i < 40; i++) {
int indexTile1 = random.nextInt(tilesCount);
int indexTile2 = random.nextInt(tilesCount);
Tile swap = tiles[indexTile1];
tiles[indexTile1] = tiles[indexTile2];
tiles[indexTile2] = swap;
int swapCol = tiles[indexTile1].currentCol;
tiles[indexTile1].currentCol = tiles[indexTile2].currentCol;
tiles[indexTile2].currentCol = swapCol;
int swapRow = tiles[indexTile1].currentRow;
tiles[indexTile1].currentRow = tiles[indexTile2].currentRow;
tiles[indexTile2].currentRow = swapRow;
}
return tiles;
}
Future<List<Tile>> splitImageInTiles(Data myProvider) async { Future<List<Tile>> splitImageInTiles(Data myProvider) async {
String imageAsset = getImageAssetName(myProvider.image); String imageAsset = getImageAssetName(myProvider.image);
Uint8List imageData = (await rootBundle.load(imageAsset)) Uint8List imageData = (await rootBundle.load(imageAsset))
...@@ -176,14 +201,27 @@ class Home extends StatelessWidget { ...@@ -176,14 +201,27 @@ class Home extends StatelessWidget {
y += height; y += height;
} }
tiles.shuffle(); return shuffleTiles(tiles);
}
return tiles; bool _checkTilesetIsOrdered(List<Tile> tiles) {
for (Tile tile in tiles) {
if (
(tile.currentRow != tile.originalRow)
||
(tile.currentCol != tile.originalCol)
) {
return false;
}
}
return true;
} }
Container _buildTilesetWidget(Data myProvider) { Container _buildTilesetWidget(Data myProvider) {
List tiles = myProvider.tiles; List tiles = myProvider.tiles;
Color borderColor = _checkTilesetIsOrdered(tiles) ? Colors.green : Colors.orange;
int tileIndex = 0; int tileIndex = 0;
Table tileset = Table( Table tileset = Table(
...@@ -204,14 +242,14 @@ class Home extends StatelessWidget { ...@@ -204,14 +242,14 @@ class Home extends StatelessWidget {
); );
return Container( return Container(
margin: EdgeInsets.all(4), margin: EdgeInsets.all(8),
padding: EdgeInsets.all(4), padding: EdgeInsets.all(8),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.blue, color: borderColor,
borderRadius: BorderRadius.circular(4), borderRadius: BorderRadius.circular(8),
border: Border.all( border: Border.all(
color: Colors.green, color: borderColor,
width: 4, width: 8,
), ),
), ),
child: tileset, child: tileset,
...@@ -226,7 +264,7 @@ class Home extends StatelessWidget { ...@@ -226,7 +264,7 @@ class Home extends StatelessWidget {
color: Colors.blue, color: Colors.blue,
borderRadius: BorderRadius.circular(4), borderRadius: BorderRadius.circular(4),
border: Border.all( border: Border.all(
color: Colors.green, color: Colors.blue,
width: 4, width: 4,
), ),
), ),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment