|
<?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; |
|
} |
|
} |