-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
187 lines (144 loc) · 5.1 KB
/
Copy pathscript.js
File metadata and controls
187 lines (144 loc) · 5.1 KB
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
'use strict';
const image = document.querySelector(".dice");
const p1CurrentElement = document.getElementById("current--0");
const p2CurrentElement = document.getElementById("current--1");
const current = document.querySelectorAll(".current");
const p1Total = document.getElementById("score--0");
const p2Total = document.getElementById("score--1");
const score = document.querySelectorAll("score");
const player0 = document.querySelector('.player--0');
const player1 = document.querySelector('.player--1');
const btnNew = document.querySelector('.btn--new');
const btnRoll = document.querySelector('.btn--roll');
const btnHold = document.querySelector('.btn--hold');
const diceEl = document.querySelector('.dice');
const body = document.querySelector(".body");
const themes = document.querySelector(".themes");
const main = document.querySelector(".main");
const themeTab = document.querySelector(".theme-tab");
let currentScore, playing;
var p1 = 0;
var p2 = 0;
var p1TotalScore = 0;
var p2TotalScore = 0;
var scores = [0, 0];
var activePlayer = 0;
// Starting conditions
const init = function () {
scores = [0, 0];
// WE USE AN ARRAY AND NOT A VARIABLE BCZ THAT WAY WE CAN HOLD BOTH SCORE VALUES
// IN A SINGLE ARRAY
currentScore = 0;
activePlayer = 0; // is base set to 0 bcz we want player 1 to start first
playing = true;
p1Total.textContent = 0;
p2Total.textContent = 0;
p1CurrentElement.textContent = 0;
p2CurrentElement.textContent = 0;
diceEl.classList.add('hidden');
player0.classList.remove('player--winner');
player1.classList.remove('player--winner');
player0.classList.add('player--active');
player1.classList.remove('player--active');
document.getElementById("name--0").textContent= "PLAYER 1";
document.getElementById("name--1").textContent= "PLAYER 2";
};
init();
var dice = Math.trunc(Math.random()*6)+1;
/*
The Math.trunc() function returns the integer part of a number by removing
any fractional digits.
Ex:
console.log(Math.trunc(13.37));
// expected output: 13
console.log(Math.trunc(42.84));
// expected output: 42
console.log(Math.trunc(0.123));
// expected output: 0
*/
var nextPlayer = function(){
currentScore=0;
image.src=`dice-${dice}.png`;
document.getElementById(`current--${activePlayer}`).textContent = currentScore;
activePlayer = activePlayer === 0 ? 1 : 0;
player0.classList.toggle("player--active");
player1.classList.toggle("player--active");
// the .toggle("class name") works like this: if the element has the class
// you specified, it will remove that class and if that element doesn't have that class
// it will add it
}
//---MAKE THE ROLL BTN WORK---
btnRoll.addEventListener("click", function(){
// Add the dice image when the game is on
if(playing){
diceEl.classList.remove('hidden');
} else {
diceEl.classList.add('hidden');
}
dice = Math.trunc(Math.random()*6)+1;
if(dice != 1 && playing){
//Add the dice to the current score
currentScore += dice;
image.src=`dice-${dice}.png`;
document.getElementById(`current--${activePlayer}`).textContent = currentScore;
}
else {
// Switch to next player
nextPlayer();
}
});
//---MAKE THE HOLD BTN WORK---
btnHold.addEventListener("click", function(){
// WE FIRST CHECK IF THE GAME IS ON OR NOT BY CHECKING IF THE VALUE OF PLAYING
// IS true OR false
if(playing){
// 1. Add current score to active Players score
scores[activePlayer] += currentScore;
document.getElementById(`score--${activePlayer}`).textContent = scores[activePlayer];
// 2. Check if score is >=100
if(scores[activePlayer] >= 100){
// 3. End the Game
document.querySelector(`.player--${activePlayer}`).classList.add("player--winner");
document.getElementById(`name--${activePlayer}`).textContent = "WINNER PLAYER 1";
playing = false;
} else {
//Else switch to the next Player
nextPlayer();
}
}
});
//---MAKE NEW GAME BTN WORK---
btnNew.addEventListener("click", function(){init()});
// ---- THEMES ----
document.querySelector(".purple").addEventListener("click", function(){
body.classList.add("purple-background-theme");
console.log(current.length);
p1Total.classList.add("purple-text");
p2Total.classList.add("purple-text");
for(let i = 0; i <current.length; i++){
current[i].classList.add("purple-theme");
console.log("AAAAAAAAAAAAA");
}
});
document.querySelector(".clasic").addEventListener("click", function(){
body.classList.remove("purple-background-theme");
p1Total.classList.remove("purple-text");
p2Total.classList.remove("purple-text");
for(let i = 0; i <current.length; i++){
current[i].classList.remove("purple-theme");
}
});
document.addEventListener("keydown", function(event){
console.log(event.key); //this will log the key that was pressed
if(event.key == "Tab"){
/*
const body = document.querySelector(".body");
const themes = document.querySelector(".themes");
const main = document.querySelector(".main");
const themeTab = document.querySelector(".theme-tab");
*/
themes.classList.toggle("hidden");
main.classList.toggle("hidden");
themeTab.classList.toggle("hidden");
}
});