From d55490f0e2d4e96d167fa05b6874e17bfd3a2e15 Mon Sep 17 00:00:00 2001 From: Joe Parker Date: Mon, 13 Apr 2015 12:40:58 -0400 Subject: [PATCH 1/2] initial fork and unit test --- .../java/com/hangman/players/YourPlayer.java | 34 ++++++++++++++++++- .../com/hangman/players/YourPlayerTest.java | 31 ++++++++++++++--- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/hangman/players/YourPlayer.java b/src/main/java/com/hangman/players/YourPlayer.java index f537169..a7a4345 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,39 @@ 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'; + latestClue = clue; + if (guesses.size() > 0) + return guesses.pop(); + 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..be6454d 100644 --- a/src/test/java/com/hangman/players/YourPlayerTest.java +++ b/src/test/java/com/hangman/players/YourPlayerTest.java @@ -1,16 +1,39 @@ package com.hangman.players; +import org.junit.Before; import org.junit.Test; + +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; 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 GetLatestClueSHouldMatchInitialClueList() { + List clue = Arrays.asList('_', '_', '_'); + player.GetGuess(Arrays.asList('_', '_', '_')); + assertEquals(clue, player.GetLatestClue()); } } From a3085375ae2955ee348d9f709fa0bc17845ae6f7 Mon Sep 17 00:00:00 2001 From: Joe Parker Date: Mon, 13 Apr 2015 13:43:22 -0400 Subject: [PATCH 2/2] additional test cases --- .../java/com/hangman/players/YourPlayer.java | 12 ++++++-- .../com/hangman/players/YourPlayerTest.java | 28 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/hangman/players/YourPlayer.java b/src/main/java/com/hangman/players/YourPlayer.java index a7a4345..245b6b0 100644 --- a/src/main/java/com/hangman/players/YourPlayer.java +++ b/src/main/java/com/hangman/players/YourPlayer.java @@ -33,9 +33,17 @@ private void SetGuesses(List chars) { @Override public char GetGuess(List clue) { + if (clue == null) { + return 0; + } + latestClue = clue; - if (guesses.size() > 0) - return guesses.pop(); + if (guesses.size() > 0) { + char c = guesses.pop(); + if (!latestClue.contains(c)) + return c; + } + return 0; } diff --git a/src/test/java/com/hangman/players/YourPlayerTest.java b/src/test/java/com/hangman/players/YourPlayerTest.java index be6454d..ffe854c 100644 --- a/src/test/java/com/hangman/players/YourPlayerTest.java +++ b/src/test/java/com/hangman/players/YourPlayerTest.java @@ -7,8 +7,7 @@ import java.util.Arrays; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.*; public class YourPlayerTest { @@ -31,7 +30,30 @@ public void GetGuessShouldReturnFirstElementInMostCommonLetterList() { } @Test - public void GetLatestClueSHouldMatchInitialClueList() { + 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());