Skip to content

Instantly share code, notes, and snippets.

@s3gfau1t
Forked from codenamegary/gist:7015030
Last active December 25, 2015 17:48
Show Gist options
  • Save s3gfau1t/7015272 to your computer and use it in GitHub Desktop.
Save s3gfau1t/7015272 to your computer and use it in GitHub Desktop.
<?php
$velocities = array("turtle", "donkey", "rabbit");
$mode = isset($argv[1]) ? $argv[1] : 'console';
$file = isset($argv[2]) ? $argv[2] : '';
$fp = null;
$raceWriters = array(
'file' => function($output)use($file, $fp){
if(!$fp) $fp = fopen($file, 'a+');
fwrite($fp, $output);
},
'console' => function($output){
echo $output;
},
);
$raceWriter = function($output)use($raceWriters, $mode, $file, $fp){
$writer = $raceWriters[$mode];
$writer($output);
};
$getPlayer = function()use($velocities){
$name_parts = array('foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'grault', 'garply', 'waldo', 'fred', 'plugh', 'xyzzy', 'thud');
return array(
'name' => ucwords($name_parts[mt_rand(0, count($name_parts)-1)]) . ' ' . ucwords($name_parts[mt_rand(0, count($name_parts)-1)]),
'velocity' => $velocities[rand(0,2)],
'actions' => array()
);
};
$getPlayers = function()use($getPlayer, $velocities){
$num = rand(2, 6);
$players = array();
for ( $i = 1; $i <= $num; $i++ )
$players[] = $getPlayer($velocities);
return $players;
};
$players = $getPlayers();
$raceWriter("(player 1) {$players['1']['name']} :: will go as fast as a {$players['1']['velocity']}\r\n");
$raceWriter("(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;
};
$milesPerTurn = function($player)use($playerDistance) {
return round($playerDistance($player) / count($player['actions']),2);
};
$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;
};
$raceWriter("AND THEY'RE OFF!!!!!!!!!!\r\n");
$raceWriter("---------------------------------------------------------\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;
$raceWriter("ROUND $round > (player " . $index . ") " . $player['name'] . " :: who is as fast as a " . $player['velocity'] . " just " . $randomAction['action'] . " for " . $randomAction['distance'] . " mile(s)\r\n");
}
}
$raceWriter("---------------------------------------------------------\r\n");
$distance1 = $playerDistance($players['1']);
$distance2 = $playerDistance($players['2']);
if($distance1 === $distance2)
{
$raceWriter("It's a tie! Both players travelled " . $distance1 . " over " . $round . " rounds.\r\n");
} else {
$winner = $distance1 > $distance2 ? array('index' => '1', 'player' => $players['1']) : array('index' => '2', 'player' => $players['2']);
$raceWriter("(player " . $winner['index'] . ") " . $winner['player']['name'] . " :: who is as fast as a " . $winner['player']['velocity'] ." WON!\r\n");
$raceWriter($playerDistance($winner['player']) . " miles travelled in " . $round . " rounds.\r\n");
$raceWriter("SPEED: " . $milesPerTurn($winner['player']) . " MPT (miles per turn)\r\n");
}
if($fp) fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment