Created
May 8, 2019 16:31
-
-
Save thebkr7/4fb49aa5da6e3f3a27167d1d19bde62b to your computer and use it in GitHub Desktop.
SudokuSolver.j
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
var board = [ | |
[4, 5, '.', 8, '.', '.', 9, '.', '.'], | |
['.', 9, '.', '.', 5, 6, '.', '.', 4], | |
[1, '.', '.', '.', '.', '.', '.', '.', 7], | |
[2, 6, '.', 5, 4, '.', '.', 9, '.'], | |
['.', '.', 4, 1, '.', 2, 3, '.', '.'], | |
['.', 7, '.', '.', 6, 9, '.', 4, 8], | |
[7, '.', '.', '.', '.', '.', '.', '.', 9], | |
[8, '.', '.', 4, 9, '.', '.', 7, '.'], | |
['.', '.', 9, '.', '.', 3, '.', 2, 5], | |
] | |
//sudokuMagician | |
//Note: run 17-19 outside of sudokuMagician | |
// 3 arrays with nested arrays for used numbers in subgrids rows, and columns | |
var subgrids = { | |
/* | |
0: { | |
changed: false, | |
usedNumbers: [1, 2, 3], | |
}, | |
1: ... | |
*/ | |
} | |
// loop through each subgrid to find used numbers | |
const subgridUsedNumbers = (rowStartIndex, columnStartIndex, gameBoard) => { | |
let usedNumbers = []; | |
for (let i = rowStartIndex; i < rowStartIndex + 3; i++) { | |
for (let x = columnStartIndex; x < columnStartIndex + 3; x++) { | |
if (gameBoard[i][x] !== '.') { | |
usedNumbers.push(gameBoard[i][x]); | |
}; | |
}; | |
}; | |
return usedNumbers; | |
}; | |
// loop through rows to find used numbers | |
const rowUsedNumbers = (rowIndex, gameBoard) => { | |
let usedNumbers = []; | |
for (let x = 0; x < 9; x++) { | |
if (gameBoard[rowIndex][x] !== '.') { | |
usedNumbers.push(gameBoard[rowIndex][x]); | |
}; | |
}; | |
return usedNumbers; | |
}; | |
// loop through columns to find used numbers | |
const columnUsedNumbers = (columnIndex, gameBoard) => { | |
let usedNumbers = []; | |
for (let x = 0; x < 9; x++) { | |
if (gameBoard[x][columnIndex] !== '.') { | |
usedNumbers.push(gameBoard[x][columnIndex]); | |
}; | |
}; | |
return usedNumbers; | |
}; | |
// return all unused numbers | |
const allUnUsedNumbers = (rowIndex, columnIndex, gameBoard) => { | |
let usedNumbers = []; | |
let usedSubgridNumbers = subgridUsedNumbers(rowIndex, columnIndex, gameBoard); | |
let usedRowNumbers = rowUsedNumbers(rowIndex, gameBoard); | |
let usedColumnNumbers = columnUsedNumbers(columnIndex, gameBoard); | |
for (let i = 0; i < 9; i++) { | |
usedSubgridNumbers | |
}; | |
} | |
// let stillGoing = true; | |
const sudokuMagician = (gameBoard) => { | |
//conitnually loop until there are no more found '.' for a full loop | |
// while (stillGoing) { | |
let dotsLeft = [] | |
//loop i for 9 itterations | |
for (let i = 0; i < 9; i++) { | |
//loop x for 9 itterations | |
for (let x = 0; x < 9; x++) { | |
//if gameBoard square at row i and column x is '.' | |
if (gameBoard[i][x] === '.') { | |
dotsLeft.push('.'); | |
//declare possibleOutcomes as an array | |
let possibleOutcomes = []; | |
let usedGridNumbers = subgridUsedNumbers(Math.floor(i/3), Math.floor(x/3), gameBoard); | |
let usedRowNumbers = rowUsedNumbers(i, gameBoard); | |
let usedColumnNumbers = columnUsedNumbers(x, gameBoard); | |
// loop through 9-1 y | |
for (let y = 9; y > 0; y--) { | |
// if y is not in the specific subgrid, row, or column | |
// Todo: could do only once out of loop then search one array | |
let gridNumberUsed = usedGridNumbers.indexOf(y); | |
let usedRowNumber = usedRowNumbers.indexOf(y); | |
let usedColumnNumber = usedColumnNumbers.indexOf(y); | |
if (gridNumberUsed < 0 && usedRowNumber < 0 && usedColumnNumber < 0) { | |
// add y to possibleOutcomes via push | |
possibleOutcomes.push(y); | |
}; | |
}; | |
// if possibleOutcomes length is 1 | |
if (possibleOutcomes.length === 1) { | |
// replace '.' with y on the board at row i and column x | |
gameBoard[i][x] = possibleOutcomes[0]; | |
}; | |
}; | |
}; | |
}; | |
if (dotsLeft.length = 0) { | |
stillGoing = true; | |
} | |
// }; | |
} | |
sudokuMagician(board); | |
console.log('board==', board); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment