-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
102 lines (92 loc) · 3.07 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
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
"use strict";
const score0El = document.querySelector("#score--0");
const score1El = document.getElementById("score--1");
const current0El = document.getElementById("current--0");
const current1El = document.getElementById("current--1");
const diceEl = document.querySelector(".dice");
const btnRoll = document.querySelector(".btn--roll");
const btnNew = document.querySelector(".btn--new");
const btnHold = document.querySelector(".btn--hold");
const player0El = document.querySelector(".player--0");
const player1El = document.querySelector(".player--1");
let currentScore = 0;
function init() {
currentScore = 0;
score0El.textContent = 0;
score1El.textContent = 0;
current0El.textContent = 0;
current1El.textContent = 0;
diceEl.classList.add("hidden");
}
function disableBtn() {
btnRoll.classList.add("disabled");
btnHold.classList.add("disabled");
}
function enableBtn() {
btnRoll.classList.remove("disabled");
btnHold.classList.remove("disabled");
}
init();
btnRoll.addEventListener("click", function () {
const dice = Math.trunc(Math.random() * 6) + 1;
diceEl.classList.remove("hidden");
diceEl.src = `dice-${dice}.png`;
let activePlayer = player0El.classList.contains("player--active") ? 0 : 1;
if (dice !== 1) {
currentScore += dice;
if (activePlayer === 0) {
current0El.textContent = currentScore;
} else if (activePlayer === 1) {
current1El.textContent = currentScore;
}
} else {
if (activePlayer === 0) {
current0El.textContent = 0;
} else if (activePlayer === 1) {
current1El.textContent = 0;
}
player0El.classList.toggle("player--active");
player1El.classList.toggle("player--active");
currentScore = 0;
}
});
btnHold.addEventListener("click", function () {
let activePlayer = player0El.classList.contains("player--active") ? 0 : 1;
if (activePlayer === 0) {
score0El.textContent =
Number(score0El.textContent) + Number(current0El.textContent);
current0El.textContent = 0;
if (Number(score0El.textContent) >= 100) {
player0El.classList.add("player--winner");
diceEl.classList.add("hidden");
disableBtn();
} else {
player0El.classList.remove("player--active");
player1El.classList.add("player--active");
}
} else if (activePlayer === 1) {
score1El.textContent =
Number(score1El.textContent) + Number(current1El.textContent);
current1El.textContent = 0;
if (Number(score1El.textContent) >= 100) {
player1El.classList.add("player--winner");
diceEl.classList.add("hidden");
disableBtn();
} else {
player1El.classList.remove("player--active");
player0El.classList.add("player--active");
}
}
currentScore = 0;
});
btnNew.addEventListener("click", function () {
init();
if (player0El.classList.contains("player--winner")) {
player0El.classList.remove("player--winner");
player0El.classList.add("player--active");
} else if (player1El.classList.contains("player--winner")) {
player1El.classList.remove("player--winner");
player1El.classList.add("player--active");
}
enableBtn();
});