-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.js
More file actions
157 lines (129 loc) · 4.17 KB
/
sudoku.js
File metadata and controls
157 lines (129 loc) · 4.17 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// 스도쿠 알고리즘
// 1. 정답판
// 2. 문제판(유일 해)
// 3. 빈 칸들에 들어가야 하는 값들
class SudokuClient {
constructor() { }
createGame(holes) {
this._holes = holes;
this._solvedBoard = this.createSolvedBoard();
this.createStartingBoard(this.solvedBoard, holes);
}
get solvedBoard() {
return this._solvedBoard.map(v => v.slice());
}
get startingBoard() {
return this._startingBoard?.map(v => v.slice())
}
get answers() {
return this._answers.slice();
}
createSolvedBoard() {
return this.fillBoard(Array.from(Array(9), () => Array(9).fill(0)));
}
createStartingBoard(solvedBoard, holes) {
let startingBoard = solvedBoard;
this.pokeHoles(startingBoard, holes);
this._startingBoard = startingBoard;
}
pokeHoles(board, holes) {
this._answers = [];
const solutions = [board.map(v => v.slice())];
let currentHoles = 0;
while (currentHoles < holes) {
const randomValue = Math.floor(Math.random() * 81);
const randomRowIndex = Math.floor(randomValue / 9);
const randomColIndex = randomValue % 9;
if (!board[randomRowIndex]) continue;
if (board[randomRowIndex][randomColIndex] == 0) continue;
const dummyBoard = board.map(v => v.slice());
dummyBoard[randomRowIndex][randomColIndex] = 0;
const candidateBoard = this.fillBoard(dummyBoard.map(v => v.slice()));
if (!this.includesInSolutions(solutions, candidateBoard)) {
solutions.push(solutions);
}
if (solutions.length > 1) {
return this.createGame(this._holes);
}
this._answers.push({
rowIndex: randomRowIndex,
colIndex: randomColIndex,
})
board[randomRowIndex][randomColIndex] = 0;
currentHoles++;
}
}
includesInSolutions(solutions, candidateBoard) {
const flatCandidateBoard = candidateBoard.flat();
for (const solution of solutions) {
const flatSolution = solution.flat();
const difference = flatCandidateBoard.filter(x => !flatSolution.includes(x));
if (difference.length > 0) {
return false;
}
}
return true;
}
fillBoard(baseBoard) {
let result = baseBoard.map(v => v.slice());
while (true) {
const emptyCell = this.nextEmptyCell(result);
if (emptyCell.rowIndex === null || emptyCell.colIndex === null) {
break;
}
const candidatesForCell = this.shuffledSudokuNumbers().filter((candidateNumber) => this.allPass(result, emptyCell, candidateNumber));
if (candidatesForCell.length === 0) {
// result = baseBoard.map(v => v.slice());
result = Array.from(Array(9), () => Array(9).fill(0));
continue;
}
result[emptyCell.rowIndex][emptyCell.colIndex] = candidatesForCell.shift();
}
return result;
}
nextEmptyCell(board) {
const result = { rowIndex: null, colIndex: null }
for (let rowIndex = 0; rowIndex < board.length; rowIndex++) {
const row = board[rowIndex];
const firstZeroCell = row.find((col) => col === 0);
if (firstZeroCell !== undefined) {
result.rowIndex = rowIndex;
result.colIndex = row.indexOf(firstZeroCell);
break;
}
}
return result;
}
shuffledSudokuNumbers() {
let result = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for (let i = result.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[result[i], result[j]] = [result[j], result[i]];
}
return result;
}
allPass(board, cell, num) {
return this.rowPass(board, cell, num) && this.colPass(board, cell, num) && this.boxPass(board, cell, num);
}
rowPass(board, cell, num) {
return board[cell.rowIndex].indexOf(num) < 0;
}
colPass(board, cell, num) {
return !board.some(row => row[cell.colIndex] === num);
}
boxPass(board, cell, num) {
const boxStartRow = cell.rowIndex - (cell.rowIndex % 3);
const boxStartCol = cell.colIndex - (cell.colIndex % 3);
for (const boxRow of [0, 1, 2]) {
for (const boxCol of [0, 1, 2]) {
if (board[boxStartRow + boxRow][boxStartCol + boxCol] === num) {
return false;
}
}
}
return true;
}
}
module.exports = {
SudokuClient
}