In these first few weeks you have learned enough PHP to be able to create a simp
ID: 3707658 • Letter: I
Question
In these first few weeks you have learned enough PHP to be able to create a simple web-based game, so let make is fun! This week you challenge is to recreate the pop-culture game "Rock, Paper, Scissors, Lizard, Spock" or RPSLS for short. The rules are simple, and the video that made the game famous explains it well. The expectations and grade rubric is quite simple: Two players have five choices to pick from, resulting in 25 unique combinations that must be evaluated. To achieve a perfect score, all rules (the 25 permutations) must be implemented and your code must calculate the correct result with 100% success rate. For every combination that does not work, you lose points. Only the implementation of rules will be graded, no other aspect. You may use any method you wish implement the new rules, but the use of functions and arrays are encouraged as they will simplify your code. The output of your program must include the item picked by both players as well as the calculated winner. Both players hand's must be chosen randomly by the program. Warnings and errors generated by your program will not be counted against your score, provided that that game still functions correctly. Coding Hints: If you are using the array approach, consider using nested arrays and the in_array function. The array_search function my also be handy, but may be more complicated than you need. If you do use array_search, pay close attention to the pink warning on php.net site. If you are not using the array approach, consider using a switch statement instead of a series if elseif's. Don't forget to change the way the players choices are randomized, remember that you have 5 possible. Remember the rules to "truthy-ness". The following values will evaluate to a false unless you use a === or !== operator to compare them. "" - an empty string 0 - 0 as an integer "0" - 0 as a string NULL FALSE array() - an empty array undeclared variables Sometimes multidimensional (nested) arrays can be scary. Often using dynamic variable names can reduce the need to use a nested array. Execute the code below for an example.
Explanation / Answer
RSPLS.php
<?php
namespace App;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleQuestionQuestion;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
class RSPLS extends Command
{
const RK = 0;
const PPR = 1;
const SCR = 2;
const LIZ = 3;
const SPCK = 4;
private $moves = [
self::RK => 'rock',
self::PPR => 'paper',
self::SCR => 'scissors',
self::LIZ => 'lizard',
self::SPCK => 'spock'
];
private $wins = [
self::RK => [
self::SCR => 'Rock crushes scissors',
self::LIZ => 'Rock crushes lizard'
],
self::PPR => [
self::RK => 'Paper covers rock',
self::SPCK => 'Paper disproves Spock'
],
self::SCR => [
self::PPR => 'Scissors cut paper',
self::LIZ => 'Scissors decapitate lizard'
],
self::LIZ => [
self::SPCK => 'Lizard poisons Spock',
self::PPR => 'Lizard eats paper'
],
self::SPCK => [
self::SCR => 'Spock smashes scissors',
self::RK => 'Spock vaporizes rock'
]
];
protected function configure()
{
$this->setName('play')
->setDescription('Play a game of Rock-Paper-Scissors-Lizard-Spock');
}
protected function exec(InputInterface $inp, OutputInterface $op)
{
$op->writeln('<info>Game begins!</info>');
$op->writeln('');
$op->writeln('<comment>Possible moves: Rock, Paper, Scissors, Lizard, Spock</comment>');
$hlpr = $this->getHelper('quest');
$quest = new Question('Your mv: ');
$quest->setMaxAttempts(3);
$quest->setValidator(function($answer) use ($op) {
$mv = array_search(strtolower(trim($answer)), $this->moves);
if ($mv === false) {
throw new RuntimeException('Wrong mv, man. Try again.');
}
return $mv;
});
try {
$mv = $hlpr->ask($inp, $op, $quest);
} catch (RuntimeException $t) {
$op->writeln('<error>Read the rules.</error>');
return;
}
$gameMv = $this->pickMv($mv);
$op->writeln('I played: ' . $this->mvToStr($gameMv));
$op->writeln('Winner: ' . $this->detWinners($mv, $gameMv));
}
protected function mvToStr($mv)
{
return ucfirst($this->moves[$mv]);
}
protected function pickMv($mv)
{
return array_rand($this->moves);
}
protected function detWinners($playaMv, $gameMv)
{
$usrWin = isset($this->wins[$playaMv][$gameMv]);
if ($usrWin) return $this->wins[$playaMv][$gameMv] . ', <info>you win</info>!';
$gmWin = isset($this->wins[$gameMv][$playaMv]);
if ($gmWin) return $this->wins[$gameMv][$playaMv] . ', <error>you lost</error>!';
return 'Tie!';
}
}
RPSLS1.php
<?php
require __DIR__ . '/vendor/autoload.php';
use AppRSPLS;
use SymfonyComponentConsoleApplication;
use SymfonyComponentConsoleInputArrayInput;
$inp = new ArrayInput(['play']);
$appl = new Application;
$appl->add(new RSPLS);
$appl->run($inp);
RSPLS.json
{
"name": "Rock-Paper-Scissors-Lizard-Spock",
"description": "Rock-Paper-Scissors-Lizard-Spock",
"require": {
"symfony/console": "3.0.*@dev"
},
"autoload": {
"psr-4": {
"App\": ""
}
},
"minimum-stability": "dev"
}