Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHIP IT #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion src/main/java/com/hangman/players/YourPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,104 @@

import com.hangman.Player;

import java.util.ArrayList;
import java.util.List;

public class YourPlayer implements Player {

private List<Character> 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<Character> wordLength1Chars = convertStringToList(wordLength1CharsProto);
public List<Character> wordLength2Chars = convertStringToList(wordLength2CharsProto);
public List<Character> wordLength3Chars = convertStringToList(wordLength3CharsProto);
public List<Character> wordLength4Chars = convertStringToList(wordLength4CharsProto);
public List<Character> wordLength5Chars = convertStringToList(wordLength5CharsProto);
public List<Character> wordLength6Chars = convertStringToList(wordLength6CharsProto);
public List<Character> wordLength7Chars = convertStringToList(wordLength7CharsProto);
public List<Character> wordLength8Chars = convertStringToList(wordLength8CharsProto);
public List<Character> wordLength9Chars = convertStringToList(wordLength9CharsProto);
public List<Character> wordLength10Chars = convertStringToList(wordLength10CharsProto);

public void setAttemptedChars(List<Character> attemptedChars) {
this.attemptedChars = attemptedChars;
}

private String protoPopularChars = "etaoinshrdlcumwfgypbvkjxqz";

public List<Character> getAttemptedChars() {
return attemptedChars;
}

public List<Character> getPopularChars() {
return popularChars;
}

private List<Character> popularChars = convertStringToList(protoPopularChars);

@Override
public char getGuess(List<Character> currentClue) {
return 'a';
if (this.popularChars == null) {
System.out.println("setting a strategy");
setCharStrategy(currentClue);
}
return _getGuess();
}

public void setCharStrategy(List<Character> 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<Character> convertStringToList(String blob) {
List<Character> newChars = new ArrayList<>();
for (Character c : blob.toCharArray()) {
newChars.add(c);
}
return newChars;
}

}
62 changes: 44 additions & 18 deletions src/test/java/com/hangman/players/YourPlayerTest.java
Original file line number Diff line number Diff line change
@@ -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<Character> 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
List<Character>wordLength3Chars = Arrays.asList(null, null, null);
player.setCharStrategy(wordLength3Chars);
player.getGuess(wordLength3Chars);

List<Character>expectedPopularChars = player.wordLength3Chars;
List<Character>actualPopularChars = 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
List<Character>wordLength11Chars = Arrays.asList(null, null, null, null, null, null, null, null, null, null, null);
player.setCharStrategy(wordLength11Chars);
player.getGuess(wordLength11Chars);

assertEquals('a', guess);
List<Character>expectedPopularChars = player.wordLength10Chars;
List<Character>actualPopularChars = player.getPopularChars();
//verify they are teh same
assertEquals(expectedPopularChars, actualPopularChars);
}

}