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

Merge branch '7-display-allowed-moves-count' into 'master'

Resolve "Display allowed moves count"

Closes #7

See merge request !6
parents 0ce6de9d 5a1e0604
No related branches found
No related tags found
1 merge request!6Resolve "Display allowed moves count"
Pipeline #3303 passed
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
app.versionName=0.0.5
app.versionCode=5
app.versionName=0.0.6
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 {
Column(
children: [
Text(
myProvider.movesCount.toString(),
GameUtils.countAllowedMoves(myProvider).toString(),
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
......
......@@ -2,7 +2,6 @@ import 'dart:math';
import 'package:solitaire_game/entities/tile.dart';
import 'package:solitaire_game/provider/data.dart';
import 'package:solitaire_game/utils/game_utils.dart';
class BoardUtils {
static printGrid(List cells) {
......
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/utils/board_utils.dart';
......@@ -44,34 +43,51 @@ class GameUtils {
}
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;
int sourceCol = source[0];
int sourceRow = source[1];
int targetCol = target[0];
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
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;
}
// ensure target exists and is empty
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;
}
// ensure source and target are in the same line/column
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;
}
// ensure source and target are separated by exactly one tile
if (((targetCol == sourceCol) && ((targetRow - sourceRow).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;
}
......@@ -79,7 +95,7 @@ class GameUtils {
int middleRow = (sourceRow + ((targetRow - sourceRow) / 2)).round();
int middleCol = (sourceCol + ((targetCol - sourceCol) / 2)).round();
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;
}
......@@ -110,19 +126,45 @@ class GameUtils {
myProvider.updateRemainingPegsCount(GameUtils.countRemainingPegs(myProvider));
}
static int countRemainingPegs(Data myProvider) {
int count = 0;
static List<Tile> listRemainingPegs(Data myProvider) {
List<Tile> pegs = [];
List<List<Tile?>> board = myProvider.board;
for (var rowIndex = 0; rowIndex < board.length; rowIndex++) {
for (var colIndex = 0; colIndex < board[rowIndex].length; colIndex++) {
Tile? tile = board[rowIndex][colIndex];
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