-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.js
165 lines (136 loc) · 4.47 KB
/
grid.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class Grid {
constructor(size) {
this.cells = [];
this.size = size;
this.solutions;
}
getCell(row, col) {
return this.cells[row * this.size + col];
}
setCell(row, col, value) {
this.cells[row * this.size + col] = new Cell(value);
return this;
}
generate() {
for(let i = 0; i < this.size ** 2; i++) {
if(this.cells[i].value === null) {
let values = shuffle([0, 1]);
for(let value of values) {
if(this.possible(Math.floor(i / this.size), i % this.size, value)) {
this.cells[i].value = value;
if(this.generate()) {
return true;
}
this.cells[i].value = null;
}
}
return false;
}
}
return true;
}
solve() {
for(let i = 0; i < this.size ** 2; i++) {
if(this.cells[i].value === null) {
for(let value = 0; value <= 1; value++) {
if(this.possible(Math.floor(i / this.size), i % this.size, value)) {
this.cells[i].value = value;
if(this.countEmptyCells() === 0) {
this.solutions++;
break;
} else if(this.solve()) {
return true;
}
this.cells[i].value = null;
}
}
return false;
}
}
return true;
}
possible(row, col, value) {
let countCol = 0;
let countRow = 0;
let colStrings = [];
let rowStrings = [];
for(let i = 0; i < this.size; i++) {
if(this.getCell(row, i).value === value) {
countCol++;
}
if(this.getCell(i, col).value === value) {
countRow++;
}
if(countCol >= this.size / 2 || countRow >= this.size / 2) {
return false;
}
let rowString = '';
let colString = '';
for(let j = 0; j < this.size; j++) {
rowString += this.getCell(i, j).value !== null ? this.getCell(i, j).value : '';
colString += this.getCell(j, i).value !== null ? this.getCell(j, i).value : '';
}
if((rowString.length === this.size && rowStrings.indexOf(rowString) > -1)
|| (colString.length === this.size && colStrings.indexOf(colString) > -1)
) {
return false;
}
rowStrings.push(rowString);
colStrings.push(colString);
}
if(col - 2 >= 0
&& this.getCell(row, col - 2).value === value === this.getCell(row, col - 1).value
) {
return false;
}
if(col - 1 >= 0 && col + 1 < this.size
&& this.getCell(row, col - 1).value === value === this.getCell(row, col + 1).value
) {
return false;
}
if(col + 2 < this.size
&& this.getCell(row, col + 1).value === value === this.getCell(row, col + 2).value
) {
return false;
}
if(row - 2 >= 0
&& this.getCell(row - 2, col).value === value === this.getCell(row - 1, col).value
) {
return false;
}
if(row - 1 >= 0 && row + 1 < this.size
&& this.getCell(row - 1, col).value === value === this.getCell(row + 1, col).value
) {
return false;
}
if(row + 2 < this.size
&& this.getCell(row + 1, col).value === value === this.getCell(row + 2, col).value
) {
return false;
}
return true;
}
countEmptyCells() {
let num = 0;
for(let i = 0; i < this.size ** 2; i++) {
if(this.cells[i].value === null) {
num++;
}
}
return num;
}
clone() {
let clone = new Grid();
for(let i = 0; i < this.size ** 2; i++) {
clone.cells[i] = new Cell(this.cells[i].value, this.cells[i].stored);
}
return clone;
}
static init(size) {
let grid = new Grid(size);
for(let i = 0; i < size ** 2; i++) {
grid.cells[i] = new Cell(null);
}
return grid;
}
}