File size: 11,696 Bytes
4255443 9ea6432 4255443 c4b2f36 4255443 34e5cd7 8412f4d 34e5cd7 8412f4d bfa94ae 34e5cd7 8412f4d 34e5cd7 8412f4d 34e5cd7 8412f4d 4255443 34e5cd7 4255443 9ea6432 4255443 34e5cd7 4255443 9ea6432 4255443 34e5cd7 4255443 34e5cd7 9ea6432 4255443 8412f4d bfa94ae 34e5cd7 9ea6432 34e5cd7 9ea6432 34e5cd7 9ea6432 34e5cd7 9ea6432 34e5cd7 9ea6432 34e5cd7 4255443 8412f4d 9ea6432 8412f4d 6e420eb c4b2f36 a621bf5 4255443 8412f4d 4255443 bfa94ae 4255443 9ea6432 4255443 9ea6432 8412f4d 4255443 9ea6432 4255443 8412f4d 4255443 8412f4d 4255443 8412f4d 4255443 05b44ba 1eee224 05b44ba 8412f4d 05b44ba 1eee224 9ea6432 1eee224 e4d2581 1eee224 9ea6432 1eee224 8412f4d bfa94ae 6e420eb bfa94ae 6e420eb bfa94ae 6e420eb bfa94ae 6e420eb bfa94ae 9ea6432 bfa94ae 6e420eb 9ea6432 6e420eb bfa94ae 6e420eb bfa94ae 6e420eb bfa94ae c4b2f36 a621bf5 05b44ba | 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | <!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>三門問題遊戲</title>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Noto Sans TC', Arial, sans-serif;
text-align: center;
padding: 20px;
background-color: #f0f0f0;
margin: 0;
min-height: 100vh;
display: flex;
flex-direction: row;
justify-content: center;
gap: 40px;
position: relative;
box-sizing: border-box;
}
.game-container {
flex: 1;
max-width: 600px;
width: 100%;
}
.stats-container {
flex: 1;
max-width: 300px;
width: 100%;
text-align: left;
}
.door-container {
display: flex;
justify-content: center;
gap: 15px;
margin: 20px 0;
flex-wrap: wrap;
}
.door {
cursor: pointer;
border: 2px solid #333;
padding: 10px;
background-color: #fff;
flex: 0 0 auto;
}
.door img {
width: 100px;
height: 150px;
max-width: 100%;
}
#message {
font-size: 1.2em;
margin: 20px 0;
word-wrap: break-word;
}
button {
padding: 10px 20px;
font-size: 1em;
margin: 5px;
cursor: pointer;
width: auto;
min-width: 120px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #333;
padding: 8px;
text-align: center;
}
th {
background-color: #ddd;
}
.footer {
position: absolute;
bottom: 10px;
right: 10px;
font-size: 0.9em;
color: #555;
}
@media (max-width: 768px) {
body {
flex-direction: column;
gap: 20px;
}
.game-container, .stats-container {
max-width: 100%;
}
.door img {
width: 80px;
height: 120px;
}
button {
font-size: 0.9em;
padding: 8px 16px;
min-width: 100px;
}
#message {
font-size: 1em;
}
.footer {
position: static;
text-align: center;
margin-top: 20px;
}
}
@media (max-width: 480px) {
.door img {
width: 60px;
height: 90px;
}
button {
font-size: 0.8em;
padding: 6px 12px;
min-width: 80px;
}
#message {
font-size: 0.9em;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1.2em;
}
}
</style>
</head>
<body>
<div class="game-container">
<h1>三門問題遊戲</h1>
<div id="intro">
<p>歡迎來到三門問題遊戲!遊戲規則如下:<br>
有三道門,任選一道,其中兩道門後面是一顆糖果,另一道門後面是一棟豪宅。<br>
你選門後,主持人會打開一道有糖果的門,然後問你要不要換門。<br>
準備好了嗎?請選擇一道門開始遊戲!</p>
</div>
<div id="doors" class="door-container">
<div class="door" onclick="chooseDoor(0)"><img src="door.jpg" alt="門 1"></div>
<div class="door" onclick="chooseDoor(1)"><img src="door.jpg" alt="門 2"></div>
<div class="door" onclick="chooseDoor(2)"><img src="door.jpg" alt="門 3"></div>
</div>
<div id="message"></div>
<div id="switch-choice" style="display: none;">
<button onclick="switchDoor(true)">換門</button>
<button onclick="switchDoor(false)">不換門</button>
</div>
<button onclick="promptForSimulation(false)">模擬 100 次:2號門不換 (需密碼)</button>
<button onclick="promptForSimulation(true)">模擬 100 次:2號門換門 (需密碼)</button>
<button onclick="promptForTenDoors()">進入十門版本 (需密碼)</button>
<button onclick="promptForTeacherVersion()">進入教師教學版本 (需密碼)</button>
</div>
<div class="stats-container">
<h2>中獎機率統計</h2>
<table id="stats-table">
<tr>
<th>選擇</th>
<th>次數</th>
<th>中獎次數</th>
<th>中獎機率</th>
</tr>
<tr>
<td>換門</td>
<td id="switch-count">0</td>
<td id="switch-wins">0</td>
<td id="switch-rate">0%</td>
</tr>
<tr>
<td>不換門</td>
<td id="no-switch-count">0</td>
<td id="no-switch-wins">0</td>
<td id="no-switch-rate">0%</td>
</tr>
</table>
</div>
<div class="footer">程式設計者:新竹縣立精華國中 藍星宇老師</div>
<script>
let playerChoice = null;
let prizeDoor = Math.floor(Math.random() * 3);
let revealedDoor = null;
let switchCount = 0;
let switchWins = 0;
let noSwitchCount = 0;
let noSwitchWins = 0;
function chooseDoor(door) {
playerChoice = door;
document.getElementById("intro").style.display = "none";
do {
revealedDoor = Math.floor(Math.random() * 3);
} while (revealedDoor === playerChoice || revealedDoor === prizeDoor);
const doors = document.getElementsByClassName("door");
doors[revealedDoor].innerHTML = '<img src="candy.jpg" alt="糖果">';
doors[revealedDoor].style.pointerEvents = "none";
document.getElementById("message").innerHTML =
`你選了門 ${playerChoice + 1},主持人開啟了門 ${revealedDoor + 1},後面是一顆糖果。<br>你要換門嗎?`;
document.getElementById("switch-choice").style.display = "block";
for (let i = 0; i < doors.length; i++) {
doors[i].style.pointerEvents = "none";
}
}
function switchDoor(doSwitch) {
let finalChoice = playerChoice;
if (doSwitch) {
finalChoice = 3 - playerChoice - revealedDoor;
switchCount++;
} else {
noSwitchCount++;
}
const doors = document.getElementsByClassName("door");
for (let i = 0; i < 3; i++) {
if (i === prizeDoor) {
doors[i].innerHTML = '<img src="house.jpg" alt="豪宅">';
} else {
doors[i].innerHTML = '<img src="candy.jpg" alt="糖果">';
}
}
const won = finalChoice === prizeDoor;
let result = "";
if (doSwitch) {
result = won ? "換門,有中" : "換門,沒中";
if (won) switchWins++;
} else {
result = won ? "不換門,有中" : "不換門,沒中";
if (won) noSwitchWins++;
}
document.getElementById("message").innerHTML =
`你最終選擇了門 ${finalChoice + 1},豪宅在門 ${prizeDoor + 1}。<br>結果:${result}<br><button onclick="resetGame()">再玩一次</button>`;
document.getElementById("switch-choice").style.display = "none";
updateStats();
}
function resetGame() {
prizeDoor = Math.floor(Math.random() * 3);
playerChoice = null;
revealedDoor = null;
document.getElementById("intro").style.display = "block";
document.getElementById("message").innerHTML = "";
document.getElementById("switch-choice").style.display = "none";
const doors = document.getElementsByClassName("door");
for (let i = 0; i < doors.length; i++) {
doors[i].innerHTML = `<img src="door.jpg" alt="門 ${i + 1}">`;
doors[i].style.pointerEvents = "auto";
}
}
function updateStats() {
document.getElementById("switch-count").innerHTML = switchCount;
document.getElementById("switch-wins").innerHTML = switchWins;
const switchRate = switchCount > 0 ? (switchWins / switchCount * 100).toFixed(1) : 0;
document.getElementById("switch-rate").innerHTML = `${switchRate}%`;
document.getElementById("no-switch-count").innerHTML = noSwitchCount;
document.getElementById("no-switch-wins").innerHTML = noSwitchWins;
const noSwitchRate = noSwitchCount > 0 ? (noSwitchWins / noSwitchCount * 100).toFixed(1) : 0;
document.getElementById("no-switch-rate").innerHTML = `${noSwitchRate}%`;
}
function promptForSimulation(doSwitch) {
const password = prompt("請輸入密碼以啟動模擬功能:");
if (password === "Ghjh") {
runSimulation(doSwitch);
} else {
alert("密碼錯誤!");
}
}
function runSimulation(doSwitch) {
for (let i = 0; i < 100; i++) {
playerChoice = 1;
prizeDoor = Math.floor(Math.random() * 3);
do {
revealedDoor = Math.floor(Math.random() * 3);
} while (revealedDoor === playerChoice || revealedDoor === prizeDoor);
let finalChoice = playerChoice;
if (doSwitch) {
finalChoice = 3 - playerChoice - revealedDoor;
switchCount++;
if (finalChoice === prizeDoor) switchWins++;
} else {
noSwitchCount++;
if (finalChoice === prizeDoor) noSwitchWins++;
}
}
updateStats();
const action = doSwitch ? "換門" : "不換門";
document.getElementById("message").innerHTML =
`模擬完成!已執行 100 次,選擇 2 號門且${action}。<br>請查看右側統計表格。<br><button onclick="resetGame()">再玩一次</button>`;
}
function promptForTenDoors() {
const password = prompt("請輸入密碼以進入十門版本:");
if (password === "TenDoors") {
window.location.href = "ten_doors.html";
} else {
alert("密碼錯誤!");
}
}
function promptForTeacherVersion() {
const password = prompt("請輸入密碼以進入教師教學版本:");
if (password === "Teacher") {
window.location.href = "teacher_version.html";
} else {
alert("密碼錯誤!");
}
}
</script>
</body>
</html> |