Skip to content

Instantly share code, notes, and snippets.

@dsasse07
Last active February 16, 2021 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsasse07/3c1d459eeca0867eacfc1ef5d3fca13c to your computer and use it in GitHub Desktop.
Save dsasse07/3c1d459eeca0867eacfc1ef5d3fca13c to your computer and use it in GitHub Desktop.
Sudoku box region safe condition
// 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