Last active
June 28, 2017 23:28
-
-
Save arimolzer/f32c6a151fa3c38efea446785892231d to your computer and use it in GitHub Desktop.
Play rock, paper, scissors against PHP - Written to help educate a friend.
This file contains hidden or 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 | |
| // This gist was written for a high school student who told me about a project he was given to write | |
| // a game of rock, paper, scissors. This gist was written to show him my solution to such a problem. | |
| // Obviously, this is a PHP script to be run in the command line. | |
| // Change the $myChoice variable to your choice as per the key below: | |
| // 0 = Rock | |
| // 1 = Paper | |
| // 2 = Scissors | |
| // Change this value to your selected option. | |
| $myChoice = 0; | |
| // The machine will randomly select an option (Note: PHP rand() will return a pseudo-random integer). | |
| $machineChoice = rand(0, 2); | |
| // Define an array of valid choices. | |
| $validChoices = [0, 1, 2]; | |
| // Validate $myChoice against the valid choices. | |
| if (in_array($myChoice, $validChoices)) { | |
| // The first level of the array is the machines choice. The array key relates to the key above. | |
| // The second level of the array is $myChoice. The array key also relates to the key above. | |
| // The resulting value, is a win/loss/draw value. Check the switch below. | |
| $matrix = [ | |
| [2, 1, 0], | |
| [0, 2, 1], | |
| [1, 0, 2] | |
| ]; | |
| $result = $matrix[$machineChoice][$myChoice]; | |
| // check the result and print a result message. | |
| switch ($result) { | |
| case 2: | |
| echo "Draw."; | |
| break; | |
| case 1: | |
| echo "You Win."; | |
| break; | |
| case 0: | |
| echo "The Computer Wins."; | |
| break; | |
| default: | |
| echo "Something went wrong... Somehow."; | |
| break; | |
| } | |
| } else { | |
| echo "Invalid Selection - Please choose 0 for Rock, 1 for Paper and 2 for Scissors."; | |
| } |
This file contains hidden or 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 | |
| namespace AppBundle\Command; | |
| use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
| use Symfony\Component\Console\Input\InputArgument; | |
| use Symfony\Component\Console\Input\InputInterface; | |
| use Symfony\Component\Console\Output\OutputInterface; | |
| /** | |
| * Rock, paper, scissors game written for a Symfony 3.0.9 command | |
| * | |
| * Class RockPaperScissorsCommand | |
| * @package AppBundle\Command | |
| */ | |
| class RockPaperScissorsCommand extends ContainerAwareCommand | |
| { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| protected function configure() | |
| { | |
| $this | |
| ->setName('app:rock_paper_scissors_command') | |
| ->setDescription('A simple game of rock, paper, scissors.') | |
| ->addArgument('choice', InputArgument::REQUIRED, 'Rock, paper or scissors?'); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| protected function execute(InputInterface $input, OutputInterface $output) | |
| { | |
| $myChoice = $this->mapChoice($input->getArgument('choice')); | |
| $machineChoice = rand(0, 2); | |
| $validChoices = [0, 1, 2]; | |
| if (in_array($myChoice, $validChoices)) { | |
| $matrix = [ | |
| [2, 1, 0], | |
| [0, 2, 1], | |
| [1, 0, 2] | |
| ]; | |
| $result = $matrix[$machineChoice][$myChoice]; | |
| switch ($result) { | |
| case 2: | |
| $result = "Draw."; | |
| break; | |
| case 1: | |
| $result = "You Win."; | |
| break; | |
| case 0: | |
| $result = "The Computer Wins."; | |
| break; | |
| default: | |
| $result = "Something went wrong... Somehow."; | |
| break; | |
| } | |
| // Tell the user who won | |
| $output->writeln('--- ' . $result . ' ---'); | |
| } | |
| } | |
| /** | |
| * @param $choice | |
| * @return int|null | |
| */ | |
| protected function mapChoice($choice){ | |
| // Allow misspellings of the choices to convert to valid options. | |
| $rockChoices = ['r', 'rock', 'rok', 'roc']; | |
| $paperChoices = ['p', 'paper', 'papr', 'papre', 'parpe']; | |
| $scissorsChoices = ['s', 'scissors', 'scissor', 'scisors', 'sissors', 'sisors']; | |
| // Return an integer value to represent the choice | |
| if (in_array($choice, $rockChoices)) { | |
| return 0; | |
| } elseif (in_array($choice, $paperChoices)) { | |
| return 1; | |
| } elseif (in_array($choice, $scissorsChoices)) { | |
| return 2; | |
| } else { | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment