Skip to content

Commit

Permalink
Feature: isSolved method (#9)
Browse files Browse the repository at this point in the history
* Bump to v2.2.0
  • Loading branch information
MaoShizhong authored Aug 26, 2024
1 parent f0776c5 commit 7c20f7a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ Changes the current grid state to the next recorded grid state (if one exists).
### reset()

Resets the current grid to the original grid state and wipes the rest of the grid history.

### isSolved()

`true` if all cells have values in them, otherwise `false`.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sudoku-grid-maker",
"version": "2.1.1",
"version": "2.2.0",
"author": "MaoShizhong",
"description": "A playable sudoku grid maker (not generator). Supports pencil marks and grid history for undo/redo.",
"repository": {
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ class Sudoku {
this.grid = this.history.currentGridState;
}

isSolved(): boolean {
return this.grid.every((row): boolean =>
row.every((cell) => cell.value)
);
}

#checkPlacementValidity(
targetCell: Cell,
number: SudokuNumber
Expand Down
39 changes: 39 additions & 0 deletions src/tests/sudoku.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,42 @@ describe('Number/pencil mark interactions', (): void => {
expect(sudoku.grid[3][3].pencilMarks.includes(9)).toBe(false);
});
});

describe('Solved state', (): void => {
const almostSolvedStartingValues: CellValue[][] = [
[null, 2, 5, 6, 3, 1, 8, 4, 7],
[6, 1, 8, 5, 7, 4, 2, 9, 3],
[3, 7, 4, 9, 8, 2, 5, 6, 1],
[7, 4, 9, 8, 2, 6, 1, 3, 5],
[8, 5, 2, 4, 1, 3, 9, 7, 6],
[1, 6, 3, 7, 9, 5, 4, 8, 2],
[2, 8, 7, 3, 5, 9, 6, 1, 4],
[4, 9, 1, 2, 6, 7, 3, 5, 8],
[5, 3, 6, 1, 4, 8, 7, 2, 9],
];
const solvedStartingValues: CellValue[][] = [
[9, 2, 5, 6, 3, 1, 8, 4, 7],
[6, 1, 8, 5, 7, 4, 2, 9, 3],
[3, 7, 4, 9, 8, 2, 5, 6, 1],
[7, 4, 9, 8, 2, 6, 1, 3, 5],
[8, 5, 2, 4, 1, 3, 9, 7, 6],
[1, 6, 3, 7, 9, 5, 4, 8, 2],
[2, 8, 7, 3, 5, 9, 6, 1, 4],
[4, 9, 1, 2, 6, 7, 3, 5, 8],
[5, 3, 6, 1, 4, 8, 7, 2, 9],
];

it('does not report an unfinished grid as solved', (): void => {
const sudoku = new Sudoku(almostSolvedStartingValues);
expect(sudoku.isSolved()).toBe(false);
});

it('reports an finished grid as solved', (): void => {
const sudoku = new Sudoku(almostSolvedStartingValues);
sudoku.addNumber({ newNumber: 9, row: 0, column: 0 });
expect(sudoku.isSolved()).toBe(true);

const preSolvedSudoku = new Sudoku(solvedStartingValues);
expect(preSolvedSudoku.isSolved()).toBe(true);
});
});

0 comments on commit 7c20f7a

Please sign in to comment.