-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
286 lines (254 loc) · 8.77 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"use strict";
//Variables of te game
let currentScore = 0;
let activePlayer = 0;
let start = false;
let controlNum1 = 0;
const scores = [0, 0];
const WIN_SCORE = 100;
let winGame = false;
//Selecting section
const sections = document.querySelectorAll(".player");
//Selecting Buttons
const btnNew = document.querySelector(".btn--new");
const btnRoll = document.querySelector(".btn--roll");
const btnHold = document.querySelector(".btn--hold");
const btnStart = document.querySelector(".btn--start");
//Selecting the dice
const diceEl = document.querySelector(".dice");
//Selecting modal elements
const modalEl = document.querySelector(".modal");
const overlayEl = document.querySelector(".overlay");
const inPlayers = document.querySelectorAll(".player--name");
const errorsEl = document.querySelectorAll(".error");
//------------------------------------------------//
//--------- Rolling dice Functionality -----------//
//------------------------------------------------//
btnRoll.addEventListener("click", function () {
//Only if there isn't a winnner
if (!winGame) {
// 1. Generating a random dice roll
let secretDice = Math.trunc(Math.random() * 7);
if (secretDice === 0) {
controlNum1 = controlNum1 + 1;
if (controlNum1 === 2) {
while (controlNum1 === 2) {
secretDice = Math.trunc(Math.random() * 7);
if (secretDice !== 0) {
controlNum1 = 0;
}
}
}
} else {
controlNum1 = 0;
}
// 2. Display dice
diceEl.classList.remove("hidden");
//2.1. set the animation of dice
if (secretDice !== 0) {
diceEl.src = `dicePhoto/dice-${secretDice}.png`;
diceEl.classList.add("dice--animation");
document.querySelector(".dice--animation").style.borderColor = "#333";
setTimeout(function () {
diceEl.classList.remove("dice--animation");
}, 200);
} else {
diceEl.src = `dicePhoto/dice-${secretDice}.png`;
diceEl.classList.add("dice--animation");
document.querySelector(".dice--animation").style.borderColor = "#c7365f";
setTimeout(function () {
diceEl.classList.remove("dice--animation");
}, 200);
}
// 3. check for rolled 1: if is true switch to next palyer
if (secretDice !== 0) {
currentScore = currentScore + secretDice;
document.querySelector(`#current--${activePlayer}`).textContent =
currentScore;
} else {
currentScore = 0;
document.querySelector(`#current--${activePlayer}`).textContent =
currentScore;
document
.querySelector(`.player--${activePlayer}`)
.classList.remove("player--active");
//Switch player
activePlayer = activePlayer === 0 ? 1 : 0;
document
.querySelector(`.player--${activePlayer}`)
.classList.add("player--active");
}
}
});
//------------------------------------------------//
//--------- New Game functionality ---------------//
//------------------------------------------------//
btnNew.addEventListener("click", function () {
//1. set the control of num 1 for no num 1 at first roll
controlNum1 = 1;
// 1.1. Reset the current score
currentScore = 0;
//1.2. Reset the win control
if (winGame) {
document
.querySelector(`.player--${activePlayer}`)
.classList.remove("player--winner");
document
.querySelector(`.player--${activePlayer}`)
.classList.add("player--active");
winGame = false;
}
//1.3. reset the scores of two players
scores[0] = 0;
scores[1] = 0;
document.querySelector(`#score--${0}`).textContent = scores[0];
document.querySelector(`#score--${1}`).textContent = scores[1];
// 2. Remove the dice
diceEl.classList.add("hidden");
// 3. Construct the initial situation
if (activePlayer === 0) {
//The best case: only reset current
document.querySelector(`#current--${activePlayer}`).textContent =
currentScore;
} else {
//The worst case: 1)reset current
document.querySelector(`#current--${activePlayer}`).textContent =
currentScore;
//2)remove active player
document
.querySelector(`.player--${activePlayer}`)
.classList.remove("player--active");
//3) reset active player
activePlayer = 0;
//4)add active player
document
.querySelector(`.player--${activePlayer}`)
.classList.add("player--active");
}
//4. View the form
modalEl.classList.remove("hidden");
overlayEl.classList.remove("hidden");
// 5. Control the starter situation ONLY THE FIRST TIME
if (!start) {
start = true;
console.log("iniziamo");
for (let i = 0; i < sections.length; i++) {
sections[i].classList.remove("hidden");
}
btnRoll.classList.remove("hidden");
btnHold.classList.remove("hidden");
}
});
//------------------------------------------------//
//--------- Start Functionality ------------------//
//------------------------------------------------//
btnStart.addEventListener("click", function () {
let control = true;
// 1. Control the name player input
for (let j = 0; j < inPlayers.length; j++) {
errorsEl[j].textContent = "";
if (!inPlayers[j].value) {
errorsEl[j].textContent = `The field cannot be empty`;
control = false;
} else if (
inPlayers[j].value.includes("1") ||
inPlayers[j].value.includes("2") ||
inPlayers[j].value.includes("3") ||
inPlayers[j].value.includes("4") ||
inPlayers[j].value.includes("5") ||
inPlayers[j].value.includes("6") ||
inPlayers[j].value.includes("7") ||
inPlayers[j].value.includes("8") ||
inPlayers[j].value.includes("9") ||
inPlayers[j].value.includes("0")
) {
errorsEl[j].textContent = `The field must contain olnly letter`;
control = false;
} else if (
inPlayers[j].value.length < 2 ||
inPlayers[j].value.length > 10
) {
errorsEl[j].textContent = `The field must contain 2 to 10 characters`;
control = false;
}
}
//2. Start the game if control is OK
if (control) {
console.log("entro");
modalEl.classList.add("hidden");
overlayEl.classList.add("hidden");
document.querySelector("#name--0").textContent = inPlayers[0].value;
document.querySelector("#name--1").textContent = inPlayers[1].value;
// 2.1. Reset the form
inPlayers[0].value = "";
inPlayers[1].value = "";
}
});
//------------------------------------------------//
//--------- Hold Functionality -------------------//
//------------------------------------------------//
btnHold.addEventListener("click", function () {
//1. set the control of num 1 for no num 1 at first roll
controlNum1 = 1;
//2. Save the score only if different from 0
if (currentScore !== 0 && !winGame) {
// 2.1. Remove the dice
diceEl.classList.add("hidden");
// 2.2. Update the score
scores[activePlayer] += currentScore;
if (scores[activePlayer] === WIN_SCORE) {
//change the win control
winGame = true;
//Reset the current score
currentScore = 0;
document.querySelector(`#current--${activePlayer}`).textContent =
currentScore;
document.querySelector(`#score--${activePlayer}`).textContent = "WIN!";
//reset the scores for all player
scores[1] = 0;
scores[0] = 0;
//Change style of the winner
document
.querySelector(`.player--${activePlayer}`)
.classList.remove("player--active");
document
.querySelector(`.player--${activePlayer}`)
.classList.add("player--winner");
} else if (scores[activePlayer] > WIN_SCORE) {
//Decrase the score with the overflow
scores[activePlayer] =
scores[activePlayer] - (scores[activePlayer] - WIN_SCORE) * 2;
//Reset the current score
currentScore = 0;
document.querySelector(`#current--${activePlayer}`).textContent =
currentScore;
document.querySelector(`#score--${activePlayer}`).textContent =
scores[activePlayer];
document
.querySelector(`.player--${activePlayer}`)
.classList.remove("player--active");
//Switch player
activePlayer = activePlayer === 0 ? 1 : 0;
console.log(activePlayer);
document
.querySelector(`.player--${activePlayer}`)
.classList.add("player--active");
} else {
//Reset the current score
currentScore = 0;
document.querySelector(`#current--${activePlayer}`).textContent =
currentScore;
document.querySelector(`#score--${activePlayer}`).textContent =
scores[activePlayer];
document
.querySelector(`.player--${activePlayer}`)
.classList.remove("player--active");
//Switch player
activePlayer = activePlayer === 0 ? 1 : 0;
console.log(activePlayer);
document
.querySelector(`.player--${activePlayer}`)
.classList.add("player--active");
}
}
});