-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
81 lines (68 loc) · 2.19 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
"use strict";
let currentscore, turn, score, playing;
// when starting the game
const score0 = document.querySelector("#score--0");
const score1 = document.getElementById("score--1");
const currentscore0 = document.getElementById("current--0");
const currentscore1 = document.getElementById("current--1");
const player0 = document.querySelector(".player--0");
const player1 = document.querySelector(".player--1");
const dice = document.querySelector(".dice");
dice.classList.add("hidden");
const hold = document.querySelector(".btn--hold");
const roll = document.querySelector(".btn--roll");
const newgame = document.querySelector(".btn--new");
// switching takes place
const switchturn = function () {
currentscore = 0;
document.getElementById(`current--${turn}`).textContent = currentscore;
document.querySelector(`.player--${turn}`).classList.remove("player--active");
turn = (turn + 1) % 2;
document.querySelector(`.player--${turn}`).classList.add("player--active");
};
roll.addEventListener("click", () => {
if (playing) {
const rand = Math.trunc(Math.random() * 6) + 1;
dice.src = `img/dice-${rand}.png`;
dice.classList.remove("hidden");
if (rand === 1) {
switchturn();
} else {
currentscore += rand;
}
document.getElementById(`current--${turn}`).textContent = currentscore;
}
});
hold.addEventListener("click", () => {
if (playing) {
score[turn] += currentscore;
document.getElementById(`score--${turn}`).textContent = score[turn];
if (score[turn] >= 20) {
playing = false;
dice.classList.add("hidden");
document
.querySelector(`.player--${turn}`)
.classList.add("player--winner");
} else {
switchturn();
}
}
});
const init = function () {
currentscore = 0;
turn = 0;
score = [0, 0];
playing = true;
playing = true;
score0.textContent = 0;
score1.textContent = 0;
currentscore0.textContent = 0;
currentscore1.textContent = 0;
dice.classList.add("hidden");
player0.classList.add("player--active");
player1.classList.remove("player--active");
player0.classList.remove("player--winner");
player1.classList.remove("player--winner");
};
init();
newgame.addEventListener("click", init);