This is the initial version of the Game class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.adaptionsoft.games.uglytrivia; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
public class Game { | |
ArrayList players = new ArrayList(); | |
int[] places = new int[6]; | |
int[] purses = new int[6]; | |
boolean[] inPenaltyBox = new boolean[6]; | |
LinkedList popQuestions = new LinkedList(); | |
LinkedList scienceQuestions = new LinkedList(); | |
LinkedList sportsQuestions = new LinkedList(); | |
LinkedList rockQuestions = new LinkedList(); | |
int currentPlayer = 0; | |
boolean isGettingOutOfPenaltyBox; | |
public Game() { | |
for (int i = 0; i < 50; i++) { | |
popQuestions.addLast("Pop Question " + i); | |
scienceQuestions.addLast(("Science Question " + i)); | |
sportsQuestions.addLast(("Sports Question " + i)); | |
rockQuestions.addLast(createRockQuestion(i)); | |
} | |
} | |
public String createRockQuestion(int index) { | |
return "Rock Question " + index; | |
} | |
public boolean isPlayable() { | |
return (howManyPlayers() >= 2); | |
} | |
public boolean add(String playerName) { | |
players.add(playerName); | |
places[howManyPlayers()] = 0; | |
purses[howManyPlayers()] = 0; | |
inPenaltyBox[howManyPlayers()] = false; | |
System.out.println(playerName + " was added"); | |
System.out.println("They are player number " + players.size()); | |
return true; | |
} | |
public int howManyPlayers() { | |
return players.size(); | |
} | |
public void roll(int roll) { | |
System.out.println(players.get(currentPlayer) + " is the current player"); | |
System.out.println("They have rolled a " + roll); | |
if (inPenaltyBox[currentPlayer]) { | |
if (roll % 2 != 0) { | |
isGettingOutOfPenaltyBox = true; | |
System.out.println(players.get(currentPlayer) | |
+ " is getting out of the penalty box"); | |
places[currentPlayer] = places[currentPlayer] + roll; | |
if (places[currentPlayer] > 11) | |
places[currentPlayer] = places[currentPlayer] - 12; | |
System.out.println(players.get(currentPlayer) + "'s new location is " | |
+ places[currentPlayer]); | |
System.out.println("The category is " + currentCategory()); | |
askQuestion(); | |
} else { | |
System.out.println(players.get(currentPlayer) | |
+ " is not getting out of the penalty box"); | |
isGettingOutOfPenaltyBox = false; | |
} | |
} else { | |
places[currentPlayer] = places[currentPlayer] + roll; | |
if (places[currentPlayer] > 11) | |
places[currentPlayer] = places[currentPlayer] - 12; | |
System.out.println(players.get(currentPlayer) + "'s new location is " | |
+ places[currentPlayer]); | |
System.out.println("The category is " + currentCategory()); | |
askQuestion(); | |
} | |
} | |
private void askQuestion() { | |
if (currentCategory() == "Pop") | |
System.out.println(popQuestions.removeFirst()); | |
if (currentCategory() == "Science") | |
System.out.println(scienceQuestions.removeFirst()); | |
if (currentCategory() == "Sports") | |
System.out.println(sportsQuestions.removeFirst()); | |
if (currentCategory() == "Rock") | |
System.out.println(rockQuestions.removeFirst()); | |
} | |
private String currentCategory() { | |
if (places[currentPlayer] == 0) | |
return "Pop"; | |
if (places[currentPlayer] == 4) | |
return "Pop"; | |
if (places[currentPlayer] == 8) | |
return "Pop"; | |
if (places[currentPlayer] == 1) | |
return "Science"; | |
if (places[currentPlayer] == 5) | |
return "Science"; | |
if (places[currentPlayer] == 9) | |
return "Science"; | |
if (places[currentPlayer] == 2) | |
return "Sports"; | |
if (places[currentPlayer] == 6) | |
return "Sports"; | |
if (places[currentPlayer] == 10) | |
return "Sports"; | |
return "Rock"; | |
} | |
public boolean wasCorrectlyAnswered() { | |
if (inPenaltyBox[currentPlayer]) { | |
if (isGettingOutOfPenaltyBox) { | |
System.out.println("Answer was correct!!!!"); | |
purses[currentPlayer]++; | |
System.out.println(players.get(currentPlayer) + " now has " + purses[currentPlayer] | |
+ " Gold Coins."); | |
boolean winner = didPlayerWin(); | |
currentPlayer++; | |
if (currentPlayer == players.size()) | |
currentPlayer = 0; | |
return winner; | |
} else { | |
currentPlayer++; | |
if (currentPlayer == players.size()) | |
currentPlayer = 0; | |
return true; | |
} | |
} else { | |
System.out.println("Answer was corrent!!!!"); | |
purses[currentPlayer]++; | |
System.out.println(players.get(currentPlayer) + " now has " + purses[currentPlayer] | |
+ " Gold Coins."); | |
boolean winner = didPlayerWin(); | |
currentPlayer++; | |
if (currentPlayer == players.size()) | |
currentPlayer = 0; | |
return winner; | |
} | |
} | |
public boolean wrongAnswer() { | |
System.out.println("Question was incorrectly answered"); | |
System.out.println(players.get(currentPlayer) + " was sent to the penalty box"); | |
inPenaltyBox[currentPlayer] = true; | |
currentPlayer++; | |
if (currentPlayer == players.size()) | |
currentPlayer = 0; | |
return true; | |
} | |
private boolean didPlayerWin() { | |
return !(purses[currentPlayer] == 6); | |
} | |
} |
And this is the code after my refactoring:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.adaptionsoft.games.uglytrivia; | |
public class Game { | |
private Rules rules; | |
private Players players; | |
private Player currentPlayer; | |
private Turn turn; | |
public Game(Players players, Rules rules, Turn turn) { | |
this.players = players; | |
this.rules = rules; | |
this.turn = turn; | |
} | |
public void run() { | |
presentPlayers(); | |
play(); | |
} | |
private void play() { | |
do { | |
nextPlayer(); | |
playTurn(); | |
} while (noPlayerHasWon()); | |
} | |
private void presentPlayers() { | |
int num = 0; | |
for (Player player : players) { | |
System.out.println(player + " was added"); | |
System.out.println("They are player number " + String.valueOf(++num)); | |
} | |
} | |
private void playTurn() { | |
turn.play(currentPlayer); | |
} | |
private void nextPlayer() { | |
currentPlayer = players.next(); | |
System.out.println(currentPlayer + " is the current player"); | |
} | |
private boolean noPlayerHasWon() { | |
return !currentPlayer.hasWonAccordingTo(rules); | |
} | |
} |
And this is the code after I revisited it again (Feb. 27th 2016):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.adaptionsoft.games.uglytrivia; | |
public class Game { | |
private Rules rules; | |
private Players players; | |
private Player currentPlayer; | |
private Turn turn; | |
public Game(Players players, Rules rules, Turn turn) { | |
this.players = players; | |
this.rules = rules; | |
this.turn = turn; | |
} | |
public void run() { | |
do { | |
nextPlayer(); | |
playTurn(); | |
} while (noPlayerHasWon()); | |
} | |
private void playTurn() { | |
turn.play(currentPlayer); | |
} | |
private void nextPlayer() { | |
currentPlayer = players.next(); | |
} | |
private boolean noPlayerHasWon() { | |
return !currentPlayer.hasWonAccordingTo(rules); | |
} | |
} |
Check my solution in GitHub with commits after each refactoring step.
No comments:
Post a Comment