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

Add abstraction to allow other games

parent a994b401
No related branches found
No related tags found
1 merge request!32Resolve "Add abstraction to allow other games"
Pipeline #1760 passed
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
app.versionName=0.0.35
app.versionCode=35
app.versionName=0.0.36
app.versionCode=36
Add abstraction to allow other games implementation.
Ajout d'abstraction pour permettre l'implémentation d'autres jeux.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'tile.dart';
import '../provider/data.dart';
class MovingTile extends Tile {
int currentCol;
int currentRow;
MovingTile(
image,
size,
originalCol,
originalRow,
this.currentCol,
this.currentRow
): super(
image,
size,
originalCol,
originalRow
);
Container _tileWidget() {
return Container(
decoration: BoxDecoration(
color: Colors.black,
border: Border.all(
color: Colors.black,
width: 1,
),
),
child: Image(
image: this.image.image,
width: this.size,
height: this.size,
fit: BoxFit.fill
),
);
}
Container widget(Data myProvider) {
return Container(
child: DragTarget<List<int>>(
builder: (
BuildContext context,
List<dynamic> accepted,
List<dynamic> rejected,
) {
return Container(
height: this.size,
width: this.size,
color: Colors.cyan,
child: Draggable<List<int>>(
data: [
this.currentCol,
this.currentRow,
],
// Widget when draggable is stationary
child: this._tileWidget(),
// Widget when draggable is being dragged
feedback: this._tileWidget(),
// Widget to display on original place when being dragged
childWhenDragging: Container(
height: this.size,
width: this.size,
color: Colors.pinkAccent,
),
),
);
},
onAccept: (List<int> data) {
myProvider.swapTiles(
[this.currentCol, this.currentRow],
data
);
},
)
);
}
bool isCorrect() {
return (
(this.currentRow == this.originalRow)
&&
(this.currentCol == this.originalCol)
);
}
}
......@@ -3,81 +3,22 @@ import 'package:provider/provider.dart';
import '../provider/data.dart';
class Tile {
class Tile{
final Image image;
final double size;
int currentCol;
int currentRow;
final int originalCol;
final int originalRow;
Tile(
@required this.image,
@required this.size,
@required this.currentCol,
@required this.currentRow,
@required this.originalCol,
@required this.originalRow,
);
Container _tileWidget() {
return Container(
decoration: BoxDecoration(
color: Colors.black,
border: Border.all(
color: Colors.black,
width: 1,
),
),
child: Image(
image: this.image.image,
width: this.size,
height: this.size,
fit: BoxFit.fill
),
);
}
Container widget(Data myProvider) {
return Container(
child: DragTarget<List<int>>(
builder: (
BuildContext context,
List<dynamic> accepted,
List<dynamic> rejected,
) {
return Container(
height: this.size,
width: this.size,
color: Colors.cyan,
child: Draggable<List<int>>(
data: [
this.currentCol,
this.currentRow,
],
// Widget when draggable is stationary
child: this._tileWidget(),
// Widget when draggable is being dragged
feedback: this._tileWidget(),
// Widget to display on original place when being dragged
childWhenDragging: Container(
height: this.size,
width: this.size,
color: Colors.pinkAccent,
),
),
);
},
onAccept: (List<int> data) {
myProvider.swapTiles(
[this.currentCol, this.currentRow],
data
);
},
)
);
return Container();
}
bool isCorrect() {}
}
import 'package:flutter/foundation.dart';
import '../entities/tile.dart';
import '../entities/moving_tile.dart';
class Data extends ChangeNotifier {
......@@ -62,7 +62,7 @@ class Data extends ChangeNotifier {
int indexTile1 = _tiles.indexWhere((tile) => ((tile.currentCol == tile1[0]) && (tile.currentRow == tile1[1])));
int indexTile2 = _tiles.indexWhere((tile) => ((tile.currentCol == tile2[0]) && (tile.currentRow == tile2[1])));
Tile swap = _tiles[indexTile1];
MovingTile swap = _tiles[indexTile1];
_tiles[indexTile1] = _tiles[indexTile2];
_tiles[indexTile2] = swap;
......
......@@ -8,6 +8,7 @@ import 'package:image/image.dart' as imglib;
import 'package:provider/provider.dart';
import '../entities/tile.dart';
import '../entities/moving_tile.dart';
import '../provider/data.dart';
import '../utils/get_images_list.dart';
......@@ -141,7 +142,7 @@ class Home extends StatelessWidget {
);
}
List<Tile> shuffleTiles(List<Tile> tiles) {
List<MovingTile> shuffleMovingTiles(List<MovingTile> tiles) {
var random = new Random();
int tilesCount = tiles.length;
......@@ -149,7 +150,7 @@ class Home extends StatelessWidget {
int indexTile1 = random.nextInt(tilesCount);
int indexTile2 = random.nextInt(tilesCount);
Tile swap = tiles[indexTile1];
MovingTile swap = tiles[indexTile1];
tiles[indexTile1] = tiles[indexTile2];
tiles[indexTile2] = swap;
......@@ -176,11 +177,11 @@ class Home extends StatelessWidget {
int width = (image.width / myProvider.tilesCount).round();
int height = (image.height / myProvider.tilesCount).round();
List<Tile> tiles = List<Tile>();
List<MovingTile> tiles = List<MovingTile>();
for (int i = 0; i < myProvider.tilesCount; i++) {
for (int j = 0; j < myProvider.tilesCount; j++) {
tiles.add(
Tile(
MovingTile(
Image.memory(
imglib.encodeJpg(
imglib.copyCrop(image, x, y, width, height)
......@@ -198,28 +199,23 @@ class Home extends StatelessWidget {
y += height;
}
myProvider.updateTiles = shuffleTiles(tiles);
myProvider.updateTiles = shuffleMovingTiles(tiles);
myProvider.updateIsShufflingBoard = false;
}
bool _checkTilesetIsOrdered(List<Tile> tiles) {
bool _checkTilesetIsCleared(List<Tile> tiles) {
for (Tile tile in tiles) {
if (
(tile.currentRow != tile.originalRow)
||
(tile.currentCol != tile.originalCol)
) {
if (!tile.isCorrect()) {
return false;
}
}
return true;
}
Container _buildTilesetWidget(Data myProvider) {
List tiles = myProvider.tiles;
Color borderColor = _checkTilesetIsOrdered(tiles) ? Colors.green : Colors.orange;
Color borderColor = _checkTilesetIsCleared(tiles) ? Colors.green : Colors.orange;
int tileIndex = 0;
Table tileset = Table(
......@@ -292,7 +288,7 @@ class Home extends StatelessWidget {
children: [
_buildTilesetWidget(myProvider),
SizedBox(height: 20),
_checkTilesetIsOrdered(myProvider.tiles) ? _buildWinMessage(myProvider) : _buildTipWidget(myProvider),
_checkTilesetIsCleared(myProvider.tiles) ? _buildWinMessage(myProvider) : _buildTipWidget(myProvider),
],
),
);
......
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