-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.js
141 lines (122 loc) · 3.53 KB
/
sudoku.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var easyGame = '003020600900305001001806400008102900700000008006708200002609500800203009005010300'
var mediumGame = '400000805030000000000700000020000060000080400000010000000603070500200000104000000'
var hardGame = '850002400720000009004000000000107002305000900040000000000080070017000000000036040'
var extremeGame = '800000000003600000070090200050007000000045700000100030001000068008500010090000400'
var Sudoku = {}
function Cell(value, index) {
this.position = index
this.row = this.rowFor(this.position)
this.column = this.columnFor(this.position)
this.box = this.boxFor(this.position)
this.options = this.fillOptions()
this.originalValue = parseInt(value)
this.currentValue = parseInt(value)
}
Cell.prototype = {
rowFor: function(idx) {
return Math.floor(idx / 9)
},
columnFor: function(idx) {
return idx % 9
},
boxFor: function(idx) {
return (Math.floor(this.columnFor(idx)/3)) + (Math.floor((this.rowFor(idx) / 3)) * 3)
},
fillOptions: function() {
return [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
}
Sudoku.BoardBuilder = (function (boardValues){
function makeIntoArray(boardValuesString){
return boardValues.split('')
}
function cellify(value,idx){
return new Cell(value, idx)
}
function populateBoard(boardValues){
var cells = []
makeIntoArray(boardValues)
.forEach(function(num,idx){
var cell = cellify(num,idx)
cells.push(cell)
})
return cells
}
return { buildBoard: populateBoard(boardValues) }
})(easyGame)
Sudoku.Solver = function(board) {
function isValueIn(category, cell) {
var result = false
Sudoku.board.forEach(function(otherCell){
if (otherCell[category] === cell[category]) {
if (otherCell.currentValue === cell.currentValue
&& otherCell.position !== cell.position) {
result = true
}
}
})
return result
}
function possibleValue(cell) {
var result =
(!isValueIn("row", cell)) &&
(!isValueIn("column", cell)) &&
(!isValueIn("box", cell))
? true : false
if (cell.currentValue > 9) { result = false }
return result
}
function retreat(board, cell) {
var currentCell = board[cell.position-1]
if (currentCell.originalValue === 0) {
currentCell.currentValue++
while(!possibleValue(currentCell)) {
if (currentCell.currentValue > 9) {
currentCell.currentValue = 0
return retreat(board, currentCell)
}
currentCell.currentValue++
displayBoard(board)
}
return currentCell.position
} else {
return retreat(board, currentCell)
}
}
//all knowing function
function solveBoard(board, index) {
var index = index || 0
for (var i = 0; i < board.length; i++) {
var cell = board[i]
if (parseInt(cell.originalValue) === 0) {
cell.currentValue++
while(!possibleValue(cell)) {
if (cell.currentValue > 9) {
cell.currentValue = 0
i = retreat(board, cell)
break
} else {
cell.currentValue++
displayBoard(board)
}
}
}
}
return board
}
return solveBoard(board)
}
Sudoku.board = Sudoku.BoardBuilder.buildBoard
var solution = Sudoku.Solver(Sudoku.board)
function displayBoard(board) {
var numbers = []
board.forEach(function(cell) {
numbers.push(cell.currentValue)
})
for(var index = 0; index < 80; index+=9) {
var num = index + 9
console.log(numbers.slice(index,num))
}
console.log('\n')
}
displayBoard(solution)