Skip to content

Instantly share code, notes, and snippets.

@codenamegary
Forked from s3gfau1t/gist:7013893
Last active December 25, 2015 17:39
Show Gist options
  • Save codenamegary/7014477 to your computer and use it in GitHub Desktop.
Save codenamegary/7014477 to your computer and use it in GitHub Desktop.
<?php
$velocities = array("turtle", "donkey", "rabbit");
$players = array(
'1' => array(
'name' => 'Foo',
'velocity' => $velocities[rand(0,2)],
'actions' => array(),
),
'2' => array(
'name' => 'Bar',
'velocity' => $velocities[rand(0,2)],
'actions' => array(),
),
);
echo "(player 1) {$players['1']['name']} :: will go as fast as a {$players['1']['velocity']}\r\n";
echo "(player 2) {$players['2']['name']} :: will go as fast as a {$players['2']['velocity']}\r\n";
$actionsFunc = function(){
return array(
array('action' => 'stumbles', 'distance' => 0 ),
array('action' => 'leaps', 'distance' => rand(1,3) ),
array('action' => 'strides', 'distance' => rand(1,3) ),
array('action' => 'sprints', 'distance' => rand(3,15) ),
array('action' => 'falls', 'distance' => 0 ),
array('action' => 'shuffles', 'distance' => rand(1,2) ),
array('action' => 'walks', 'distance' => rand(1,4) ),
array('action' => 'runs', 'distance' => rand(2,7) ),
array('action' => 'skips', 'distance' => rand(1,2) ),
array('action' => 'gasps', 'distance' => 0 ),
array('action' => 'cavorts', 'distance' => rand(1,3) ),
array('action' => 'dances', 'distance' => rand(1,3) ),
array('action' => 'pauses', 'distance' => 0 ),
array('action' => 'zips', 'distance' => rand(1,5) ),
array('action' => 'zooms', 'distance' => rand(1,5) ),
array('action' => 'collapses', 'distance' => 0 ),
array('action' => 'faceplants', 'distance' => 0 ),
);
};
$track_length = rand(50, 1000);
$playerDistance =
function($player){
$distance = 0;
foreach($player['actions'] as $action)
$distance += $action['distance'];
return $distance;
};
$raceEnded =
function($players)use($playerDistance, $track_length){
$p1_distance = $playerDistance($players['1']);
$p2_distance = $playerDistance($players['2']);
if( $p1_distance >= $track_length && $p2_distance >= $track_length ) return true;
return false;
};
echo "AND THEY'RE OFF!!!!!!!!!!\r\n";
echo "---------------------------------------------------------\r\n";
$round = 0;
while(!$raceEnded($players)) {
$round++;
foreach($players as $index => &$player)
{
$actions = $actionsFunc();
$randomAction = $actions[mt_rand(0, count($actions)-1)];
$player['actions'][] = $randomAction;
echo "ROUND $round > (player " . $index . ") " . $player['name'] . " :: who is as fast as a " . $player['velocity'] . " just " . $randomAction['action'] . " for " . $randomAction['distance'] . " mile(s)\r\n";
}
}
echo "---------------------------------------------------------\r\n";
$milesPerTurn = function($player)use($playerDistance) {
return round($playerDistance($player) / count($player['actions']),2);
};
$distance1 = $playerDistance($players['1']);
$distance2 = $playerDistance($players['2']);
if($distance1 === $distance2)
{
echo "It's a tie! Both players travelled " . $distance1 . " over " . $round . " rounds.\r\n";
exit;
}
$winner = $distance1 > $distance2 ? array('index' => '1', 'player' => $players['1']) : array('index' => '2', 'player' => $players['2']);
echo "(player " . $winner['index'] . ") " . $winner['player']['name'] . " :: who is as fast as a " . $winner['player']['velocity'] ." WON!\r\n";
echo $playerDistance($winner['player']) . " miles travelled in " . $round . " rounds.\r\n";
echo "SPEED: " . $milesPerTurn($winner['player']) . " MPT (miles per turn)\r\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment