-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
157 lines (138 loc) · 4.49 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";
//box
const box_0 = document.querySelector(".box-0");
const box_1 = document.querySelector(".box-1");
const box_2 = document.querySelector(".box-2");
const box_3 = document.querySelector(".box-3");
//player score
const scoreZero = document.querySelector(".score-0");
const scoreFirst = document.querySelector(".score-1");
const scoreSecond = document.querySelector(".score-2");
const scoreThird = document.querySelector(".score-3");
//curretn box selection
const currentFirstText = document.querySelector(".current-0-text");
const currentSecondText = document.querySelector(".current-1-text");
const currentThirdText = document.querySelector(".current-2-text");
const currentForthText = document.querySelector(".current-3-text");
//btn selection
const btnNew = document.querySelector(".btn-new");
const btnRoll = document.querySelector(".btn-roll");
const btnDice = document.querySelector(".btn-dice");
//image selection
const imageOfDice = document.querySelector(".image-2");
//Changing the value of score
scoreZero.textContent = 0;
scoreFirst.textContent = 0;
scoreSecond.textContent = 0;
scoreThird.textContent = 0;
imageOfDice.classList.add("hidden");
//Changing the value of current score
currentFirstText.textContent = 0;
currentSecondText.textContent = 0;
currentThirdText.textContent = 0;
currentForthText.textContent = 0;
//bright class
box_0.classList.add("bright");
//Variables
let currentValue = 0;
let currentPlayer = 0;
let totalScore = [0, 0, 0, 0];
let playing = true;
//Creating function for roll dice
btnRoll.addEventListener("click", function () {
if (playing) {
let vlaueOfDice = Math.trunc(Math.random() * 6) + 1;
//Targeting image
imageOfDice.classList.remove("hidden");
imageOfDice.src = `dice-${vlaueOfDice}.png`;
//If else condition
if (vlaueOfDice !== 1) {
currentValue += vlaueOfDice;
document.querySelector(`.current-${currentPlayer}-text`).textContent =
currentValue;
} else {
currentValue = 0;
document.querySelector(`.current-${currentPlayer}-text`).textContent =
currentValue;
if (currentPlayer === 0) {
currentValue = 0;
currentPlayer = 1;
box_0.classList.toggle("bright");
box_1.classList.toggle("bright");
} else if (currentPlayer === 1) {
currentValue = 0;
currentPlayer = 2;
box_1.classList.toggle("bright");
box_2.classList.toggle("bright");
} else if (currentPlayer === 2) {
currentValue = 0;
currentPlayer = 3;
box_2.classList.toggle("bright");
box_3.classList.toggle("bright");
} else if (currentPlayer === 3) {
currentValue = 0;
currentPlayer = 0;
box_3.classList.toggle("bright");
box_0.classList.toggle("bright");
}
}
}
});
btnDice.addEventListener("click", function () {
if(playing) {
totalScore[currentPlayer] += currentValue;
document.querySelector(`.score-${currentPlayer}`).textContent =
totalScore[currentPlayer];
if (totalScore[currentPlayer] >= 20) {
document.querySelector(`.box-${currentPlayer}`).classList.add('winner');
playing = false;
} else {
currentValue = 0;
document.querySelector(`.current-${currentPlayer}-text`).textContent =
currentValue;
if (currentPlayer === 0) {
currentValue = 0;
currentPlayer = 1;
box_0.classList.toggle("bright");
box_1.classList.toggle("bright");
} else if (currentPlayer === 1) {
currentValue = 0;
currentPlayer = 2;
box_1.classList.toggle("bright");
box_2.classList.toggle("bright");
} else if (currentPlayer === 2) {
currentValue = 0;
currentPlayer = 3;
box_2.classList.toggle("bright");
box_3.classList.toggle("bright");
} else if (currentPlayer === 3) {
currentValue = 0;
currentPlayer = 0;
box_3.classList.toggle("bright");
box_0.classList.toggle("bright");
}
}
}
});
btnNew.addEventListener('click', function() {
playing = true;
currentValue = 0;
totalScore = [0, 0, 0, 0];
currentPlayer = 0;
scoreZero.textContent = totalScore[0];
scoreFirst.textContent = totalScore[1];
scoreSecond.textContent = totalScore[2];
scoreThird.textContent = totalScore[3];
currentFirstText.textContent = currentValue;
currentSecondText.textContent = currentValue;
currentThirdText.textContent = currentValue;
currentForthText.textContent = currentValue;
box_0.classList.remove('winner');
box_1.classList.remove('winner');
box_2.classList.remove('winner');
box_3.classList.remove('winner');
box_0.classList.add("bright");
box_1.classList.remove("bright");
box_2.classList.remove("bright");
box_3.classList.remove("bright");
});