Lashtw commited on
Commit
fefe9cc
·
verified ·
1 Parent(s): c4b2f36

Create ten_doors.html

Browse files
Files changed (1) hide show
  1. ten_doors.html +347 -0
ten_doors.html ADDED
@@ -0,0 +1,347 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="zh-TW">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>十門問題遊戲</title>
7
+ <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;700&display=swap" rel="stylesheet">
8
+ <style>
9
+ body {
10
+ font-family: 'Noto Sans TC', Arial, sans-serif;
11
+ text-align: center;
12
+ padding: 20px;
13
+ background-color: #f0f0f0;
14
+ margin: 0;
15
+ min-height: 100vh;
16
+ display: flex;
17
+ flex-direction: row;
18
+ justify-content: center;
19
+ gap: 40px;
20
+ position: relative;
21
+ box-sizing: border-box;
22
+ }
23
+ .game-container {
24
+ flex: 1;
25
+ max-width: 800px;
26
+ width: 100%;
27
+ }
28
+ .stats-container {
29
+ flex: 1;
30
+ max-width: 300px;
31
+ width: 100%;
32
+ text-align: left;
33
+ }
34
+ .door-container {
35
+ display: flex;
36
+ justify-content: center;
37
+ gap: 10px;
38
+ margin: 20px 0;
39
+ flex-wrap: wrap;
40
+ }
41
+ .door {
42
+ cursor: pointer;
43
+ border: 2px solid #333;
44
+ padding: 5px;
45
+ background-color: #fff;
46
+ flex: 0 0 auto;
47
+ }
48
+ .door img {
49
+ width: 60px;
50
+ height: 90px;
51
+ max-width: 100%;
52
+ }
53
+ #message {
54
+ font-size: 1.2em;
55
+ margin: 20px 0;
56
+ word-wrap: break-word;
57
+ }
58
+ button {
59
+ padding: 10px 20px;
60
+ font-size: 1em;
61
+ margin: 5px;
62
+ cursor: pointer;
63
+ width: auto;
64
+ min-width: 120px;
65
+ }
66
+ table {
67
+ width: 100%;
68
+ border-collapse: collapse;
69
+ margin-top: 20px;
70
+ }
71
+ th, td {
72
+ border: 1px solid #333;
73
+ padding: 8px;
74
+ text-align: center;
75
+ }
76
+ th {
77
+ background-color: #ddd;
78
+ }
79
+ .footer {
80
+ position: absolute;
81
+ bottom: 10px;
82
+ right: 10px;
83
+ font-size: 0.9em;
84
+ color: #555;
85
+ }
86
+
87
+ @media (max-width: 768px) {
88
+ body {
89
+ flex-direction: column;
90
+ gap: 20px;
91
+ }
92
+ .game-container, .stats-container {
93
+ max-width: 100%;
94
+ }
95
+ .door img {
96
+ width: 50px;
97
+ height: 75px;
98
+ }
99
+ button {
100
+ font-size: 0.9em;
101
+ padding: 8px 16px;
102
+ min-width: 100px;
103
+ }
104
+ #message {
105
+ font-size: 1em;
106
+ }
107
+ .footer {
108
+ position: static;
109
+ text-align: center;
110
+ margin-top: 20px;
111
+ }
112
+ }
113
+
114
+ @media (max-width: 480px) {
115
+ .door img {
116
+ width: 40px;
117
+ height: 60px;
118
+ }
119
+ button {
120
+ font-size: 0.8em;
121
+ padding: 6px 12px;
122
+ min-width: 80px;
123
+ }
124
+ #message {
125
+ font-size: 0.9em;
126
+ }
127
+ h1 {
128
+ font-size: 1.5em;
129
+ }
130
+ h2 {
131
+ font-size: 1.2em;
132
+ }
133
+ }
134
+ </style>
135
+ </head>
136
+ <body>
137
+ <div class="game-container">
138
+ <h1>十門問題遊戲</h1>
139
+ <div id="intro">
140
+ <p>歡迎來到十門問題遊戲!遊戲規則如下:<br>
141
+ 有十道門,任選一道,其中九道門後面是一顆糖果,另一道門後面是一棟豪宅。<br>
142
+ 你選門後,主持人會打開所有有糖果的門,然後問你要不要換門。<br>
143
+ 準備好了嗎?請選擇一道門開始遊戲!</p>
144
+ </div>
145
+ <div id="doors" class="door-container">
146
+ <div class="door" onclick="chooseDoor(0)"><img src="door.jpg" alt="門 1"></div>
147
+ <div class="door" onclick="chooseDoor(1)"><img src="door.jpg" alt="門 2"></div>
148
+ <div class="door" onclick="chooseDoor(2)"><img src="door.jpg" alt="門 3"></div>
149
+ <div class="door" onclick="chooseDoor(3)"><img src="door.jpg" alt="門 4"></div>
150
+ <div class="door" onclick="chooseDoor(4)"><img src="door.jpg" alt="門 5"></div>
151
+ <div class="door" onclick="chooseDoor(5)"><img src="door.jpg" alt="門 6"></div>
152
+ <div class="door" onclick="chooseDoor(6)"><img src="door.jpg" alt="門 7"></div>
153
+ <div class="door" onclick="chooseDoor(7)"><img src="door.jpg" alt="門 8"></div>
154
+ <div class="door" onclick="chooseDoor(8)"><img src="door.jpg" alt="門 9"></div>
155
+ <div class="door" onclick="chooseDoor(9)"><img src="door.jpg" alt="門 10"></div>
156
+ </div>
157
+ <div id="message"></div>
158
+ <div id="switch-choice" style="display: none;">
159
+ <button onclick="switchDoor(true)">換門</button>
160
+ <button onclick="switchDoor(false)">不換門</button>
161
+ </div>
162
+ <button onclick="promptForSimulation(false)">模擬 100 次:2號門不換 (需密碼)</button>
163
+ <button onclick="promptForSimulation(true)">模擬 100 次:2號門換門 (需密碼)</button>
164
+ <button onclick="window.location.href='index.html'">返回三門版本</button>
165
+ </div>
166
+ <div class="stats-container">
167
+ <h2>中獎機率統計</h2>
168
+ <table id="stats-table">
169
+ <tr>
170
+ <th>選擇</th>
171
+ <th>次數</th>
172
+ <th>中獎次數</th>
173
+ <th>中獎機率</th>
174
+ </tr>
175
+ <tr>
176
+ <td>換門</td>
177
+ <td id="switch-count">0</td>
178
+ <td id="switch-wins">0</td>
179
+ <td id="switch-rate">0%</td>
180
+ </tr>
181
+ <tr>
182
+ <td>不換門</td>
183
+ <td id="no-switch-count">0</td>
184
+ <td id="no-switch-wins">0</td>
185
+ <td id="no-switch-rate">0%</td>
186
+ </tr>
187
+ </table>
188
+ </div>
189
+ <div class="footer">程式設計者:新竹縣立精華國中 藍星宇老師</div>
190
+
191
+ <script>
192
+ const TOTAL_DOORS = 10;
193
+ let playerChoice = null;
194
+ let prizeDoor = Math.floor(Math.random() * TOTAL_DOORS);
195
+ let remainingDoor = null;
196
+
197
+ let switchCount = 0;
198
+ let switchWins = 0;
199
+ let noSwitchCount = 0;
200
+ let noSwitchWins = 0;
201
+
202
+ function chooseDoor(door) {
203
+ playerChoice = door;
204
+ document.getElementById("intro").style.display = "none";
205
+
206
+ // 主持人開啟所有非豪宅的門
207
+ const doors = document.getElementsByClassName("door");
208
+ if (playerChoice === prizeDoor) {
209
+ // 玩家選中豪宅,隨機選一扇門留下,其餘全開
210
+ do {
211
+ remainingDoor = Math.floor(Math.random() * TOTAL_DOORS);
212
+ } while (remainingDoor === playerChoice);
213
+ for (let i = 0; i < TOTAL_DOORS; i++) {
214
+ if (i !== playerChoice && i !== remainingDoor) {
215
+ doors[i].innerHTML = '<img src="candy.jpg" alt="糖果">';
216
+ doors[i].style.pointerEvents = "none";
217
+ }
218
+ }
219
+ } else {
220
+ // 玩家選中糖果,留下豪宅門,其餘全開
221
+ remainingDoor = prizeDoor;
222
+ for (let i = 0; i < TOTAL_DOORS; i++) {
223
+ if (i !== playerChoice && i !== prizeDoor) {
224
+ doors[i].innerHTML = '<img src="candy.jpg" alt="糖果">';
225
+ doors[i].style.pointerEvents = "none";
226
+ }
227
+ }
228
+ }
229
+
230
+ // 提示玩家是否換門
231
+ document.getElementById("message").innerHTML =
232
+ `你選了門 ${playerChoice + 1},主持人開啟了其他糖果門,剩下門 ${remainingDoor + 1}。<br>你要換門嗎?`;
233
+ document.getElementById("switch-choice").style.display = "block";
234
+
235
+ // 禁用所有門的點擊
236
+ for (let i = 0; i < doors.length; i++) {
237
+ doors[i].style.pointerEvents = "none";
238
+ }
239
+ }
240
+
241
+ function switchDoor(doSwitch) {
242
+ let finalChoice = playerChoice;
243
+ if (doSwitch) {
244
+ finalChoice = remainingDoor;
245
+ switchCount++;
246
+ } else {
247
+ noSwitchCount++;
248
+ }
249
+
250
+ // 顯示所有門後面的結果
251
+ const doors = document.getElementsByClassName("door");
252
+ for (let i = 0; i < TOTAL_DOORS; i++) {
253
+ if (i === prizeDoor) {
254
+ doors[i].innerHTML = '<img src="house.jpg" alt="豪宅">';
255
+ } else {
256
+ doors[i].innerHTML = '<img src="candy.jpg" alt="糖果">';
257
+ }
258
+ }
259
+
260
+ // 判斷結果並更新統計
261
+ const won = finalChoice === prizeDoor;
262
+ let result = "";
263
+ if (doSwitch) {
264
+ result = won ? "換門,有中" : "換門,沒中";
265
+ if (won) switchWins++;
266
+ } else {
267
+ result = won ? "不換門,有中" : "不換門,沒中";
268
+ if (won) noSwitchWins++;
269
+ }
270
+ document.getElementById("message").innerHTML =
271
+ `你最終選擇了門 ${finalChoice + 1},豪宅在門 ${prizeDoor + 1}。<br>結果:${result}<br><button onclick="resetGame()">再玩一次</button>`;
272
+
273
+ document.getElementById("switch-choice").style.display = "none";
274
+
275
+ updateStats();
276
+ }
277
+
278
+ function resetGame() {
279
+ prizeDoor = Math.floor(Math.random() * TOTAL_DOORS);
280
+ playerChoice = null;
281
+ remainingDoor = null;
282
+
283
+ document.getElementById("intro").style.display = "block";
284
+ document.getElementById("message").innerHTML = "";
285
+ document.getElementById("switch-choice").style.display = "none";
286
+
287
+ const doors = document.getElementsByClassName("door");
288
+ for (let i = 0; i < doors.length; i++) {
289
+ doors[i].innerHTML = `<img src="door.jpg" alt="門 ${i + 1}">`;
290
+ doors[i].style.pointerEvents = "auto";
291
+ }
292
+ }
293
+
294
+ function updateStats() {
295
+ document.getElementById("switch-count").innerHTML = switchCount;
296
+ document.getElementById("switch-wins").innerHTML = switchWins;
297
+ const switchRate = switchCount > 0 ? (switchWins / switchCount * 100).toFixed(1) : 0;
298
+ document.getElementById("switch-rate").innerHTML = `${switchRate}%`;
299
+
300
+ document.getElementById("no-switch-count").innerHTML = noSwitchCount;
301
+ document.getElementById("no-switch-wins").innerHTML = noSwitchWins;
302
+ const noSwitchRate = noSwitchCount > 0 ? (noSwitchWins / noSwitchCount * 100).toFixed(1) : 0;
303
+ document.getElementById("no-switch-rate").innerHTML = `${noSwitchRate}%`;
304
+ }
305
+
306
+ function promptForSimulation(doSwitch) {
307
+ const password = prompt("請輸入密碼以啟動模擬功能:");
308
+ if (password === "Ghjh") {
309
+ runSimulation(doSwitch);
310
+ } else {
311
+ alert("密碼錯誤!");
312
+ }
313
+ }
314
+
315
+ function runSimulation(doSwitch) {
316
+ for (let i = 0; i < 100; i++) {
317
+ playerChoice = 1;
318
+ prizeDoor = Math.floor(Math.random() * TOTAL_DOORS);
319
+
320
+ if (playerChoice === prizeDoor) {
321
+ do {
322
+ remainingDoor = Math.floor(Math.random() * TOTAL_DOORS);
323
+ } while (remainingDoor === playerChoice);
324
+ } else {
325
+ remainingDoor = prizeDoor;
326
+ }
327
+
328
+ let finalChoice = playerChoice;
329
+ if (doSwitch) {
330
+ finalChoice = remainingDoor;
331
+ switchCount++;
332
+ if (finalChoice === prizeDoor) switchWins++;
333
+ } else {
334
+ noSwitchCount++;
335
+ if (finalChoice === prizeDoor) noSwitchWins++;
336
+ }
337
+ }
338
+
339
+ updateStats();
340
+
341
+ const action = doSwitch ? "換門" : "不換門";
342
+ document.getElementById("message").innerHTML =
343
+ `模擬完成!已執行 100 次,選擇 2 號門且${action}。<br>請查看右側統計表格。<br><button onclick="resetGame()">再玩一次</button>`;
344
+ }
345
+ </script>
346
+ </body>
347
+ </html>