From 4884ca0e7e554c94f846723d76b6e24fac5d8ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Harrault?= <benoit@harrault.fr> Date: Fri, 28 May 2021 14:49:38 +0200 Subject: [PATCH] Check tiles are correctly ordered --- android/gradle.properties | 4 +-- lib/entities/tile.dart | 5 ++-- lib/provider/data.dart | 8 ++++++ lib/screens/home.dart | 56 ++++++++++++++++++++++++++++++++------- 4 files changed, 59 insertions(+), 14 deletions(-) diff --git a/android/gradle.properties b/android/gradle.properties index 6bf54a6..f0be9fb 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,5 +1,5 @@ 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 diff --git a/lib/entities/tile.dart b/lib/entities/tile.dart index 2860b50..e60d059 100644 --- a/lib/entities/tile.dart +++ b/lib/entities/tile.dart @@ -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 diff --git a/lib/provider/data.dart b/lib/provider/data.dart index 3f05b5a..6980aa5 100644 --- a/lib/provider/data.dart +++ b/lib/provider/data.dart @@ -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(); } diff --git a/lib/screens/home.dart b/lib/screens/home.dart index a2fdd2f..c978488 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -1,4 +1,5 @@ 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, ), ), -- GitLab