From 7c20f7a90b47a546ccb5eb9691da7c2daf150c2f Mon Sep 17 00:00:00 2001 From: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:59:55 +0100 Subject: [PATCH] Feature: isSolved method (#9) * Bump to v2.2.0 --- README.md | 4 ++++ package-lock.json | 4 ++-- package.json | 2 +- src/index.ts | 6 ++++++ src/tests/sudoku.test.ts | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 40abbb7..759b5a3 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/package-lock.json b/package-lock.json index 862c350..72817ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "sudoku-grid-maker", - "version": "2.1.1", + "version": "2.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "sudoku-grid-maker", - "version": "2.1.1", + "version": "2.2.0", "devDependencies": { "@eslint/js": "^8.57.0", "@types/jest": "^29.5.12", diff --git a/package.json b/package.json index d263459..aa057dd 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.ts b/src/index.ts index 81368fb..d486c6b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/tests/sudoku.test.ts b/src/tests/sudoku.test.ts index e6e4bf3..2c4aaba 100644 --- a/src/tests/sudoku.test.ts +++ b/src/tests/sudoku.test.ts @@ -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); + }); +});