Permalink
Browse files

All pieces are now working together

  • Loading branch information...
1 parent e8725c0 commit 36ec31e6a14b1e5b2d72c4e0f341bd35aa6e25bd @samidalouche committed Feb 14, 2010
@@ -1,6 +1,10 @@
package com.dalouche.experiments.rockpaperscissors;
+import java.util.Map;
+
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
public final class GameOutcome {
private ImmutableList<Round> rounds;
@@ -9,4 +13,21 @@ public GameOutcome(Iterable<Round> rounds) {
super();
this.rounds = ImmutableList.copyOf(rounds);
}
+
+ public ImmutableMap<Player, Integer> numberOfWinsPerPlayer() {
+ Map<Player, Integer> numberOfWinsPerPlayer = Maps.newHashMap();
+ for(Round r : rounds) {
+ incrementNumberOfWinsForPlayer(r.getRoundWinner(), numberOfWinsPerPlayer);
+ }
+
+ return ImmutableMap.copyOf(numberOfWinsPerPlayer);
+ }
+
+ private void incrementNumberOfWinsForPlayer(Player roundWinner, Map<Player, Integer> numberOfWinsPerPlayer) {
+ Integer i = numberOfWinsPerPlayer.get(roundWinner);
+ if(i == null) {
+ i = 0;
+ }
+ numberOfWinsPerPlayer.put(roundWinner, ++i);
+ }
}
@@ -18,6 +18,11 @@ public static Game computerVersusHuman() {
NumberOfRoundsProvider numberOfRoundsProvider =
new ConsoleNumberOfRoundsProvider(System.in, System.out);
- return new Game(roundSynchronizer, numberOfRoundsProvider, null, null);
+ PrintRoundOutcome roundProgressListener =
+ new PrintRoundOutcome(System.out, consolePlayer);
+ PrintGameOutcome printGameOutcome =
+ new PrintGameOutcome(System.out, consolePlayer, computerPlayer);
+
+ return new Game(roundSynchronizer, numberOfRoundsProvider, roundProgressListener, printGameOutcome);
}
}
@@ -3,7 +3,7 @@
public class Main {
/**
- * @param args
+ * @param args
*/
public static void main(String[] args) {
Game game = Games.computerVersusHuman();
@@ -0,0 +1,78 @@
+package com.dalouche.experiments.rockpaperscissors;
+
+import static com.dalouche.experiments.rockpaperscissors.Nobody.nobody;
+
+import java.io.PrintStream;
+
+import org.apache.commons.lang.Validate;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+public final class PrintGameOutcome implements GameProgressListener{
+
+ private PrintStream outputStream;
+ private Player playerInterestedByTheResults;
+ private Player otherPlayer;
+
+ public PrintGameOutcome(PrintStream outputStream, Player playerInterestedByTheResults, Player otherPlayer) {
+ super();
+ Validate.notNull(outputStream);
+ Validate.notNull(playerInterestedByTheResults);
+ Validate.notNull(otherPlayer);
+ this.outputStream = outputStream;
+ this.playerInterestedByTheResults = playerInterestedByTheResults;
+ this.otherPlayer = otherPlayer;
+ }
+
+ public void roundFinished(Round round) {
+ if(Nobody.nobody().equals(round.getRoundWinner())) {
+ outputStream.println("The round is tied. Nobody wins.");
+ } else if(playerInterestedByTheResults.equals(round.getRoundWinner())) {
+ outputStream.println(String.format("You win. %s", additionalSymbolInformation(round)));
+ } else {
+ outputStream.println(String.format("You lose. %s", additionalSymbolInformation(round)));
+ }
+ outputStream.println();
+ }
+
+ private String additionalSymbolInformation(Round round) {
+ return String.format("%s %s %s", // rock blunts scissors
+ round.getWinnerMove().getSymbol().name(),
+ round.getWinnerMove().getSymbol().actionName(),
+ round.getLoserMove().getSymbol().name());
+ }
+
+ @Override
+ public void gameFinished(Game game, GameOutcome gameOutcome) {
+ outputStream.println();
+ outputStream.println(" = Game results = ");
+
+ for(Player p: ImmutableList.of(playerInterestedByTheResults, otherPlayer, nobody())) {
+ outputStream.println(statusFor(p, gameOutcome.numberOfWinsPerPlayer()));
+ }
+ }
+
+ private String statusFor(Player player, ImmutableMap<Player, Integer> numberOfWinsPerPlayer) {
+ return String.format("%s : %s round(s)", playerDescription(player), numberOfWins(numberOfWinsPerPlayer, player));
+ }
+
+ private Integer numberOfWins(
+ ImmutableMap<Player, Integer> numberOfWinsPerPlayer, Player p) {
+ Integer wins = numberOfWinsPerPlayer.get(p);
+ if(wins == null) {
+ wins = 0;
+ }
+ return wins;
+ }
+
+ private String playerDescription(Player player) {
+ if(nobody().equals(player)) {
+ return "Rounds tied";
+ } else if(playerInterestedByTheResults.equals(player)) {
+ return "You";
+ } else {
+ return "Computer";
+ }
+ }
+}
@@ -0,0 +1,38 @@
+package com.dalouche.experiments.rockpaperscissors;
+
+import java.io.PrintStream;
+
+import org.apache.commons.lang.Validate;
+
+public final class PrintRoundOutcome implements RoundProgressListener{
+
+ private PrintStream outputStream;
+ private Player playerInterestedByTheResults;
+
+ public PrintRoundOutcome(PrintStream outputStream, Player playerInterestedByTheResults) {
+ super();
+ Validate.notNull(outputStream);
+ Validate.notNull(playerInterestedByTheResults);
+ this.outputStream = outputStream;
+ this.playerInterestedByTheResults = playerInterestedByTheResults;
+ }
+
+ @Override
+ public void roundFinished(Round round) {
+ if(Nobody.nobody().equals(round.getRoundWinner())) {
+ outputStream.println("The round is tied. Nobody wins.");
+ } else if(playerInterestedByTheResults.equals(round.getRoundWinner())) {
+ outputStream.println(String.format("You win. %s", additionalSymbolInformation(round)));
+ } else {
+ outputStream.println(String.format("You lose. %s", additionalSymbolInformation(round)));
+ }
+ outputStream.println();
+ }
+
+ private String additionalSymbolInformation(Round round) {
+ return String.format("%s %s %s", // rock blunts scissors
+ round.getWinnerMove().getSymbol().name(),
+ round.getWinnerMove().getSymbol().actionName(),
+ round.getLoserMove().getSymbol().name());
+ }
+}
@@ -43,12 +43,12 @@ public Player getRoundLoser() {
*
* @return null if it is a tie
*/
- private Move getWinnerMove() {
+ public Move getWinnerMove() {
Outcome outcome = getRoundOutcome();
return outcome != null ? outcome.winner : null;
}
- private Move getLoserMove() {
+ public Move getLoserMove() {
Outcome outcome = getRoundOutcome();
return outcome != null ? outcome.loser : null;
}

0 comments on commit 36ec31e

Please sign in to comment.