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
This commit is part of merge request !14. Comments created here will be created in the context of that merge request.
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
app.versionName=0.0.10
app.versionCode=10
app.versionName=0.0.11
app.versionCode=11
......@@ -6,8 +6,8 @@ import '../provider/data.dart';
class Tile {
final Image image;
final double size;
final int currentCol;
final int currentRow;
int currentCol;
int currentRow;
final int originalCol;
final int originalRow;
......@@ -72,7 +72,6 @@ class Tile {
);
},
onAccept: (List<int> data) {
print(data.toString());
myProvider.swapTiles(
[this.currentCol, this.currentRow],
data
......
......@@ -58,6 +58,14 @@ class Data extends ChangeNotifier {
_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;
notifyListeners();
}
......
import 'dart:typed_data';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
......@@ -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 {
String imageAsset = getImageAssetName(myProvider.image);
Uint8List imageData = (await rootBundle.load(imageAsset))
......@@ -176,14 +201,27 @@ class Home extends StatelessWidget {
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) {
List tiles = myProvider.tiles;
Color borderColor = _checkTilesetIsOrdered(tiles) ? Colors.green : Colors.orange;
int tileIndex = 0;
Table tileset = Table(
......@@ -204,14 +242,14 @@ class Home extends StatelessWidget {
);
return Container(
margin: EdgeInsets.all(4),
padding: EdgeInsets.all(4),
margin: EdgeInsets.all(8),
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(4),
color: borderColor,
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Colors.green,
width: 4,
color: borderColor,
width: 8,
),
),
child: tileset,
......@@ -226,7 +264,7 @@ class Home extends StatelessWidget {
color: Colors.blue,
borderRadius: BorderRadius.circular(4),
border: Border.all(
color: Colors.green,
color: Colors.blue,
width: 4,
),
),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment