-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (42 loc) · 1.72 KB
/
script.js
File metadata and controls
49 lines (42 loc) · 1.72 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
document.addEventListener('DOMContentLoaded', () => {
const playerNameInput = document.getElementById('playerName');
const startGameButton = document.getElementById('startGame');
const gameArea = document.querySelector('.game-area');
const playerDisplay = document.getElementById('playerDisplay');
const choices = document.querySelectorAll('.choice');
const computerChoiceDisplay = document.getElementById('computerChoice');
const gameResultDisplay = document.getElementById('gameResult');
const options = ['rock', 'paper', 'scissors'];
startGameButton.addEventListener('click', () => {
const playerName = playerNameInput.value.trim();
if (playerName === '') {
alert('Please enter your name to start!');
return;
}
playerDisplay.textContent = playerName;
document.querySelector('.player-info').classList.add('hidden');
gameArea.classList.remove('hidden');
});
choices.forEach(choice => {
choice.addEventListener('click', () => {
const playerChoice = choice.dataset.choice;
const computerChoice = options[Math.floor(Math.random() * options.length)];
computerChoiceDisplay.textContent = computerChoice;
const result = determineWinner(playerChoice, computerChoice);
gameResultDisplay.textContent = result;
});
});
function determineWinner(player, computer) {
if (player === computer) {
return 'It\'s a tie!';
}
if (
(player === 'rock' && computer === 'scissors') ||
(player === 'paper' && computer === 'rock') ||
(player === 'scissors' && computer === 'paper')
) {
return 'You win!';
}
return 'You lose!';
}
});