This repository was archived by the owner on Jul 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (33 loc) · 1.55 KB
/
Copy pathscript.js
File metadata and controls
40 lines (33 loc) · 1.55 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
let playerScore = 0;
let computerScore = 0;
const choices = ['rock', 'paper', 'scissors'];
const emojis = { rock: '🪨', paper: '📄', scissors: '✂️' };
function play(playerChoice) {
const computerChoice = choices[Math.floor(Math.random() * 3)];
document.getElementById('playerChoice').textContent = emojis[playerChoice];
document.getElementById('computerChoice').textContent = emojis[computerChoice];
const result = getResult(playerChoice, computerChoice);
if (result === 'win') {
playerScore++;
document.getElementById('result').textContent = '你贏了!';
document.getElementById('result').className = 'text-xl font-bold mb-4 h-6 text-green-600';
} else if (result === 'lose') {
computerScore++;
document.getElementById('result').textContent = '你輸了!';
document.getElementById('result').className = 'text-xl font-bold mb-4 h-6 text-red-600';
} else {
document.getElementById('result').textContent = '平手!';
document.getElementById('result').className = 'text-xl font-bold mb-4 h-6 text-gray-600';
}
document.getElementById('playerScore').textContent = playerScore;
document.getElementById('computerScore').textContent = computerScore;
}
function getResult(player, computer) {
if (player === computer) return 'tie';
if ((player === 'rock' && computer === 'scissors') ||
(player === 'paper' && computer === 'rock') ||
(player === 'scissors' && computer === 'paper')) {
return 'win';
}
return 'lose';
}