-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
74 lines (74 loc) · 2.32 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
'use strict';
// Elements & Variables
const active0 = document.querySelector('.player--0');
const active1 = document.querySelector('.player--1');
const score0 = document.getElementById('score--0');
const score1 = document.getElementById('score--1');
const newGame = document.querySelector('.btn--new');
const rollDice = document.querySelector('.btn--roll');
const hold = document.querySelector('.btn--hold');
const dice = document.querySelector('.dice');
let currentScore0 = document.getElementById('current--0');
let currentScore1 = document.getElementById('current--1');
let currentScore = 0;
let activePlayer = 0;
const scores = [0, 0];
let playing = true;
const switchPlayer = function () {
document.getElementById(`current--${activePlayer}`).textContent = 0;
currentScore = 0;
activePlayer = activePlayer === 0 ? 1 : 0;
active0.classList.toggle('player--active');
active1.classList.toggle('player--active');
};
// Starting Point
dice.classList.add('hidden');
score0.textContent = 0;
score1.textContent = 0;
// New Game Button
newGame.addEventListener('click', function () {
playing = true;
dice.classList.add('hidden');
active0.classList.remove('player--winner');
active1.classList.remove('player--winner');
score0.textContent = 0;
score1.textContent = 0;
currentScore0.textContent = 0;
currentScore1.textContent = 0;
});
// Roll Dice Button
rollDice.addEventListener('click', function () {
if (playing) {
dice.classList.remove('hidden');
const rDice = Math.trunc(Math.random() * 6) + 1;
dice.src = `dice-${rDice}.png`;
if (rDice !== 1) {
currentScore += rDice;
document.getElementById(`current--${activePlayer}`).textContent =
currentScore;
} else {
switchPlayer();
}
}
});
// Hold Button
hold.addEventListener('click', function () {
if (playing) {
scores[activePlayer] += currentScore;
document.getElementById(`score--${activePlayer}`).textContent =
scores[activePlayer];
// Check if player score is 77
if (scores[activePlayer] >= 77) {
playing = false;
document
.querySelector(`.player--${activePlayer}`)
.classList.add('player--winner');
document
.querySelector(`.player--${activePlayer}`)
.classList.remove('player--active');
dice.classList.add('hidden');
} else {
}
switchPlayer();
}
});