-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
168 lines (134 loc) · 4.45 KB
/
Copy pathscript.js
File metadata and controls
168 lines (134 loc) · 4.45 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
'use strict';
const player0 = document.querySelector(`.player--0`)
const player1 = document.querySelector(`.player--1`)
const totalScoresEl = document.querySelector(`.score`);
const currentScores = document.querySelectorAll(`.current-score`);
let totalScoreP1 = document.querySelector(`#score--0`);
let totalScoreP2 = document.querySelector(`#score--1`);
let currentScoreP1 = document.querySelector(`#current--0`);
let currentScoreP2 = document.querySelector(`#current--1`);
let dice = document.querySelector(`.dice`);
const totalScores = [0, 0]
let P1TS = 0
let P2TS = 0
let currentScore = 0
let side = 1
let activePlayer = 0;
let playing = true;
const makeRandomNumber = function () {
let randomNum = Math.floor(Math.random() * 6) + 1
return randomNum;
}
const newGame = function () {
playing = true;
dice.classList.add(`hidden`);
for (let i = 0; i < totalScores.length; i++) {
totalScores[i] = 0;
}
P1TS = 0
P2TS = 0
currentScore = 0
side = 1
activePlayer = 0;
currentScoreP1.textContent = 0
totalScoreP1.textContent = 0
currentScoreP2.textContent = 0
totalScoreP2.textContent = 0
document.querySelector(`.player--${activePlayer}`).classList.remove(`player--winner`);
document.querySelector(`.player--${activePlayer}`).classList.add(`player--active`);
}
const switchSides = function () {
document.getElementById(`current--${activePlayer}`).textContent = 0;
currentScore = 0;
player0.classList.toggle(`player--active`);
player1.classList.toggle(`player--active`);
if (activePlayer === 0) {
activePlayer = 1;
} else {
activePlayer = 0
}
}
const rollDice = function () {
if(playing){
let rand = makeRandomNumber();
dice.src = `dice-${rand}.png`
dice.classList.remove(`hidden`)
if (rand === 1) {
switchSides();
} else {
currentScore += rand
document.querySelector(`#current--${activePlayer}`).textContent = currentScore;
}
}
}
// const rollDice = function () {
// let rand = makeRandomNumber();
// dice.src =`dice-${rand}.png`
// dice.classList.remove(`hidden`)
// switch (side) {
// case 1:
// if (rand === 1) {
// currentScore = 0;
// currentScoreP1.textContent = 0;
// side = side * (-1);
// } else {
// currentScore += rand
// currentScoreP1.textContent = currentScore
// }
// break;
// case -1:
// if (rand === 1) {
// currentScore = 0;
// currentScoreP2.textContent = 0;
// side = side * (-1);
// } else {
// currentScore += rand
// currentScoreP2.textContent = currentScore
// }
// break;
// }
// }
const holdScore = function () {
if(playing){
totalScores[activePlayer] += currentScore;
document.getElementById(`score--${activePlayer}`).textContent = totalScores[activePlayer];
if (totalScores[activePlayer] >= 100) {
dice.classList.add(`hidden`)
playing = false;
document.querySelector(`.player--${activePlayer}`).classList.add(`player--winner`);
document.querySelector(`.player--${activePlayer}`).classList.remove(`player--active`);
} else {
switchSides();
}
}
}
// const holdScore = function () {
// switch (side) {
// case 1:
// if (P1TS < 100) {
// P1TS += currentScore
// totalScoreP1.textContent = P1TS
// currentScore = 0
// currentScoreP1.textContent = 0
// side = side * (-1);
// } else {
// // tagir style
// }
// break
// case -1:
// if (P2TS < 100) {
// P2TS += currentScore
// totalScoreP2.textContent = P2TS
// currentScore = 0;
// currentScoreP2.textContent = 0;
// side = side * (-1);
// } else {
// // tagir style
// }
// break
// }
// }
newGame()
document.querySelector(`.btn--roll`).addEventListener(`click`, rollDice);
document.querySelector(`.btn--hold`).addEventListener(`click`, holdScore);
document.querySelector(`.btn--new`).addEventListener(`click`, newGame)