-
-
Save s3gfau1t/7014684 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$velocities = array("turtle", "donkey", "rabbit"); | |
$mode = $argv[1]; | |
$file = $argv[2]; | |
$output = ''; | |
$players = array( | |
'1' => array( | |
'name' => 'Foo', | |
'velocity' => $velocities[rand(0,2)], | |
'actions' => array(), | |
), | |
'2' => array( | |
'name' => 'Bar', | |
'velocity' => $velocities[rand(0,2)], | |
'actions' => array(), | |
), | |
); | |
if($mode == 'file'){ | |
$output = $output . "(player 1) {$players['1']['name']} :: will go as fast as a {$players['1']['velocity']}\r\n"; | |
$output = $output . "(player 2) {$players['2']['name']} :: will go as fast as a {$players['2']['velocity']}\r\n"; | |
} | |
else{ | |
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; | |
}; | |
if($mode == 'file'){ | |
$output = $output . "AND THEY'RE OFF!!!!!!!!!!\r\n"; | |
$output = $output . "---------------------------------------------------------\r\n"; | |
} | |
else{ | |
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; | |
if($mode == 'file'){ | |
$output = $output . "ROUND $round > (player " . $index . ") " . $player['name'] . " :: who is as fast as a " . $player['velocity'] . " just " . $randomAction['action'] . " for " . $randomAction['distance'] . " mile(s)\r\n"; | |
} | |
else{ | |
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"; | |
} | |
} | |
} | |
if($mode == 'file'){ | |
$output = $output . "---------------------------------------------------------\r\n"; | |
} | |
else{ | |
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) | |
{ | |
if($mode == 'file'){ | |
$output = $output . "It's a tie! Both players travelled " . $distance1 . " over " . $round . " rounds.\r\n"; | |
} | |
else{ | |
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']); | |
if($mode == 'file'){ | |
$output = $output . "(player " . $winner['index'] . ") " . $winner['player']['name'] . " :: who is as fast as a " . $winner['player']['velocity'] ." WON!\r\n"; | |
$output = $output . $playerDistance($winner['player']) . " miles travelled in " . $round . " rounds.\r\n"; | |
$output = $output . "SPEED: " . $milesPerTurn($winner['player']) . " MPT (miles per turn)\r\n"; | |
$fp = fopen($file, 'a+'); | |
fwrite($fp, $output); | |
fclose($fp); | |
} | |
else{ | |
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