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

Display allowed moves count

parent 0ce6de9d
No related branches found
No related tags found
1 merge request!6Resolve "Display allowed moves count"
Pipeline #3300 passed
org.gradle.jvmargs=-Xmx1536M org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true android.useAndroidX=true
android.enableJetifier=true android.enableJetifier=true
app.versionName=0.0.5 app.versionName=0.0.6
app.versionCode=5 app.versionCode=6
Count and display allowed moves count
\ No newline at end of file
Calcule et affiche le nombre de coups possibles
\ No newline at end of file
...@@ -47,7 +47,7 @@ class Game { ...@@ -47,7 +47,7 @@ class Game {
Column( Column(
children: [ children: [
Text( Text(
myProvider.movesCount.toString(), GameUtils.countAllowedMoves(myProvider).toString(),
style: TextStyle( style: TextStyle(
fontSize: 20, fontSize: 20,
fontWeight: FontWeight.w600, fontWeight: FontWeight.w600,
......
...@@ -2,7 +2,6 @@ import 'dart:math'; ...@@ -2,7 +2,6 @@ import 'dart:math';
import 'package:solitaire_game/entities/tile.dart'; import 'package:solitaire_game/entities/tile.dart';
import 'package:solitaire_game/provider/data.dart'; import 'package:solitaire_game/provider/data.dart';
import 'package:solitaire_game/utils/game_utils.dart';
class BoardUtils { class BoardUtils {
static printGrid(List cells) { static printGrid(List cells) {
......
import 'package:solitaire_game/entities/tile.dart'; import 'package:solitaire_game/entities/tile.dart';
import 'package:solitaire_game/layout/game.dart';
import 'package:solitaire_game/provider/data.dart'; import 'package:solitaire_game/provider/data.dart';
import 'package:solitaire_game/utils/board_utils.dart'; import 'package:solitaire_game/utils/board_utils.dart';
...@@ -44,34 +43,51 @@ class GameUtils { ...@@ -44,34 +43,51 @@ class GameUtils {
} }
static bool isMoveAllowed(Data myProvider, List<int> source, List<int> target) { static bool isMoveAllowed(Data myProvider, List<int> source, List<int> target) {
// print('(test) Pick from ' + source.toString() + ' and drop on ' + target.toString());
List<List<Tile?>> board = myProvider.board; List<List<Tile?>> board = myProvider.board;
int sourceCol = source[0]; int sourceCol = source[0];
int sourceRow = source[1]; int sourceRow = source[1];
int targetCol = target[0]; int targetCol = target[0];
int targetRow = target[1]; int targetRow = target[1];
// ensure source and target are inside range
if (sourceRow < 0 ||
sourceRow > (myProvider.boardSize - 1) ||
sourceCol < 0 ||
sourceCol > (myProvider.boardSize - 1)) {
// print('move forbidden: source is out of board');
return false;
}
if (targetRow < 0 ||
targetRow > (myProvider.boardSize - 1) ||
targetCol < 0 ||
targetCol > (myProvider.boardSize - 1)) {
// print('move forbidden: target is out of board');
return false;
}
// ensure source exists and has a peg // ensure source exists and has a peg
if (board[sourceRow][sourceCol] == null || board[sourceRow][sourceCol]?.hasPeg == false) { if (board[sourceRow][sourceCol] == null || board[sourceRow][sourceCol]?.hasPeg == false) {
print('move forbidden: source peg does not exist'); // print('move forbidden: source peg does not exist');
return false; return false;
} }
// ensure target exists and is empty // ensure target exists and is empty
if (board[targetRow][targetCol] == null || board[targetRow][targetCol]?.hasPeg == true) { if (board[targetRow][targetCol] == null || board[targetRow][targetCol]?.hasPeg == true) {
print('move forbidden: target does not exist or already with a peg'); // print('move forbidden: target does not exist or already with a peg');
return false; return false;
} }
// ensure source and target are in the same line/column // ensure source and target are in the same line/column
if ((targetCol != sourceCol) && (targetRow != sourceRow)) { if ((targetCol != sourceCol) && (targetRow != sourceRow)) {
print('move forbidden: source and target are not in the same line or column'); // print('move forbidden: source and target are not in the same line or column');
return false; return false;
} }
// ensure source and target are separated by exactly one tile // ensure source and target are separated by exactly one tile
if (((targetCol == sourceCol) && ((targetRow - sourceRow).abs() != 2)) || if (((targetCol == sourceCol) && ((targetRow - sourceRow).abs() != 2)) ||
((targetRow == sourceRow) && ((targetCol - sourceCol).abs() != 2))) { ((targetRow == sourceRow) && ((targetCol - sourceCol).abs() != 2))) {
print('move forbidden: source and target must be separated by exactly one tile'); // print('move forbidden: source and target must be separated by exactly one tile');
return false; return false;
} }
...@@ -79,7 +95,7 @@ class GameUtils { ...@@ -79,7 +95,7 @@ class GameUtils {
int middleRow = (sourceRow + ((targetRow - sourceRow) / 2)).round(); int middleRow = (sourceRow + ((targetRow - sourceRow) / 2)).round();
int middleCol = (sourceCol + ((targetCol - sourceCol) / 2)).round(); int middleCol = (sourceCol + ((targetCol - sourceCol) / 2)).round();
if (board[middleRow][middleCol] == null || board[middleRow][middleCol]?.hasPeg == false) { if (board[middleRow][middleCol] == null || board[middleRow][middleCol]?.hasPeg == false) {
print('move forbidden: tile between source and target does not contain a peg'); // print('move forbidden: tile between source and target does not contain a peg');
return false; return false;
} }
...@@ -110,19 +126,45 @@ class GameUtils { ...@@ -110,19 +126,45 @@ class GameUtils {
myProvider.updateRemainingPegsCount(GameUtils.countRemainingPegs(myProvider)); myProvider.updateRemainingPegsCount(GameUtils.countRemainingPegs(myProvider));
} }
static int countRemainingPegs(Data myProvider) { static List<Tile> listRemainingPegs(Data myProvider) {
int count = 0; List<Tile> pegs = [];
List<List<Tile?>> board = myProvider.board; List<List<Tile?>> board = myProvider.board;
for (var rowIndex = 0; rowIndex < board.length; rowIndex++) { for (var rowIndex = 0; rowIndex < board.length; rowIndex++) {
for (var colIndex = 0; colIndex < board[rowIndex].length; colIndex++) { for (var colIndex = 0; colIndex < board[rowIndex].length; colIndex++) {
Tile? tile = board[rowIndex][colIndex]; Tile? tile = board[rowIndex][colIndex];
if (tile != null && tile.hasPeg == true) { if (tile != null && tile.hasPeg == true) {
count++; pegs.add(tile);
}
} }
} }
return pegs;
} }
return count; static int countRemainingPegs(Data myProvider) {
return GameUtils.listRemainingPegs(myProvider).length;
}
static int countAllowedMoves(Data myProvider) {
int allowedMovesCount = 0;
List<Tile> pegs = GameUtils.listRemainingPegs(myProvider);
pegs.forEach((tile) {
int row = tile.currentRow;
int col = tile.currentCol;
List<int> source = [col, row];
List<List<int>> targets = [
[col - 2, row],
[col + 2, row],
[col, row - 2],
[col, row + 2],
];
targets.forEach((target) {
if (GameUtils.isMoveAllowed(myProvider, source, target)) {
allowedMovesCount++;
}
});
});
return allowedMovesCount;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment