Skip to content

Instantly share code, notes, and snippets.

@yourwebmaker
Last active June 10, 2016 05:04
Show Gist options
  • Save yourwebmaker/9cf5acad17c4ed36c2f8 to your computer and use it in GitHub Desktop.
Save yourwebmaker/9cf5acad17c4ed36c2f8 to your computer and use it in GitHub Desktop.

Rock-Paper-Scissors-Spock-Lizard

http://bigbangtheory.wikia.com/wiki/Rock_Paper_Scissors_Lizard_Spock

This is an expansion of the classic Rock-Paper-Scissors game, which adds "Spock" and "lizard" to the standard three choices.

The rules are:

  • Scissors cuts Paper
  • Paper covers Rock
  • Rock crushes Lizard
  • Lizard poisons Spock
  • Spock smashes Scissors
  • Scissors decapitates Lizard
  • Lizard eats Paper
  • Paper disproves Spock
  • Spock vaporizes Rock
  • Rock crushes scissors

You now should create a PHP version of this game where two players can use one of the five weapons. It then should display the outcome, eg: "Scissors cuts Paper", and say what player won the round.

Nice to have:

  • Interactive weapon choice using CLI
  • Test cases
<?php
function play($weapon1, $weapon2) {
$weapons = [
['Scissors', 'cuts' , 'Paper'],
['Paper', 'covers', 'Rock'],
['Rock', 'crushes', 'Lizard'],
['Lizard', 'poisons', 'Spock'],
['Spock', 'smashes', 'Scissors'],
['Scissors', 'decapitates', 'Lizard'],
['Lizard', 'eats', 'Paper'],
['Paper', 'disproves', 'Spock'],
['Spock', 'vaporizes', 'Rock'],
['Rock', 'crushes', 'Scissors']
];
$match = false;
foreach ($weapons as $weapon) {
if ($weapon1 == $weapon[0] && $weapon2 == $weapon[2]) {
$message = "Player 1 wins: {$weapon1} {$weapon[1]} {$weapon2}";
$match = true;
}
if ($weapon2 == $weapon[0] && $weapon1 == $weapon[2]) {
$message = "Player 2 wins: {$weapon2} {$weapon[1]} {$weapon1}";
$match = true;
}
}
if (!$match) {
$message = 'Incompatible Game';
}
return $message;
}
/*The rules are:
- Scissors cuts Paper
- Paper covers Rock
- Rock crushes Lizard
- Lizard poisons Spock
- Spock smashes Scissors
- Scissors decapitates Lizard
- Lizard eats Paper
- Paper disproves Spock
- Spock vaporizes Rock
- Rock crushes scissors*/
$games = [
['Scissors', 'Paper', 'Player 1 wins: Scissors cuts Paper'],
['Paper', 'Scissors', 'Player 2 wins: Scissors cuts Paper'],
['Paper', 'Rock', 'Player 1 wins: Paper covers Rock'],
['Spock', 'Scissors', 'Player 1 wins: Spock smashes Scissors'],
['Scissors', 'Rock', 'Player 2 wins: Rock crushes Scissors'],
['Spock', 'Rock', 'Player 1 wins: Spock vaporizes Rock'],
['Rock', 'Spock', 'Player 2 wins: Spock vaporizes Rock'],
['Lizard', 'Scis', 'Incompatible Game'],
];
foreach ($games as $game) {
$player1 = $game[0];
$player2 = $game[1];
$phrase = $game[2];
$outcome = play($player1, $player2);
if ($outcome != $phrase) {
echo "Fail...." . "Expected: '{$phrase}', got '{$outcome}'";
echo PHP_EOL;
} else {
echo 'Pass';
echo PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment