error_reporting(E_ALL & ~E_NOTICE);
// A monopoly simulation
$GAMES = 10000;
if (array_key_exists('numtrials', $_POST)
&& is_numeric($_POST['numtrials'])) {
$GAMES = max(1, $_POST['numtrials']);
$GAMES = min(100000, $GAMES);
}
// will the dice auto-avoid landing on jail space
$avoidgotojailspace = 0;
if (array_key_exists('avoidgotojailspace', $_POST)) {
$avoidgotojailspace = $_POST['avoidgotojailspace'];
}
// will the dice auto-avoid three doubles
$avoidtripledoubles = 0;
if (array_key_exists('avoidtripledoubles', $_POST)) {
$avoidtripledoubles = $_POST['avoidtripledoubles'];
}
$chancejail = 11;
if (array_key_exists('chancejail', $_POST)
&& is_numeric($_POST['chancejail'])) {
$chancejail = min(100, max(0, $_POST['chancejail']));
}
$limitbygo = 160;
if (array_key_exists('limitbygo', $_POST)
&& is_numeric($_POST['limitbygo'])) {
$limitbygo = max(0, $_POST['limitbygo']);
}
$rollsperminute = 3.5;
if (array_key_exists('rollsperminute', $_POST)
&& is_numeric($_POST['rollsperminute'])) {
$rollsperminute = max(1, $_POST['rollsperminute']);
}
// will the dice auto-avoid landing on jail space
$showdicerolls = 1;
if (array_key_exists('showdicerolls', $_POST)) {
$showdicerolls = $_POST['showdicerolls'];
}
$max_space = 40;
$chance_space = array(7, 22, 36);
$jail_space = 10;
$gotojail_space = 30;
$current_space = 0;
$go_count = 0;
$jail_count = 0;
$max_doubles = 3;
$d1 = 0;
$d2 = 0;
$results = array();
$dice_results = array();
mt_srand((float)microtime()*1000000);
function roll($num_sides) {
return mt_rand(1,$num_sides);
}
function rolldice () {
global $d1;
global $d2;
$d1 = roll(6);
$d2 = roll(6);
return;
}
$g = 0;
while ($g < $GAMES && ($limitbygo == 0 || $go_count < $limitbygo)) {
$subroll = 0;
$progress = 0;
$interim_progress = 0;
$injail = 0;
$d1 = 0;
$d2 = 0;
// while we are on the first roll or doubles, and not in jail
while ($d1 == $d2 && $injail == 0) {
rolldice();
$subroll++;
// check to see if we're on the Go To Jail space
while (($current_space + $progress + $d1 + $d2) % $max_space == $gotojail_space && $injail == 0) {
if ($avoidgotojailspace) {
rolldice();
} else {
$injail = 1;
$progress = $jail_space - $gotojail_space;
}
}
// check to see if we've rolled too many doubles
while ($subroll == $max_doubles && $d1 == $d2 && $injail == 0) {
if ($avoidtripledoubles) {
rolldice();
} else {
$injail = 1;
$progress = $jail_space - $current_space - $progress;
}
}
// check to see if we're on a chance space
foreach ($chance_space as $s) {
if (($current_space + $progress + $d1 + $d2) % $max_space == $s) {
if ($chancejail >= roll(100)) {
$injail = 1;
$progress = $jail_space - $current_space - $progress - $d1 - $d2;
}
}
}
// store progress
if (!$injail) {
$progress += $d1 + $d2;
// if we just passed go and are on doubles, credit the Go now
if ($current_space + $progress >= $max_space && $d1 == $d2) {
$current_space = $current_space + $progress - $max_space;
$interim_progress = $progress;
$progress = 0;
$go_count++;
}
}
// error checking
$dice_results[] = $d1;
$dice_results[] = $d2;
} // while we're rolling
// update current space
if ($injail) {
$jail_count++;
$current_space = $jail_space;
} else {
$current_space += $progress;
}
// did we pass go?
while ($current_space >= $max_space) {
$current_space -= $max_space;
$go_count++;
}
// keep track of the results
// if we passed go in the middle of the roll
// and we went to jail, then it's simple
if ($injail) {
$results[$progress]++;
}
// if we didn't go to jail, we need to add interim progress
else {
$results[$progress + $interim_progress]++;
}
$g++;
} // for each roll
krsort($results);
// Calculate the average squares advanced per roll
$averageProgress = 0;
$freqErrorCheck = 0;
foreach ($results as $progress => $freq) {
$averageProgress += $progress * $freq;
$freqErrorCheck += $freq;
}
$averageProgress /= $g;
// Calculate the averages
$rollspergo = $g / $go_count;
$averageJailChance = $jail_count / $g * 100;
?>
A Monopoly simulation with rolls
The average squares advanced per roll is
0.
The average number of rolls to Pass Go is 0.
The chance of going to Jail on a roll is 0%.
if ($limitbygo > 0) {
echo " In this simulation, it took ";
echo "".round($g/$rollsperminute,0)." minutes to pass Go ";
echo $limitbygo." times. ";
}
?>
Roll Progress
Frequency
Percentage
foreach ($results as $progress => $freq) { ?>
0%
} ?>
if ($showdicerolls) {
echo "The first 100 die rolls: ";
$i = 1;
while ($i < count($dice_results) && $i < 100) {
echo "(".$dice_results[$i-1]." ".$dice_results[$i].") ";
$i += 2;
}
}
?>