-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
57 lines (46 loc) · 1.01 KB
/
script.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
const board = document.getElementById('game-board'),
scoreboard = document.getElementById('score');
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
equals(x, y) {
return this.x === x && this.y === y;
}
}
const ships = [
[new Point(1, 1), new Point(1, 2), new Point(1, 3), new Point(1, 4)],
[new Point(3, 0), new Point(4, 0), new Point(5, 0)]
];
let score = 0;
for (let i = 0; i < 10; i++) {
const row = document.createElement('div');
row.className = 'row';
for (let j = 0; j < 10; j++) {
const tile = document.createElement('div');
tile.className = 'tile';
tile.addEventListener('click', () => {
if (isHit(i, j)) {
tile.classList.add('hit');
score++;
scoreboard.textContent = score.toString();
} else {
tile.classList.add('miss');
}
});
row.appendChild(tile);
}
board.appendChild(row);
}
function isHit(i, j) {
let hit = false;
ships.forEach((ship) => {
ship.forEach((point) => {
if (point.equals(i, j)) {
hit = true;
}
});
});
return hit;
}