-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
75 lines (71 loc) · 2.44 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"use strict";
const score0El = document.querySelector("#score--0");
const score1El = document.getElementById("score--1");
score0El.textContent = 0;
score1El.textContent = 0;
const diceEl = document.querySelector(".dice");
const newBtn = document.querySelector(".btn--new");
const holdBtn = document.querySelector(".btn--hold");
const rollBtn = document.querySelector(".btn--roll");
const current0El = document.getElementById("current--0");
const current1El = document.getElementById("current--1");
const player0 = document.querySelector(".player--0");
const player1 = document.querySelector(".player--1");
let playing, currentScore, activePlayer, scores;
const init = function () {
playing = true;
scores = [0, 0];
currentScore = 0;
activePlayer = 0;
score0El.textContent = 0;
score1El.textContent = 0;
current0El.textContent = 0;
current1El.textContent = 0;
diceEl.classList.add("hidden");
player0.classList.remove("player--winner");
player1.classList.remove("player--winner");
player0.classList.add("player--active");
player1.classList.remove("player--active");
};
init();
const switchPlayer = function () {
document.getElementById(`current--${activePlayer}`).textContent = 0;
activePlayer = activePlayer === 0 ? 1 : 0;
player0.classList.toggle("player--active");
player1.classList.toggle("player--active");
currentScore = 0;
};
rollBtn.addEventListener("click", function () {
if (playing) {
const rndNumber = Math.trunc(Math.random() * 6) + 1;
diceEl.classList.remove("hidden");
diceEl.src = `imagini-zar/dice-${rndNumber}.png`;
if (rndNumber !== 1) {
currentScore += rndNumber;
document.getElementById(
`current--${activePlayer}`
).textContent = currentScore;
console.log(currentScore);
} else {
switchPlayer();
}
}
});
holdBtn.addEventListener("click", function () {
if (playing) {
scores[activePlayer] += currentScore;
document.getElementById(`score--${activePlayer}`).textContent =
scores[activePlayer];
if (scores[activePlayer] >= 100) {
playing = false;
diceEl.classList.add("hidden");
document
.querySelector(`.player--${activePlayer}`)
.classList.add("player--winner");
document
.querySelector(`.player--${activePlayer}`)
.classList.remove("player--active");
} else switchPlayer();
}
});
newBtn.addEventListener("click", init);