Last active
February 16, 2021 05:08
-
-
Save dsasse07/3c1d459eeca0867eacfc1ef5d3fca13c to your computer and use it in GitHub Desktop.
Sudoku box region safe condition
This file contains hidden or 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
// puzzleArray is the game board being solved. A 9x9 matrix | |
// emptyCell = {rowIndex: INT , colIndex: INT } INT = coordinates of currently empty cell | |
// num = integer value 1-9 being tested | |
const boxSafe = (puzzleArray, emptyCell, num) => { | |
// Define top left corner of box region for empty cell | |
boxStartRow = emptyCell.rowIndex - (emptyCell.rowIndex % 3) | |
boxStartCol = emptyCell.colIndex - (emptyCell.colIndex % 3) | |
let safe = true | |
for ( boxRow of [0,1,2] ) { // Each box region has 3 rows | |
for ( boxCol of [0,1,2] ) { // Each box region has 3 columns | |
// Is num is present in box region? | |
if ( puzzleArray[boxStartRow + boxRow][boxStartCol + boxCol] == num ) { | |
safe = false // If number is found, it is not safe to place | |
} | |
} | |
} | |
return safe | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment