From 2689cd04151fa1a7c65daf9635cc3eefa79cda79 Mon Sep 17 00:00:00 2001 From: Lucas Schwarz Date: Tue, 14 Jun 2016 13:17:44 -0700 Subject: [PATCH] SHIP IT --- .../java/com/hangman/players/YourPlayer.java | 95 ++++++++++++++++++- .../com/hangman/players/YourPlayerTest.java | 62 ++++++++---- 2 files changed, 138 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/hangman/players/YourPlayer.java b/src/main/java/com/hangman/players/YourPlayer.java index df4a96a..4d93b05 100644 --- a/src/main/java/com/hangman/players/YourPlayer.java +++ b/src/main/java/com/hangman/players/YourPlayer.java @@ -2,11 +2,104 @@ import com.hangman.Player; +import java.util.ArrayList; import java.util.List; public class YourPlayer implements Player { + + private List attemptedChars = new ArrayList<>(); + + private String wordLength1CharsProto = "ai"; + private String wordLength2CharsProto = "aoeimhnustyblpxdfrwgjk"; + private String wordLength3CharsProto = "aeoitsuprndbgmylhwfckxvjzq"; + private String wordLength4CharsProto = "aesoirltnudpmhcbkgywfvjzxq"; + private String wordLength5CharsProto = "searioltnudcypmhgbkfwvzxjq"; + private String wordLength6CharsProto = "esariolntducmpghbykfwvzxjq"; + private String wordLength7CharsProto = "esiarntolducgpmhbyfkwvzxjq"; + private String wordLength8CharsProto = "esiarntoldcugmphbyfkwvzxqj"; + private String wordLength9CharsProto = "esirantolcdugmphbyfvkwzxqj"; + private String wordLength10CharsProto = "eisrantolcdugmphbyfvkwzxqj"; + + + public List wordLength1Chars = convertStringToList(wordLength1CharsProto); + public List wordLength2Chars = convertStringToList(wordLength2CharsProto); + public List wordLength3Chars = convertStringToList(wordLength3CharsProto); + public List wordLength4Chars = convertStringToList(wordLength4CharsProto); + public List wordLength5Chars = convertStringToList(wordLength5CharsProto); + public List wordLength6Chars = convertStringToList(wordLength6CharsProto); + public List wordLength7Chars = convertStringToList(wordLength7CharsProto); + public List wordLength8Chars = convertStringToList(wordLength8CharsProto); + public List wordLength9Chars = convertStringToList(wordLength9CharsProto); + public List wordLength10Chars = convertStringToList(wordLength10CharsProto); + + public void setAttemptedChars(List attemptedChars) { + this.attemptedChars = attemptedChars; + } + + private String protoPopularChars = "etaoinshrdlcumwfgypbvkjxqz"; + + public List getAttemptedChars() { + return attemptedChars; + } + + public List getPopularChars() { + return popularChars; + } + + private List popularChars = convertStringToList(protoPopularChars); + @Override public char getGuess(List currentClue) { - return 'a'; + if (this.popularChars == null) { + System.out.println("setting a strategy"); + setCharStrategy(currentClue); + } + return _getGuess(); } + + public void setCharStrategy(List currentClue) { + Integer size = currentClue.size(); + switch (size) { + case 1: popularChars = wordLength1Chars; + break; + case 2: popularChars = wordLength2Chars; + break; + case 3: popularChars = wordLength3Chars; + break; + case 4: popularChars = wordLength4Chars; + break; + case 5: popularChars = wordLength5Chars; + break; + case 6: popularChars = wordLength6Chars; + break; + case 7: popularChars = wordLength7Chars; + break; + case 8: popularChars = wordLength8Chars; + break; + case 9: popularChars = wordLength9Chars; + break; + case 10: popularChars = wordLength10Chars; + break; + default: popularChars = wordLength10Chars; + break; + + } + + } + + public char _getGuess() { + char guess = popularChars.get(0); + this.attemptedChars.add(guess); + this.popularChars.remove(0); //hmm + return guess; + } + + private List convertStringToList(String blob) { + List newChars = new ArrayList<>(); + for (Character c : blob.toCharArray()) { + newChars.add(c); + } + return newChars; + } + } diff --git a/src/test/java/com/hangman/players/YourPlayerTest.java b/src/test/java/com/hangman/players/YourPlayerTest.java index b1e80e5..57fec01 100644 --- a/src/test/java/com/hangman/players/YourPlayerTest.java +++ b/src/test/java/com/hangman/players/YourPlayerTest.java @@ -1,34 +1,60 @@ package com.hangman.players; import org.junit.Test; + +import java.util.ArrayList; import java.util.Arrays; -import static org.junit.Assert.assertEquals; +import java.util.List; -public class YourPlayerTest { - @Test - public void guessesAWhenThereAreNoSuccessfulCharactersGuessedYet() { - YourPlayer player = new YourPlayer(); +import static org.junit.Assert.*; - char guess = player.getGuess(Arrays.asList(null, null, null)); +public class YourPlayerTest { - assertEquals('a', guess); - } @Test - public void guessesAWhenThereAreSuccessfulCharactersGuessedThatAreNotA() { - YourPlayer player = new YourPlayer(); - - char guess = player.getGuess(Arrays.asList('m', null, 'n')); + public void alwaysPickNextPopularChar() { + YourPlayer player = new YourPlayer(); + player.setCharStrategy(Arrays.asList(null, null, null)); + //note the two most popular characters + List expectedGuesses = new ArrayList<>(); + expectedGuesses.add(player.getPopularChars().get(0)); + expectedGuesses.add(player.getPopularChars().get(1)); + //make two guesses + char guess1 = player.getGuess(Arrays.asList(null, null, null)); + char guess2 = player.getGuess(Arrays.asList(null, null, null)); + System.out.println(guess1); + System.out.println(guess2); + //verify those guesses match our popular characters + assertEquals(expectedGuesses, player.getAttemptedChars()); + } - assertEquals('a', guess); + @Test + public void chooseStrategyByClueSize() { + YourPlayer player = new YourPlayer(); + //make a clue of a given size and feed it to player + ListwordLength3Chars = Arrays.asList(null, null, null); + player.setCharStrategy(wordLength3Chars); + player.getGuess(wordLength3Chars); + + ListexpectedPopularChars = player.wordLength3Chars; + ListactualPopularChars = player.getPopularChars(); + //verify they are teh same + assertEquals(expectedPopularChars, actualPopularChars); } - @Test - public void guessesAWhenAIsThereAreAsInTheClueAsWell() { - YourPlayer player = new YourPlayer(); + @Test + public void chooseLargestStrategyIfNoMatch() { + YourPlayer player = new YourPlayer(); - char guess = player.getGuess(Arrays.asList(null, 'a', null)); + //make a clue of an unsupported size and feed it to player + ListwordLength11Chars = Arrays.asList(null, null, null, null, null, null, null, null, null, null, null); + player.setCharStrategy(wordLength11Chars); + player.getGuess(wordLength11Chars); - assertEquals('a', guess); + ListexpectedPopularChars = player.wordLength10Chars; + ListactualPopularChars = player.getPopularChars(); + //verify they are teh same + assertEquals(expectedPopularChars, actualPopularChars); } + }