Skip to content

Instantly share code, notes, and snippets.

@lucablackwell
Last active November 29, 2022 10:51
Show Gist options
  • Save lucablackwell/ee2329efe3fdc804908831e834ee2a6d to your computer and use it in GitHub Desktop.
Save lucablackwell/ee2329efe3fdc804908831e834ee2a6d to your computer and use it in GitHub Desktop.
rock paper scissors with various options, file functionality
<?php
$choices = array('r', 'p', 's');
$start = microtime(true);
$wins = 0;
$draws = 0;
$losses = 0;
$prandom = true;
// Get input
$input = getopt('rpshdg:o::f:', array('rock', 'paper', 'scissors', 'help', 'long', 'short', 'delete', 'games:', 'file:'));
// Help Menu
if (array_key_exists('help', $input) || array_key_exists('h', $input) || empty($input)) {
echo "\n HELP MENU\nSHORT LONG DESCRIPTION\n-h --help Display this text.\n-g --games Choose number of games (1 by default.)\n-r --rock Choose rock for every game (random by default.)\n-p --paper Choose paper for every game (random by default.)\n-s --scissors Choose scissors for every game (random by default.)\n-o --outcomes Display outcome messages (slows program down.)\n-ol --long Display long outcome messages (slows program down.)\n-d --delete Delete contents of output file (reset game history.)\n-w --write Write to the output file.\n-f --file Set name of output file, will create if non-existent ('results.txt' by default.)\n\n";
exit;
}
// Get number of games
if (array_key_exists('games', $input) && $input['games'] >= 1) {
$gamenumber = $input['games'];
} elseif (array_key_exists('g', $input) && $input['g'] >= 1){
$gamenumber = $input['g'];
} else {
$gamenumber = 1;
}
// Get output filename
if (array_key_exists('file', $input) && !empty($input['file'])) {
if (!array_key_exists('o', $input) && !array_key_exists('short', $input) && !array_key_exists('long', $input)) {
echo "Enable output for file writing.\n";
}
$filename = $input['file'];
} elseif (array_key_exists('f', $input) && !empty($input['f'])){
if (!array_key_exists('o', $input) && !array_key_exists('short', $input) && !array_key_exists('long', $input)) {
echo "Enable output for file writing.\n";
}
$filename = $input['f'];
} else {
$filename = "results.txt";
}
$file = fopen($filename, 'a');
// Output results
function output($pchoice, $cchoice, $op, $point, $file) {
echo "$pchoice $op $cchoice, $point\n";
fwrite($file, "$pchoice $op $cchoice, $point\n");
}
function outcomeLong($player, $computer, $file) {
switch ([$player, $computer]) {
case ['r', 'r']:
output('Rock', 'Rock', 'is the same as', 'draw!', $file);
return 'draws';
case ['r', 'p']:
output('Rock', 'Paper', 'is covered by', 'you lose!', $file);
return 'loses';
case ['r', 's']:
output('Rock', 'Scissors', 'smashes', 'you win!', $file);
return 'wins';
case ['p', 'r']:
output('Paper', 'Rock', 'covers', 'you win!', $file);
return 'wins';
case ['p', 'p']:
output('Paper', 'Paper', 'is the same as', 'draw!', $file);
return 'draws';
case ['p', 's']:
output('Paper', 'Scissors', 'is cut by', 'you lose!', $file);
return 'loses';
case ['s', 'r']:
output('Scissors', 'Rock', 'is smashed by', 'you lose!', $file);
return 'loses';
case ['s', 'p']:
output('Scissors', 'Paper', 'cuts', 'you win!', $file);
return 'wins';
case ['s', 's']:
output('Scissors', 'Scissors', 'is the same as', 'draw!', $file);
return 'draws';
default:
echo 'INVALID COMBINATION';
echo "\n";
}
}
function outcomeShort($player, $computer, $file){
switch ([$player, $computer]) {
case ['r', 'r']:
output('R', 'R', '=', '+0', $file);
return 'draws';
case ['r', 'p']:
output('R', 'P', '<', '-1', $file);
return 'loses';
case ['r', 's']:
output('R', 'S', '>', '+1', $file);
return 'wins';
case ['p', 'r']:
output('P', 'R', '>', '+1', $file);
return 'wins';
case ['p', 'p']:
output('P', 'P', '=', '+0', $file);
return 'draws';
case ['p', 's']:
output('P', 'S', '<', '-1', $file);
return 'loses';
case ['s', 'r']:
output('S', 'R', '<', '-1', $file);
return 'loses';
case ['s', 'p']:
output('P', 'S', '>', '+1', $file);
return 'wins';
case ['s', 's']:
output('S', 'S', '=', '+0', $file);
return 'draws';
default:
echo 'INVALID COMBINATION';
echo "\n";
}
}
// Evaluate and output results
function getResults($player, $computer, $input, $file){
if (array_key_exists('o', $input)){
switch ($input['o']){
case 'l':
return outcomeLong($player, $computer, $file);
default:
return outcomeShort($player, $computer, $file);
}
} elseif (array_key_exists('long', $input)) {
return outcomeLong($player, $computer, $file);
} elseif (array_key_exists('short', $input)) {
return outcomeShort($player, $computer, $file);
} else {
switch ([$player, $computer]) {
case ['r', 'r']:
return 'draws';
case ['r', 'p']:
return 'loses';
case ['r', 's']:
return 'wins';
case ['p', 'r']:
return 'wins';
case ['p', 'p']:
return 'draws';
case ['p', 's']:
return 'loses';
case ['s', 'r']:
return 'loses';
case ['s', 'p']:
return 'wins';
case ['s', 's']:
return 'draws';
default:
echo 'INVALID COMBINATION';
echo "\n";
}
}
}
// Player options:
if (array_key_exists('rock', $input) || array_key_exists('r', $input)) { // Is rock
$playerchoice = 'r';
$prandom = false;
} elseif (array_key_exists('paper', $input)|| array_key_exists('p', $input)) { // Is paper
$playerchoice = 'p';
$prandom = false;
} elseif (array_key_exists('scissors', $input) || array_key_exists('s', $input)) { // Is scissors
$playerchoice = 's';
$prandom = false;
}
// Loop through games
for ($x = 0; $x <= ($gamenumber-1); $x++) {
// Computer chooses
$compchoice = $choices[array_rand($choices)];
if ($prandom) {
$playerchoice = $choices[array_rand($choices)]; // Is random
}
switch (getResults($playerchoice, $compchoice, $input, $file)) { // Evaluate results
case 'wins':
$wins++;
break;
case 'draws':
$draws++;
break;
case 'loses':
$losses++;
break;
default:
echo 'INVALID OUTCOME';
echo "\n";
}
}
// Delete history
if (array_key_exists('delete', $input) || array_key_exists('d', $input)) {
echo "DELETING...\n";
$file = fopen($filename,'w');
}
if (array_key_exists('o', $input)) {
if (array_key_exists('long', $input) || $input['o'] == 'l') {
echo "\nWins: $wins\nLosses: $losses\nDraws: $draws\nTotal: $gamenumber\nWritten output to '$filename'\nTime: ".(microtime(true)-$start)."\n";
} else {
echo "\nW/L/D/T\n$wins/$losses/$draws/$gamenumber\n\nWritten output to '$filename'\nTime: ".(microtime(true)-$start)."\n";
}
} else {
echo "\nW/L/D/T\n$wins/$losses/$draws/$gamenumber\nTime: ".(microtime(true)-$start)."\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment