-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (146 loc) · 4.81 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';
const btnNew = document.querySelector('.btn--new');
//Select score elements
const player0ScoreEl = document.getElementById('score--0');
const player1ScoreEl = document.getElementById('score--1');
const player0ScoreCurrent = document.getElementById('current--0');
const player1ScoreCurrent = document.getElementById('current--1');
const diceEl = document.querySelector('.dice');
const diceRoll = document.querySelector('.btn--roll');
const btnHold = document.querySelector('.btn--hold');
let scores = [0, 0];
let currentScore = 0;
let activePlayer = 0;
let gamePlay = true;
let winScore = 100;
btnNew.addEventListener('click', resetBoard);
diceRoll.addEventListener('click', rollDie);
btnHold.addEventListener('click', holdScore);
resetBoard();
function resetBoard() {
diceEl.classList.add('hide');
diceRoll.classList.remove('hide');
btnHold.classList.remove('hide');
document
.querySelector(`.player--${activePlayer}`)
.classList.remove('player--winner');
document.querySelector('.player--0').classList.add('player--active');
document.querySelector('.player--1').classList.remove('player--active');
player0ScoreEl.textContent = '0';
player1ScoreEl.textContent = '0';
scores = [0, 0];
player0ScoreCurrent.textContent = 0;
player1ScoreCurrent.textContent = 0;
activePlayer = 0;
currentScore = 0;
gamePlay = true;
}
//Rolling dice functionality
function rollDie() {
if (gamePlay) {
if (diceEl.classList.contains('hide')) {
diceEl.classList.remove('hide');
}
//generate random roll
let diceNumber = Math.trunc(Math.random() * 6) + 1;
console.log(diceNumber);
//generate image
diceEl.src = `dice-${diceNumber}.png`;
//if 1 is rolled, remove points and switch player
if (diceNumber != 1) {
currentScore += diceNumber;
document.getElementById(`current--${activePlayer}`).textContent =
currentScore;
if (scores[activePlayer] + currentScore > winScore) {
console.log('scores[activePlayer]: ', scores[activePlayer]);
win();
}
} else {
//document.getElementById(`score--${activePlayer}`).textContent = 0;
changePlayer();
}
}
}
function holdScore() {
if (gamePlay) {
console.log('Active Player: ', activePlayer);
console.log('Score Held');
//add current score to active player if aggregate score is under 100
scores[activePlayer] += currentScore;
document.getElementById(`score--${activePlayer}`).textContent =
scores[activePlayer];
if (scores[activePlayer] > winScore) {
win();
} else {
changePlayer();
}
//Che player
}
}
function win() {
scores[activePlayer] += currentScore;
document.getElementById(`score--${activePlayer}`).textContent =
scores[activePlayer];
gamePlay = false;
diceEl.classList.add('hide');
document
.querySelector(`.player--${activePlayer}`)
.classList.toggle('player--winner');
document
.querySelector(`.player--${activePlayer}`)
.classList.toggle('player--active');
console.log(gamePlay);
}
function changePlayer() {
currentScore = 0;
document.getElementById(`current--${activePlayer}`).textContent =
currentScore;
document
.querySelector(`.player--${activePlayer}`)
.classList.toggle('player--active');
activePlayer = activePlayer === 0 ? 1 : 0;
document
.querySelector(`.player--${activePlayer}`)
.classList.toggle('player--active');
}
// if (activePlayer === 'player0') {
// console.log('the current player is: ', activePlayer);
// scorePlayer0current += diceNumber;
// player0ScoreCurrent.textContent = scorePlayer0current;
// } else {
// console.log('the current player is: ', activePlayer);
// scorePlayer1current += diceNumber;
// player1ScoreCurrent.textContent = scorePlayer1current;
// }
// } else {
// if (activePlayer === 'player0') {
// scorePlayer0current = 0;
// player0ScoreCurrent.textContent = scorePlayer0current;
// player0ScoreEl.textContent = 0;
// currentPlayer = 'player1';
// } else {
// scorePlayer1current = 0;
// player1ScoreCurrent.textContent = scorePlayer1current;
// player1ScoreEl.textContent = 0;
// currentPlayer = 'player0';
// }
// for (let i = 0; i < btnsOpenModal.length; i++) {
// btnsOpenModal[i].addEventListener('click', openModal);
// }
// modalClsBtn.addEventListener('click', closeModal);
// overlay.addEventListener('click', closeModal);
// document.addEventListener('keydown', function (event) {
// if (event.key === 'Escape') {
// if (!modalWindow.classList.contains('hidden')) {
// closeModal();
// }
// }
// });
// function openModal() {
// modalWindow.classList.remove('hidden');
// overlay.classList.remove('hidden');
// }
// function closeModal() {
// modalWindow.classList.add('hidden');
// overlay.classList.add('hidden');
// }