-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
97 lines (80 loc) · 3 KB
/
app.js
File metadata and controls
97 lines (80 loc) · 3 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
const CHOICES = ['Rock', 'Paper', 'Scissors'];
const WINNING_SCORE = 5;
class Game {
constructor() {
this.scores = { player: 0, computer: 0 };
this.elements = {
player: document.getElementById('playerChoice'),
computer: document.getElementById('computerChoice'),
result: document.getElementById('result'),
playerScore: document.getElementById('playerScore'),
computerScore: document.getElementById('computerScore')
};
this.initializeGame();
}
initializeGame() {
document.querySelectorAll('.buttons button').forEach(button => {
button.addEventListener('click', () => this.handleChoice(button.dataset.choice));
});
}
async handleChoice(playerChoice) {
const computerChoice = CHOICES[Math.floor(Math.random() * CHOICES.length)];
this.resetAnimations();
await this.animateChoices(playerChoice, computerChoice);
this.updateScores(playerChoice, computerChoice);
}
resetAnimations() {
Object.values(this.elements).forEach(el => {
if (el.id !== 'playerScore' && el.id !== 'computerScore') {
el.classList.remove('show');
el.style.transform = 'translateY(20px)';
}
});
}
async animateChoices(playerChoice, computerChoice) {
await this.animate(this.elements.player, playerChoice, 300);
await this.animate(this.elements.computer, computerChoice, 500);
const result = this.getResult(playerChoice, computerChoice);
await this.animate(this.elements.result, result, 500);
}
animate(element, text, delay) {
return new Promise(resolve => {
setTimeout(() => {
element.textContent = text;
element.classList.add('show');
element.style.transform = 'translateY(0)';
resolve();
}, delay);
});
}
getResult(playerChoice, computerChoice) {
if (playerChoice === computerChoice) return '🤝 Draw!';
const winConditions = {
Rock: 'Scissors',
Paper: 'Rock',
Scissors: 'Paper'
};
return winConditions[playerChoice] === computerChoice ? '🎉 You Win!' : '💔 You Lose!';
}
updateScores(playerChoice, computerChoice) {
if (playerChoice === computerChoice) return;
const playerWins = this.getResult(playerChoice, computerChoice).includes('Win');
this.scores[playerWins ? 'player' : 'computer']++;
this.updateScoreDisplay();
this.checkGameEnd();
}
updateScoreDisplay() {
this.elements.playerScore.textContent = `Player Score: ${this.scores.player}`;
this.elements.computerScore.textContent = `Computer Score: ${this.scores.computer}`;
}
checkGameEnd() {
if (this.scores.player === WINNING_SCORE || this.scores.computer === WINNING_SCORE) {
const winner = this.scores.player === WINNING_SCORE ? '🎉 You Win' : '💔 You Lose';
this.elements.result.textContent = `${winner} the Game!`;
this.scores.player = this.scores.computer = 0;
this.updateScoreDisplay();
}
}
}
// Initialize game when DOM is loaded
document.addEventListener('DOMContentLoaded', () => new Game());