-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent-handlers.js
More file actions
113 lines (103 loc) · 3.36 KB
/
event-handlers.js
File metadata and controls
113 lines (103 loc) · 3.36 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
import { selectGame, handleClick, resetGame, aiShoot } from "./index.js";
import { data } from "./data.js";
const CHAR_CODE_OF_A = "A".codePointAt(0);
const PLAYING_AREA_HEIGHT = 80;
function initGame() {
document.querySelector("#reset").addEventListener("click", resetGame);
document.querySelector("#aiShoot").addEventListener("click", () => {
const boardSize = document.querySelectorAll(".container1 .row").length;
aiShoot({
x: String.fromCharCode(
Math.floor(Math.random() * boardSize + CHAR_CODE_OF_A)
),
y: Math.floor(Math.random() * boardSize + 1),
});
});
for (const gameNumber in data) {
document.getElementById("mode").insertAdjacentHTML(
"beforeend",
`
<option value=${data[gameNumber]}>${gameNumber}</option>
`
);
}
document
.querySelector(".mode > select")
.addEventListener("input", (e) => selectGame(e.target.value));
}
function handleClickHelper(event, x, y, clickType, source) {
event.preventDefault();
handleClick({
x: String.fromCharCode(CHAR_CODE_OF_A + x),
y: y + 1,
clickType,
source,
});
}
export function displayBoard(boardStruct) {
const grid = boardStruct.board;
const containerElement = document.querySelector(
`.container${boardStruct.boardNumber}`
);
console.log(containerElement);
containerElement.innerHTML = "";
containerElement.insertAdjacentHTML("afterbegin", creatHeadRow(grid.length));
for (let x = 0; x < grid.length; x++) {
const rowElement = document.createElement("div");
rowElement.classList.add("row");
rowElement.insertAdjacentHTML(
"afterbegin",
`<div class="head-cell" style='heigth: ${
90 / (grid.length + 1)
}vh; width: 3vh'>${String.fromCharCode(CHAR_CODE_OF_A + x)}</div>`
);
for (let y = 0; y < grid[x].length; y++) {
const cellElement = document.createElement("div");
cellElement.classList.add("cell");
cellElement.innerHTML = grid[x][y];
cellElement.style.width = `${
PLAYING_AREA_HEIGHT / (grid[x].length + 1)
}vh`;
cellElement.style.height = `${
PLAYING_AREA_HEIGHT / (grid[x].length + 1)
}vh`;
cellElement.style.fontSize = `${
PLAYING_AREA_HEIGHT / (grid[x].length + 1) - 5
}vh`;
cellElement.addEventListener("click", (event) => {
handleClickHelper(event, x, y, "left", boardStruct.boardNumber);
});
cellElement.addEventListener("contextmenu", (event) => {
handleClickHelper(event, x, y, "right", boardStruct.boardNumber);
});
rowElement.appendChild(cellElement);
}
containerElement.appendChild(rowElement);
}
}
function creatHeadRow(length) {
let result = `<div class='head-row'><div style='width: ${
PLAYING_AREA_HEIGHT / (length + 1)
}vh'></div>`;
for (let i = 1; i <= length; i++) {
console.log(length);
result += `<div class="head-cell" style='width: ${
PLAYING_AREA_HEIGHT / (length + 1)
}vh'>${i}</div>`;
}
return `${result}</div>`;
}
function display(target, message, color) {
const targetElement = document.getElementById(target);
targetElement.style.color = color;
targetElement.innerHTML = message;
}
export function displayMessage(...rest) {
display("display", ...rest);
}
export function displayTextMessage(...rest) {
display("textDisplay", ...rest);
}
window.addEventListener("load", () => {
initGame();
});