diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 8a0405d4..3da067ae 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -2,137 +2,85 @@ namespace trivia; -function echoln($string) { - echo $string."\n"; +function echoln($string) +{ + echo $string . "\n"; } -class Game { - var $players; - var $places; - var $purses ; - var $inPenaltyBox ; - - var $popQuestions; - var $scienceQuestions; - var $sportsQuestions; - var $rockQuestions; - - var $currentPlayer = 0; - var $isGettingOutOfPenaltyBox; - - function __construct(){ - - $this->players = array(); - $this->places = array(0); - $this->purses = array(0); - $this->inPenaltyBox = array(0); - - $this->popQuestions = array(); - $this->scienceQuestions = array(); - $this->sportsQuestions = array(); - $this->rockQuestions = array(); - - for ($i = 0; $i < 50; $i++) { - array_push($this->popQuestions, "Pop Question " . $i); - array_push($this->scienceQuestions, ("Science Question " . $i)); - array_push($this->sportsQuestions, ("Sports Question " . $i)); - array_push($this->rockQuestions, $this->createRockQuestion($i)); - } - } - - function createRockQuestion($index){ +class Game +{ + var $players; + var $places; + var $board; + var $purses; + var $inPenaltyBox; + + var $popQuestions; + var $scienceQuestions; + var $sportsQuestions; + var $rockQuestions; + + var $currentPlayer = 0; + var $isGettingOutOfPenaltyBox; + + function __construct() + { + $this->board = new GameBoard(); + + + $this->players = array(); + $this->places = array(0); + $this->purses = array(0); + $this->inPenaltyBox = array(0); + } + + function createRockQuestion($index) + { return "Rock Question " . $index; } - function isPlayable() { - return ($this->howManyPlayers() >= 2); + function isPlayable() + { + return ($this->getPlayerCount() >= 2); } - function add($playerName) { - array_push($this->players, $playerName); - $this->places[$this->howManyPlayers()] = 0; - $this->purses[$this->howManyPlayers()] = 0; - $this->inPenaltyBox[$this->howManyPlayers()] = false; + function addPlayer($playerName) + { + array_push($this->players, $playerName); + $this->places[$this->getPlayerCount()] = 0; + $this->purses[$this->getPlayerCount()] = 0; + $this->inPenaltyBox[$this->getPlayerCount()] = false; - echoln($playerName . " was added"); - echoln("They are player number " . count($this->players)); + echoln($playerName . " was added"); + echoln("They are player number " . count($this->players)); return true; } - function howManyPlayers() { + function getPlayerCount() + { return count($this->players); } - function roll($roll) { + function rollDice($roll) + { echoln($this->players[$this->currentPlayer] . " is the current player"); echoln("They have rolled a " . $roll); if ($this->inPenaltyBox[$this->currentPlayer]) { - if ($roll % 2 != 0) { - $this->isGettingOutOfPenaltyBox = true; - - echoln($this->players[$this->currentPlayer] . " is getting out of the penalty box"); - $this->places[$this->currentPlayer] = $this->places[$this->currentPlayer] + $roll; - if ($this->places[$this->currentPlayer] > 11) $this->places[$this->currentPlayer] = $this->places[$this->currentPlayer] - 12; - - echoln($this->players[$this->currentPlayer] - . "'s new location is " - .$this->places[$this->currentPlayer]); - echoln("The category is " . $this->currentCategory()); - $this->askQuestion(); - } else { - echoln($this->players[$this->currentPlayer] . " is not getting out of the penalty box"); - $this->isGettingOutOfPenaltyBox = false; - } + $this->isRollOddAndDoesOtherThings($roll); } else { - - $this->places[$this->currentPlayer] = $this->places[$this->currentPlayer] + $roll; - if ($this->places[$this->currentPlayer] > 11) $this->places[$this->currentPlayer] = $this->places[$this->currentPlayer] - 12; - - echoln($this->players[$this->currentPlayer] - . "'s new location is " - .$this->places[$this->currentPlayer]); - echoln("The category is " . $this->currentCategory()); - $this->askQuestion(); + $this->movePlaces($roll); } - } - function askQuestion() { - if ($this->currentCategory() == "Pop") - echoln(array_shift($this->popQuestions)); - if ($this->currentCategory() == "Science") - echoln(array_shift($this->scienceQuestions)); - if ($this->currentCategory() == "Sports") - echoln(array_shift($this->sportsQuestions)); - if ($this->currentCategory() == "Rock") - echoln(array_shift($this->rockQuestions)); - } - - - function currentCategory() { - if ($this->places[$this->currentPlayer] == 0) return "Pop"; - if ($this->places[$this->currentPlayer] == 4) return "Pop"; - if ($this->places[$this->currentPlayer] == 8) return "Pop"; - if ($this->places[$this->currentPlayer] == 1) return "Science"; - if ($this->places[$this->currentPlayer] == 5) return "Science"; - if ($this->places[$this->currentPlayer] == 9) return "Science"; - if ($this->places[$this->currentPlayer] == 2) return "Sports"; - if ($this->places[$this->currentPlayer] == 6) return "Sports"; - if ($this->places[$this->currentPlayer] == 10) return "Sports"; - return "Rock"; - } - function wasCorrectlyAnswered() { - if ($this->inPenaltyBox[$this->currentPlayer]){ + function wasCorrectlyAnswered() + { + if ($this->inPenaltyBox[$this->currentPlayer]) { if ($this->isGettingOutOfPenaltyBox) { echoln("Answer was correct!!!!"); - $this->purses[$this->currentPlayer]++; - echoln($this->players[$this->currentPlayer] - . " now has " - .$this->purses[$this->currentPlayer] - . " Gold Coins."); + $this->getAnswerOutcome('correct'); $winner = $this->didPlayerWin(); $this->currentPlayer++; @@ -144,38 +92,84 @@ function wasCorrectlyAnswered() { if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0; return true; } - - - } else { echoln("Answer was corrent!!!!"); - $this->purses[$this->currentPlayer]++; - echoln($this->players[$this->currentPlayer] - . " now has " - .$this->purses[$this->currentPlayer] - . " Gold Coins."); + $this->getAnswerOutcome('correct'); $winner = $this->didPlayerWin(); - $this->currentPlayer++; - if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0; + + $this->nextPlayer(); return $winner; } } - function wrongAnswer(){ - echoln("Question was incorrectly answered"); - echoln($this->players[$this->currentPlayer] . " was sent to the penalty box"); - $this->inPenaltyBox[$this->currentPlayer] = true; + function wasNotCorrectlyAnswered() + { + $this->getAnswerOutcome('incorrect'); + + $this->inPenaltyBox[$this->currentPlayer] = true; + + $this->nextPlayer(); - $this->currentPlayer++; - if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0; return true; } - function didPlayerWin() { + function didPlayerWin() + { return !($this->purses[$this->currentPlayer] == 6); } + + function nextPlayer() + { + $this->currentPlayer++; + if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0; + } + + function getAnswerOutcome($answer) + { + if ($answer === "correct") { + $this->purses[$this->currentPlayer]++; + echoln($this->players[$this->currentPlayer] + . " now has " + . $this->purses[$this->currentPlayer] + . " Gold Coins."); + } else { + echoln("Question was incorrectly answered"); + echoln($this->players[$this->currentPlayer] . " was sent to the penalty box"); + } + } + + function movePlaces($places) + { + $this->places[$this->currentPlayer] = $this->currentPlayerPlace() + $places; + if ($this->places[$this->currentPlayer] > 11) $this->places[$this->currentPlayer] = $this->currentPlayerPlace() - 12; + + echoln($this->players[$this->currentPlayer] + . "'s new location is " + . $this->currentPlayerPlace()); + echoln("The category is " . $this->board->getcurrentCategory($this->currentPlayerPlace())); + + $this->board->askQuestion($this->currentPlayerPlace()); + } + + function currentPlayerPlace() + { + return $this->places[$this->currentPlayer]; + } + + function isRollOddAndDoesOtherThings($roll) + { + if ($roll % 2 != 0) { + $this->isGettingOutOfPenaltyBox = true; + + echoln($this->players[$this->currentPlayer] . " is getting out of the penalty box"); + $this->movePlaces($roll); + } else { + echoln($this->players[$this->currentPlayer] . " is not getting out of the penalty box"); + $this->isGettingOutOfPenaltyBox = false; + } + } } diff --git a/exercises/php/trivia/GameBoard.php b/exercises/php/trivia/GameBoard.php new file mode 100644 index 00000000..0fd6dd78 --- /dev/null +++ b/exercises/php/trivia/GameBoard.php @@ -0,0 +1,59 @@ +places = [ + "Pop", "Science", "Sports", + "Rock", "Pop", "Science", + "Sports", "Rock", "Pop", + "Science", "Sports", "Rock" + ]; + + $this->popQuestions = array(); + $this->scienceQuestions = array(); + $this->sportsQuestions = array(); + $this->rockQuestions = array(); + + for ($index = 0; $index < self::MAX_QUESTIONS_PER_CATEGORY; $index++) { + array_push($this->popQuestions, "Pop Question " . $index); + array_push($this->scienceQuestions, ("Science Question " . $index)); + array_push($this->sportsQuestions, ("Sports Question " . $index)); + array_push($this->rockQuestions, $this->createRockQuestion($index)); + } + } + + function createRockQuestion($index) + { + return "Rock Question " . $index; + } + + + function getcurrentCategory($currentPlayerPosition) + { + return $this->places[$currentPlayerPosition]; + } + + function askQuestion($currentPlayerPosition) + { + if ($this->getcurrentCategory($currentPlayerPosition) == "Pop") + echoln(array_shift($this->popQuestions)); + if ($this->getcurrentCategory($currentPlayerPosition) == "Science") + echoln(array_shift($this->scienceQuestions)); + if ($this->getcurrentCategory($currentPlayerPosition) == "Sports") + echoln(array_shift($this->sportsQuestions));; + if ($this->getcurrentCategory($currentPlayerPosition) == "Rock") + echoln(array_shift($this->rockQuestions)); + } +} diff --git a/exercises/php/trivia/GameRunner.php b/exercises/php/trivia/GameRunner.php index 30f5c68b..2af06acd 100644 --- a/exercises/php/trivia/GameRunner.php +++ b/exercises/php/trivia/GameRunner.php @@ -9,17 +9,17 @@ public function runGame($seed): void $aGame = new Game(); - $aGame->add("Chet"); - $aGame->add("Pat"); - $aGame->add("Sue"); + $aGame->addPlayer("Chet"); + $aGame->addPlayer("Pat"); + $aGame->addPlayer("Sue"); srand($seed); do { - $aGame->roll(rand(0,5) + 1); + $aGame->rollDice(rand(0,5) + 1); if (rand(0,9) == 7) { - $notAWinner = $aGame->wrongAnswer(); + $notAWinner = $aGame->wasNotCorrectlyAnswered(); } else { $notAWinner = $aGame->wasCorrectlyAnswered(); }