From ea9241217c2d69b49385871d77bde1b4df15dff4 Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Tue, 18 Feb 2020 19:33:48 +0000 Subject: [PATCH] Final update --- Game.php | 174 ++++++++++++++++++++++++++++++++++++++ LICENSE | 8 ++ composer.json | 7 +- src/Base/BaseGameItem.php | 18 ++-- src/Base/Player.php | 98 +++++++++++++++++++++ src/Core/Paper.php | 2 +- src/Helpers/Menu.php | 29 +++++++ src/Input/Input.php | 21 +++++ 8 files changed, 347 insertions(+), 10 deletions(-) create mode 100644 Game.php create mode 100644 LICENSE create mode 100644 src/Base/Player.php create mode 100644 src/Helpers/Menu.php create mode 100644 src/Input/Input.php diff --git a/Game.php b/Game.php new file mode 100644 index 0000000..6a533bb --- /dev/null +++ b/Game.php @@ -0,0 +1,174 @@ +setName('CPU Player'); + +while ($gameRuntime) +{ + + Menu::buildMainMenu(); + $input = getcmd(); + + switch ($input) + { + case 1: + Menu::clearScreen(); + echo '... Welcome. Starting a new game!' . PHP_EOL; + echo 'Introduce yourself. What\'s your username? [Player]' . PHP_EOL; + + $playerName = getcmd() ?? 'Player'; + $human->setName($playerName); + + echo 'Got it ' . $playerName . '! Let\'s start, shall we? ' . PHP_EOL; + echo PHP_EOL; + echo 'Each player starts with 20 points and 4 lives;' . PHP_EOL; + echo 'Each time you win, your score is multiplied by 3;' . PHP_EOL; + echo 'Each time you lose, your score is subtracted from the result of the score multiplier (which is 3) times 2.' . PHP_EOL; + echo 'This also applies for the bot. Good luck! Starting in 15 seconds.' . PHP_EOL; + sleep(15); + + Menu::clearScreen(); + + do { + // Runs forever until game over + + echo 'Roll the dice! Choose one: ' . PHP_EOL; + echo '[1] Rock' . PHP_EOL; + echo '[2] Paper' . PHP_EOL; + echo '[3] Scissors' . PHP_EOL; + + $humanSelection = getcmd(); + $botSelection = mt_rand(1, 3); + + switch ($humanSelection) + { + case 1: + + $humanChoiceItem = new Rock(); + break; + + case 2: + $humanChoiceItem = new Paper(); + break; + + case 3: + $humanChoiceItem = new Scissors(); + break; + + default: + echo 'Error: Please choose a number between 1 and 3, denoting Rock, Paper and Scissors respectively.'; + sleep(3); + Menu::clearScreen(); + + continue 2; + + } + + + switch ($botSelection) + { + case 1: + + $computerChoiceItem = new Rock(); + break; + + case 2: + $computerChoiceItem = new Paper(); + break; + + case 3: + $computerChoiceItem = new Scissors(); + break; + } + + $result = $humanChoiceItem->evaluateMatch($computerChoiceItem); + + if ($result['defeated']) + { + echo 'You lose! The computer chose ' .$computerChoiceItem->getName() . PHP_EOL; + $human->registerLoss(); + $robot->registerWin(); + } + elseif($result['draw'] == true) + { + echo 'Game draw! You and the computer chose ' . $computerChoiceItem->getName() . PHP_EOL; + } + else + { + echo 'You win! The computer chose ' . $computerChoiceItem->getName() . PHP_EOL; + $human->registerWin(); + $robot->registerLoss(); + } + + echo PHP_EOL; + echo '-------------- Round Over -----------------------'; + echo PHP_EOL; + echo 'Your score: ' . $human->getInitialScore() . PHP_EOL; + echo 'Computer\'s score: ' . $robot->getInitialScore() . PHP_EOL; + echo PHP_EOL; + echo 'Your lives: ' . $human->getLives() . PHP_EOL; + echo 'Computer\'s lives: ' . $robot->getLives() . PHP_EOL; + echo PHP_EOL; + echo PHP_EOL; + echo 'Continuing in 10 seconds...'; + + if ($human->getLives() == 0 || $robot->getLives() == 0) + { + $gameOver = true; + } + + sleep(10); + Menu::clearScreen(); + + } while (!$gameOver); + + echo 'G A M E O V E R - Someone ran out of lives. Tallying scores!' . PHP_EOL; + echo 'Your final score: ' . $human->getInitialScore() . PHP_EOL; + echo 'Computer\'s final score: ' . $robot->getInitialScore() . PHP_EOL; + + if ($human->getInitialScore() > $robot->getInitialScore()) + { + echo 'You won this match of Rock Paper Scissors! Thank you for playing!' . PHP_EOL; + } + else + { + echo 'You lost this match of Rock Paper Scissors! Thank you for playing!' . PHP_EOL; + } + echo PHP_EOL; + echo 'The main menu will appear in five seconds...'; + + sleep(5); + + Menu::clearScreen(); + unset($robot, $human, $computerChoiceItem, $humanChoiceItem); + + + break; + + case 2: + $gameRuntime = false; + Menu::clearScreen(); + break; + + default: + echo 'Invalid menu option! Please try again.' . PHP_EOL; + sleep(3); + Menu::clearScreen(); + break; + + } + +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7bde5f3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ + +Copyright 2020 Miguel Nogueira (https://www.debian.org/legal/licenses/mit) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/composer.json b/composer.json index eda4085..45cb6d3 100644 --- a/composer.json +++ b/composer.json @@ -9,8 +9,13 @@ "email": "miguel456@spacejewel-hosting.com" } ], - "require": {}, + "require": { + "ext-readline": "*" + }, "autoload": { + "files": [ + "src/Input/Input.php" + ], "psr-4": { "miguel456\\rps\\": "src/" } diff --git a/src/Base/BaseGameItem.php b/src/Base/BaseGameItem.php index 2e1d81f..f52d7da 100644 --- a/src/Base/BaseGameItem.php +++ b/src/Base/BaseGameItem.php @@ -38,6 +38,14 @@ abstract class BaseGameItem $modifiers = $item->getModifiers(); + if ($currentGameItemName == $item->getComputerName()) + { + return [ + 'defeated' => false, + 'draw' => true + ]; + } + if (in_array($currentGameItemName, $modifiers['defeated_by'])) { return [ @@ -54,15 +62,9 @@ abstract class BaseGameItem 'win_by' => $currentGameItemName ]; } - else - { - return [ - - 'defeated' => false, - 'draw' => true - ]; - } } + + return false; } } \ No newline at end of file diff --git a/src/Base/Player.php b/src/Base/Player.php new file mode 100644 index 0000000..6bfd825 --- /dev/null +++ b/src/Base/Player.php @@ -0,0 +1,98 @@ +name = $name; + return $this; + } + + + protected function setLives($lives) + { + $this->lives = $lives; + return $this; + } + + + /** + * @return int + */ + public function getLives() + { + return $this->lives; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + /** + * @return int + */ + public function getInitialScore(): int + { + return $this->initialScore; + } + + + /** + * @return $this + */ + protected function decreaseLives() + { + $this->lives = $this->lives - 1; + return $this; + } + + protected function increaseLives() + { + $this->lives = $this->lives + 1; + } + + public function registerWin() + { + if ($this->initialScore > 500) + { + $this->increaseLives(); + } + + $this->initialScore = $this->initialScore * $this->multiplier; + } + + public function registerLoss() + { + if ($this->initialScore <= 0) + { + $this->decreaseLives(); + } + else + { + $this->initialScore = $this->initialScore - ($this->multiplier * 2); + } + + + } + +} diff --git a/src/Core/Paper.php b/src/Core/Paper.php index 8a9ca9b..8ccce13 100644 --- a/src/Core/Paper.php +++ b/src/Core/Paper.php @@ -31,7 +31,7 @@ class Paper extends BaseGameItem */ function getName() { - return $this->getName(); + return $this->name; } /** diff --git a/src/Helpers/Menu.php b/src/Helpers/Menu.php new file mode 100644 index 0000000..5462b9a --- /dev/null +++ b/src/Helpers/Menu.php @@ -0,0 +1,29 @@ +---------' . PHP_EOL; + echo 'Welcome to Rock Paper Scissors | Powered by the PHP programming language' . PHP_EOL; + echo 'Choose a menu option: ' . PHP_EOL; + echo '1 -------------------------- NEW GAME (CPU Opponent)' . PHP_EOL; + echo '2 -------------------------- EXIT' . PHP_EOL; + } + + public static function clearScreen() + { + for ($i = 0; $i < 100; $i++) + { + print(PHP_EOL); + } + } + +} \ No newline at end of file diff --git a/src/Input/Input.php b/src/Input/Input.php new file mode 100644 index 0000000..2d9fcb7 --- /dev/null +++ b/src/Input/Input.php @@ -0,0 +1,21 @@ +