-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
215 lines (184 loc) · 6.43 KB
/
game.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
class Cell {
constructor(value, solution) {
this.value = value;
this.state = 'normal'; // normal, enabled, disabled
this.solution = solution; // true, false
}
reset = () => this.state = 'normal';
enable() {
this.state = 'enabled';
}
disable() {
this.state = 'disabled';
}
isDisabled() {
return this.state === 'disabled';
}
isEnabled() {
return this.state === 'enabled';
}
isNormal = () => this.state === 'normal';
}
class Board {
constructor(dimension) {
this.dimension = dimension;
this.cells = [];
this.generate();
}
getCell(row, col) {
return this.cells[row * this.dimension + col];
}
setCell(row, col, value) {
this.cells[row * this.dimension + col] = value;
}
getColSum(col) {
let sum = 0;
for (let row = 0; row < this.dimension; row++) {
if (!this.getCell(row, col).isDisabled()) {
sum += this.getCell(row, col).value;
}
}
return sum;
}
getRowSum(row) {
let sum = 0;
for (let col = 0; col < this.dimension; col++) {
if (!this.getCell(row, col).isDisabled()) {
sum += this.getCell(row, col).value;
}
}
return sum;
}
getSolutionRowSum(row) {
let sum = 0;
for (let col = 0; col < this.dimension; col++) {
if (this.getCell(row, col).solution) {
sum += this.getCell(row, col).value;
}
}
return sum;
}
getSolutionColSum(col) {
let sum = 0;
for (let row = 0; row < this.dimension; row++) {
if (this.getCell(row, col).solution) {
sum += this.getCell(row, col).value;
}
}
return sum;
}
generate() {
for (let i = 0; i < this.dimension * this.dimension; i++) {
let randValue = Math.floor(Math.random() * 9 + 1);
let randSolution = Math.random() < 0.6;
this.cells.push(new Cell(randValue, randSolution));
}
}
}
class Game {
constructor(dimension) {
this.board = new Board(dimension);
this.table = this.tableGenerator(dimension);
document.getElementById('game').innerHTML = '';
document.getElementById('game').appendChild(this.table);
this.initialTime = new Date().getTime();
}
tableGenerator(dimension) {
let table = document.createElement('table');
table.classList.add('table');
table.classList.add('is-bordered');
for (let i = 0; i < dimension; i++) {
let row = document.createElement('tr');
for (let j = 0; j < dimension; j++) {
let cell = document.createElement('td');
cell.id = 'cell-' + i + '-' + j;
cell.classList.add('cell');
cell.classList.add('is-clickable');
cell.onclick = () => this.toggleCell(i, j);
// cell.onclick = "game.toggleCell(" + i + "," + j + ")";
row.appendChild(cell);
}
let sumRow = document.createElement('td');
sumRow.id = 'sum-row-' + i;
sumRow.classList.add('cell');
sumRow.classList.add('is-info');
row.appendChild(sumRow);
table.appendChild(row);
}
let row = document.createElement('tr');
for (let i = 0; i < dimension; i++) {
let sumCol = document.createElement('td');
sumCol.id = 'sum-col-' + i;
sumCol.classList.add('cell');
sumCol.classList.add('is-info');
row.appendChild(sumCol);
}
table.appendChild(row);
return table;
}
start() {
console.log('Game started');
for (let i = 0; i < this.board.dimension; i++) {
for (let j = 0; j < this.board.dimension; j++) {
let cell = this.board.getCell(i, j);
document.getElementById('cell-' + i + '-' + j).innerText = cell.value;
// console.log(this.board.getCell(i, j));
}
}
for (let i = 0; i < this.board.dimension; i++) {
document.getElementById('sum-row-' + i).innerText = this.board.getSolutionRowSum(i);
document.getElementById('sum-col-' + i).innerText = this.board.getSolutionColSum(i);
}
this.check();
}
toggleCell(row, col) {
let cell = this.board.getCell(row, col);
let element = document.getElementById('cell-' + row + '-' + col)
if (cell.isEnabled()) {
cell.disable();
element.classList.remove('is-success');
element.classList.add('is-danger');
} else if (cell.isDisabled()) {
element.classList.remove('is-danger');
cell.reset();
} else if (cell.isNormal()) {
cell.enable();
element.classList.remove('is-danger');
element.classList.add('is-success');
}
this.check();
}
check() {
for (let i = 0; i < this.board.dimension; i++) {
let sumRow = this.board.getRowSum(i);
let sumCol = this.board.getColSum(i);
if (sumRow == this.board.getSolutionRowSum(i)) {
document.getElementById('sum-row-' + i).classList.add('is-success');
} else {
document.getElementById('sum-row-' + i).classList.remove('is-success');
}
if (sumCol == this.board.getSolutionColSum(i)) {
document.getElementById('sum-col-' + i).classList.add('is-success');
} else {
document.getElementById('sum-col-' + i).classList.remove('is-success');
}
}
let win = true;
for (let i = 0; i < this.board.dimension; i++) {
if (!document.getElementById('sum-row-' + i).classList.contains('is-success')
|| !document.getElementById('sum-col-' + i).classList.contains('is-success')) {
win = false;
}
}
if (win) {
setTimeout(() => {
alert('You won in ' + (new Date().getTime() - this.initialTime) / 1000 + ' seconds!');
window.location.reload();
}, 10);
}
}
timer() {
let time = new Date().getTime() - this.initialTime;
document.getElementById('timer').innerText = Math.round(time / 1000);
}
}