-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcodenames.js
More file actions
40 lines (35 loc) · 1.47 KB
/
codenames.js
File metadata and controls
40 lines (35 loc) · 1.47 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
document.addEventListener('DOMContentLoaded', function() {
function createField() {
document.querySelectorAll('td').forEach(td => {
td.style.backgroundColor = "white";
});
let counter = 0;
let previous_values = new Set();
let startingTeam = Math.round(Math.random());
let startingTeamColor = "#1e88e5";
let otherTeamColor = "#d32f2f";
if (startingTeam === 1) {
[startingTeamColor, otherTeamColor] = [otherTeamColor, startingTeamColor];
}
document.getElementById("grid").style.borderColor = startingTeamColor;
while (counter < 18) {
let random = Math.floor(Math.random() * 25) + 1;
if (!previous_values.has(random)) {
if (counter < 8) {
document.getElementById(random.toString()).style.backgroundColor = otherTeamColor;
} else if (counter > 8 && counter < 17) {
document.getElementById(random.toString()).style.backgroundColor = startingTeamColor;
} else {
document.getElementById(random.toString()).style.backgroundColor = "grey";
}
previous_values.add(random);
counter++;
}
}
}
let intervalId = setInterval(createField, 150);
document.getElementById("generate").addEventListener("click", function() {
clearInterval(intervalId);
createField();
});
});