-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (123 loc) · 4.18 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
let dice = document.querySelector('.dice');
let scoreTotal0 = document.getElementById('score--0');
let scoreTotal1 = document.getElementById('score--1');
let curr0El = document.getElementById('current--0');
let curr1El = document.getElementById('current--1');
// Reset The Game
scoreTotal0.textContent = 0;
scoreTotal1.textContent = 0;
dice.classList.add('hidden');
// Selecting buttons
let btnNew = document.querySelector('.btn--new');
let btnRoll = document.querySelector('.btn--roll');
let btnHold = document.querySelector('.btn--hold');
// Roll Dice button Text :
let rollText = btnRoll.textContent;
let holdText = btnHold.textContent;
let genNumber;
let total0 = 0,
total1 = 0,
curr0 = 0,
curr1 = 0;
let turn = 0;
// Generating number
function generateRandomNumber(min, max) {
return Math.trunc(Math.random() * (max - min + 1)) + min;
}
// Change current player
let changeTurn = function () {
// Remove the 'player--active' class from the current player
document.querySelector(`.player--${turn}`).classList.remove('player--active');
// Toggle the turn between 0 and 1
turn = turn === 0 ? 1 : 0;
// Add the 'player--active' class to the new current player
document.querySelector(`.player--${turn}`).classList.add('player--active');
};
// Calc current score
let calcCurrentScore = function (number) {
if (number !== 1) {
turn === 0 ? (curr0 += number) : (curr1 += number);
} else {
turn === 0 ? (curr0 = 0) : (curr1 = 0);
// reset current score and change turns
document.getElementById(`current--${turn}`).textContent = 0;
changeTurn();
}
document.getElementById(`current--${turn}`).textContent =
turn == 0 ? curr0 : curr1;
};
// Rolling dice function
let rollClicked = function () {
genNumber = generateRandomNumber(1, 6);
console.log(genNumber);
// Change the dice image
dice.src = `dice-${genNumber}.png`;
// Display the dice if hidden
dice.classList.remove('hidden');
// Calc current score
calcCurrentScore(genNumber, turn);
};
// Clicking the "Roll Dice" button
btnRoll.addEventListener('click', rollClicked);
let checkWinner = function () {
if (total0 >= 100 || total1 >= 100) {
document.querySelector(`.player--${turn}`).classList.add('player--winner');
btnRoll.disabled = true;
btnHold.disabled = true;
btnRoll.textContent = `🎉 PLAYER ${turn + 1} WON`;
btnHold.textContent = `🙏 PLEASE START NEW GAME`;
return true;
}
return false;
};
// Clicking hold
let holdClicked = function () {
// When clicking hold -> add the current score of the player to the total and change the player
// sum the score
turn === 0 ? (total0 += curr0) : (total1 += curr1);
// display the new total
document.getElementById(`score--${turn}`).textContent =
turn == 0 ? total0 : total1;
// set the current score of the current turn to 0
turn === 0 ? (curr0 = 0) : (curr1 = 0);
document.getElementById(`current--${turn}`).textContent =
turn == 0 ? curr0 : curr1;
// Check winner, if yes, end the game and don't change turns right now
// change the turn
if (!checkWinner()) changeTurn();
};
// Clicking the "HOLD" button
btnHold.addEventListener('click', holdClicked);
// Starting new game
let resetGame = function () {
// -> Reset current and total scores elements and variables
// -> reset the turn to player1
// -> reset the color of the winner
// -> enable "ROll" and "HOLD" buttons
// -> Hide the dice
// 1- Reset current and total scores elements and variables
curr0El.textContent = 0;
curr1El.textContent = 0;
curr0 = 0;
curr1 = 0;
scoreTotal0.textContent = 0;
scoreTotal1.textContent = 0;
total0 = 0;
total1 = 0;
// 2- Reset the color of the winner
document.querySelector(`.player--${turn}`).classList.remove('player--winner');
// 3- Reset the turn to player1
// make the turn to player2 then call the function changeTurn and it will change the turn for us
turn = 1;
changeTurn();
// 4- enable "ROll" and "HOLD" buttons
btnRoll.disabled = false;
btnHold.disabled = false;
btnRoll.textContent = rollText;
btnHold.textContent = holdText;
// hide the dice
dice.classList.add('hidden');
};
// clicking NEW GAME button
btnNew.addEventListener('click', resetGame);