Skip to content

Commit a70dfe9

Browse files
authored
Added backtracking (sudoku) (#205)
* Added backtracking (sudoku) * Coverted into class based program
1 parent 9e414b7 commit a70dfe9

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

back-tracking/Sudoku.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
class Sudoku {
2+
// Sudoku Class to hold the board and related functions
3+
constructor (board) {
4+
this.board = board
5+
}
6+
7+
findEmptyCell () {
8+
// Find a empty cell in the board (returns [-1, -1] if all cells are filled)
9+
for (let i = 0; i < 9; i++) {
10+
for (let j = 0; j < 9; j++) {
11+
if (this.board[i][j] === 0) return [i, j]
12+
}
13+
}
14+
return [-1, -1]
15+
}
16+
17+
check ([y, x], value) {
18+
// checks if the value to be added in the board is an acceptable value for the cell
19+
20+
// checking through the row
21+
for (let i = 0; i < 9; i++) {
22+
if (this.board[i][x] === value) return false
23+
}
24+
// checking through the column
25+
for (let i = 0; i < 9; i++) {
26+
if (this.board[y][i] === value) return false
27+
}
28+
29+
// checking through the 3x3 block of the cell
30+
const secRow = Math.floor(y / 3)
31+
const secCol = Math.floor(x / 3)
32+
for (let i = (secRow * 3); i < ((secRow * 3) + 3); i++) {
33+
for (let j = (secCol * 3); j < ((secCol * 3) + 3); j++) {
34+
if (y !== i && x !== j && this.board[i][j] === value) return false
35+
}
36+
}
37+
38+
return true
39+
}
40+
41+
solve () {
42+
const [y, x] = this.findEmptyCell()
43+
44+
// checking if the board is complete
45+
if (y === -1 && x === -1) return true
46+
47+
for (let val = 1; val < 10; val++) {
48+
if (this.check([y, x], val)) {
49+
this.board[y][x] = val
50+
if (this.solve()) return true
51+
// backtracking if the board cannot be solved using current configuration
52+
this.board[y][x] = 0
53+
}
54+
}
55+
// returning false the board cannot be solved using current configuration
56+
return false
57+
}
58+
59+
getSection (row, [start, end]) {
60+
return this.board[row].slice(start, end)
61+
}
62+
63+
printBoard () {
64+
// helper function to display board
65+
for (let i = 0; i < 9; i++) {
66+
if (i % 3 === 0 && i !== 0) console.log('- - - - - - - - - - - -')
67+
console.log(
68+
...this.getSection(i, [0, 3]), ' | ',
69+
...this.getSection(i, [3, 6]), ' | ',
70+
...this.getSection(i, [6, 9]))
71+
}
72+
}
73+
}
74+
75+
function main () {
76+
// main function with an example
77+
const sudokuBoard = new Sudoku([
78+
[3, 0, 6, 5, 0, 8, 4, 0, 0],
79+
[5, 2, 0, 0, 0, 0, 0, 0, 0],
80+
[0, 8, 7, 0, 0, 0, 0, 3, 1],
81+
[0, 0, 3, 0, 1, 0, 0, 8, 0],
82+
[9, 0, 0, 8, 6, 3, 0, 0, 5],
83+
[0, 5, 0, 0, 9, 0, 6, 0, 0],
84+
[1, 3, 0, 0, 0, 0, 2, 5, 0],
85+
[0, 0, 0, 0, 0, 0, 0, 7, 4],
86+
[0, 0, 5, 2, 0, 6, 3, 0, 0]
87+
])
88+
89+
sudokuBoard.printBoard()
90+
91+
console.log('\n')
92+
sudokuBoard.solve()
93+
94+
sudokuBoard.printBoard()
95+
}
96+
97+
main()

0 commit comments

Comments
 (0)