-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
232 lines (181 loc) · 6.17 KB
/
Copy pathmain.js
File metadata and controls
232 lines (181 loc) · 6.17 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
let roundNumber = 1;
let wins = 0;
let losses = 0;
let roundData = {};
let overlays = document.querySelectorAll('#container .hidden');
let roundArea = document.querySelector('#round-area');
let roundNumberIndicator = document.querySelector('#round-number');
let playButtons = document.querySelector('#play-buttons-container');
let roundRecap = document.querySelector('#round-recap');
let playerChoiceText = document.querySelector('#player-choice-text');
let playerChoiceImg = document.querySelector('#player-choice-img');
let computerChoiceText = document.querySelector('#computer-choice-text');
let computerChoiceImg = document.querySelector('#computer-choice-img');
let roundResult = document.querySelector('#round-result');
let gameOverArea = document.querySelector('#game-over-area');
let gameResultText = document.querySelector('#game-result-text');
let continueButtons = document.querySelector('#continue-buttons');
let playAgain = document.querySelector('#play-again');
let playerScore = document.querySelector('#player-score');
let computerScore = document.querySelector('#computer-score');
let scores = document.querySelectorAll('.score');
let roundRecapTimeout;
let updateRoundNumber;
function showRoundRecap() {
overlays.forEach(element => {
element.classList.remove('hidden');
element.classList.add('shown');
});
// Hide elements after a delay
roundRecapTimeout = setTimeout(hideRoundRecap, 1500);
updateRoundNumber = setTimeout(updateRoundNumberIndicator, 1500);
}
function hideRoundRecap() {
overlays.forEach(element => {
element.classList.remove('shown');
element.classList.add('hidden');
})
}
function showGameOverArea() {
gameOverArea.classList.add('shown');
gameOverArea.classList.remove('hidden');
}
function hideGameOverArea() {
gameOverArea.classList.remove('shown');
gameOverArea.classList.add('hidden');
}
function toTitleCase(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}
playAgain.addEventListener('click', (e) => {
resetGame();
})
playButtons.addEventListener('click', (e) => {
let target = e.target.id;
// Target validation to avoid playing round if user clicks between buttons
if (target === 'rock' || target === 'paper' || target === 'scissors') {
target = toTitleCase(target);
playRound(`${target}`);
}
});
/*
Log area:
- If this was first round: hide empty state and show log columns
- Add round to log
*/
function playRound(playerSelection) {
computerSelection = getComputerChoice();
roundData.roundNumber = roundNumber;
roundData.playerSelection = playerSelection;
roundData.computerSelection = computerSelection;
if (playerSelection === computerSelection) {
roundData.result = 'Tie';
} else if
((playerSelection === 'Rock' && computerSelection === 'Paper') ||
(playerSelection === 'Paper' && computerSelection === 'Scissors') ||
(playerSelection === 'Scissors' && computerSelection === 'Rock')) {
roundData.result = 'Loss';
addLoss();
} else if
((computerSelection === 'Rock' && playerSelection === 'Paper') ||
(computerSelection === 'Paper' && playerSelection === 'Scissors') ||
(computerSelection === 'Scissors' && playerSelection === 'Rock')) {
roundData.result = 'Win';
addWin();
}
updateroundRecap()
showRoundRecap();
gameWinnerCheck();
}
function addWin() {
wins++;
updateScore();
}
function addLoss() {
losses++;
updateScore();
}
function updateScore() {
playerScore.textContent = `${wins}`;
computerScore.textContent = `${losses}`;
}
function getComputerChoice() {
let randomNumber = Math.floor((Math.random() * 3) + 1);
if (randomNumber === 1) {
return 'Rock';
} else if (randomNumber === 2) {
return 'Paper';
} else if (randomNumber === 3) {
return 'Scissors';
}
}
function updateroundRecap() {
roundResult.textContent = roundData.result;
playerChoiceText.textContent = roundData.playerSelection;
computerChoiceText.textContent = roundData.computerSelection;
switch (roundData.playerSelection) {
case 'Rock':
playerChoiceImg.src='./assets/rock-right.png';
playerChoiceText.style.color='#70D6F9';
break;
case 'Paper':
playerChoiceImg.src='./assets/paper-right.png';
playerChoiceText.style.color='#87D147';
break;
case 'Scissors':
playerChoiceImg.src='./assets/scissors-right.png';
playerChoiceText.style.color='#FC657E';
break;
}
switch (roundData.computerSelection) {
case 'Rock':
computerChoiceImg.src='./assets/rock-left.png';
computerChoiceText.style.color='#70D6F9';
break;
case 'Paper':
computerChoiceImg.src='./assets/paper-left.png';
computerChoiceText.style.color='#87D147';
break;
case 'Scissors':
computerChoiceImg.src='./assets/scissors-left.png';
computerChoiceText.style.color='#FC657E';
break;
}
}
function updateRoundNumberIndicator() {
roundNumberIndicator.textContent = `Round ${roundNumber}`;
}
function gameWinnerCheck() {
if (wins >= 5) {
endGame('win');
} else if (losses >= 5) {
endGame('loss');
} else {
continueGame();
}
}
function continueGame() {
roundNumber++;
}
function endGame(result) {
if (result === 'win') {
playerScore.classList.add('score-winner');
gameResultText.textContent = 'You win the game!';
} else if (result === 'loss') {
computerScore.classList.add('score-winner');
gameResultText.textContent = 'You lose the game!'
}
showGameOverArea();
clearTimeout(roundRecapTimeout);
}
function resetGame() {
roundNumber = 1;
updateRoundNumberIndicator();
wins = 0;
losses = 0;
roundData = {};
scores.forEach((score) => {score.classList.remove('score-winner')});
updateScore();
hideRoundRecap();
hideGameOverArea();
}