From 10e380b9fdd1e543f8a09a340a4b24a05df5b27b Mon Sep 17 00:00:00 2001 From: Eric Smith Date: Wed, 24 Sep 2014 00:00:36 -0700 Subject: [PATCH] Finish implementation I got out of the habit of small commits. Late night programming hazard. A lot of changes so the scorer can get the info it needs. --- .../java/com/hangman/AnswerGenerator.java | 1 - src/main/java/com/hangman/Game.java | 7 +- src/main/java/com/hangman/HangmanGame.java | 35 ++++++-- src/main/java/com/hangman/HangmanRunner.java | 16 +++- src/main/java/com/hangman/Runner.java | 8 ++ src/main/java/com/hangman/SleepTicker.java | 6 +- src/main/java/com/hangman/Ticker.java | 2 +- .../java/com/hangman/players/YourPlayer.java | 11 +-- .../java/com/hangman/ConsoleDisplayTest.java | 10 +++ src/test/java/com/hangman/FakeGame.java | 18 ++++ .../java/com/hangman/HangmanGameTest.java | 83 ++++++++++++++++++- .../java/com/hangman/HangmanRunnerTest.java | 14 ++++ .../com/hangman/players/YourPlayerTest.java | 1 - 13 files changed, 188 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/hangman/Runner.java diff --git a/src/main/java/com/hangman/AnswerGenerator.java b/src/main/java/com/hangman/AnswerGenerator.java index 028a98a..4564e7b 100644 --- a/src/main/java/com/hangman/AnswerGenerator.java +++ b/src/main/java/com/hangman/AnswerGenerator.java @@ -1,5 +1,4 @@ package com.hangman; - import java.util.List; public interface AnswerGenerator { diff --git a/src/main/java/com/hangman/Game.java b/src/main/java/com/hangman/Game.java index ea6c874..0599278 100644 --- a/src/main/java/com/hangman/Game.java +++ b/src/main/java/com/hangman/Game.java @@ -3,8 +3,11 @@ import java.util.List; public interface Game { + List CurrentClue(); + + int numGuesses(); + public boolean IsOver(); void Guess(char guess); - - List CurrentClue(); + public boolean IsWinner(); } diff --git a/src/main/java/com/hangman/HangmanGame.java b/src/main/java/com/hangman/HangmanGame.java index d9e73ce..880019c 100644 --- a/src/main/java/com/hangman/HangmanGame.java +++ b/src/main/java/com/hangman/HangmanGame.java @@ -5,26 +5,32 @@ import java.util.Observable; public class HangmanGame extends Observable implements Game { - public static int MaxGuesses = 9; + public static int MaxIncorrectGuesses = 9; + private int incorrectGuesses; private List answer; - private List guesses; + private List correctGuesses; private AnswerGenerator answerGenerator = new RandomAnswerGeneratorFromList(); public HangmanGame() { - guesses = new ArrayList(); + correctGuesses = new ArrayList(); + incorrectGuesses = 0; } @Override public boolean IsOver() { - return (guesses.size() >= MaxGuesses) || this.guesses.containsAll(this.Answer()); + return incorrectGuesses >= MaxIncorrectGuesses || IsWinner(); } @Override public void Guess(char guess) { - guesses.add(Character.toLowerCase(guess)); + if (isIncorrect(guess)) + incorrectGuesses++; + else { + correctGuesses.add(Character.toLowerCase(guess)); + } setChanged(); notifyObservers(); } @@ -36,7 +42,7 @@ public List CurrentClue() for (int i = 0; i < Answer().size(); i++) { Character c = Answer().get(i); - if (guesses.contains(Character.toLowerCase(c))) + if (correctGuesses.contains(c)) clue.add(c); else clue.add('_'); @@ -45,14 +51,29 @@ public List CurrentClue() return clue; } + @Override + public boolean IsWinner() + { + return correctGuesses.containsAll(this.Answer()); + } + + @Override + public int numGuesses() { + return incorrectGuesses + correctGuesses.size(); + } + public void SetAnswerGenerator(AnswerGenerator answerGenerator) { this.answerGenerator = answerGenerator; } + private boolean isIncorrect(char guess) { + return !this.Answer().contains(guess) || this.correctGuesses.contains(guess); + } + protected List Answer() { if (answer == null) { answer = new ArrayList(); - for (Character c : answerGenerator.generateAnswer().toCharArray()) + for (Character c : answerGenerator.generateAnswer().toLowerCase().toCharArray()) answer.add(c); } return answer; diff --git a/src/main/java/com/hangman/HangmanRunner.java b/src/main/java/com/hangman/HangmanRunner.java index e903323..5b28717 100644 --- a/src/main/java/com/hangman/HangmanRunner.java +++ b/src/main/java/com/hangman/HangmanRunner.java @@ -1,8 +1,7 @@ package com.hangman; - import com.hangman.players.YourPlayer; -public class HangmanRunner { +public class HangmanRunner implements Runner { private GameOverDisplay display; private Game game; @@ -17,7 +16,8 @@ public HangmanRunner(Game game, GameOverDisplay display, Player player, Ticker t this.ticker = ticker; } - public void run() throws Exception { + @Override + public void run() { while(!game.IsOver()) { char guess = player.GetGuess(game.CurrentClue()); game.Guess(guess); @@ -26,6 +26,16 @@ public void run() throws Exception { display.GameOver(); } + @Override + public boolean isWinner() { + return game.IsWinner(); + } + + @Override + public int numGuesses() { + return game.numGuesses(); + } + public static void main(String [] args) throws Exception { HangmanGame game = new HangmanGame(); ConsoleDisplay display = new ConsoleDisplay(); diff --git a/src/main/java/com/hangman/Runner.java b/src/main/java/com/hangman/Runner.java new file mode 100644 index 0000000..7cdc0f1 --- /dev/null +++ b/src/main/java/com/hangman/Runner.java @@ -0,0 +1,8 @@ +package com.hangman; + +public interface Runner { + public void run(); + public boolean isWinner(); + public int numGuesses(); +} + diff --git a/src/main/java/com/hangman/SleepTicker.java b/src/main/java/com/hangman/SleepTicker.java index 2ff51c0..ddadf9d 100644 --- a/src/main/java/com/hangman/SleepTicker.java +++ b/src/main/java/com/hangman/SleepTicker.java @@ -2,7 +2,11 @@ public class SleepTicker implements Ticker { @Override - public void tick() throws Exception { + public void tick() { + try { Thread.sleep(100); + } + catch (Exception e) { + } } } diff --git a/src/main/java/com/hangman/Ticker.java b/src/main/java/com/hangman/Ticker.java index b7c0c17..fb2417e 100644 --- a/src/main/java/com/hangman/Ticker.java +++ b/src/main/java/com/hangman/Ticker.java @@ -1,5 +1,5 @@ package com.hangman; public interface Ticker { - public void tick() throws Exception; + public void tick(); } diff --git a/src/main/java/com/hangman/players/YourPlayer.java b/src/main/java/com/hangman/players/YourPlayer.java index c37078c..f537169 100644 --- a/src/main/java/com/hangman/players/YourPlayer.java +++ b/src/main/java/com/hangman/players/YourPlayer.java @@ -1,12 +1,13 @@ package com.hangman.players; - import com.hangman.Player; +import java.util.Arrays; +import java.util.LinkedList; import java.util.List; public class YourPlayer implements Player { - @Override - public char GetGuess(List clue) { - return 'a'; - } + @Override + public char GetGuess(List clue) { + return 'a'; + } } diff --git a/src/test/java/com/hangman/ConsoleDisplayTest.java b/src/test/java/com/hangman/ConsoleDisplayTest.java index ca0e933..68fbd06 100644 --- a/src/test/java/com/hangman/ConsoleDisplayTest.java +++ b/src/test/java/com/hangman/ConsoleDisplayTest.java @@ -49,6 +49,16 @@ public void Guess(char guess) { } + @Override + public boolean IsWinner() { + return false; + } + + @Override + public int numGuesses() { + return 0; + } + @Override public List CurrentClue() { return currentClue; diff --git a/src/test/java/com/hangman/FakeGame.java b/src/test/java/com/hangman/FakeGame.java index e88ec37..96ab479 100644 --- a/src/test/java/com/hangman/FakeGame.java +++ b/src/test/java/com/hangman/FakeGame.java @@ -10,6 +10,8 @@ public class FakeGame implements Game { private ArrayList guesses; private char lastGuess; private List clue; + private boolean winner; + private int numGuesses; public FakeGame() { this.done = false; @@ -44,7 +46,23 @@ public List CurrentClue() { return clue; } + @Override + public boolean IsWinner() { + return winner; + } + + @Override + public int numGuesses() { + return numGuesses; + } + + public void SetToWinner() {winner = true;} + public void SetCurrentClue(List clue) { this.clue = clue; } + + public void SetNumGuesses(int guesses) { + numGuesses = guesses; + } } diff --git a/src/test/java/com/hangman/HangmanGameTest.java b/src/test/java/com/hangman/HangmanGameTest.java index 5b591bd..6fdd9ec 100644 --- a/src/test/java/com/hangman/HangmanGameTest.java +++ b/src/test/java/com/hangman/HangmanGameTest.java @@ -26,7 +26,7 @@ public void ItIsNotOverWhenTheMaxGuessesAreNotUsedYet() { simpleAnswerGenerator.setNextAnswer("ab"); game.SetAnswerGenerator(simpleAnswerGenerator); - for(int i=0; i < HangmanGame.MaxGuesses; i++) { + for(int i=0; i < HangmanGame.MaxIncorrectGuesses; i++) { assertFalse(game.IsOver()); game.Guess('c'); } @@ -37,7 +37,7 @@ public void ItIsOverWhenTheMaxGuessesAreUsed() { simpleAnswerGenerator.setNextAnswer("ab"); game.SetAnswerGenerator(simpleAnswerGenerator); - for(int i=0; i < HangmanGame.MaxGuesses; i++) { + for(int i=0; i < HangmanGame.MaxIncorrectGuesses; i++) { game.Guess('c'); } @@ -77,6 +77,28 @@ public void TheAnswerCanBeGuessedWhenThereAreDuplicates() { assertTrue(game.IsOver()); } + @Test + public void TheAnswerCanBeGuessedWhenTheWordHasCapitalLetters() { + simpleAnswerGenerator.setNextAnswer("AAbb"); + game.SetAnswerGenerator(simpleAnswerGenerator); + + game.Guess('b'); + game.Guess('a'); + + assertTrue(game.IsOver()); + } + + @Test + public void TheGameCanTellYouIfYouAreAWinner() { + simpleAnswerGenerator.setNextAnswer("AAbb"); + game.SetAnswerGenerator(simpleAnswerGenerator); + + game.Guess('b'); + game.Guess('a'); + + assertTrue(game.IsWinner()); + } + @Test public void TheCurrentClueStartsAsAListOfEmptySlots() { simpleAnswerGenerator.setNextAnswer("eric"); @@ -123,7 +145,7 @@ public void CaseIsIrrelevant() { game.Guess('p'); - assertEquals(Arrays.asList('P', '_', '_', '_', '_', '_'), game.CurrentClue()); + assertEquals(Arrays.asList('p', '_', '_', '_', '_', '_'), game.CurrentClue()); } @Test @@ -137,6 +159,61 @@ public void ItSendsNotificationsOnGuesses() { assertSame(observedObject, game); } + @Test + public void ItReturnsTheGuessCount() { + simpleAnswerGenerator.setNextAnswer("PLEASE"); + game.SetAnswerGenerator(simpleAnswerGenerator); + + game.Guess('c'); + game.Guess('p'); + + assertEquals(2, game.numGuesses()); + } + + @Test + public void ItIsAbleToGuessAWordLongerThanMaxGuesses() { + String longWord = ""; + + for (int i = 0; i <= HangmanGame.MaxIncorrectGuesses; i++) { + char nextChar = (char) ('a' + i); + longWord += nextChar; + } + + simpleAnswerGenerator.setNextAnswer(longWord); + game.SetAnswerGenerator(simpleAnswerGenerator); + + for (int i = 0; i < HangmanGame.MaxIncorrectGuesses; i++) { + game.Guess((char) ('a' + i)); + } + + assertFalse(game.IsOver()); + game.Guess((char) ('a' + HangmanGame.MaxIncorrectGuesses)); + + assertTrue(game.IsOver()); + } + + @Test + public void ItCountsAlreadyGuessedValuesAsWrongGuesses() { + String longWord = ""; + + for (int i = 0; i <= HangmanGame.MaxIncorrectGuesses; i++) { + char nextChar = (char) ('a' + i); + longWord += nextChar; + } + + simpleAnswerGenerator.setNextAnswer(longWord); + game.SetAnswerGenerator(simpleAnswerGenerator); + + for (int i = 0; i < HangmanGame.MaxIncorrectGuesses; i++) { + game.Guess('a'); + } + + assertFalse(game.IsOver()); + game.Guess('a'); + + assertTrue(game.IsOver()); + } + @Override public void update(Observable o, Object arg) { observedObject = o; diff --git a/src/test/java/com/hangman/HangmanRunnerTest.java b/src/test/java/com/hangman/HangmanRunnerTest.java index 8e65dc2..3a1a5ae 100644 --- a/src/test/java/com/hangman/HangmanRunnerTest.java +++ b/src/test/java/com/hangman/HangmanRunnerTest.java @@ -35,6 +35,20 @@ public void DisplaysGameOverWhenTheGameIsDone() throws Exception { assertTrue(display.GameOverDisplayed()); } + @Test + public void TellsYouIfGameIsAWinner() { + game.SetToWinner(); + + assertTrue(runner.isWinner()); + } + + @Test + public void ItGetsTheNumberOfGuessesFromTheGame() { + game.SetNumGuesses(3); + + assertEquals(3, runner.numGuesses()); + } + @Test public void ItPlaysATurnThatCanEndTheGame() throws Exception { game.SetLastGuess('z'); diff --git a/src/test/java/com/hangman/players/YourPlayerTest.java b/src/test/java/com/hangman/players/YourPlayerTest.java index d57966f..bc92a3b 100644 --- a/src/test/java/com/hangman/players/YourPlayerTest.java +++ b/src/test/java/com/hangman/players/YourPlayerTest.java @@ -5,7 +5,6 @@ import static org.junit.Assert.assertEquals; public class YourPlayerTest { - @Test public void AlwaysGuessA() { YourPlayer player = new YourPlayer();