-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
343 lines (298 loc) · 8.95 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// canvas.arc(x, y, radius, startAngle, endAngle [, counterclockwise]);
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
const width = canvas.getAttribute("width");
const height = canvas.getAttribute("height");
const size = 9;
const cell_count = size * size;
const item_size = width / size;
const gap_size = item_size / 7;
const inner_rect_size = item_size - gap_size * 2;
const clue_colors = [];
let cells_status = [];
let first_cell = true;
let mousedown_x = 0;
let mousedown_y = 0;
let game_over = false;
let bomb_count = 10;
let empty_cells_remaining = cell_count - bomb_count;
const BOMB = 0;
const BOMB_AND_FLAG = 1;
const FLAG = 2;
const OPENED = 3;
const NO_BOMB = 4;
canvas.onmouseout = () => {
if (mousedown_x != -1 && mousedown_y != -1) {
clickCancelled();
}
}
canvas.addEventListener("mousedown", (e) => {
const [x, y] = getMousePosition(e);
[mousedown_x, mousedown_y] = translatePixelToIndex(x, y);
if (cells_status[getArrayIndexOfXY(mousedown_x, mousedown_y)] == OPENED ||
game_over ||
empty_cells_remaining == 0) {
return;
}
drawCellMouseDown(mousedown_x, mousedown_y);
});
canvas.addEventListener("click", async (e) => {
if (game_over || empty_cells_remaining == 0) {
drawField();
game_over = false;
empty_cells_remaining = -1;
first_cell = true;
cells_status = []
return;
}
let [click_x, click_y] = translatePixelToIndex(...getMousePosition(e));
if (clickCancelled(click_x, click_y)) {
return;
}
if (first_cell) {
init(click_x, click_y);
}
let cell_status = cells_status[getArrayIndexOfXY(click_x, click_y)];
switch (cell_status) {
case BOMB:
game_over = true;
first_cell = true;
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
if (cells_status[getArrayIndexOfXY(i, j)] == BOMB) {
draw(i, j, s=BOMB);
await sleep(2);
}
}
}
draw(click_x, click_y, BOMB, "#FF0000");
drawGameOverScreen(win = false);
break;
case FLAG:
case BOMB_AND_FLAG:
draw(click_x, click_y, FLAG);
break;
case NO_BOMB:
calculateValueForOpenedCell(click_x, click_y);
if (empty_cells_remaining == 0) {
drawGameOverScreen(win = true);
}
break;
}
});
canvas.addEventListener("contextmenu", (e) => {
e.preventDefault();
if (game_over || empty_cells_remaining == 0) {
return;
}
let [click_x, click_y] = translatePixelToIndex(...getMousePosition(e));
if (clickCancelled(click_x, click_y))
return;
if (first_cell) {
drawCell(click_x, click_y)
return;
};
const i = getArrayIndexOfXY(click_x, click_y)
const cell_value = cells_status[i];
if (cell_value == OPENED) {
} else if (cell_value == BOMB_AND_FLAG || cell_value == FLAG) {
drawCell(click_x, click_y);
if (cell_value == BOMB_AND_FLAG) {
cells_status[i] = BOMB;
} else if (cell_value == FLAG) {
cells_status[i] = NO_BOMB;
}
} else {
draw(click_x, click_y, FLAG);
if (cell_value == BOMB) {
cells_status[i] = BOMB_AND_FLAG;
} else if (cell_value == NO_BOMB) {
cells_status[i] = FLAG;
}
}
});
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getRandomInteger(min, max) {
return Math.trunc(Math.random() * (max - min) + min);
}
function getMousePosition(event) {
const bounding_client_rect = canvas.getBoundingClientRect();
const x = event.clientX - bounding_client_rect.left;
const y = event.clientY - bounding_client_rect.top;
console.log("Coordinate x: " + x, "Coordinate y: " + y);
return [x, y];
}
function clickCancelled(x, y) {
if (x != mousedown_x || y != mousedown_y) {
const cell_status = cells_status[getArrayIndexOfXY(mousedown_x, mousedown_y)];
switch (cell_status) {
case NO_BOMB:
case BOMB:
case undefined:
drawCell(mousedown_x, mousedown_y);
break;
case BOMB_AND_FLAG:
case FLAG:
draw(mousedown_x, mousedown_y, FLAG);
break;
}
mousedown_x = -1;
mousedown_y = -1;
return true;
}
return false;
}
function getArrayIndexOfXY(x, y) {
return x + y * size;
}
function translatePixelToIndex(x, y) {
return [Math.trunc(x / item_size), Math.trunc(y / item_size)];
}
function drawCell(x, y) {
const indices = [
item_size * x,
item_size * y,
item_size * x + item_size,
item_size * y + item_size
];
context.fillStyle = "#065A82";
fillTriangle(
indices[0], indices[1],
indices[2], indices[1],
indices[0], indices[3]
)
context.fillStyle = "#002A66";
fillTriangle(
indices[2], indices[3],
indices[0], indices[3],
indices[2], indices[1]
)
context.fillStyle = "#003F9A";
context.fillRect(
indices[0] + gap_size,
indices[1] + gap_size,
inner_rect_size,
inner_rect_size);
}
function draw(x, y, s, c = "#00398C") {
const asset = s == BOMB ? "💣" : "🚩";
s == BOMB ? drawCellMouseDown(x, y, c) : drawCell(x, y);
context.font = (item_size * 0.5) + "px Arial";
context.fillText(
asset,
x * item_size + item_size * 0.2,
y * item_size + item_size * 0.6
);
}
function drawCellMouseDown(x, y, c = "#00398C") {
context.fillStyle = c;
context.fillRect(
x * item_size,
y * item_size,
item_size,
item_size
);
}
function drawGameOverScreen(win) {
color = win ? "green" : "red";
context.fillStyle = color;
const w = gap_size;
context.fillRect(0, 0, width, w);
context.fillRect(0, height - w, width, w);
context.fillRect(0, 0, w, height);
context.fillRect(height - w, 0, w, height);
}
function drawField() {
context.fillStyle = "#00398C";
context.fillRect(0, 0, width, height);
for (let x = 0; x < size; x++) {
for (let y = 0; y < size; y++) {
drawCell(x, y);
}
}
}
function fillTriangle(x1, y1, x2, y2, x3, y3) {
context.beginPath();
context.moveTo(x1, y1);
context.lineTo(x2, y2);
context.lineTo(x3, y3);
context.closePath();
context.fill();
}
function countNearBombs(x, y) {
let near_bombs_count = 0
for (let i = -1; i < 2; i++) {
for (let j = -1; j < 2; j++) {
const xi = x + i;
const yj = y + j;
if ((xi >= 0 && xi < size) && (yj >= 0 && yj < size)) {
if (i == 0 && j == 0) {
continue;
}
const ii = getArrayIndexOfXY(xi, yj);
if (cells_status[ii] == BOMB || cells_status[ii] == BOMB_AND_FLAG) {
near_bombs_count++;
}
}
}
}
return near_bombs_count;
}
async function calculateValueForOpenedCell(x, y) {
cells_status[getArrayIndexOfXY(x, y)] = OPENED;
empty_cells_remaining--;
let near_bombs_count = countNearBombs(x, y);
drawCellMouseDown(x, y);
if (near_bombs_count > 0) {
context.fillStyle = "#7f675b";
context.font = (item_size * 0.6) + "px Arial";
context.fillText(
near_bombs_count.toString(),
x * item_size + item_size * 0.3,
y * item_size + item_size * 0.7
);
await sleep(25);
} else {
for (let j = -1; j < 2; j++) {
for (let i = -1; i < 2; i++) {
const xi = x + i;
const yj = y + j;
if ((xi >= 0 && xi < size) && (yj >= 0 && yj < size)) {
if (i == 0 && j == 0) {
continue;
}
const ii = getArrayIndexOfXY(xi, yj);
if (cells_status[ii] == FLAG || cells_status[ii] == NO_BOMB) {
calculateValueForOpenedCell(xi, yj);
await sleep(25);
}
}
}
}
}
}
function init(x, y) {
cells_status = []
mousedown_x = 0;
mousedown_y = 0;
empty_cells_remaining = cell_count - bomb_count;
first_cell = false;
const reserved_cell = getArrayIndexOfXY(x, y);
for (let index = 0; index < cell_count; index++) {
cells_status.push(NO_BOMB);
}
for (let index = 0; index < bomb_count; index++) {
let i = getRandomInteger(0, cell_count);
if (i == reserved_cell || cells_status[i] == BOMB) {
index--;
continue;
}
cells_status[i] = BOMB;
}
}
function run() {
drawField()
}
run()