Skip to content

Commit bc4b470

Browse files
suryaprakash0010sumn2u
authored andcommitted
Update script.js
1 parent 8504165 commit bc4b470

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

examples/Rock-Paper-Scissors/script.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ class RockPaperScissorsGame {
6565
}
6666

6767
animateChoices(playerChoice, computerChoice) {
68-
// Add selected animation to buttons
69-
this.rockBtn.classList.remove('selected');
70-
this.paperBtn.classList.remove('selected');
71-
this.scissorsBtn.classList.remove('selected');
68+
69+
document.querySelectorAll('.choice-btn.selected')
70+
.forEach(el => el.classList.remove('selected'));
71+
72+
const playerBtn = document.querySelector(`[data-choice="${playerChoice}"]`);
73+
if (playerBtn) {
74+
playerBtn.classList.add('selected');
75+
}
7276

73-
const playerBtn = document.querySelector(`[data-choice="${playerChoice}"]`);
74-
if (playerBtn) {
75-
playerBtn.classList.add('selected');
76-
}
7777

7878
// Add shake animation to choice displays
7979
this.playerChoiceEl.classList.add('shake');
@@ -136,7 +136,8 @@ class RockPaperScissorsGame {
136136
result: result
137137
};
138138

139-
this.gameHistory.unshift(historyItem);
139+
this.gameHistory.unshift(historyItem);
140+
if (this.gameHistory.length > 10) this.gameHistory.length = 10;
140141
this.displayHistory();
141142
}
142143

@@ -212,20 +213,18 @@ class RockPaperScissorsGame {
212213
return str.charAt(0).toUpperCase() + str.slice(1);
213214
}
214215
}
216+
if (typeof module !== 'undefined' && module.exports) {
217+
module.exports = RockPaperScissorsGame;
218+
}
215219

216-
// Initialize the game when DOM is loaded
217220
document.addEventListener('DOMContentLoaded', () => {
218221
const game = new RockPaperScissorsGame();
219222

220-
// Add keyboard support for accessibility
221223
document.addEventListener('keydown', (e) => {
222-
if (e.key.toLowerCase() === 'r') {
223-
game.playRound('rock');
224-
} else if (e.key.toLowerCase() === 'p') {
225-
game.playRound('paper');
226-
} else if (e.key.toLowerCase() === 's') {
227-
game.playRound('scissors');
228-
}
224+
const key = e.key.toLowerCase();
225+
if (key === 'r') game.playRound('rock');
226+
else if (key === 'p') game.playRound('paper');
227+
else if (key === 's') game.playRound('scissors');
229228
});
230229

231230
// Add sound effects (optional)
@@ -235,9 +234,6 @@ document.addEventListener('DOMContentLoaded', () => {
235234
// audio.volume = 0.3;
236235
// audio.play().catch(e => console.log('Audio play failed:', e));
237236
};
238-
239-
// Export for potential testing
240-
if (typeof module !== 'undefined' && module.exports) {
241-
module.exports = RockPaperScissorsGame;
242-
}
243237
});
238+
239+

0 commit comments

Comments
 (0)