diff --git a/src/main/java/com/hangman/players/YourPlayer.java b/src/main/java/com/hangman/players/YourPlayer.java index f537169..245b6b0 100644 --- a/src/main/java/com/hangman/players/YourPlayer.java +++ b/src/main/java/com/hangman/players/YourPlayer.java @@ -1,4 +1,5 @@ package com.hangman.players; +import com.hangman.HangmanGame; import com.hangman.Player; import java.util.Arrays; @@ -6,8 +7,47 @@ import java.util.List; public class YourPlayer implements Player { + + private LinkedList guesses; + private List latestClue; + + public static final List mostCommon2LeastLetters; + + static { + mostCommon2LeastLetters = Arrays.asList( + 'e', 'a', 'r', 'i', 'o', 't', 'n', 's', 'l', 'c', 'u', 'd', 'p', 'm', 'h', 'g', + 'b', 'f', 'y', 'w', 'k', 'v', 'x', 'z', 'j', 'q'); + } + + public YourPlayer() { + guesses = new LinkedList(); + this.SetGuesses(mostCommon2LeastLetters); + } + + private void SetGuesses(List chars) { + this.guesses = new LinkedList(); + for(char guess : chars) { + this.guesses.add(guess); + } + } + @Override public char GetGuess(List clue) { - return 'a'; + if (clue == null) { + return 0; + } + + latestClue = clue; + if (guesses.size() > 0) { + char c = guesses.pop(); + if (!latestClue.contains(c)) + return c; + } + + return 0; + } + + public List GetLatestClue() { + return latestClue; } } diff --git a/src/test/java/com/hangman/players/YourPlayerTest.java b/src/test/java/com/hangman/players/YourPlayerTest.java index bc92a3b..ffe854c 100644 --- a/src/test/java/com/hangman/players/YourPlayerTest.java +++ b/src/test/java/com/hangman/players/YourPlayerTest.java @@ -1,16 +1,61 @@ package com.hangman.players; +import org.junit.Before; import org.junit.Test; + +import java.util.ArrayList; import java.util.Arrays; -import static org.junit.Assert.assertEquals; +import java.util.List; + +import static org.junit.Assert.*; public class YourPlayerTest { + + private YourPlayer player; + + @Before + public void setUp() { + player = new YourPlayer(); + } + @Test - public void AlwaysGuessA() { - YourPlayer player = new YourPlayer(); + public void GetLatestClueShouldReturnNothing() { + assertNull(player.GetLatestClue()); + } - char guess = player.GetGuess(Arrays.asList('a', 'b', 'c')); + @Test + public void GetGuessShouldReturnFirstElementInMostCommonLetterList() { + char guess = player.GetGuess(Arrays.asList('_', '_', '_')); + assertEquals(guess, YourPlayer.mostCommon2LeastLetters.get(0).charValue()); + } - assertEquals('a', guess); + @Test + public void GetGuessShouldReturnZeroWhenClueIsNull() { + char c = player.GetGuess(null); + assertTrue(c == 0); + } + + @Test + public void GetGuessShouldAlwaysReturnElementInMostCommonLetterList() { + for(int i = 0; i < YourPlayer.mostCommon2LeastLetters.size(); i++) { + char guess = player.GetGuess(Arrays.asList('_', '_', '_')); + assertTrue(Character.isAlphabetic(guess)); + } + } + + @Test + public void GetGuessShouldNotReturnLetterMorN() { + for(int i = 0; i < YourPlayer.mostCommon2LeastLetters.size(); i++) { + char c = player.GetGuess(Arrays.asList('m', '_', 'n')); + assertNotEquals(c, 'm'); + assertNotEquals(c, 'n'); + } + } + + @Test + public void GetLatestClueShouldMatchInitialClueList() { + List clue = Arrays.asList('_', '_', '_'); + player.GetGuess(Arrays.asList('_', '_', '_')); + assertEquals(clue, player.GetLatestClue()); } }