Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • android/org.benoitharrault.sudoku
1 result
Show changes
......@@ -4,14 +4,14 @@ import 'package:sudoku/provider/data.dart';
class BoardAnimate {
// Start game animation: blinking tiles
static List<List<List<bool>>> createStartGameAnimationPatterns(Data myProvider) {
List<List<List<bool>>> patterns = [];
static AnimatedBoardSequence createStartGameAnimationPatterns(Data myProvider) {
AnimatedBoardSequence patterns = [];
int patternsCount = 3;
int boardSideLength = myProvider.blockSizeHorizontal * myProvider.blockSizeVertical;
for (int patternIndex = 0; patternIndex < patternsCount; patternIndex++) {
List<List<bool>> pattern = [];
AnimatedBoard pattern = [];
for (int row = 0; row < boardSideLength; row++) {
List<bool> patternRow = [];
for (int col = 0; col < boardSideLength; col++) {
......@@ -26,14 +26,14 @@ class BoardAnimate {
}
// Win game animation: fill board with colored rows, from bottom to top
static List<List<List<bool>>> createWinGameAnimationPatterns(Data myProvider) {
List<List<List<bool>>> patterns = [];
static AnimatedBoardSequence createWinGameAnimationPatterns(Data myProvider) {
AnimatedBoardSequence patterns = [];
int boardSideLength = myProvider.blockSizeHorizontal * myProvider.blockSizeVertical;
int patternsCount = boardSideLength + 6;
for (int patternIndex = 0; patternIndex < patternsCount; patternIndex++) {
List<List<bool>> pattern = [];
AnimatedBoard pattern = [];
for (int row = 0; row < boardSideLength; row++) {
List<bool> patternRow = [];
for (int col = 0; col < boardSideLength; col++) {
......@@ -48,14 +48,14 @@ class BoardAnimate {
}
// Default multi-purpose animation: sliding stripes, from top left to right bottom
static List<List<List<bool>>> createDefaultAnimationPatterns(Data myProvider) {
List<List<List<bool>>> patterns = [];
static AnimatedBoardSequence createDefaultAnimationPatterns(Data myProvider) {
AnimatedBoardSequence patterns = [];
int boardSideLength = myProvider.blockSizeHorizontal * myProvider.blockSizeVertical;
int patternsCount = boardSideLength;
for (int patternIndex = 0; patternIndex < patternsCount; patternIndex++) {
List<List<bool>> pattern = [];
AnimatedBoard pattern = [];
for (int row = 0; row < boardSideLength; row++) {
List<bool> patternRow = [];
for (int col = 0; col < boardSideLength; col++) {
......@@ -70,7 +70,7 @@ class BoardAnimate {
}
static void startAnimation(Data myProvider, String animationType) {
List<List<List<bool>>> patterns = [];
AnimatedBoardSequence patterns = [];
switch (animationType) {
case 'start':
......
......@@ -6,7 +6,7 @@ import 'package:sudoku/utils/random_pick_grid.dart';
import 'package:sudoku/utils/tools.dart';
class BoardUtils {
static printGrid(List<List<Cell>> cells, List<List<Cell>> solvedCells) {
static printGrid(Board cells, Board solvedCells) {
String stringValues = '0123456789ABCDEFG';
printlog('');
printlog('-------');
......@@ -42,16 +42,21 @@ class BoardUtils {
myProvider.updateCellsSolved(BoardUtils.getSolvedGrid(myProvider));
myProvider.selectCell(null, null);
printGrid(myProvider.cells, myProvider.cellsSolved);
printGrid(myProvider.board, myProvider.boardSolved);
}
}
static List<List<Cell>> createEmptyBoard(final int boardSize) {
final List<List<Cell>> cells = [];
static Board createEmptyBoard(final int boardSize) {
final Board cells = [];
for (int rowIndex = 0; rowIndex < boardSize; rowIndex++) {
final List<Cell> row = [];
for (int colIndex = 0; colIndex < boardSize; colIndex++) {
row.add(Cell(0, false));
row.add(Cell(
row: rowIndex,
col: colIndex,
value: 0,
isFixed: false,
));
}
cells.add(row);
}
......@@ -59,8 +64,8 @@ class BoardUtils {
return cells;
}
static List<List<Cell>> createBoardFromSavedState(Data myProvider, String savedBoard) {
final List<List<Cell>> cells = [];
static Board createBoardFromSavedState(Data myProvider, String savedBoard) {
final Board cells = [];
final int boardSize = int.parse(pow((savedBoard.length / 2), 1 / 2).toStringAsFixed(0));
const String stringValues = '0123456789ABCDEFG';
......@@ -75,7 +80,12 @@ class BoardUtils {
final String isFixedString = savedBoard[index++];
final bool isFixed = (isFixedString != ' ');
row.add(Cell(value, isFixed));
row.add(Cell(
row: rowIndex,
col: colIndex,
value: value,
isFixed: isFixed,
));
}
cells.add(row);
}
......@@ -83,12 +93,17 @@ class BoardUtils {
return cells;
}
static List<List<Cell>> copyBoard(List cells) {
final List<List<Cell>> copiedGrid = [];
static Board copyBoard(List cells) {
final Board copiedGrid = [];
for (int rowIndex = 0; rowIndex < cells.length; rowIndex++) {
final List<Cell> row = [];
for (int colIndex = 0; colIndex < cells[rowIndex].length; colIndex++) {
row.add(Cell(cells[rowIndex][colIndex].value, false));
row.add(Cell(
row: rowIndex,
col: colIndex,
value: cells[rowIndex][colIndex].value,
isFixed: false,
));
}
copiedGrid.add(row);
}
......@@ -96,8 +111,8 @@ class BoardUtils {
return copiedGrid;
}
static List<List<Cell>> createBoardFromTemplate(String grid, bool isSymetric) {
List<List<Cell>> cells = [];
static Board createBoardFromTemplate(String grid, bool isSymetric) {
Board cells = [];
final int boardSize = int.parse(pow(grid.length, 1 / 2).toStringAsFixed(0));
const String stringValues = '0123456789ABCDEFG';
......@@ -108,7 +123,12 @@ class BoardUtils {
for (int colIndex = 0; colIndex < boardSize; colIndex++) {
final String stringValue = grid[index++];
final int value = stringValues.indexOf(stringValue);
row.add(Cell(value, (value != 0)));
row.add(Cell(
row: rowIndex,
col: colIndex,
value: value,
isFixed: (value != 0),
));
}
cells.add(row);
}
......@@ -131,11 +151,15 @@ class BoardUtils {
switch (flip) {
case 'horizontal':
{
final List<List<Cell>> transformedBoard = createEmptyBoard(boardSize);
final Board transformedBoard = createEmptyBoard(boardSize);
for (int rowIndex = 0; rowIndex < boardSize; rowIndex++) {
for (int colIndex = 0; colIndex < boardSize; colIndex++) {
transformedBoard[rowIndex][colIndex].value =
cells[boardSize - rowIndex - 1][colIndex].value;
transformedBoard[rowIndex][colIndex] = Cell(
row: rowIndex,
col: colIndex,
value: cells[boardSize - rowIndex - 1][colIndex].value,
isFixed: false,
);
}
}
cells = transformedBoard;
......@@ -143,11 +167,15 @@ class BoardUtils {
break;
case 'vertical':
{
final List<List<Cell>> transformedBoard = createEmptyBoard(boardSize);
final Board transformedBoard = createEmptyBoard(boardSize);
for (int rowIndex = 0; rowIndex < boardSize; rowIndex++) {
for (int colIndex = 0; colIndex < boardSize; colIndex++) {
transformedBoard[rowIndex][colIndex].value =
cells[rowIndex][boardSize - colIndex - 1].value;
transformedBoard[rowIndex][colIndex] = Cell(
row: rowIndex,
col: colIndex,
value: cells[rowIndex][boardSize - colIndex - 1].value,
isFixed: false,
);
}
}
cells = transformedBoard;
......@@ -158,11 +186,15 @@ class BoardUtils {
switch (rotate) {
case 'left':
{
final List<List<Cell>> transformedBoard = createEmptyBoard(boardSize);
final Board transformedBoard = createEmptyBoard(boardSize);
for (int rowIndex = 0; rowIndex < boardSize; rowIndex++) {
for (int colIndex = 0; colIndex < boardSize; colIndex++) {
transformedBoard[rowIndex][colIndex].value =
cells[colIndex][boardSize - rowIndex - 1].value;
transformedBoard[rowIndex][colIndex] = Cell(
row: rowIndex,
col: colIndex,
value: cells[colIndex][boardSize - rowIndex - 1].value,
isFixed: false,
);
}
}
cells = transformedBoard;
......@@ -170,11 +202,15 @@ class BoardUtils {
break;
case 'right':
{
final List<List<Cell>> transformedBoard = createEmptyBoard(boardSize);
final Board transformedBoard = createEmptyBoard(boardSize);
for (int rowIndex = 0; rowIndex < boardSize; rowIndex++) {
for (int colIndex = 0; colIndex < boardSize; colIndex++) {
transformedBoard[rowIndex][colIndex].value =
cells[boardSize - colIndex - 1][rowIndex].value;
transformedBoard[rowIndex][colIndex] = Cell(
row: rowIndex,
col: colIndex,
value: cells[boardSize - colIndex - 1][rowIndex].value,
isFixed: false,
);
}
}
cells = transformedBoard;
......@@ -185,8 +221,12 @@ class BoardUtils {
// Fix cells fixed states
for (int rowIndex = 0; rowIndex < boardSize; rowIndex++) {
for (int colIndex = 0; colIndex < boardSize; colIndex++) {
cells[rowIndex][colIndex].isFixed =
(cells[rowIndex][colIndex].value != 0) ? true : false;
cells[rowIndex][colIndex] = Cell(
row: rowIndex,
col: colIndex,
value: cells[rowIndex][colIndex].value,
isFixed: (cells[rowIndex][colIndex].value != 0) ? true : false,
);
}
}
......@@ -194,7 +234,8 @@ class BoardUtils {
}
static bool checkBoardIsSolved(Data myProvider) {
final List<List<Cell>> cells = myProvider.cells;
final Board cells = myProvider.board;
final ConflictsCount conflicts = myProvider.boardConflicts;
final int blockSizeHorizontal = myProvider.blockSizeHorizontal;
final int blockSizeVertical = myProvider.blockSizeVertical;
......@@ -207,7 +248,7 @@ class BoardUtils {
// check grid is fully completed and does not contain conflict
for (int row = 0; row < boardSize; row++) {
for (int col = 0; col < boardSize; col++) {
if (cells[row][col].value == 0 || cells[row][col].conflictsCount != 0) {
if (cells[row][col].value == 0 || conflicts[row][col] != 0) {
return false;
}
}
......@@ -219,7 +260,7 @@ class BoardUtils {
}
static bool isValueAllowed(
List<List<Cell>> cells,
Board cells,
int blockSizeHorizontal,
int blockSizeVertical,
int? candidateCol,
......@@ -300,7 +341,8 @@ class BoardUtils {
}
static void computeConflictsInBoard(Data myProvider) {
final List<List<Cell>> cells = myProvider.cells;
final Board cells = myProvider.board;
final ConflictsCount conflicts = myProvider.boardConflicts;
final int blockSizeHorizontal = myProvider.blockSizeHorizontal;
final int blockSizeVertical = myProvider.blockSizeVertical;
......@@ -310,7 +352,7 @@ class BoardUtils {
// reset conflict states
for (int row = 0; row < boardSize; row++) {
for (int col = 0; col < boardSize; col++) {
cells[row][col].conflictsCount = 0;
conflicts[row][col] = 0;
}
}
......@@ -328,7 +370,7 @@ class BoardUtils {
printlog('line $row contains duplicates');
// Add line to cells in conflict
for (int col = 0; col < boardSize; col++) {
cells[row][col].conflictsCount++;
conflicts[row][col]++;
}
}
}
......@@ -347,7 +389,7 @@ class BoardUtils {
printlog('column $col contains duplicates');
// Add column to cells in conflict
for (int row = 0; row < boardSize; row++) {
cells[row][col].conflictsCount++;
conflicts[row][col]++;
}
}
}
......@@ -378,17 +420,19 @@ class BoardUtils {
for (int colInBlock = 0; colInBlock < blockSizeHorizontal; colInBlock++) {
int row = (blockRow * blockSizeVertical) + rowInBlock;
int col = (blockCol * blockSizeHorizontal) + colInBlock;
cells[row][col].conflictsCount++;
conflicts[row][col]++;
}
}
}
}
}
myProvider.updateConflicts(conflicts);
}
static List<List<int>> getCellsWithWrongValue(
final List<List<Cell>> cells,
final List<List<Cell>> cellsSolved,
final Board cells,
final Board cellsSolved,
final int blockSizeHorizontal,
final int blockSizeVertical,
) {
......@@ -407,31 +451,8 @@ class BoardUtils {
return cellsWithWrongValue;
}
static List<List<int>> getConflictingCells(
final List<List<Cell>> cells,
final int blockSizeHorizontal,
final int blockSizeVertical,
) {
final List<List<int>> conflictingCells = [];
final int boardSize = blockSizeHorizontal * blockSizeVertical;
for (int row = 0; row < boardSize; row++) {
for (int 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<List<int>> getCellsWithUniqueAvailableValue(
List<List<Cell>> cells,
Board cells,
final int blockSizeHorizontal,
final int blockSizeVertical,
) {
......@@ -460,8 +481,8 @@ class BoardUtils {
return candidateCells;
}
static List<List<Cell>> getSolvedGrid(Data myProvider) {
final List<List<Cell>> cells = copyBoard(myProvider.cells);
static Board getSolvedGrid(Data myProvider) {
final Board cells = copyBoard(myProvider.board);
final int blockSizeHorizontal = myProvider.blockSizeHorizontal;
int blockSizeVertical = myProvider.blockSizeVertical;
......@@ -479,7 +500,12 @@ class BoardUtils {
int col = cellsWithUniqueAvailableValue[i][0];
int row = cellsWithUniqueAvailableValue[i][1];
int value = cellsWithUniqueAvailableValue[i][2];
cells[row][col].value = value;
cells[row][col] = Cell(
row: row,
col: col,
value: value,
isFixed: cells[row][col].isFixed,
);
}
} while (true);
......
import 'package:sudoku/entities/cell.dart';
import 'package:sudoku/provider/data.dart';
import 'package:sudoku/utils/board_animate.dart';
import 'package:sudoku/utils/board_utils.dart';
......@@ -19,6 +18,8 @@ class GameUtils {
myProvider.shuffleCellValues();
myProvider.updateCells(BoardUtils.createEmptyBoard(
myProvider.blockSizeHorizontal * myProvider.blockSizeVertical));
myProvider.initAnimatedBackground();
myProvider.initConflictsBoard();
BoardUtils.pickGrid(myProvider);
BoardAnimate.startAnimation(myProvider, 'start');
}
......@@ -57,7 +58,7 @@ class GameUtils {
}
static void showTip(Data myProvider) {
if (myProvider.currentCellCol == null || myProvider.currentCellRow == null) {
if (myProvider.selectedCellCol == null || myProvider.selectedCellRow == null) {
// no selected cell -> pick one
GameUtils.helpSelectCell(myProvider);
} else {
......@@ -68,14 +69,14 @@ class GameUtils {
}
static void helpSelectCell(Data myProvider) {
final List<List<Cell>> cells = myProvider.cells;
final Board cells = myProvider.board;
final int blockSizeHorizontal = myProvider.blockSizeHorizontal;
final int blockSizeVertical = myProvider.blockSizeVertical;
// pick one of wrong value cells, if found
final List<List<int>> wrongValueCells = BoardUtils.getCellsWithWrongValue(
cells,
myProvider.cellsSolved,
myProvider.boardSolved,
blockSizeHorizontal,
blockSizeVertical,
);
......@@ -116,7 +117,7 @@ class GameUtils {
}
static void helpFillCell(Data myProvider) {
final List<List<Cell>> cells = myProvider.cells;
final Board cells = myProvider.board;
final int blockSizeHorizontal = myProvider.blockSizeHorizontal;
final int blockSizeVertical = myProvider.blockSizeVertical;
......@@ -129,13 +130,13 @@ class GameUtils {
int allowedValuesCount = 0;
for (int value = 1; value <= boardSize; value++) {
if (BoardUtils.isValueAllowed(cells, blockSizeHorizontal, blockSizeVertical,
myProvider.currentCellCol, myProvider.currentCellRow, value)) {
myProvider.selectedCellCol, myProvider.selectedCellRow, value)) {
allowedValuesCount++;
eligibleValue = value;
}
}
myProvider.updateCellValue(myProvider.currentCellCol, myProvider.currentCellRow,
myProvider.updateCellValue(myProvider.selectedCellCol, myProvider.selectedCellRow,
allowedValuesCount == 1 ? eligibleValue : 0);
myProvider.selectCell(null, null);
if (BoardUtils.checkBoardIsSolved(myProvider)) {
......
name: sudoku
description: A sudoku game application.
publish_to: 'none'
version: 0.1.15+64
version: 0.1.18+67
environment:
sdk: '^3.0.0'
......