diff --git a/H5_Skunk/bin/SkunkApp.class b/H5_Skunk/bin/SkunkApp.class index 3f03f75..42a3c74 100644 Binary files a/H5_Skunk/bin/SkunkApp.class and b/H5_Skunk/bin/SkunkApp.class differ diff --git a/H5_Skunk/bin/SkunkDomain.class b/H5_Skunk/bin/SkunkDomain.class index aafa96a..32be10b 100644 Binary files a/H5_Skunk/bin/SkunkDomain.class and b/H5_Skunk/bin/SkunkDomain.class differ diff --git a/H5_Skunk/bin/SkunkUI.class b/H5_Skunk/bin/SkunkUI.class index d4ec9cd..9c3b9f7 100644 Binary files a/H5_Skunk/bin/SkunkUI.class and b/H5_Skunk/bin/SkunkUI.class differ diff --git a/H5_Skunk/src/SkunkApp.java b/H5_Skunk/src/SkunkApp.java index feb4554..d7e0508 100644 --- a/H5_Skunk/src/SkunkApp.java +++ b/H5_Skunk/src/SkunkApp.java @@ -28,6 +28,7 @@ public boolean run() public static void main(String[] args) { new SkunkApp().run(); + } } diff --git a/H5_Skunk/src/SkunkDomain.java b/H5_Skunk/src/SkunkDomain.java index b0f325c..ff692f5 100644 --- a/H5_Skunk/src/SkunkDomain.java +++ b/H5_Skunk/src/SkunkDomain.java @@ -4,11 +4,11 @@ public class SkunkDomain { public SkunkUI skunkUI; - public UI ui; public int numberOfPlayers; public String[] playerNames; public ArrayList players; public int kitty; + private int prevRoundScore; public Player activePlayer; public int activePlayerIndex; @@ -21,7 +21,6 @@ public class SkunkDomain public SkunkDomain(SkunkUI ui) { this.skunkUI = ui; - this.ui = ui; // hide behind the interface UI this.playerNames = new String[20]; this.players = new ArrayList(); @@ -32,58 +31,49 @@ public SkunkDomain(SkunkUI ui) public boolean run() { - ui.println("Welcome to Skunk 0.47\n"); - - String numberPlayersString = skunkUI.promptReadAndReturn("How many players?"); + skunkUI.welcome(); //REFACTORED + + String numberPlayersString = skunkUI.numberOfPlayers(); //REFACTORED - moved message to SkunkUI this.numberOfPlayers = Integer.parseInt(numberPlayersString); for (int playerNumber = 0; playerNumber < numberOfPlayers; playerNumber++) { - ui.print("Enter name of player " + (playerNumber + 1) + ": "); - playerNames[playerNumber] = StdIn.readLine(); + playerNames[playerNumber] = skunkUI.nameOfPlayer(playerNumber + 1); //REFACTORED- moved message to SkunkUI this.players.add(new Player(50)); } + activePlayerIndex = 0; activePlayer = players.get(activePlayerIndex); - ui.println("Starting game...\n"); + skunkUI.gameBegins(); //REFACTORED- moved message to SkunkUI boolean gameNotOver = true; while (gameNotOver) { - ui.println("Next player is " + playerNames[activePlayerIndex] + "."); - String wantsToRollStr = ui.promptReadAndReturn("Roll? y or n"); + + + String wantsToRollStr = skunkUI.wantToRoll(playerNames[activePlayerIndex]); //REFACTORED- moved message to SkunkUI boolean wantsToRoll = 'y' == wantsToRollStr.toLowerCase().charAt(0); while (wantsToRoll) { activePlayer.setRollScore(0); skunkDice.roll(); - if (skunkDice.getLastRoll() == 2) + if (skunkDice.getLastRoll() == 2) //if double skunk { - ui.println("Two Skunks! You lose the turn, the round score, plus pay 4 chips to the kitty"); - kitty += 4; - activePlayer.setNumberChips(activePlayer.getNumberChips() - 4); - activePlayer.setTurnScore(0); - activePlayer.setRoundScore(0); + doubleSkunk(); //REFACTORED - moved code to method below. wantsToRoll = false; break; } - else if (skunkDice.getLastRoll() == 3) + else if (skunkDice.getLastRoll() == 3) //if skunk deuce { - ui.println("Skunks and Deuce! You lose the turn, the turn score, plus pay 2 chips to the kitty"); - kitty += 2; - activePlayer.setNumberChips(activePlayer.getNumberChips() - 2); - activePlayer.setTurnScore(0); + skunkDeuce();//REFACTORED - moved code to method below. wantsToRoll = false; break; } - else if (skunkDice.getDie1().getLastRoll() == 1 || skunkDice.getDie2().getLastRoll() == 1) + else if (skunkDice.getDie1().getLastRoll() == 1 || skunkDice.getDie2().getLastRoll() == 1) //if single skunk { - ui.println("One Skunk! You lose the turn, the turn score, plus pay 1 chip to the kitty"); - kitty += 1; - activePlayer.setNumberChips(activePlayer.getNumberChips() - 1); - activePlayer.setTurnScore(0); + singleSkunk();//REFACTORED - moved code to method below. wantsToRoll = false; break; @@ -91,113 +81,88 @@ else if (skunkDice.getDie1().getLastRoll() == 1 || skunkDice.getDie2().getLastRo activePlayer.setRollScore(skunkDice.getLastRoll()); activePlayer.setTurnScore(activePlayer.getTurnScore() + skunkDice.getLastRoll()); - ui.println( - "Roll of " + skunkDice.toString() + ", gives new turn score of " + activePlayer.getTurnScore()); + + skunkUI.rollResponse(skunkDice.toString(), activePlayer.getTurnScore());//REFACTORED- moved message to SkunkUI + - wantsToRollStr = ui.promptReadAndReturn("Roll again? y or n"); + wantsToRollStr = skunkUI.wantToRollAgain(); //REFACTORED- moved message to SkunkUI wantsToRoll = 'y' == wantsToRollStr.toLowerCase().charAt(0); - + } - - ui.println("End of turn for " + playerNames[activePlayerIndex]); - ui.println("Score for this turn is " + activePlayer.getTurnScore() + ", added to..."); - ui.println("Previous round score of " + activePlayer.getRoundScore()); + prevRoundScore = activePlayer.getRoundScore(); //REFACTORED - moved messages to SkunkUI activePlayer.setRoundScore(activePlayer.getRoundScore() + activePlayer.getTurnScore()); - ui.println("Giving new round score of " + activePlayer.getRoundScore()); + skunkUI.endTurn(playerNames[activePlayerIndex], activePlayer.getTurnScore(), prevRoundScore, activePlayer.getRoundScore()); - ui.println(""); if (activePlayer.getRoundScore() >= 100) gameNotOver = false; - - ui.println("Scoreboard: "); - ui.println("Kitty has " + kitty); - ui.println("player name -- turn score -- round score -- chips"); - ui.println("-----------------------"); + + skunkUI.scoreBoard(kitty); //REFACTORED - moved messages to SkunkUI for (int i = 0; i < numberOfPlayers; i++) { - ui.println(playerNames[i] + " -- " + players.get(i).turnScore + " -- " + players.get(i).roundScore - + " -- " + players.get(i).getNumberChips()); + skunkUI.scoreBoardRows(playerNames[i], players.get(i).turnScore, players.get(i).roundScore, players.get(i).getNumberChips()); + } - ui.println("-----------------------"); + + skunkUI.passTurn(); //REFACTORED - moved messages to SkunkUI - ui.println("Turn passes to right..."); activePlayerIndex = (activePlayerIndex + 1) % numberOfPlayers; activePlayer = players.get(activePlayerIndex); } // last round: everyone but last activePlayer gets another shot - - ui.println("Last turn for all..."); - + skunkUI.lastTurnMsg(); //REFACTORED - moved message to SkunkUI for (int i = activePlayerIndex, count = 0; count < numberOfPlayers - 1; i = (i++) % numberOfPlayers, count++) { - ui.println("Last round for player " + playerNames[activePlayerIndex] + "..."); activePlayer.setTurnScore(0); - - String wantsToRollStr = ui.promptReadAndReturn("Roll? y or n"); + skunkUI.lastTurn(playerNames[activePlayerIndex]);//REFACTORED - moved message to SkunkUI + String wantsToRollStr = skunkUI.wantToRollLast(); //REFACTORED- moved message to SkunkUI boolean wantsToRoll = 'y' == wantsToRollStr.toLowerCase().charAt(0); + while (wantsToRoll) { skunkDice.roll(); - ui.println("Roll is " + skunkDice.toString() + "\n"); - - if (skunkDice.getLastRoll() == 2) + skunkUI.rollIs(skunkDice.toString()); + + if (skunkDice.getLastRoll() == 2) //BUG FIX - Double skunk handled as Single Skunk originally { - ui.println("Two Skunks! You lose the turn, the turn score, plus pay 4 chips to the kitty"); - kitty += 4; - activePlayer.setNumberChips(activePlayer.getNumberChips() - 4); - activePlayer.setTurnScore(0); + doubleSkunk(); //REFACTORED - moved code to method below. wantsToRoll = false; break; } else if (skunkDice.getLastRoll() == 3) { - ui.println("Skunks and Deuce! You lose the turn, the turn score, plus pay 2 chips to the kitty"); - kitty += 2; - activePlayer.setNumberChips(activePlayer.getNumberChips() - 2); - activePlayer.setTurnScore(0); + skunkDeuce(); //REFACTORED - moved code to method below. wantsToRoll = false; } else if (skunkDice.getDie1().getLastRoll() == 1 || skunkDice.getDie2().getLastRoll() == 1) - { - ui.println("One Skunk! You lose the turn, the turn core, plus pay 1 chip to the kitty"); - kitty += 1; - activePlayer.setNumberChips(activePlayer.getNumberChips() - 1); - activePlayer.setTurnScore(0); - activePlayer.setRoundScore(0); + {//BUG FIX - Single Skunk handled as Double Skunk originally + singleSkunk(); //REFACTORED - moved code to method below. wantsToRoll = false; } else { activePlayer.setTurnScore(activePlayer.getRollScore() + skunkDice.getLastRoll()); - ui.println("Roll of " + skunkDice.toString() + ", giving new turn score of " - + activePlayer.getTurnScore()); - - ui.println("Scoreboard: "); - ui.println("Kitty has " + kitty); - ui.println("player name -- turn score -- round score -- total chips"); - ui.println("-----------------------"); + skunkUI.rollResponse(skunkDice.toString(), activePlayer.getTurnScore());//REFACTORED- moved message to SkunkUI + + skunkUI.scoreBoard(kitty);//REFACTORED- moved message to SkunkUI for (int pNumber = 0; pNumber < numberOfPlayers; pNumber++) { - ui.println(playerNames[pNumber] + " -- " + players.get(pNumber).turnScore + " -- " - + players.get(pNumber).roundScore + " -- " + players.get(pNumber).getNumberChips()); + skunkUI.scoreBoardRows(playerNames[pNumber], players.get(pNumber).turnScore, players.get(pNumber).roundScore, players.get(pNumber).getNumberChips()); } - ui.println("-----------------------"); - - wantsToRollStr = ui.promptReadAndReturn("Roll again? y or n"); + skunkUI.dividerLine(); + wantsToRollStr = skunkUI.wantToRoll(playerNames[activePlayerIndex]);//REFACTORED- moved message to SkunkUI wantsToRoll = 'y' == wantsToRollStr.toLowerCase().charAt(0); } } activePlayer.setTurnScore(activePlayer.getRollScore() + skunkDice.getLastRoll()); - ui.println("Last roll of " + skunkDice.toString() + ", giving final round score of " - + activePlayer.getRollScore()); + skunkUI.finalRoundResponse(skunkDice.toString(), activePlayer.getRollScore());//REFACTORED- moved message to SkunkUI } @@ -207,36 +172,51 @@ else if (skunkDice.getDie1().getLastRoll() == 1 || skunkDice.getDie2().getLastRo for (int player = 0; player < numberOfPlayers; player++) { Player nextPlayer = players.get(player); - ui.println("Final round score for " + playerNames[player] + " is " + nextPlayer.getRoundScore() + "."); + skunkUI.finalRoundScore(playerNames[player], nextPlayer.getRoundScore()); if (nextPlayer.getRoundScore() > winnerScore) { winner = player; winnerScore = nextPlayer.getRoundScore(); } } - - ui.println("Round winner is " + playerNames[winner] + " with score of " + players.get(winner).getRoundScore()); players.get(winner).setNumberChips(players.get(winner).getNumberChips() + kitty); - ui.println("\nRound winner earns " + kitty + ", finishing with " + players.get(winner).getNumberChips()); + skunkUI.finalScoreBoard(playerNames[winner], players.get(winner).getRoundScore(), kitty, players.get(winner).getNumberChips()); - ui.println("\nFinal scoreboard for this round:"); - ui.println("player name -- round score -- total chips"); - ui.println("-----------------------"); for (int pNumber = 0; pNumber < numberOfPlayers; pNumber++) { - ui.println(playerNames[pNumber] + " -- " + players.get(pNumber).roundScore + " -- " - + players.get(pNumber).getNumberChips()); + skunkUI.scoreBoardRows(playerNames[pNumber], players.get(pNumber).roundScore, players.get(pNumber).getNumberChips()); } - ui.println("-----------------------"); + skunkUI.dividerLine(); return true; } - public static void main(String[] args) + private void doubleSkunk() { - // TODO Auto-generated method stub + skunkUI.doubleSkunk(); //REFACTORED- moved message to SkunkUI + kitty += 4; + activePlayer.setNumberChips(activePlayer.getNumberChips() - 4); + activePlayer.setTurnScore(0); + activePlayer.setRoundScore(0); + } + + private void skunkDeuce() + { + skunkUI.skunkDeuce(); //REFACTORED- moved message to SkunkUI + kitty += 2; + activePlayer.setNumberChips(activePlayer.getNumberChips() - 2); + activePlayer.setTurnScore(0); + } + private void singleSkunk() + { + skunkUI.singleSkunk(); //REFACTORED- moved message to SkunkUI + kitty += 1; + activePlayer.setNumberChips(activePlayer.getNumberChips() - 1); + activePlayer.setTurnScore(0); } + + } diff --git a/H5_Skunk/src/SkunkUI.java b/H5_Skunk/src/SkunkUI.java index 01fdb79..45d36d5 100644 --- a/H5_Skunk/src/SkunkUI.java +++ b/H5_Skunk/src/SkunkUI.java @@ -35,5 +35,139 @@ public void println(String toPrint) StdOut.println(toPrint); } + + public void welcome() + { + StdOut.println("Welcome to Skunk 0.47\n"); + } + + public String numberOfPlayers() + { + return promptReadAndReturn("How many players?"); + + } + public String nameOfPlayer(int playerNum) + { + return promptReadAndReturn("Enter name of player " + (playerNum) + ": "); + + } + + public void gameBegins() + { + StdOut.println("Starting game...\n"); + } + + public String wantToRoll(String playerName) + { + StdOut.println("Next player is " + playerName + ".\n"); + return promptReadAndReturn("Roll? y or n"); + } + + public String wantToRollLast() + { + return promptReadAndReturn("Roll? y or n"); + } + + public String wantToRollAgain() + { + return promptReadAndReturn("Roll again? y or n"); + } + + public void doubleSkunk() + { + StdOut.println("Two Skunks! You lose the turn, the round score, plus pay 4 chips to the kitty"); + } + + public void skunkDeuce() //REFACTORED- moved message to SkunkUI + { + StdOut.println("Skunks and Deuce! You lose the turn, the turn score, plus pay 2 chips to the kitty"); + } + + public void singleSkunk() //REFACTORED- moved message to SkunkUI + { + StdOut.println("One Skunk! You lose the turn, the turn score, plus pay 1 chip to the kitty"); + } + + public void endTurn(String playerName, int turnScore, int prevRoundScore, int roundScore) + { + StdOut.println(""); + StdOut.println("End of turn for " + playerName); + StdOut.println("Score for this turn is " + turnScore + ", added to..."); + StdOut.println("Previous round score of " + prevRoundScore); + StdOut.println("Giving new round score of " + roundScore); + StdOut.println(""); + } + + public void scoreBoard(int kitty) + { + StdOut.println("Scoreboard: "); + StdOut.println("Kitty has " + kitty); + StdOut.println("player name -- turn score -- round score -- chips"); + StdOut.println("-----------------------"); + } + + public void finalScoreBoard(String playerName, int roundScore, int kitty, int chipTotal) + { + StdOut.println("Round winner is " + playerName + " with score of " + roundScore); + StdOut.println("\nRound winner earns " + kitty + ", finishing with " + chipTotal); + + StdOut.println("\nFinal scoreboard for this round:"); + StdOut.println("player name -- round score -- total chips"); + StdOut.println("-----------------------"); + } + + public void scoreBoardRows(String name, int turnScore, int roundScore, int chips) + { + StdOut.println(name + " -- " + turnScore + " -- " + roundScore + + " -- " + chips); + } + + public void scoreBoardRows(String name, int roundScore, int chips) + { + StdOut.println(name + " -- " + roundScore + " -- " + chips); + } + + public void dividerLine() + { + StdOut.println("-----------------------"); + } + public void passTurn() + { + dividerLine(); + + StdOut.println("Turn passes to right..."); + } + + public void lastTurnMsg() + { + StdOut.println("Last turn for all..."); + } + + public void lastTurn(String playerName) + { + StdOut.println("Last round for player " + playerName + "..."); + + } + + public void rollResponse(String diceRoll, int turnScore) + { + StdOut.println("Roll of " + diceRoll + ", gives new turn score of " + turnScore); + } + + public void rollIs(String diceRoll) + { + StdOut.println("Roll is " + diceRoll + "\n"); + } + + public void finalRoundResponse(String diceRoll, int playerRoll) + { + StdOut.println("Last roll of " + diceRoll + ", giving final round score of " + + playerRoll); + } + + public void finalRoundScore(String playerName, int playerScore) + { + StdOut.println("Final round score for " + playerName + " is " + playerScore + "."); + } }