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

Merge branch '48-solve-grid-to-improve-check-wrongs-values' into 'master'

Resolve "Solve grid to improve check wrongs values"

Closes #48

See merge request !42
parents d74b4d93 a0240023
No related branches found
No related tags found
1 merge request!42Resolve "Solve grid to improve check wrongs values"
Pipeline #1835 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.38 app.versionName=0.0.39
app.versionCode=38 app.versionCode=39
Improve tip on grid if cell with wrong value, compute solved grid on new game
Amélioration du coup de pouce en cas de cellule avec une valeur fausse, calcul de la grille résolue au début de la partie
...@@ -91,12 +91,16 @@ class Cell { ...@@ -91,12 +91,16 @@ class Cell {
Color backgroundColor = Colors.grey[200]; Color backgroundColor = Colors.grey[200];
List cells = myProvider.cells;
int blockSizeHorizontal = myProvider.blockSizeHorizontal;
int blockSizeVertical = myProvider.blockSizeVertical;
if ( if (
myProvider.showConflicts myProvider.showConflicts
&& myProvider.currentCellCol != null && myProvider.currentCellCol != null
&& myProvider.currentCellRow != null && myProvider.currentCellRow != null
) { ) {
if (!BoardUtils.isValueAllowed(myProvider, myProvider.currentCellCol, myProvider.currentCellRow, this.value)) { if (!BoardUtils.isValueAllowed(cells, blockSizeHorizontal, blockSizeVertical, myProvider.currentCellCol, myProvider.currentCellRow, this.value)) {
backgroundColor = Colors.pink[100]; backgroundColor = Colors.pink[100];
} }
} }
......
...@@ -22,6 +22,7 @@ class Data extends ChangeNotifier { ...@@ -22,6 +22,7 @@ class Data extends ChangeNotifier {
int _blockSizeVertical = null; int _blockSizeVertical = null;
int _blockSizeHorizontal = null; int _blockSizeHorizontal = null;
List _cells = []; List _cells = [];
List _cellsSolved = [];
int _currentCellCol = null; int _currentCellCol = null;
int _currentCellRow = null; int _currentCellRow = null;
int _currentCellValue = null; int _currentCellValue = null;
...@@ -94,6 +95,11 @@ class Data extends ChangeNotifier { ...@@ -94,6 +95,11 @@ class Data extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
List get cellsSolved => _cellsSolved;
set updateCellsSolved(List cells) {
_cellsSolved = cells;
}
int get currentCellCol => _currentCellCol; int get currentCellCol => _currentCellCol;
set updateCurrentCellCol(int currentCellCol) { set updateCurrentCellCol(int currentCellCol) {
_currentCellCol = currentCellCol; _currentCellCol = currentCellCol;
......
...@@ -6,16 +6,18 @@ import '../provider/data.dart'; ...@@ -6,16 +6,18 @@ import '../provider/data.dart';
class BoardUtils { class BoardUtils {
static printGrid(List cells) { static printGrid(List cells, List solvedCells) {
String stringValues = '0123456789ABCDEFG'; String stringValues = '0123456789ABCDEFG';
print(''); print('');
print('-------'); print('-------');
for (var rowIndex = 0; rowIndex < cells.length; rowIndex++) { for (var rowIndex = 0; rowIndex < cells.length; rowIndex++) {
String row = ''; String row = '';
String rowSolved = '';
for (var colIndex = 0; colIndex < cells[rowIndex].length; colIndex++) { for (var colIndex = 0; colIndex < cells[rowIndex].length; colIndex++) {
row += stringValues[cells[rowIndex][colIndex].value]; row += stringValues[cells[rowIndex][colIndex].value];
rowSolved += stringValues[solvedCells[rowIndex][colIndex].value];
} }
print(row); print(row + ' | ' + rowSolved);
} }
print('-------'); print('-------');
print(''); print('');
...@@ -40,13 +42,14 @@ class BoardUtils { ...@@ -40,13 +42,14 @@ class BoardUtils {
print('Picked grid from template: ' + grid); print('Picked grid from template: ' + grid);
bool isSymetric = (blockSizeHorizontal == blockSizeVertical); bool isSymetric = (blockSizeHorizontal == blockSizeVertical);
myProvider.updateCells = BoardUtils.createBoardFromTemplate(grid, isSymetric); myProvider.updateCells = BoardUtils.createBoardFromTemplate(grid, isSymetric);
myProvider.updateCellsSolved = BoardUtils.getSolvedGrid(myProvider);
myProvider.selectCell(null, null); myProvider.selectCell(null, null);
printGrid(myProvider.cells, myProvider.cellsSolved);
} }
} }
static List createEmptyBoard(int boardSize) { static List createEmptyBoard(int boardSize) {
int index = 0;
List cells = []; List cells = [];
for (var rowIndex = 0; rowIndex < boardSize; rowIndex++) { for (var rowIndex = 0; rowIndex < boardSize; rowIndex++) {
List row = []; List row = [];
...@@ -59,6 +62,18 @@ class BoardUtils { ...@@ -59,6 +62,18 @@ class BoardUtils {
return cells; return cells;
} }
static List copyBoard(List cells) {
List copiedGrid = [];
for (var rowIndex = 0; rowIndex < cells.length; rowIndex++) {
List row = [];
for (var colIndex = 0; colIndex < cells[rowIndex].length; colIndex++) {
row.add(Cell(cells[rowIndex][colIndex].value, false));
}
copiedGrid.add(row);
}
return copiedGrid;
}
static List createBoardFromTemplate(String grid, bool isSymetric) { static List createBoardFromTemplate(String grid, bool isSymetric) {
List cells = []; List cells = [];
...@@ -145,12 +160,9 @@ class BoardUtils { ...@@ -145,12 +160,9 @@ class BoardUtils {
} }
} }
printGrid(cells);
return cells; return cells;
} }
static bool checkBoardIsSolved(Data myProvider) { static bool checkBoardIsSolved(Data myProvider) {
List cells = myProvider.cells; List cells = myProvider.cells;
...@@ -178,16 +190,11 @@ class BoardUtils { ...@@ -178,16 +190,11 @@ class BoardUtils {
return true; return true;
} }
static bool isValueAllowed(Data myProvider, int candidateCol, int candidateRow, int candidateValue) { static bool isValueAllowed(List cells, int blockSizeHorizontal, int blockSizeVertical, int candidateCol, int candidateRow, int candidateValue) {
if (candidateValue == 0) { if (candidateValue == 0) {
return true; return true;
} }
List cells = myProvider.cells;
int blockSizeHorizontal = myProvider.blockSizeHorizontal;
int blockSizeVertical = myProvider.blockSizeVertical;
int boardSize = blockSizeHorizontal * blockSizeVertical; int boardSize = blockSizeHorizontal * blockSizeVertical;
// check lines does not contains a value twice // check lines does not contains a value twice
...@@ -257,7 +264,6 @@ class BoardUtils { ...@@ -257,7 +264,6 @@ class BoardUtils {
return true; return true;
} }
static void computeConflictsInBoard(Data myProvider) { static void computeConflictsInBoard(Data myProvider) {
List cells = myProvider.cells; List cells = myProvider.cells;
...@@ -345,4 +351,82 @@ class BoardUtils { ...@@ -345,4 +351,82 @@ class BoardUtils {
} }
} }
static List getCellsWithWrongValue(List cells, List cellsSolved, int blockSizeHorizontal, int blockSizeVertical) {
List cellsWithWrongValue = [];
int boardSize = blockSizeHorizontal * blockSizeVertical;
for (var row = 0; row < boardSize; row++) {
for (var col = 0; col < boardSize; col++) {
if (cells[row][col].value != 0 && cells[row][col].value != cellsSolved[row][col].value) {
cellsWithWrongValue.add([col, row]);
}
}
}
return cellsWithWrongValue;
}
static List getConflictingCells(List cells, int blockSizeHorizontal, int blockSizeVertical) {
List conflictingCells = [];
int boardSize = blockSizeHorizontal * blockSizeVertical;
for (var row = 0; row < boardSize; row++) {
for (var col = 0; col < boardSize; col++) {
if (!cells[row][col].isFixed && cells[row][col].value != 0) {
if (cells[row][col].conflictsCount != 0 && !BoardUtils.isValueAllowed(cells, blockSizeHorizontal, blockSizeVertical, col, row, cells[row][col].value)) {
conflictingCells.add([col, row]);
}
}
}
}
return conflictingCells;
}
static List getCellsWithUniqueAvailableValue(List cells, int blockSizeHorizontal, int blockSizeVertical) {
List candidateCells = [];
int boardSize = blockSizeHorizontal * blockSizeVertical;
for (var row = 0; row < boardSize; row++) {
for (var col = 0; col < boardSize; col++) {
if (cells[row][col].value == 0) {
int allowedValuesCount = 0;
int candidateValue = 0;
for (var value = 1; value <= boardSize; value++) {
if (BoardUtils.isValueAllowed(cells, blockSizeHorizontal, blockSizeVertical, col, row, value)) {
candidateValue = value;
allowedValuesCount++;
}
}
if (allowedValuesCount == 1) {
candidateCells.add([col, row, candidateValue]);
}
}
}
}
return candidateCells;
}
static List getSolvedGrid(Data myProvider) {
List cells = copyBoard(myProvider.cells);
int blockSizeHorizontal = myProvider.blockSizeHorizontal;
int blockSizeVertical = myProvider.blockSizeVertical;
do {
List cellsWithUniqueAvailableValue = BoardUtils.getCellsWithUniqueAvailableValue(cells, blockSizeHorizontal, blockSizeVertical);
if (cellsWithUniqueAvailableValue.length == 0) {
break;
}
for (var i = 0; i < cellsWithUniqueAvailableValue.length; i++) {
int col = cellsWithUniqueAvailableValue[i][0];
int row = cellsWithUniqueAvailableValue[i][1];
int value = cellsWithUniqueAvailableValue[i][2];
cells[row][col].value = value;
}
} while (true);
return cells;
}
} }
...@@ -29,42 +29,25 @@ class GameUtils { ...@@ -29,42 +29,25 @@ class GameUtils {
static void helpSelectCell(Data myProvider) { static void helpSelectCell(Data myProvider) {
List cells = myProvider.cells; List cells = myProvider.cells;
int boardSize = myProvider.blockSizeHorizontal * myProvider.blockSizeVertical; int blockSizeHorizontal = myProvider.blockSizeHorizontal;
int blockSizeVertical = myProvider.blockSizeVertical;
// pick one of conflicting cells, if found // pick one of wrong value cells, if found
List conflictingCells = []; List wrongValueCells = BoardUtils.getCellsWithWrongValue(cells, myProvider.cellsSolved, blockSizeHorizontal, blockSizeVertical);
for (var row = 0; row < boardSize; row++) { if (wrongValueCells.length != 0) {
for (var col = 0; col < boardSize; col++) { GameUtils.pickRandomFromList(myProvider, wrongValueCells);
if (!cells[row][col].isFixed && cells[row][col].value != 0) { return;
if (cells[row][col].conflictsCount != 0 && !BoardUtils.isValueAllowed(myProvider, col, row, cells[row][col].value)) {
conflictingCells.add([col, row]);
}
}
}
} }
// pick one of conflicting cells, if found
List conflictingCells = BoardUtils.getCellsWithUniqueAvailableValue(cells, blockSizeHorizontal, blockSizeVertical);
if (conflictingCells.length != 0) { if (conflictingCells.length != 0) {
GameUtils.pickRandomFromList(myProvider, conflictingCells); GameUtils.pickRandomFromList(myProvider, conflictingCells);
return; return;
} }
// pick one form cells with unique non-conflicting candidate value // pick one form cells with unique non-conflicting candidate value
List candidateCells = []; List candidateCells = BoardUtils.getCellsWithUniqueAvailableValue(cells, blockSizeHorizontal, blockSizeVertical);
for (var row = 0; row < boardSize; row++) {
for (var col = 0; col < boardSize; col++) {
if (cells[row][col].value == 0) {
int allowedValuesCount = 0;
for (var value = 1; value <= boardSize; value++) {
if (BoardUtils.isValueAllowed(myProvider, col, row, value)) {
allowedValuesCount++;
}
}
if (allowedValuesCount == 1) {
candidateCells.add([col, row]);
}
}
}
}
if (candidateCells.length != 0) { if (candidateCells.length != 0) {
GameUtils.pickRandomFromList(myProvider, candidateCells); GameUtils.pickRandomFromList(myProvider, candidateCells);
return; return;
...@@ -80,14 +63,19 @@ class GameUtils { ...@@ -80,14 +63,19 @@ class GameUtils {
} }
static void helpFillCell(Data myProvider) { static void helpFillCell(Data myProvider) {
int boardSize = myProvider.blockSizeHorizontal * myProvider.blockSizeVertical; List cells = myProvider.cells;
int blockSizeHorizontal = myProvider.blockSizeHorizontal;
int blockSizeVertical = myProvider.blockSizeVertical;
int boardSize = blockSizeHorizontal * blockSizeVertical;
// Will clean cell if no eligible value found
int eligibleValue = 0; int eligibleValue = 0;
// ensure there is only one eligible value for this cell // ensure there is only one eligible value for this cell
int allowedValuesCount = 0; int allowedValuesCount = 0;
for (var value = 1; value <= boardSize; value++) { for (var value = 1; value <= boardSize; value++) {
if (BoardUtils.isValueAllowed(myProvider, myProvider.currentCellCol, myProvider.currentCellRow, value)) { if (BoardUtils.isValueAllowed(cells, blockSizeHorizontal, blockSizeVertical, myProvider.currentCellCol, myProvider.currentCellRow, value)) {
allowedValuesCount++; allowedValuesCount++;
eligibleValue = value; eligibleValue = value;
} }
...@@ -97,4 +85,5 @@ class GameUtils { ...@@ -97,4 +85,5 @@ class GameUtils {
myProvider.selectCell(null, null); myProvider.selectCell(null, null);
BoardUtils.computeConflictsInBoard(myProvider); BoardUtils.computeConflictsInBoard(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