Files
2025-12-19 23:19:28 +01:00

71 lines
2.2 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function() {
let words = [];
fetch('./codenames.txt')
.then(function(response) {
return response.text();
})
.then(function(text) {
words = text.split("\n");
words = getMultipleRandom(words, 25);
console.log(words);
});
function getMultipleRandom(arr, num) {
let i = 0;
let result = [];
while (i < num) {
let index = Math.floor(Math.random() * arr.length);
result.push(arr[index]);
arr.splice(index, 1);
i++;
}
return result;
}
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++;
}
}
}
function fillFields() {
for (let i = 1; i <= 25; i++) {
document.getElementById(i).innerHTML = words[i-1];
}
}
let intervalId = setInterval(createField, 150);
document.getElementById("generate").addEventListener("click", function() {
clearInterval(intervalId);
createField();
fillFields();
});
});