-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock-paper-scissor.html
125 lines (107 loc) · 3.68 KB
/
rock-paper-scissor.html
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>石头剪刀布游戏</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
.container {
max-width: 800px;
margin: 0 auto;
text-align: center;
padding-top: 50px;
}
h1 {
font-size: 36px;
margin-bottom: 30px;
}
.choices {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
.choice-btn {
background-color: #4caf50;
border: none;
color: white;
font-size: 18px;
padding: 10px 24px;
margin: 0 10px;
cursor: pointer;
transition: 0.3s;
}
.choice-btn:hover {
background-color: #45a049;
}
.result {
font-size: 24px;
}
</style>
</head>
<body>
<div class="container">
<h1>石头剪刀布游戏</h1>
<div class="choices">
<button class="choice-btn" id="rock">石头</button>
<button class="choice-btn" id="scissors">剪刀</button>
<button class="choice-btn" id="paper">布</button>
</div>
<div class="result">
<p id="result-text">选择你的手势,开始游戏!</p>
</div>
<div class="scoreboard">
<p id="wins">胜场数: 0</p>
<p id="win-rate">胜率: 0%</p>
</div>
</div>
<script>
const rockBtn = document.getElementById("rock");
const scissorsBtn = document.getElementById("scissors");
const paperBtn = document.getElementById("paper");
const resultText = document.getElementById("result-text");
rockBtn.addEventListener("click", () => playGame("石头"));
scissorsBtn.addEventListener("click", () => playGame("剪刀"));
paperBtn.addEventListener("click", () => playGame("布"));
let wins = 0;
let gamesPlayed = 0;
function updateScoreboard(result) {
if (result.includes("你赢了")) {
wins++;
}
gamesPlayed++;
const winRate = ((wins / gamesPlayed) * 100).toFixed(2);
document.getElementById("wins").textContent = `胜场数: ${wins}`;
document.getElementById("win-rate").textContent = `胜率: ${winRate}%`;
}
function playGame(playerChoice) {
const computerChoice = getComputerChoice();
const result = getResult(playerChoice, computerChoice);
resultText.textContent = result;
updateScoreboard(result);
}
function getComputerChoice() {
const choices = ["石头", "剪刀", "布"];
const randomIndex = Math.floor(Math.random() * 3);
return choices[randomIndex];
}
function getResult(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return `平局!你们都选择了${playerChoice}。`;
}
if (
(playerChoice === "石头" && computerChoice === "剪刀") ||
(playerChoice === "剪刀" && computerChoice === "布") ||
(playerChoice === "布" && computerChoice === "石头")
) {
return `你赢了!${playerChoice}击败了${computerChoice}。`;
} else {
return `你输了!${computerChoice}击败了${playerChoice}。`;
}
}
</script>
</body>
</html>