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

Finalize gameplay

parent b965e1de
No related branches found
No related tags found
1 merge request!5Resolve "Finalize gameplay"
Pipeline #1794 passed
......@@ -5,14 +5,16 @@ import '../provider/data.dart';
class Board {
static Container buildGameBoard(Data myProvider) {
Color borderColor = myProvider.reportMode ? Colors.blue : Colors.black;
return Container(
margin: EdgeInsets.all(2),
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
color: Colors.black,
color: borderColor,
borderRadius: BorderRadius.circular(2),
border: Border.all(
color: Colors.black,
color: borderColor,
width: 2,
),
),
......@@ -41,4 +43,40 @@ class Board {
);
}
static FlatButton buildToggleFlagModeButton(Data myProvider) {
String reportModeSuffix = myProvider.reportMode ? 'on' : 'off';
return FlatButton(
child: Container(
child: Image(
image: AssetImage('assets/skins/' + myProvider.skin + '_button_mark_mine_' + reportModeSuffix + '.png'),
fit: BoxFit.fill,
),
),
onPressed: () => myProvider.updateReportMode(!myProvider.reportMode),
);
}
static Table buildToggleFlagModeLayout(Data myProvider) {
Image paddingBlock = Image(
image: AssetImage('assets/skins/empty.png'),
fit: BoxFit.fill,
);
return Table(
defaultColumnWidth: IntrinsicColumnWidth(),
children: [
TableRow(
children: [
TableCell(child: paddingBlock),
TableCell(child: paddingBlock),
TableCell(child: buildToggleFlagModeButton(myProvider)),
TableCell(child: paddingBlock),
TableCell(child: paddingBlock),
],
),
],
);
}
}
......@@ -14,7 +14,9 @@ class Game {
children: [
Board.buildGameBoard(myProvider),
SizedBox(height: 2),
(myProvider.gameWin || myProvider.gameFail) ? Game.buildEndGameMessage(myProvider) : SizedBox(height: 2),
(myProvider.gameWin || myProvider.gameFail)
? Game.buildEndGameMessage(myProvider)
: Board.buildToggleFlagModeLayout(myProvider)
],
),
);
......
......@@ -15,9 +15,10 @@ class Data extends ChangeNotifier {
String _skin = 'default';
// Game data
bool _stateRunning = false;
bool _gameRunning = false;
bool _gameWin = false;
bool _gameFail = false;
bool _reportMode = false;
int _sizeVertical = null;
int _sizeHorizontal = null;
List _cells = [];
......@@ -79,12 +80,22 @@ class Data extends ChangeNotifier {
void setCellAsExplored(int row, int col) {
_cells[row][col].isExplored = true;
if (_cells[row][col].isMined) {
_cells[row][col].isExploded = true;
};
notifyListeners();
}
void toggleCellMark(int row, int col) {
_cells[row][col].isMarked = !_cells[row][col].isMarked;
updateReportMode(false);
notifyListeners();
}
bool get stateRunning => _stateRunning;
void updateStateRunning(bool stateRunning) {
_stateRunning = stateRunning;
bool get gameRunning => _gameRunning;
void updateGameRunning(bool gameRunning) {
_gameRunning = gameRunning;
updateGameWin(false);
updateGameFail(false);
notifyListeners();
......@@ -92,15 +103,19 @@ class Data extends ChangeNotifier {
bool get gameWin => _gameWin;
void updateGameWin(bool gameWin) {
print('updateGameWin: ' + (gameWin ? 'true' : 'false'));
_gameWin = gameWin;
notifyListeners();
}
bool get gameFail => _gameFail;
void updateGameFail(bool gameFail) {
print('updateGameFail: ' + (gameFail ? 'true' : 'false'));
_gameFail = gameFail;
notifyListeners();
}
bool get reportMode => _reportMode;
void updateReportMode(bool reportMode) {
_reportMode = reportMode;
notifyListeners();
}
}
......@@ -15,7 +15,7 @@ class Home extends StatelessWidget {
List<Widget> menuActions = [];
if (myProvider.stateRunning) {
if (myProvider.gameRunning) {
menuActions = [
FlatButton(
child: Container(
......@@ -43,7 +43,7 @@ class Home extends StatelessWidget {
),
body: SafeArea(
child: Center(
child: myProvider.stateRunning
child: myProvider.gameRunning
? Game.buildGameWidget(myProvider)
: Parameters.buildParametersSelector(myProvider)
),
......
......@@ -5,15 +5,20 @@ class BoardUtils {
static printGrid(List cells) {
print('');
print('-------');
String line = '--';
for (var i = 0; i < cells[0].length; i++) {
line += '-';
}
print(line);
for (var rowIndex = 0; rowIndex < cells.length; rowIndex++) {
String row = '';
String row = '|';
for (var colIndex = 0; colIndex < cells[rowIndex].length; colIndex++) {
row += cells[rowIndex][colIndex].isMined ? 'X' : '.';
}
row += '|';
print(row);
}
print('-------');
print(line);
print('');
}
......@@ -89,8 +94,13 @@ class BoardUtils {
return cells;
}
static void reportCell(Data myProvider, int row, int col) {
if (!myProvider.cells[row][col].isExplored) {
myProvider.toggleCellMark(row, col);
}
}
static void walkOnCell(Data myProvider, int row, int col) {
print('Walk on cell [' + col.toString() + ',' + row.toString() + ']');
myProvider.setCellAsExplored(row, col);
if (myProvider.cells[row][col].minesCountAround == 0) {
......@@ -154,8 +164,7 @@ class BoardUtils {
return minesCountAround;
}
static bool checkBoardIsSolved(Data myProvider) {
print('checkBoardIsSolved');
static bool checkGameIsFinished(Data myProvider) {
List cells = myProvider.cells;
int sizeHorizontal = cells.length;
int sizeVertical = cells[0].length;
......@@ -166,7 +175,7 @@ class BoardUtils {
for (var row = 0; row < sizeVertical; row++) {
for (var col = 0; col < sizeHorizontal; col++) {
// Walked on a mine
if (cells[row][col].isMined == true && cells[row][col].isExplored == true) {
if (cells[row][col].isExploded == true) {
myProvider.updateGameFail(true);
return false;
}
......
......@@ -4,13 +4,13 @@ import '../utils/board_utils.dart';
class GameUtils {
static void resetGame(Data myProvider) {
myProvider.updateStateRunning(false);
myProvider.updateGameRunning(false);
}
static void startGame(Data myProvider) {
print('Starting game: ' + myProvider.size + ' - ' + myProvider.level);
myProvider.updateSize(myProvider.size);
myProvider.updateStateRunning(true);
myProvider.updateGameRunning(true);
myProvider.updateCells(
BoardUtils.createBoard(
myProvider.sizeHorizontal,
......@@ -19,4 +19,5 @@ class GameUtils {
)
);
}
}
......@@ -28,7 +28,7 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
......@@ -73,7 +73,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.0"
version: "1.7.0"
nested:
dependency: transitive
description:
......@@ -141,7 +141,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.0"
version: "0.4.1"
typed_data:
dependency: transitive
description:
......
......@@ -18,6 +18,5 @@ dev_dependencies:
flutter:
uses-material-design: true
assets:
- assets/files/
- assets/icons/
- assets/skins/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment