diff --git a/src/main/java/com/hangman/players/YourPlayer.java b/src/main/java/com/hangman/players/YourPlayer.java index df4a96a..b316dac 100644 --- a/src/main/java/com/hangman/players/YourPlayer.java +++ b/src/main/java/com/hangman/players/YourPlayer.java @@ -2,11 +2,98 @@ import com.hangman.Player; -import java.util.List; +import java.util.*; public class YourPlayer implements Player { + private List<Character> previousGuesses = new ArrayList<>(); + + private char[] alpha = "abcdefghijklmnopqrstuvwxyz".toCharArray(); + private char[] vowels = "aeiouy".toCharArray(); + private char[] charByFrequency = "etaoinshrdlcumwfgypbvkjxqz".toCharArray(); + + private Map<Integer, char[]> clueLengthToCharacters = new HashMap<>(); + @Override public char getGuess(List<Character> currentClue) { - return 'a'; + setClueCharacter(); + char guess; + + do{ + guess = getGuessExecute(currentClue); + }while(previousGuesses.contains(guess)); + + previousGuesses.add(guess); + + return guess; } + + private char getGuessExecute(List<Character> currentClue){ + char guess; + + if(currentClue.contains('q') && !previousGuesses.contains('u')) { + guess = 'u'; + }else if(currentClue.contains('t') && !previousGuesses.contains('h')){ + guess = 'h'; + }else { + guess = getCleanCharByFrequency(currentClue).get(0); + } + + return guess; + } + + private List<Character> getCleanCharByFrequency(List<Character> currentClue){ + List<Character> cleanArray = new ArrayList<>(); + char[] characterList = getCharacterList(currentClue); + for(char letter : characterList){ + if(!previousGuesses.contains(letter)){ + cleanArray.add(letter); + } + } + + return cleanArray; + } + + private char[] getCharacterList(List<Character> currentClue){ + if(currentClue.size() > 20){ + return charByFrequency; + }else{ + char[] charList = clueLengthToCharacters.get(currentClue.size()); + if(previousGuesses.size() >= charList.length){ + return charByFrequency; + }else{ + return charList; + } + } + } + + private void setClueCharacter(){ + clueLengthToCharacters.put(1 ,"ai".toCharArray()); + clueLengthToCharacters.put(2 ,"aoumbh".toCharArray()); + clueLengthToCharacters.put(3 ,"aeouyhbck".toCharArray()); + clueLengthToCharacters.put(4 ,"aeouysbf".toCharArray()); + clueLengthToCharacters.put(5 ,"seaouyh".toCharArray()); + clueLengthToCharacters.put(6 ,"eiusY".toCharArray()); + clueLengthToCharacters.put(7 ,"iaus".toCharArray()); + clueLengthToCharacters.put(8 ,"iaou".toCharArray()); + clueLengthToCharacters.put(9 ,"iaou".toCharArray()); + clueLengthToCharacters.put(10 ,"ioau".toCharArray()); + clueLengthToCharacters.put(11 ,"ioad".toCharArray()); + clueLengthToCharacters.put(12 ,"ioaf".toCharArray()); + clueLengthToCharacters.put(13 ,"ieoa".toCharArray()); + clueLengthToCharacters.put(14 ,"ieo".toCharArray()); + clueLengthToCharacters.put(15 ,"iea".toCharArray()); + clueLengthToCharacters.put(16 ,"ieh".toCharArray()); + clueLengthToCharacters.put(17 ,"ier".toCharArray()); + clueLengthToCharacters.put(18 ,"iea".toCharArray()); + clueLengthToCharacters.put(19 ,"iea".toCharArray()); + clueLengthToCharacters.put(20 ,"ie".toCharArray()); + } + + public void setPreviousGuesses(Character... guesses){ + previousGuesses.addAll(Arrays.asList(guesses)); + } + + public int getGuessCount(){ + return previousGuesses.size(); + } } diff --git a/src/test/java/com/hangman/players/YourPlayerTest.java b/src/test/java/com/hangman/players/YourPlayerTest.java index b1e80e5..119498f 100644 --- a/src/test/java/com/hangman/players/YourPlayerTest.java +++ b/src/test/java/com/hangman/players/YourPlayerTest.java @@ -1,34 +1,91 @@ package com.hangman.players; import org.junit.Test; -import java.util.Arrays; + +import java.util.*; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; public class YourPlayerTest { - @Test - public void guessesAWhenThereAreNoSuccessfulCharactersGuessedYet() { - YourPlayer player = new YourPlayer(); - char guess = player.getGuess(Arrays.asList(null, null, null)); + @Test + public void should_increment_guess_count(){ + YourPlayer player = new YourPlayer(); + player.getGuess(Arrays.asList(null, null, null)); + + assertEquals(1, player.getGuessCount()); + } + + @Test + public void should_after_first_two_guesses_guess_a(){ + YourPlayer player = new YourPlayer(); + player.setPreviousGuesses('e','t'); + char guess = player.getGuess(Arrays.asList(null, null, null)); + + assertEquals('a', guess); + } + + @Test + public void should_not_guess_the_same_thing_twice(){ + YourPlayer player = new YourPlayer(); + Set<Character> guesses = new HashSet<>(); + + for(int i=1;i<27;i++){ + guesses.add(player.getGuess(Arrays.asList(null, null, null))); + } + + assertEquals(26, guesses.size()); + } + + @Test + public void should_guess_u_if_clue_has_q(){ + YourPlayer player = new YourPlayer(); + char guess = player.getGuess(Arrays.asList('q', null, null)); + + assertEquals('u', guess); + } + + @Test + public void should_not_guess_u_if_clue_has_q_but_already_guess_u(){ + YourPlayer player = new YourPlayer(); + player.setPreviousGuesses('u'); + char guess = player.getGuess(Arrays.asList('q', null, null)); + + assertNotEquals('u', guess); + } + + @Test + public void should_guess_h_if_clue_has_t(){ + YourPlayer player = new YourPlayer(); + char guess = player.getGuess(Arrays.asList('t', null, null)); - assertEquals('a', guess); - } + assertEquals('h', guess); + } - @Test - public void guessesAWhenThereAreSuccessfulCharactersGuessedThatAreNotA() { - YourPlayer player = new YourPlayer(); + @Test + public void should_not_guess_what_has_already_been_guessed(){ + YourPlayer player = new YourPlayer(); + player.setPreviousGuesses('t'); + char guess = player.getGuess(Arrays.asList('e', null, null)); - char guess = player.getGuess(Arrays.asList('m', null, 'n')); + assertNotEquals('t', guess); + } - assertEquals('a', guess); - } + @Test + public void if_clue_length_is_2_guess_a(){ + YourPlayer player = new YourPlayer(); + char guess = player.getGuess(Arrays.asList(null, null)); - @Test - public void guessesAWhenAIsThereAreAsInTheClueAsWell() { - YourPlayer player = new YourPlayer(); + assertEquals('a', guess); + } - char guess = player.getGuess(Arrays.asList(null, 'a', null)); + @Test + public void if_clue_length_is_7_guess_i(){ + YourPlayer player = new YourPlayer(); + char guess = player.getGuess(Arrays.asList(null, null, null, null, null, null, null)); - assertEquals('a', guess); - } + assertEquals('i', guess); + } }