Final update

This commit is contained in:
Miguel Nogueira 2020-02-18 19:33:48 +00:00
parent 2ab84fafde
commit ea9241217c
8 changed files with 347 additions and 10 deletions

174
Game.php Normal file
View File

@ -0,0 +1,174 @@
<?php
require 'vendor/autoload.php';
use miguel456\rps\Base\Player;
use miguel456\rps\Core\Paper;
use miguel456\rps\Core\Rock;
use miguel456\rps\Core\Scissors;
use miguel456\rps\Helpers\Menu;
$gameRuntime = true;
$gameOver = false;
$human = new Player();
$robot = new Player();
$robot->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;
}
}

8
LICENSE Normal file
View File

@ -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.

View File

@ -9,8 +9,13 @@
"email": "miguel456@spacejewel-hosting.com"
}
],
"require": {},
"require": {
"ext-readline": "*"
},
"autoload": {
"files": [
"src/Input/Input.php"
],
"psr-4": {
"miguel456\\rps\\": "src/"
}

View File

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

98
src/Base/Player.php Normal file
View File

@ -0,0 +1,98 @@
<?php
namespace miguel456\rps\Base;
class Player
{
private $name;
private $initialScore = 20;
private $multiplier = 3;
private $lives = 4;
public function setName($name)
{
$this->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);
}
}
}

View File

@ -31,7 +31,7 @@ class Paper extends BaseGameItem
*/
function getName()
{
return $this->getName();
return $this->name;
}
/**

29
src/Helpers/Menu.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace miguel456\rps\Helpers;
class Menu
{
public static function buildMainMenu()
{
echo '---------- ROCK PAPER SCISSORS ------------------------------------------' . PHP_EOL;
echo '-----------by Miguel Nogueira <miguel456@spacejewel-hosting.com>---------' . 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);
}
}
}

21
src/Input/Input.php Normal file
View File

@ -0,0 +1,21 @@
<?php
// Windows Compatibility wrapper
/**
* Prompts the user for input; Uses a non-conventional method for Windows systems
* @return false|string
* @link https://www.php.net/manual/en/function.readline.php#104181
*/
function getcmd()
{
if (PHP_OS == 'WINNT') {
echo '$ ';
return stream_get_line(STDIN, 1024, PHP_EOL);
} else {
$line = readline('$ ');
readline_add_history($line);
return $line;
}
}