-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (70 loc) · 2.94 KB
/
index.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
// Function to update dice images based on random numbers
function updateDiceImages() {
var randomNumbers = [];
// Generate random numbers for each dice and update images
for (var i = 1; i <= 4; i++) {
var randomNumber = Math.floor(Math.random() * 6) + 1;
updateImg(randomNumber, `.img${i}`);
randomNumbers.push(randomNumber);
}
// Determine the winner or if it's a draw
determineWinner(randomNumbers);
}
// Function to update individual dice image based on random number
function updateImg(randomNumber, imgClass) {
var imgElement = document.querySelector(imgClass);
imgElement.setAttribute("src", `./images/dice${randomNumber}.png`);
}
// Function to determine winner or draw
function determineWinner(randomNumbers) {
var resultElement = document.querySelector('h2');
var sum = randomNumbers.reduce((acc, curr) => acc + curr, 0);
// Determine if there is a winner or a draw
var maxNumber = Math.max(...randomNumbers);
var winners = randomNumbers.filter(number => number === maxNumber);
if (winners.length === 1) {
var winnerIndex = randomNumbers.indexOf(maxNumber) + 1;
resultElement.textContent = `${getPlayerName(winnerIndex)} Wins!`;
} else {
resultElement.textContent = "It's a Draw!";
}
// Show the result element
resultElement.style.display = 'block';
}
// Function to limit input length to 15 characters
function limitInputLength(event, element, maxLength) {
if (element.textContent.length >= maxLength && event.key !== 'Backspace') {
event.preventDefault();
}
if (event.key === 'Enter') {
event.preventDefault(); // Prevent default behavior (creating a new line)
element.blur(); // Remove focus from the editable element
}
}
// Function to update player names and store in sessionStorage
function updateName(playerNumber) {
let playerName = document.getElementById(`player${playerNumber}`).textContent.trim();
sessionStorage.setItem(`player${playerNumber}`, playerName);
}
// Function to load player names from sessionStorage on window load
function loadNames() {
for (let i = 1; i <= 4; i++) {
const savedName = sessionStorage.getItem(`player${i}`);
if (savedName) {
document.getElementById(`player${i}`).textContent = savedName;
}
attachEventListeners(i);
}
}
function attachEventListeners(playerNumber) {
const playerElement = document.getElementById(`player${playerNumber}`);
playerElement.addEventListener('input', () => updateName(playerNumber));
}
function getPlayerName(playerIndex){
return document.getElementById(`player${playerIndex}`).textContent.trim();
}
// Initialize: Roll dice and determine winner/draw on page load
window.onload = function() {
loadNames();
document.getElementById('playButton').addEventListener('click', updateDiceImages);
};