-
Notifications
You must be signed in to change notification settings - Fork 66
Code review exercise: Kevin Thompson #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
dee61f5
115e31a
be16a46
725ff5e
000f47a
812f128
4d3cf6d
5a3e8b0
4e6dd2c
aaeda05
ba01637
eb86bcb
7fd09ff
a1d7e14
02deb70
de3db5d
7a4c647
c9c98fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could probably extract this and add it to a variable on the top called "correct" or "correctAnswer". |
||
|
|
||
| $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'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same here. |
||
|
|
||
| $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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see a magic number that you could probably extract it. |
||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
|
|
||
| namespace trivia; | ||
|
|
||
| class GameBoard | ||
| { | ||
| private $places; | ||
| private $popQuestions; | ||
| private $scienceQuestions; | ||
| private $sportsQuestions; | ||
| private $rockQuestions; | ||
|
|
||
| private const MAX_QUESTIONS_PER_CATEGORY = 50; | ||
|
|
||
| function __construct() | ||
| { | ||
| $this->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)); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was it possible to avoid this magic number and add it somewhere else?