forked from sumn2u/learn-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
239 lines (195 loc) Β· 8.1 KB
/
script.js
File metadata and controls
239 lines (195 loc) Β· 8.1 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
233
234
235
236
237
// Rock Paper Scissors Game - JavaScript
// Demonstrates randomization, event handling, DOM manipulation, and game logic
class RockPaperScissorsGame {
constructor() {
this.choices = ['rock', 'paper', 'scissors'];
this.playerScore = 0;
this.computerScore = 0;
this.roundNumber = 0;
this.gameHistory = [];
this.initializeElements();
this.bindEvents();
}
initializeElements() {
this.playerScoreEl = document.getElementById('playerScore');
this.computerScoreEl = document.getElementById('computerScore');
this.playerChoiceEl = document.getElementById('playerChoice');
this.computerChoiceEl = document.getElementById('computerChoice');
this.playerChoiceTextEl = document.getElementById('playerChoiceText');
this.computerChoiceTextEl = document.getElementById('computerChoiceText');
this.gameResultEl = document.getElementById('gameResult');
this.rockBtn = document.getElementById('rockBtn');
this.paperBtn = document.getElementById('paperBtn');
this.scissorsBtn = document.getElementById('scissorsBtn');
this.resetBtn = document.getElementById('resetBtn');
this.historyListEl = document.getElementById('historyUl');
}
bindEvents() {
this.rockBtn.addEventListener('click', () => this.playRound('rock'));
this.paperBtn.addEventListener('click', () => this.playRound('paper'));
this.scissorsBtn.addEventListener('click', () => this.playRound('scissors'));
this.resetBtn.addEventListener('click', () => this.resetGame());
}
playRound(playerChoice) {
const computerChoice = this.getComputerChoice();
const result = this.determineWinner(playerChoice, computerChoice);
this.animateChoices(playerChoice, computerChoice);
this.displayResult(playerChoice, computerChoice, result);
this.updateScore(result);
this.addToHistory(playerChoice, computerChoice, result);
}
getComputerChoice() {
const randomIndex = Math.floor(Math.random() * this.choices.length);
return this.choices[randomIndex];
}
determineWinner(player, computer) {
if (player === computer) {
return 'tie';
}
const winConditions = {
rock: 'scissors',
paper: 'rock',
scissors: 'paper'
};
return winConditions[player] === computer ? 'win' : 'lose';
}
animateChoices(playerChoice, computerChoice) {
document.querySelectorAll('.choice-btn.selected')
.forEach(el => el.classList.remove('selected'));
const playerBtn = document.querySelector(`[data-choice="${playerChoice}"]`);
if (playerBtn) {
playerBtn.classList.add('selected');
}
// Add shake animation to choice displays
this.playerChoiceEl.classList.add('shake');
this.computerChoiceEl.classList.add('shake');
setTimeout(() => {
this.playerChoiceEl.classList.remove('shake');
this.computerChoiceEl.classList.remove('shake');
}, 500);
}
displayResult(playerChoice, computerChoice, result) {
// Update choice displays
this.playerChoiceTextEl.textContent = this.capitalize(playerChoice);
this.computerChoiceTextEl.textContent = this.capitalize(computerChoice);
// Update choice icons
const playerIcon = this.getChoiceIcon(playerChoice);
const computerIcon = this.getChoiceIcon(computerChoice);
this.playerChoiceEl.querySelector('.choice-icon').textContent = playerIcon;
this.computerChoiceEl.querySelector('.choice-icon').textContent = computerIcon;
// Display result
const resultMessages = {
win: 'π You Win!',
lose: 'π Computer Wins!',
tie: 'π€ It\'s a Tie!'
};
this.gameResultEl.innerHTML = `<h2>${resultMessages[result]}</h2>`;
}
updateScore(result) {
if (result === 'win') {
this.playerScore++;
this.playerScoreEl.textContent = this.playerScore;
} else if (result === 'lose') {
this.computerScore++;
this.computerScoreEl.textContent = this.computerScore;
}
// Add score animation
if (result !== 'tie') {
const scoreEl = result === 'win' ? this.playerScoreEl : this.computerScoreEl;
scoreEl.style.transform = 'scale(1.2)';
setTimeout(() => {
scoreEl.style.transform = 'scale(1)';
}, 200);
}
}
addToHistory(playerChoice, computerChoice, result) {
this.roundNumber++;
const historyItem = {
round: this.roundNumber,
player: playerChoice,
computer: computerChoice,
result: result
};
this.gameHistory.unshift(historyItem);
if (this.gameHistory.length > 10) this.gameHistory.length = 10;
this.displayHistory();
}
displayHistory() {
if (this.gameHistory.length === 0) {
this.historyListEl.innerHTML = '<li class="no-history">No rounds played yet. Make your first move!</li>';
return;
}
this.historyListEl.innerHTML = '';
this.gameHistory.slice(0, 10).forEach(item => {
const historyItemEl = document.createElement('li');
historyItemEl.className = `history-item ${item.result}`;
historyItemEl.innerHTML = `
<div class="round-number">Round ${item.round}</div>
<div class="moves">
<div class="player-move">
<span>${this.getChoiceIcon(item.player)}</span>
<span>${this.capitalize(item.player)}</span>
</div>
<span class="vs-small">vs</span>
<div class="computer-move">
<span>${this.getChoiceIcon(item.computer)}</span>
<span>${this.capitalize(item.computer)}</span>
</div>
</div>
<div class="result">${this.capitalize(item.result)}</div>
`;
this.historyListEl.appendChild(historyItemEl);
});
}
resetGame() {
this.playerScore = 0;
this.computerScore = 0;
this.roundNumber = 0;
this.gameHistory = [];
this.playerScoreEl.textContent = '0';
this.computerScoreEl.textContent = '0';
// Reset choice displays
this.playerChoiceTextEl.textContent = 'Make your move!';
this.computerChoiceTextEl.textContent = 'Waiting...';
this.playerChoiceEl.querySelector('.choice-icon').textContent = 'β';
this.computerChoiceEl.querySelector('.choice-icon').textContent = 'β';
// Reset result display
this.gameResultEl.innerHTML = '<h2>Choose your move to start!</h2>';
// Remove selected state from buttons
this.rockBtn.classList.remove('selected');
this.paperBtn.classList.remove('selected');
this.scissorsBtn.classList.remove('selected');
// Reset history
this.displayHistory();
}
getChoiceIcon(choice) {
const icons = {
rock: 'πͺ¨',
paper: 'π',
scissors: 'βοΈ'
};
return icons[choice] || 'β';
}
capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = RockPaperScissorsGame;
}
document.addEventListener('DOMContentLoaded', () => {
const game = new RockPaperScissorsGame();
document.addEventListener('keydown', (e) => {
const key = e.key.toLowerCase();
if (key === 'r') game.playRound('rock');
else if (key === 'p') game.playRound('paper');
else if (key === 's') game.playRound('scissors');
});
// Add sound effects (optional)
const playSound = (type) => {
// You can add sound files in the same directory and uncomment this
// const audio = new Audio(`${type}.mp3`);
// audio.volume = 0.3;
// audio.play().catch(e => console.log('Audio play failed:', e));
};
});