-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeDoor.php
More file actions
49 lines (43 loc) · 1.4 KB
/
ThreeDoor.php
File metadata and controls
49 lines (43 loc) · 1.4 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
<?php
class ThreeDoor
{
private $max_player = 100000;
public function run()
{
$yes = 0;
$no = 0;
for ($player = 1; $player <= $this->max_player; $player++) {
// 答案
$answer = rand(1, 3);
// 選擇
$choice = rand(1, 3);
// 排除選擇後的選擇
$other = [1, 2, 3];
unset($other[$choice - 1]);
// 如果 選擇等於答案 那麼就任意開一扇門給他
if ($choice === $answer) {
$point = array_rand($other) + 1;
} else {
// 否則 就排除答案的門 開另一扇門給他
$temp = $other;
unset($temp[$answer - 1]);
$temp = array_values($temp);
$point = $temp[0];
}
// 取得剩餘的那道門,玩家換的話的門
$temp = $other;
unset($temp[$point - 1]);
$temp = array_values($temp);
$player_change2 = $temp[0];
if ($answer === $player_change2) {
$yes += 1;
} elseif ($answer === $choice) {
$no += 1;
}
}
echo "換的話,中獎的機率:" . $yes / $this->max_player . "<br>";
echo "不換的話,中獎的機率:" . $no / $this->max_player . "<br>";
}
}
$game = new ThreeDoor();
$game->run();