-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.js
213 lines (161 loc) · 4.61 KB
/
simple.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
//*Variables
var scores, roundScores, activePlayer, dice;
//Variable Initialization
scores = [0, 0];
roundScore = 0;
activePlayer = 0;
//Hiding the Oppoite player's roll button and the dice
document
.querySelector(".dice")
.style.display = "none";
document
.querySelector(".btn-roll" + (activePlayer + 1))
.style.display = "none";
//*New Game Function (Resets Everything)
function newGame() {
document
.querySelector(".player-" + activePlayer + "-panel")
.classList.remove("winner");
document
.querySelector(".player-0-panel")
.classList.add("active");
document
.querySelector(".player-1-panel")
.classList.remove("active");
document
.getElementById("name-" + activePlayer)
.textContent = "Player "+(activePlayer+1);
scores[0] = 0;
scores[1] = 0;
roundScore = 0;
activePlayer = 0;
document
.querySelector(".dice")
.style.display = "none";
document
.querySelector(".btn-roll" + activePlayer)
.style.display = "block";
document
.querySelector(".btn-roll" + (activePlayer + 1))
.style.display = "none";
document
.querySelector(".btn-hold")
.style.display = "block";
document
.getElementById("score-0")
.textContent = 0;
document
.getElementById("score-1")
.textContent = 0;
document
.getElementById("current-0")
.textContent = 0;
document
.getElementById("current-1")
.textContent = 0;
}
//*Roll Button Press Funtion (Adds the rolled amount to round score)
function btn() {
var diceDOM = document.querySelector(".dice");
dice = Math.floor(Math.random() * 6) + 1;
console.log(dice);
if (dice != 1) {
diceDOM.style.display = "block";
diceDOM.src = "assets/dice-" + dice + ".png";
roundScore += dice;
document
.getElementById("current-" + activePlayer)
.textContent = roundScore;
} else {
roundScore = 0;
diceDOM.style.display = "block";
diceDOM.src = "assets/dice-1.png";
document
.getElementById("current-" + activePlayer)
.textContent = roundScore;
changeActivePlayer();
}
}
//*Hold Button Function (Adds round score to global score checks for winner)
function hold() {
if (roundScore === 0) {
return;
}
scores[activePlayer] += roundScore;
roundScore = 0;
document
.getElementById("score-" + activePlayer)
.textContent = scores[activePlayer];
document
.getElementById("current-" + activePlayer)
.textContent = roundScore;
if (scores[activePlayer] >= 100) {
//Doesn't change activePlayer
playerWin();
return;
}
changeActivePlayer();
//*Player Win Function (Removes all buttons and dice)
}
function playerWin() {
document
.getElementById("name-" + activePlayer)
.textContent = "Winner!";
document
.querySelector(".player-" + activePlayer + "-panel")
.classList.add("winner");
document
.querySelector(".player-" + activePlayer + "-panel")
.classList.remove("active");
//Hides Dice from view
document
.querySelector(".dice")
.style.display = "none";
//Automatically forces players to start new game
document
.querySelector(".btn-roll" + activePlayer)
.style.display = "none";
document
.querySelector(".btn-hold")
.style.display = "none";
}
//*Active Player Swap Function
function changeActivePlayer() {
document
.querySelector(".player-" + activePlayer + "-panel")
.classList.remove("active");
document
.querySelector(".player-" + activePlayer + "-current-box")
.classList.remove("active-box");
document
.querySelector(".btn-roll" + activePlayer)
.style.display = "none";
if (activePlayer === 0) {
activePlayer = 1;
} else {
activePlayer = 0;
}
//Also changes "Current" box's attribute to active
document
.querySelector(".player-" + activePlayer + "-panel")
.classList.add("active");
document
.querySelector(".player-" + activePlayer + "-current-box")
.classList.add("active-box");
document
.querySelector(".btn-roll" + activePlayer)
.style.display = "block";
}
//*Button Listeners
document
.querySelector(".btn-roll0")
.addEventListener("click", btn);
document
.querySelector(".btn-roll1")
.addEventListener("click", btn);
document
.querySelector(".btn-hold")
.addEventListener("click", hold);
document
.querySelector(".btn-new")
.addEventListener("click", newGame);