Skip to content

Commit

Permalink
Finish implementation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Eric Smith committed Sep 24, 2014
1 parent 7625f01 commit 10e380b
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/hangman/AnswerGenerator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.hangman;

import java.util.List;

public interface AnswerGenerator {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/hangman/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import java.util.List;

public interface Game {
List<Character> CurrentClue();

int numGuesses();

public boolean IsOver();
void Guess(char guess);

List<Character> CurrentClue();
public boolean IsWinner();
}
35 changes: 28 additions & 7 deletions src/main/java/com/hangman/HangmanGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Character> answer;
private List<Character> guesses;
private List<Character> correctGuesses;
private AnswerGenerator answerGenerator = new RandomAnswerGeneratorFromList();

public HangmanGame()
{
guesses = new ArrayList<Character>();
correctGuesses = new ArrayList<Character>();
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();
}
Expand All @@ -36,7 +42,7 @@ public List<Character> 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('_');
Expand All @@ -45,14 +51,29 @@ public List<Character> 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<Character> Answer() {
if (answer == null) {
answer = new ArrayList<Character>();
for (Character c : answerGenerator.generateAnswer().toCharArray())
for (Character c : answerGenerator.generateAnswer().toLowerCase().toCharArray())
answer.add(c);
}
return answer;
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/hangman/HangmanRunner.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/hangman/Runner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hangman;

public interface Runner {
public void run();
public boolean isWinner();
public int numGuesses();
}

6 changes: 5 additions & 1 deletion src/main/java/com/hangman/SleepTicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/hangman/Ticker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.hangman;

public interface Ticker {
public void tick() throws Exception;
public void tick();
}
11 changes: 6 additions & 5 deletions src/main/java/com/hangman/players/YourPlayer.java
Original file line number Diff line number Diff line change
@@ -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<Character> clue) {
return 'a';
}
@Override
public char GetGuess(List<Character> clue) {
return 'a';
}
}
10 changes: 10 additions & 0 deletions src/test/java/com/hangman/ConsoleDisplayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public void Guess(char guess) {

}

@Override
public boolean IsWinner() {
return false;
}

@Override
public int numGuesses() {
return 0;
}

@Override
public List<Character> CurrentClue() {
return currentClue;
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/hangman/FakeGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class FakeGame implements Game {
private ArrayList<Character> guesses;
private char lastGuess;
private List<Character> clue;
private boolean winner;
private int numGuesses;

public FakeGame() {
this.done = false;
Expand Down Expand Up @@ -44,7 +46,23 @@ public List<Character> CurrentClue() {
return clue;
}

@Override
public boolean IsWinner() {
return winner;
}

@Override
public int numGuesses() {
return numGuesses;
}

public void SetToWinner() {winner = true;}

public void SetCurrentClue(List<Character> clue) {
this.clue = clue;
}

public void SetNumGuesses(int guesses) {
numGuesses = guesses;
}
}
83 changes: 80 additions & 3 deletions src/test/java/com/hangman/HangmanGameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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');
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -123,7 +145,7 @@ public void CaseIsIrrelevant() {

game.Guess('p');

assertEquals(Arrays.asList('P', '_', '_', '_', '_', '_'), game.CurrentClue());
assertEquals(Arrays.asList('p', '_', '_', '_', '_', '_'), game.CurrentClue());
}

@Test
Expand All @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/hangman/HangmanRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/hangman/players/YourPlayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.junit.Assert.assertEquals;

public class YourPlayerTest {

@Test
public void AlwaysGuessA() {
YourPlayer player = new YourPlayer();
Expand Down

0 comments on commit 10e380b

Please sign in to comment.