-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPS.js
264 lines (208 loc) · 8.56 KB
/
RPS.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
document.addEventListener('DOMContentLoaded', function() {
//choices
const playerChoices = document.querySelectorAll('.playerChoices > div');
const computerChoices = document.querySelectorAll('.computerChoices > div');
const resetBtn = document.getElementById('resetBtn');
let playerChoice = '';
let computerChoice = '';
// result
let result = '';
// html scores
const playerScoreboard = document.querySelector('.player-scoreboard')
const computerScoreboard = document.querySelector('.computer-scoreboard')
const computerScorevalue = document.querySelector('.computer-scorevalue')
const playerScorevalue = document.querySelector('.player-scorevalue')
// score counter
let playerScore = 0;
let computerScore = 0;
// html bot and player selections
const playerRock = document.querySelector('#player-rock');
const playerPaper = document.querySelector('#player-paper');
const playerScissors = document.querySelector('#player-scissors');
const botRock = document.querySelector('#bot-rock');
const botPaper = document.querySelector('#bot-paper');
const botScissors = document.querySelector('#bot-scissors');
for (let i = 0; i < playerChoices.length; i++) {
playerChoices[i].addEventListener('click', function() {
const choices = ['rock', 'paper', 'scissors'];
playerChoice = choices[i];
computerChoice = getComputerChoice();
result = determineScore(playerChoice, computerChoice);
determineWinner(playerScore, computerScore);
})}
// random computer choice
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
const randomIndex = Math.floor(Math.random() * choices.length);
const computerChoice = choices[randomIndex];
// Add active class to the corresponding element
const computerChoiceElement = document.querySelector(`#bot-${computerChoice}`);
computerChoiceElement.classList.add('active');
// Remove active class after a delay
setTimeout(() => {
computerChoiceElement.classList.remove('active');
}, 1000);
return computerChoice;
}
// determine score
function determineScore(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return 'tie'
}
if (playerChoice === "rock") {
if (computerChoice === "paper") {
computerScore++;
computerScorevalue.textContent = computerScore;
} else {
playerScore++;
playerScorevalue.textContent = playerScore;
}
}
if (playerChoice === "paper") {
if (computerChoice === "scissors") {
computerScore++;
computerScorevalue.textContent = computerScore;
} else {
playerScore++;
playerScorevalue.textContent = playerScore;
}
}
if (playerChoice === "scissors") {
if (computerChoice === "rock") {
computerScore++;
computerScorevalue.textContent = computerScore;
} else {
playerScore++;
playerScorevalue.textContent = playerScore;
}
}
}
// determine final winner, first to 5 wins
function determineWinner(playerScore, computerScore) {
if (playerScore === 5) {
// Hide player choices
playerChoices.forEach(icon => {
icon.style.display = 'none';
});
// Show reset button
resetBtn.style.display = 'block';
// change element color
playerScoreboard.classList.add('active');
playerScorevalue.classList.add('active');
return 'Player reaches 5, player wins';
} else if (computerScore === 5) {
// Hide player choices
playerChoices.forEach(icon => {
icon.style.display = 'none';
});
// Show reset button
resetBtn.style.display = 'block';
// change element color
computerScoreboard.classList.add('active');
computerScorevalue.classList.add('active');
return 'Computer reaches 5, computer wins';
}
}
// restart game
resetBtn.addEventListener('click', function () {
//reset everything
playerScore = 0;
computerScore = 0;
playerScoreboard.classList.remove('active');
playerScorevalue.classList.remove('active');
computerScoreboard.classList.remove('active');
computerScorevalue.classList.remove('active');
playerScorevalue.innerHTML = '0'
computerScorevalue.innerHTML = '0'
// hide reset button
resetBtn.style.display = 'none'
// display player choices
playerChoices.forEach(icon => {
icon.style.display = 'block';
});
})
});
/*
gpt 'cleaner' codes
document.addEventListener('DOMContentLoaded', function() {
const playerChoices = document.querySelectorAll('.playerChoices > div');
const computerChoices = document.querySelectorAll('.computerChoices > div');
const resetBtn = document.getElementById('resetBtn');
let playerScore = 0;
let computerScore = 0;
const playerScorevalue = document.querySelector('.player-scorevalue');
const computerScorevalue = document.querySelector('.computer-scorevalue');
const playerScoreboard = document.querySelector('.player-scoreboard');
const computerScoreboard = document.querySelector('.computer-scoreboard');
playerChoices.forEach((choice, index) => {
choice.addEventListener('click', function() {
const choices = ['rock', 'paper', 'scissors'];
const playerChoice = choices[index];
const computerChoice = getComputerChoice();
determineScore(playerChoice, computerChoice);
determineWinner(playerScore, computerScore);
});
});
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
const randomIndex = Math.floor(Math.random() * choices.length);
const computerChoice = choices[randomIndex];
const computerChoiceElement = document.querySelector(`#bot-${computerChoice}`);
computerChoiceElement.classList.add('active');
setTimeout(() => {
computerChoiceElement.classList.remove('active');
}, 1000);
return computerChoice;
}
function determineScore(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return;
}
if (
(playerChoice === 'rock' && computerChoice === 'scissors') ||
(playerChoice === 'paper' && computerChoice === 'rock') ||
(playerChoice === 'scissors' && computerChoice === 'paper')
) {
playerScore++;
playerScorevalue.textContent = playerScore;
} else {
computerScore++;
computerScorevalue.textContent = computerScore;
}
}
function determineWinner(playerScore, computerScore) {
if (playerScore === 5 || computerScore === 5) {
playerChoices.forEach(icon => {
icon.style.display = 'none';
});
resetBtn.style.display = 'block';
if (playerScore === 5) {
playerScoreboard.classList.add('active');
playerScorevalue.classList.add('active');
return 'Player reaches 5, player wins';
} else {
computerScoreboard.classList.add('active');
computerScorevalue.classList.add('active');
return 'Computer reaches 5, computer wins';
}
}
}
resetBtn.addEventListener('click', function() {
playerScore = 0;
computerScore = 0;
playerScoreboard.classList.remove('active');
playerScorevalue.classList.remove('active');
computerScoreboard.classList.remove('active');
computerScorevalue.classList.remove('active');
playerScorevalue.textContent = '0';
computerScorevalue.textContent = '0';
resetBtn.style.display = 'none';
playerChoices.forEach(icon => {
icon.style.display = 'block';
});
});
});
1. Used the forEach loop to iterate over player choices and attach event listeners, reducing the need for an explicit for loop.
2. Simplified the scoring logic by using a conditional statement to check if the player wins.
3. Combined the element selections for player score and computer score into a single querySelector or querySelectorAll call, improving code readability and performance.
*/