rps/main.php

175 lines
5.4 KiB
PHP
Raw Normal View History

2020-02-18 19:33:48 +00:00
<?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 2;' . PHP_EOL;
2020-02-18 19:33:48 +00:00
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 5 seconds...';
2020-02-18 19:33:48 +00:00
if ($human->getLives() == 0 || $robot->getLives() == 0)
{
$gameOver = true;
}
sleep(5);
2020-02-18 19:33:48 +00:00
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;
2020-02-19 00:28:35 +00:00
echo 'The main menu will appear in fifteen seconds...';
2020-02-18 19:33:48 +00:00
2020-02-19 00:28:35 +00:00
sleep(15);
2020-02-18 19:33:48 +00:00
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;
}
}