From dee61f53229fc95c4ca8716215bf2004e82553bd Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Mon, 8 Jan 2024 13:49:02 +0000 Subject: [PATCH 01/18] change add function to addPlayer --- exercises/php/trivia/Game.php | 2 +- exercises/php/trivia/GameRunner.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 8a0405d4..1a8e7e47 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -48,7 +48,7 @@ function isPlayable() { return ($this->howManyPlayers() >= 2); } - function add($playerName) { + function addPlayer($playerName) { array_push($this->players, $playerName); $this->places[$this->howManyPlayers()] = 0; $this->purses[$this->howManyPlayers()] = 0; diff --git a/exercises/php/trivia/GameRunner.php b/exercises/php/trivia/GameRunner.php index 30f5c68b..96eafa78 100644 --- a/exercises/php/trivia/GameRunner.php +++ b/exercises/php/trivia/GameRunner.php @@ -9,9 +9,9 @@ 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 { From 115e31aeb5b7da297170b7d8ffdbcaf7c2db5d89 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Mon, 8 Jan 2024 13:54:35 +0000 Subject: [PATCH 02/18] change howManyPlayers to getPlayerCount --- exercises/php/trivia/Game.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 1a8e7e47..fc410984 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -45,21 +45,21 @@ function createRockQuestion($index){ } function isPlayable() { - return ($this->howManyPlayers() >= 2); + return ($this->getPlayerCount() >= 2); } function addPlayer($playerName) { array_push($this->players, $playerName); - $this->places[$this->howManyPlayers()] = 0; - $this->purses[$this->howManyPlayers()] = 0; - $this->inPenaltyBox[$this->howManyPlayers()] = false; + $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)); return true; } - function howManyPlayers() { + function getPlayerCount() { return count($this->players); } From be16a4657ef07f0a8a89ad0eb6f2f8bce4e9ed70 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Mon, 8 Jan 2024 14:05:47 +0000 Subject: [PATCH 03/18] change roll to rollDice --- exercises/php/trivia/Game.php | 2 +- exercises/php/trivia/GameRunner.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index fc410984..31c486d6 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -63,7 +63,7 @@ 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); diff --git a/exercises/php/trivia/GameRunner.php b/exercises/php/trivia/GameRunner.php index 96eafa78..eea267c9 100644 --- a/exercises/php/trivia/GameRunner.php +++ b/exercises/php/trivia/GameRunner.php @@ -16,7 +16,7 @@ public function runGame($seed): void srand($seed); do { - $aGame->roll(rand(0,5) + 1); + $aGame->rollDice(rand(0,5) + 1); if (rand(0,9) == 7) { $notAWinner = $aGame->wrongAnswer(); From 725ff5e1f9a2338e02b41c5f19b91d409daf4ed5 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Mon, 8 Jan 2024 14:10:20 +0000 Subject: [PATCH 04/18] change currentCategory to getCurrentCategory --- exercises/php/trivia/Game.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 31c486d6..52bd2675 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -78,7 +78,7 @@ function rollDice($roll) { echoln($this->players[$this->currentPlayer] . "'s new location is " .$this->places[$this->currentPlayer]); - echoln("The category is " . $this->currentCategory()); + echoln("The category is " . $this->getcurrentCategory()); $this->askQuestion(); } else { echoln($this->players[$this->currentPlayer] . " is not getting out of the penalty box"); @@ -93,25 +93,25 @@ function rollDice($roll) { echoln($this->players[$this->currentPlayer] . "'s new location is " .$this->places[$this->currentPlayer]); - echoln("The category is " . $this->currentCategory()); + echoln("The category is " . $this->getcurrentCategory()); $this->askQuestion(); } } function askQuestion() { - if ($this->currentCategory() == "Pop") + if ($this->getcurrentCategory() == "Pop") echoln(array_shift($this->popQuestions)); - if ($this->currentCategory() == "Science") + if ($this->getcurrentCategory() == "Science") echoln(array_shift($this->scienceQuestions)); - if ($this->currentCategory() == "Sports") + if ($this->getcurrentCategory() == "Sports") echoln(array_shift($this->sportsQuestions)); - if ($this->currentCategory() == "Rock") + if ($this->getcurrentCategory() == "Rock") echoln(array_shift($this->rockQuestions)); } - function currentCategory() { + function getcurrentCategory() { if ($this->places[$this->currentPlayer] == 0) return "Pop"; if ($this->places[$this->currentPlayer] == 4) return "Pop"; if ($this->places[$this->currentPlayer] == 8) return "Pop"; From 000f47a484af4d37486018470daadd7380bf35fd Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Mon, 8 Jan 2024 14:11:45 +0000 Subject: [PATCH 05/18] change wasWrongAnswer to wasNotCorrectlyAnswered --- exercises/php/trivia/Game.php | 2 +- exercises/php/trivia/GameRunner.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 52bd2675..4adf72d4 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -164,7 +164,7 @@ function wasCorrectlyAnswered() { } } - function wrongAnswer(){ + function wasNotCorrectlyAnswered(){ echoln("Question was incorrectly answered"); echoln($this->players[$this->currentPlayer] . " was sent to the penalty box"); $this->inPenaltyBox[$this->currentPlayer] = true; diff --git a/exercises/php/trivia/GameRunner.php b/exercises/php/trivia/GameRunner.php index eea267c9..2af06acd 100644 --- a/exercises/php/trivia/GameRunner.php +++ b/exercises/php/trivia/GameRunner.php @@ -19,7 +19,7 @@ public function runGame($seed): void $aGame->rollDice(rand(0,5) + 1); if (rand(0,9) == 7) { - $notAWinner = $aGame->wrongAnswer(); + $notAWinner = $aGame->wasNotCorrectlyAnswered(); } else { $notAWinner = $aGame->wasCorrectlyAnswered(); } From 812f12872d8909f1cfeaae2511b2118642e1d8a2 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Mon, 8 Jan 2024 14:21:30 +0000 Subject: [PATCH 06/18] create nextPlayer method --- exercises/php/trivia/Game.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 4adf72d4..f2141e1c 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -157,9 +157,9 @@ function wasCorrectlyAnswered() { . " Gold Coins."); $winner = $this->didPlayerWin(); - $this->currentPlayer++; - if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0; - + + $this->nextPlayer(); + return $winner; } } @@ -169,8 +169,8 @@ function wasNotCorrectlyAnswered(){ echoln($this->players[$this->currentPlayer] . " was sent to the penalty box"); $this->inPenaltyBox[$this->currentPlayer] = true; - $this->currentPlayer++; - if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0; + $this->nextPlayer(); + return true; } @@ -178,4 +178,9 @@ function wasNotCorrectlyAnswered(){ function didPlayerWin() { return !($this->purses[$this->currentPlayer] == 6); } + + function nextPlayer() { + $this->currentPlayer++; + if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0; + } } From 4d3cf6de73a50e3cf99b1e0aabba69556abfd77d Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Mon, 8 Jan 2024 14:50:41 +0000 Subject: [PATCH 07/18] extract getAnswerOutcome to print outcome of answering question --- exercises/php/trivia/Game.php | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index f2141e1c..7612868c 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -128,11 +128,7 @@ 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++; @@ -148,13 +144,9 @@ function wasCorrectlyAnswered() { } 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(); @@ -165,12 +157,12 @@ function wasCorrectlyAnswered() { } function wasNotCorrectlyAnswered(){ - echoln("Question was incorrectly answered"); - echoln($this->players[$this->currentPlayer] . " was sent to the penalty box"); + $this->getAnswerOutcome('incorrect'); + $this->inPenaltyBox[$this->currentPlayer] = true; $this->nextPlayer(); - + return true; } @@ -183,4 +175,17 @@ 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"); + } + } } From 5a3e8b0b23157e80fc08fdfc34edb8fe1193de97 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Mon, 8 Jan 2024 15:00:09 +0000 Subject: [PATCH 08/18] extract movePlaces to calculate new player location and ask question --- exercises/php/trivia/Game.php | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 7612868c..5789d952 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -72,29 +72,14 @@ function rollDice($roll) { $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->getcurrentCategory()); - $this->askQuestion(); + $this->movePlaces($roll); } else { echoln($this->players[$this->currentPlayer] . " is not getting out of the penalty box"); $this->isGettingOutOfPenaltyBox = false; } } 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->getcurrentCategory()); - $this->askQuestion(); + $this->movePlaces($roll); } } @@ -188,4 +173,16 @@ function getAnswerOutcome($answer){ echoln($this->players[$this->currentPlayer] . " was sent to the penalty box"); } } + + function movePlaces($places) { + $this->places[$this->currentPlayer] = $this->places[$this->currentPlayer] + $places; + 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->getcurrentCategory()); + + $this->askQuestion(); + } } From 4e6dd2c31bf94ddd73b523a65e7ff971f5b85a0b Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Mon, 8 Jan 2024 15:05:58 +0000 Subject: [PATCH 09/18] set constant to set max number of questions per category --- exercises/php/trivia/Game.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 5789d952..b176021b 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -6,6 +6,8 @@ function echoln($string) { echo $string."\n"; } +const MAX_QUESTIONS_PER_CATEGORY = 50; + class Game { var $players; var $places; @@ -20,6 +22,8 @@ class Game { var $currentPlayer = 0; var $isGettingOutOfPenaltyBox; + // const MAX_QUESTIONS_PER_CATEGORY = 50; + function __construct(){ $this->players = array(); @@ -32,7 +36,7 @@ function __construct(){ $this->sportsQuestions = array(); $this->rockQuestions = array(); - for ($i = 0; $i < 50; $i++) { + for ($i = 0; $i < MAX_QUESTIONS_PER_CATEGORY; $i++) { array_push($this->popQuestions, "Pop Question " . $i); array_push($this->scienceQuestions, ("Science Question " . $i)); array_push($this->sportsQuestions, ("Sports Question " . $i)); From aaeda058fd388c6d53024e9c3af3d007e013edd8 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Mon, 8 Jan 2024 15:07:20 +0000 Subject: [PATCH 10/18] rename index variable --- exercises/php/trivia/Game.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index b176021b..f2b6a978 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -22,7 +22,6 @@ class Game { var $currentPlayer = 0; var $isGettingOutOfPenaltyBox; - // const MAX_QUESTIONS_PER_CATEGORY = 50; function __construct(){ @@ -36,11 +35,11 @@ function __construct(){ $this->sportsQuestions = array(); $this->rockQuestions = array(); - for ($i = 0; $i < MAX_QUESTIONS_PER_CATEGORY; $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)); + for ($index = 0; $index < 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)); } } From ba016370aeb6d29bd31351f5250610b3a3277073 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Tue, 9 Jan 2024 11:27:36 +0000 Subject: [PATCH 11/18] make MAX_QUESTIONS_PER_CATEGORY private --- exercises/php/trivia/Game.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index f2b6a978..8ede2d02 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -6,8 +6,6 @@ function echoln($string) { echo $string."\n"; } -const MAX_QUESTIONS_PER_CATEGORY = 50; - class Game { var $players; var $places; @@ -22,6 +20,7 @@ class Game { var $currentPlayer = 0; var $isGettingOutOfPenaltyBox; + private const MAX_QUESTIONS_PER_CATEGORY = 50; function __construct(){ @@ -35,7 +34,7 @@ function __construct(){ $this->sportsQuestions = array(); $this->rockQuestions = array(); - for ($index = 0; $index < MAX_QUESTIONS_PER_CATEGORY; $index++) { + 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)); From eb86bcb9c07a4b1fac2add808dffed58ba00d7e9 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Tue, 9 Jan 2024 14:06:42 +0000 Subject: [PATCH 12/18] add game board and refactor getCurrentCategory --- exercises/php/trivia/Game.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 8ede2d02..06ddc259 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -9,6 +9,12 @@ function echoln($string) { class Game { var $players; var $places; + var $board = [ + "Pop", "Science", "Sports", + "Rock", "Pop", "Science", + "Sports", "Rock", "Pop", + "Science", "Sports", "Rock" + ]; var $purses ; var $inPenaltyBox ; @@ -99,16 +105,8 @@ function askQuestion() { function getcurrentCategory() { - 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"; + $playerPlace = $this->places[$this->currentPlayer]; + return $this->board[$playerPlace]; } function wasCorrectlyAnswered() { From 7fd09ff75d63541cbe6704bef922ad6e14009034 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Tue, 9 Jan 2024 14:09:51 +0000 Subject: [PATCH 13/18] extract odd roll into function isRollOdd --- exercises/php/trivia/Game.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 06ddc259..d2e0c981 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -76,15 +76,8 @@ function rollDice($roll) { 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->movePlaces($roll); - } else { - echoln($this->players[$this->currentPlayer] . " is not getting out of the penalty box"); - $this->isGettingOutOfPenaltyBox = false; - } + + $this->isRollOdd($roll); } else { $this->movePlaces($roll); @@ -185,4 +178,16 @@ function movePlaces($places) { $this->askQuestion(); } + + function isRollOdd($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; + } + } } From a1d7e14e7a7bb53e968ae5ef727d488b1d38f545 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Tue, 9 Jan 2024 14:12:39 +0000 Subject: [PATCH 14/18] rename isRollOdd to isRollOddAndDoesOtherThings --- exercises/php/trivia/Game.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index d2e0c981..0e6f1b16 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -77,7 +77,7 @@ function rollDice($roll) { if ($this->inPenaltyBox[$this->currentPlayer]) { - $this->isRollOdd($roll); + $this->isRollOddAndDoesOtherThings($roll); } else { $this->movePlaces($roll); @@ -179,7 +179,7 @@ function movePlaces($places) { $this->askQuestion(); } - function isRollOdd($roll) { + function isRollOddAndDoesOtherThings($roll) { if ($roll % 2 != 0) { $this->isGettingOutOfPenaltyBox = true; From 02deb702e71439555ee7f2d3d77a2a76e22f793e Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Tue, 9 Jan 2024 14:31:53 +0000 Subject: [PATCH 15/18] create GameBoard class and extract getCurrentCategory --- exercises/php/trivia/Game.php | 24 +++++++++--------------- exercises/php/trivia/GameBoard.php | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 exercises/php/trivia/GameBoard.php diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 0e6f1b16..914c74ac 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -9,12 +9,7 @@ function echoln($string) { class Game { var $players; var $places; - var $board = [ - "Pop", "Science", "Sports", - "Rock", "Pop", "Science", - "Sports", "Rock", "Pop", - "Science", "Sports", "Rock" - ]; + var $board; var $purses ; var $inPenaltyBox ; @@ -29,6 +24,8 @@ class Game { private const MAX_QUESTIONS_PER_CATEGORY = 50; function __construct(){ + $this->board = new GameBoard(); + $this->players = array(); $this->places = array(0); @@ -86,21 +83,18 @@ function rollDice($roll) { } function askQuestion() { - if ($this->getcurrentCategory() == "Pop") + if ($this->board->getcurrentCategory($this->places[$this->currentPlayer]) == "Pop") echoln(array_shift($this->popQuestions)); - if ($this->getcurrentCategory() == "Science") + if ($this->board->getcurrentCategory($this->places[$this->currentPlayer]) == "Science") echoln(array_shift($this->scienceQuestions)); - if ($this->getcurrentCategory() == "Sports") + if ($this->board->getcurrentCategory($this->places[$this->currentPlayer]) == "Sports") echoln(array_shift($this->sportsQuestions)); - if ($this->getcurrentCategory() == "Rock") + if ($this->board->getcurrentCategory($this->places[$this->currentPlayer]) == "Rock") echoln(array_shift($this->rockQuestions)); } - function getcurrentCategory() { - $playerPlace = $this->places[$this->currentPlayer]; - return $this->board[$playerPlace]; - } + function wasCorrectlyAnswered() { if ($this->inPenaltyBox[$this->currentPlayer]){ @@ -174,7 +168,7 @@ function movePlaces($places) { echoln($this->players[$this->currentPlayer] . "'s new location is " . $this->places[$this->currentPlayer]); - echoln("The category is " . $this->getcurrentCategory()); + echoln("The category is " . $this->board->getcurrentCategory($this->places[$this->currentPlayer])); $this->askQuestion(); } diff --git a/exercises/php/trivia/GameBoard.php b/exercises/php/trivia/GameBoard.php new file mode 100644 index 00000000..8aa0ff85 --- /dev/null +++ b/exercises/php/trivia/GameBoard.php @@ -0,0 +1,22 @@ +places = [ + "Pop", "Science", "Sports", + "Rock", "Pop", "Science", + "Sports", "Rock", "Pop", + "Science", "Sports", "Rock" + ]; + } + + function getcurrentCategory($currentPlayerPosition ) { + return $this->places[$currentPlayerPosition]; + } +} From de3db5d1d6207c39d7b15ba26d9da6e36a76296d Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Tue, 9 Jan 2024 15:21:43 +0000 Subject: [PATCH 16/18] refactor movePlaces and create currentPlayerPlace --- exercises/php/trivia/Game.php | 145 +++++++++++++++++++--------------- 1 file changed, 81 insertions(+), 64 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 914c74ac..4fd7f4f9 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -2,87 +2,94 @@ namespace trivia; -function echoln($string) { - echo $string."\n"; +function echoln($string) +{ + echo $string . "\n"; } -class Game { - var $players; - var $places; +class Game +{ + var $players; + var $places; var $board; - var $purses ; - var $inPenaltyBox ; + var $purses; + var $inPenaltyBox; - var $popQuestions; - var $scienceQuestions; - var $sportsQuestions; - var $rockQuestions; + var $popQuestions; + var $scienceQuestions; + var $sportsQuestions; + var $rockQuestions; - var $currentPlayer = 0; - var $isGettingOutOfPenaltyBox; + var $currentPlayer = 0; + var $isGettingOutOfPenaltyBox; private const MAX_QUESTIONS_PER_CATEGORY = 50; - function __construct(){ + function __construct() + { $this->board = new GameBoard(); - $this->players = array(); - $this->places = array(0); - $this->purses = array(0); - $this->inPenaltyBox = array(0); + $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(); + $this->popQuestions = array(); + $this->scienceQuestions = array(); + $this->sportsQuestions = array(); + $this->rockQuestions = array(); - for ($index = 0; $index < self::MAX_QUESTIONS_PER_CATEGORY; $index++) { + 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){ + function createRockQuestion($index) + { return "Rock Question " . $index; } - function isPlayable() { + function isPlayable() + { return ($this->getPlayerCount() >= 2); } - function addPlayer($playerName) { - array_push($this->players, $playerName); - $this->places[$this->getPlayerCount()] = 0; - $this->purses[$this->getPlayerCount()] = 0; - $this->inPenaltyBox[$this->getPlayerCount()] = 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 getPlayerCount() { + function getPlayerCount() + { return count($this->players); } - function rollDice($roll) { + function rollDice($roll) + { echoln($this->players[$this->currentPlayer] . " is the current player"); echoln("They have rolled a " . $roll); if ($this->inPenaltyBox[$this->currentPlayer]) { - - $this->isRollOddAndDoesOtherThings($roll); + $this->isRollOddAndDoesOtherThings($roll); } else { $this->movePlaces($roll); } - } - function askQuestion() { + function askQuestion() + { if ($this->board->getcurrentCategory($this->places[$this->currentPlayer]) == "Pop") echoln(array_shift($this->popQuestions)); if ($this->board->getcurrentCategory($this->places[$this->currentPlayer]) == "Science") @@ -94,10 +101,11 @@ function askQuestion() { } - - function wasCorrectlyAnswered() { - if ($this->inPenaltyBox[$this->currentPlayer]){ + + function wasCorrectlyAnswered() + { + if ($this->inPenaltyBox[$this->currentPlayer]) { if ($this->isGettingOutOfPenaltyBox) { echoln("Answer was correct!!!!"); $this->getAnswerOutcome('correct'); @@ -112,26 +120,24 @@ function wasCorrectlyAnswered() { if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0; return true; } - - - } else { - + echoln("Answer was corrent!!!!"); $this->getAnswerOutcome('correct'); $winner = $this->didPlayerWin(); - + $this->nextPlayer(); - + return $winner; } } - function wasNotCorrectlyAnswered(){ + function wasNotCorrectlyAnswered() + { $this->getAnswerOutcome('incorrect'); - $this->inPenaltyBox[$this->currentPlayer] = true; + $this->inPenaltyBox[$this->currentPlayer] = true; $this->nextPlayer(); @@ -139,41 +145,52 @@ function wasNotCorrectlyAnswered(){ } - function didPlayerWin() { + function didPlayerWin() + { return !($this->purses[$this->currentPlayer] == 6); } - function nextPlayer() { + function nextPlayer() + { $this->currentPlayer++; if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0; } - function getAnswerOutcome($answer){ + function getAnswerOutcome($answer) + { if ($answer === "correct") { $this->purses[$this->currentPlayer]++; - echoln($this->players[$this->currentPlayer] - . " now has " - .$this->purses[$this->currentPlayer] - . " Gold Coins."); + 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->places[$this->currentPlayer] + $places; - if ($this->places[$this->currentPlayer] > 11) $this->places[$this->currentPlayer] = $this->places[$this->currentPlayer] - 12; + 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->places[$this->currentPlayer]); - echoln("The category is " . $this->board->getcurrentCategory($this->places[$this->currentPlayer])); + . $this->currentPlayerPlace()); + echoln("The category is " . $this->board->getcurrentCategory($this->currentPlayerPlace())); $this->askQuestion(); } - function isRollOddAndDoesOtherThings($roll) { + function currentPlayerPlace() + { + return $this->places[$this->currentPlayer]; + } + + function isRollOddAndDoesOtherThings($roll) + { if ($roll % 2 != 0) { $this->isGettingOutOfPenaltyBox = true; From 7a4c647086c459129cdb883d5c4fc61a7aac5232 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Tue, 9 Jan 2024 15:24:09 +0000 Subject: [PATCH 17/18] refactor askQuestion --- exercises/php/trivia/Game.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 4fd7f4f9..806cf525 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -90,13 +90,13 @@ function rollDice($roll) function askQuestion() { - if ($this->board->getcurrentCategory($this->places[$this->currentPlayer]) == "Pop") + if ($this->board->getcurrentCategory($this->currentPlayerPlace()) == "Pop") echoln(array_shift($this->popQuestions)); - if ($this->board->getcurrentCategory($this->places[$this->currentPlayer]) == "Science") + if ($this->board->getcurrentCategory($this->currentPlayerPlace()) == "Science") echoln(array_shift($this->scienceQuestions)); - if ($this->board->getcurrentCategory($this->places[$this->currentPlayer]) == "Sports") + if ($this->board->getcurrentCategory($this->currentPlayerPlace()) == "Sports") echoln(array_shift($this->sportsQuestions)); - if ($this->board->getcurrentCategory($this->places[$this->currentPlayer]) == "Rock") + if ($this->board->getcurrentCategory($this->currentPlayerPlace()) == "Rock") echoln(array_shift($this->rockQuestions)); } @@ -172,7 +172,6 @@ function getAnswerOutcome($answer) function movePlaces($places) { - $this->places[$this->currentPlayer] = $this->currentPlayerPlace() + $places; if ($this->places[$this->currentPlayer] > 11) $this->places[$this->currentPlayer] = $this->currentPlayerPlace() - 12; From c9c98fcb48a1e5ee36a5708cd05ac82832686a3e Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Tue, 9 Jan 2024 16:03:07 +0000 Subject: [PATCH 18/18] extract question functionality into GameBoard class --- exercises/php/trivia/Game.php | 30 +-------------------- exercises/php/trivia/GameBoard.php | 43 +++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/exercises/php/trivia/Game.php b/exercises/php/trivia/Game.php index 806cf525..3da067ae 100644 --- a/exercises/php/trivia/Game.php +++ b/exercises/php/trivia/Game.php @@ -23,8 +23,6 @@ class Game var $currentPlayer = 0; var $isGettingOutOfPenaltyBox; - private const MAX_QUESTIONS_PER_CATEGORY = 50; - function __construct() { $this->board = new GameBoard(); @@ -34,18 +32,6 @@ function __construct() $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 ($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) @@ -88,20 +74,6 @@ function rollDice($roll) } } - function askQuestion() - { - if ($this->board->getcurrentCategory($this->currentPlayerPlace()) == "Pop") - echoln(array_shift($this->popQuestions)); - if ($this->board->getcurrentCategory($this->currentPlayerPlace()) == "Science") - echoln(array_shift($this->scienceQuestions)); - if ($this->board->getcurrentCategory($this->currentPlayerPlace()) == "Sports") - echoln(array_shift($this->sportsQuestions)); - if ($this->board->getcurrentCategory($this->currentPlayerPlace()) == "Rock") - echoln(array_shift($this->rockQuestions)); - } - - - function wasCorrectlyAnswered() { @@ -180,7 +152,7 @@ function movePlaces($places) . $this->currentPlayerPlace()); echoln("The category is " . $this->board->getcurrentCategory($this->currentPlayerPlace())); - $this->askQuestion(); + $this->board->askQuestion($this->currentPlayerPlace()); } function currentPlayerPlace() diff --git a/exercises/php/trivia/GameBoard.php b/exercises/php/trivia/GameBoard.php index 8aa0ff85..0fd6dd78 100644 --- a/exercises/php/trivia/GameBoard.php +++ b/exercises/php/trivia/GameBoard.php @@ -5,6 +5,12 @@ class GameBoard { private $places; + private $popQuestions; + private $scienceQuestions; + private $sportsQuestions; + private $rockQuestions; + + private const MAX_QUESTIONS_PER_CATEGORY = 50; function __construct() { @@ -14,9 +20,40 @@ function __construct() "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 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)); + } }