// Did we actually get values sent?
$firstVisit = true;
if (count($_POST) > 0) {
$firstVisit = false;
}
$trials = 10000;
$length = 60;
$frequency = 1.5; // editable
$chance = 4; // editable
$timeBonus = 0; // editable
$alert = "";
if (array_key_exists('frequency', $_POST) && is_numeric($_POST['frequency'])) {
$frequency = max(0.1, $_POST['frequency']);
}
if (array_key_exists('timeBonus', $_POST) && is_numeric($_POST['timeBonus'])) {
$timeBonus = max(0, $_POST['timeBonus']);
}
if (array_key_exists('chance', $_POST) && is_numeric($_POST['chance'])) {
if ( $_POST['chance'] < 0) {
$alert .= "Alert: Chance of powerup was less than 0%. \n";
}
elseif ($_POST['chance'] > 100) {
$alert .= "Alert: Chance of powerup was greater than 100%. \n";
}
$chance = max(0, $_POST['chance']);
$chance = min($chance, 100);
}
$totalPowerups = 0;
$totalTiles = 0;
$totalTime = 0;
?>
Puzzle 4
You are an assistant game designer working on a puzzle game.
In the game, tiles appear every few seconds, and sometimes a tile has a powerup.
The lead designer tells you to balance the game so that players see
5 powerups per game, on average.
Note: Not all of the numbers below are editable.
srand((float)microtime()*1000000);
function randFloat() {
return mt_rand() / mt_getrandmax();
}
if (!$firstVisit) {
for ($t = 0; $t < $trials; $t++) {
$currentTime = $length;
$totalTime += $length;
while ($currentTime > 0) {
// draw a tile
$currentTime -= $frequency;
$totalTiles++;
// did we draw a powerup?
if (randFloat()*100 <= $chance) {
$totalPowerups++;
// possible to get into an infinite loop if time bonus is too high.
// that's OK - PHP page will bail eventually
$currentTime += $timeBonus;
$totalTime += $timeBonus;
}
}
}
echo $alert;
echo "After ".$trials." tries, the average number of powerups per game was ";
echo round($totalPowerups/$trials, 1)." powerups. \n";
echo "After ".$trials." tries, each game had an average of ";
echo round($totalTiles/$trials, 1)." tiles. \n";
echo "After ".$trials." tries, each game took an average of ";
echo round($totalTime/$trials, 1)." seconds. \n";
echo " \n";
}
?>
Back to Puzzle List