-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor sudoku code to get buttons looking right and working, implem…
…ent save/load
- Loading branch information
Showing
5 changed files
with
170 additions
and
125 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
export function checkSolution(puzzle, values, solution) { | ||
if (!puzzle.length) { | ||
return false; | ||
} | ||
|
||
const currentBoard = puzzle | ||
.map((boardRow, rowIdx) => | ||
boardRow | ||
.map((value, colIdx) => { | ||
if (value !== null) { | ||
return value.toString(); | ||
} | ||
|
||
const liveValue = values[rowIdx][colIdx]; | ||
|
||
return liveValue === -1 ? '-' : liveValue; | ||
}) | ||
.join('') | ||
) | ||
.join(''); | ||
|
||
return currentBoard === solution; | ||
} | ||
|
||
export function getInvalids(values, rows) { | ||
return values.map((row, rowIdx) => | ||
row.map((value, colIdx) => { | ||
if (value === -1) { | ||
return true; | ||
} | ||
|
||
const targetRow = rows[rowIdx]; | ||
|
||
if ( | ||
((targetRow && | ||
targetRow.indexOf(value) === colIdx && | ||
targetRow.lastIndexOf(value) === colIdx) || | ||
(targetRow.indexOf(value) === -1 && | ||
targetRow.lastIndexOf(value) === -1) || | ||
row.indexOf(value) !== colIdx || | ||
row.lastIndexOf(value) !== colIdx) && | ||
rows.every((searchRow) => searchRow[colIdx] !== value) | ||
) { | ||
const cellRow = Math.floor(rowIdx / 3); | ||
const cellCol = Math.floor(colIdx / 3); | ||
|
||
for ( | ||
let searchRowIdx = cellRow * 3; | ||
searchRowIdx < (cellRow + 1) * 3; | ||
searchRowIdx++ | ||
) { | ||
for ( | ||
let searchColIdx = cellCol * 3; | ||
searchColIdx < (cellCol + 1) * 3; | ||
searchColIdx++ | ||
) { | ||
if ( | ||
searchRowIdx !== rowIdx && | ||
searchColIdx !== colIdx && | ||
(rows[searchRowIdx][searchColIdx] === value || | ||
values[searchRowIdx][searchColIdx] === value) | ||
) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} else { | ||
return false; | ||
} | ||
}) | ||
); | ||
} |