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

Fix/clean some code, minor improvements

parent 2863585b
Branches
Tags
1 merge request!20Resolve "Fix/clean some code"
Pipeline #1959 passed
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
app.versionName=0.0.18
app.versionCode=18
app.versionName=0.0.19
app.versionCode=19
Fix/clean some code, add minor improvements
Nettoyage du code, améliorations mineures
import 'package:flutter/material.dart';
import '../provider/data.dart';
import '../utils/board_utils.dart';
import '../utils/board_animate.dart';
import '../utils/board_utils.dart';
class Cell {
bool isMined = false;
......@@ -10,26 +10,23 @@ class Cell {
bool isMarked = false;
bool isExploded = false;
int minesCountAround = 0;
bool isAnimated = false;
Cell(
@required this.isMined,
);
/*
* Build widget for board cell, with interactions
*/
Container widget(Data myProvider, int row, int col) {
bool showSolution = myProvider.gameWin || myProvider.gameFail;
Color backgroundColor = this.getBackgroundColor(myProvider);
Color borderColor = this.getBorderColor(myProvider);
String imageAsset = this.getImageAssetName(myProvider.skin, showSolution);
String imageAsset = this.getImageAssetName(myProvider);
return Container(
decoration: BoxDecoration(
color: backgroundColor,
border: Border.all(
color: borderColor,
width: 1,
),
color: this.getBackgroundColor(myProvider),
border: this.getCellBorders(myProvider, row, col),
),
child: GestureDetector(
child: AnimatedSwitcher(
......@@ -44,6 +41,7 @@ class Cell {
),
),
onTap: () {
// Set mines on board after first player try
if (!myProvider.isBoardMined) {
myProvider.updateCells(BoardUtils.createBoard(myProvider, row, col));
myProvider.updateIsBoardMined(true);
......@@ -61,27 +59,31 @@ class Cell {
}
}
},
),
)
);
}
String getImageAssetName(String skin, bool showSolution) {
String imageAsset = 'assets/skins/' + skin + '_tile_unknown.png';
/*
* Compute image asset name, from skin and cell value/state
*/
String getImageAssetName(Data myProvider) {
String imageAsset = 'assets/skins/' + myProvider.skin + '_tile_unknown.png';
bool showSolution = myProvider.gameWin || myProvider.gameFail;
if (!showSolution) {
// Running game
if (this.isExplored) {
if (this.isMined) {
// Boom
imageAsset = 'assets/skins/' + skin + '_tile_mine.png';
imageAsset = 'assets/skins/' + myProvider.skin + '_tile_mine.png';
} else {
// Show mines count around
imageAsset = 'assets/skins/' + skin + '_tile_' + this.minesCountAround.toString() + '.png';
imageAsset = 'assets/skins/' + myProvider.skin + '_tile_' + this.minesCountAround.toString() + '.png';
}
} else {
if (this.isMarked) {
// Danger!
imageAsset = 'assets/skins/' + skin + '_tile_flag.png';
imageAsset = 'assets/skins/' + myProvider.skin + '_tile_flag.png';
}
}
} else {
......@@ -89,37 +91,57 @@ class Cell {
if (this.isMined) {
if (this.isExploded) {
// Mine exploded
imageAsset = 'assets/skins/' + skin + '_tile_mine.png';
imageAsset = 'assets/skins/' + myProvider.skin + '_tile_mine.png';
} else {
// Mine not found
imageAsset = 'assets/skins/' + skin + '_tile_mine_not_found.png';
imageAsset = 'assets/skins/' + myProvider.skin + '_tile_mine_not_found.png';
}
} else {
// Show all mines counts
imageAsset = 'assets/skins/' + skin + '_tile_' + this.minesCountAround.toString() + '.png';
imageAsset = 'assets/skins/' + myProvider.skin + '_tile_' + this.minesCountAround.toString() + '.png';
}
}
return imageAsset;
}
// Compute cell background color, from cell state
Color getBackgroundColor(Data myProvider) {
if (myProvider.gameWin) {
return this.isAnimated ? Colors.green[400] : Colors.green[500];
} else if (myProvider.gameFail) {
}
if (myProvider.gameFail) {
return this.isAnimated ? Colors.pink[200] : Colors.pink[400];
}
return this.isAnimated ? Colors.white : Colors.grey[200];
}
Color getBorderColor(Data myProvider) {
// Compute cell borders, from board size and cell state
Border getCellBorders(Data myProvider, int row, int col) {
Color cellBorderColor = Colors.grey[500];
double cellBorderWidth = 4;
// Reduce cell border width on big boards
int boardSize = myProvider.sizeHorizontal;
if (boardSize > 8) {
cellBorderWidth = 2;
if (boardSize > 10) {
cellBorderWidth = 1;
}
}
if (myProvider.gameWin) {
return Colors.green[700];
cellBorderColor = Colors.green[700];
} else if (myProvider.gameFail) {
return Colors.pink[300];
cellBorderColor = Colors.pink[300];
}
return Colors.grey[500];
Border borders = Border.all(
color: cellBorderColor,
width: cellBorderWidth,
);
return borders;
}
}
......@@ -7,6 +7,8 @@ import '../utils/game_utils.dart';
class Game {
static Container buildGameWidget(Data myProvider) {
bool gameIsFinished = myProvider.gameWin || myProvider.gameFail;
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
......@@ -14,7 +16,7 @@ class Game {
children: [
Board.buildGameBoard(myProvider),
SizedBox(height: 2),
(myProvider.gameWin || myProvider.gameFail)
gameIsFinished
? Game.buildEndGameMessage(myProvider)
: Board.buildToggleFlagModeLayout(myProvider)
],
......@@ -43,7 +45,7 @@ class Game {
? 'assets/icons/game_fail.png'
: ''
),
fit: BoxFit.fill,
fit: BoxFit.fill
);
return Container(
......
......@@ -25,6 +25,15 @@ class Parameters {
}
static Container buildStartGameButton(Data myProvider) {
Column decorationImage = Column(
children: [
Image(
image: AssetImage('assets/icons/game_win.png'),
fit: BoxFit.fill
),
]
);
return Container(
margin: EdgeInsets.all(2),
padding: EdgeInsets.all(2),
......@@ -34,14 +43,7 @@ class Parameters {
children: [
TableRow(
children: [
Column(
children: [
Image(
image: AssetImage('assets/skins/empty.png'),
fit: BoxFit.fill,
),
]
),
decorationImage,
Column(
children: [
FlatButton(
......@@ -55,14 +57,7 @@ class Parameters {
),
]
),
Column(
children: [
Image(
image: AssetImage('assets/skins/empty.png'),
fit: BoxFit.fill,
),
]
),
decorationImage,
],
),
]
......
......@@ -22,16 +22,16 @@ class Data extends ChangeNotifier {
// Game data
bool _assetsPreloaded = false;
bool _gameRunning = false;
bool _isBoardMined = false;
int _minesCount = 0;
bool _gameWin = false;
bool _gameFail = false;
bool _gameIsRunning = false;
bool _animationInProgress = false;
bool _reportMode = false;
int _sizeVertical = null;
int _sizeHorizontal = null;
List _cells = [];
bool _isBoardMined = false;
int _minesCount = 0;
bool _reportMode = false;
bool _gameWin = false;
bool _gameFail = false;
String get level => _level;
void updateLevel(String level) {
......@@ -97,6 +97,27 @@ class Data extends ChangeNotifier {
setParameterValue('skin', prefs.getString('skin') ?? _skinDefault);
}
bool get gameIsRunning => _gameIsRunning;
void updateGameIsRunning(bool gameIsRunning) {
_gameIsRunning = gameIsRunning;
updateGameWin(false);
updateGameFail(false);
updateReportMode(false);
notifyListeners();
}
bool get gameWin => _gameWin;
void updateGameWin(bool gameWin) {
_gameWin = gameWin;
notifyListeners();
}
bool get gameFail => _gameFail;
void updateGameFail(bool gameFail) {
_gameFail = gameFail;
notifyListeners();
}
List get cells => _cells;
void updateCells(List cells) {
_cells = cells;
......@@ -123,14 +144,6 @@ class Data extends ChangeNotifier {
_assetsPreloaded = assetsPreloaded;
}
bool get gameRunning => _gameRunning;
void updateGameRunning(bool gameRunning) {
_gameRunning = gameRunning;
updateGameWin(false);
updateGameFail(false);
updateReportMode(false);
notifyListeners();
}
bool get isBoardMined => _isBoardMined;
void updateIsBoardMined(bool isBoardMined) {
......@@ -143,17 +156,6 @@ class Data extends ChangeNotifier {
_minesCount = minesCount;
}
bool get gameWin => _gameWin;
void updateGameWin(bool gameWin) {
_gameWin = gameWin;
notifyListeners();
}
bool get gameFail => _gameFail;
void updateGameFail(bool gameFail) {
_gameFail = gameFail;
notifyListeners();
}
bool get reportMode => _reportMode;
void updateReportMode(bool reportMode) {
......
......@@ -79,7 +79,7 @@ class _HomeState extends State<Home> {
List<Widget> menuActions = [];
if (myProvider.gameRunning) {
if (myProvider.gameIsRunning) {
menuActions = [
FlatButton(
child: Container(
......@@ -107,7 +107,7 @@ class _HomeState extends State<Home> {
),
body: SafeArea(
child: Center(
child: myProvider.gameRunning
child: myProvider.gameIsRunning
? Game.buildGameWidget(myProvider)
: Parameters.buildParametersSelector(myProvider)
),
......
import 'dart:async';
import 'dart:math';
import '../entities/cell.dart';
import '../provider/data.dart';
class BoardAnimate {
......
......@@ -5,14 +5,14 @@ import '../utils/board_utils.dart';
class GameUtils {
static void resetGame(Data myProvider) {
myProvider.updateGameRunning(false);
myProvider.updateGameIsRunning(false);
}
static void startGame(Data myProvider) {
print('Starting game: ' + myProvider.size + ' - ' + myProvider.level);
myProvider.updateSize(myProvider.size);
myProvider.updateMinesCount(BoardUtils.getMinesCount(myProvider.sizeHorizontal, myProvider.sizeVertical, myProvider.level));
myProvider.updateGameRunning(true);
myProvider.updateGameIsRunning(true);
BoardUtils.createInitialEmptyBoard(myProvider);
BoardAnimate.startAnimation(myProvider, 'start');
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment