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

sigh... #26

Open
wants to merge 3 commits 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
160 changes: 156 additions & 4 deletions src/main/java/com/hangman/players/YourPlayer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,165 @@
package com.hangman.players;
import com.hangman.Player;
import com.hangman.WordList;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

public class YourPlayer implements Player {
private boolean hasOneRightGuess;
List<Character> attemptedCharacters;
char[] frequentUsedChars = "etaoinshrdlcumwfgypbvkjxqz".toCharArray();
int index;
private char lastCharGuessed;
private String lastClue;
private boolean lastAttemptResult;
public static final boolean RIGHT_GUESS = true;
public static final boolean WRONG_GUESS = false;
private String[] words;

public YourPlayer () {
attemptedCharacters = new ArrayList<Character>();
index = 0;
hasOneRightGuess = false;
words = WordList.words.clone();
for (int i=0; i < words.length; i++) {
words[i] = words[i].toLowerCase();
}

}

public static List<String> getWordMatchingLength(String[] words, int length) {
List<String> matchingStrings = new ArrayList<String>();
for(String word : words) {
if ( word.length() == length ) {
matchingStrings.add(word);
}
}
return matchingStrings;
}


@Override
public char GetGuess(List<Character> clue) {
return 'a';

StringBuilder sb = new StringBuilder(clue.size());
for (Character c : clue)
sb.append(c);
String clueAsString = sb.toString();

if (clueAsString.equals(lastClue) || lastClue == null) {
lastAttemptResult = WRONG_GUESS;
}
else {
lastAttemptResult = RIGHT_GUESS;
hasOneRightGuess = true;
}

List<String> wordList = YourPlayer.getWordMatchingLength(words,clue.size());
words = new String[wordList.size()];
words = wordList.toArray(words);



wordList = YourPlayer.getWordMatchingPattern(words,clue);
words = new String[wordList.size()];
words = wordList.toArray(words);


if(getLastAttemptResult() == WRONG_GUESS) {
wordList = YourPlayer.getWordsWithoutChars(words, getLastCharGuessed());
words = new String[wordList.size()];
words = wordList.toArray(words);

}




if(words.length > 0 ) {
lastCharGuessed = getMostFrequentChar(words, attemptedCharacters);
}
else{
while(attemptedCharacters.contains(frequentUsedChars[index])){
index++;
}
lastCharGuessed = frequentUsedChars[index++];
}
attemptedCharacters.add(lastCharGuessed);
lastClue = clueAsString;
return lastCharGuessed;
}


public static List<String> getWordMatchingPattern(String[] words, List<Character> pattern) {
List<String> matchingStrings = new ArrayList<String>(Arrays.asList(words));
for(int index=0; index < pattern.size(); index++) {
if(pattern.get(index) == '_') {
continue;
}
for(String word : words) {
if(word.charAt(index) != pattern.get(index)) {
matchingStrings.remove(word);
continue;
}
}
}
return matchingStrings;
}

public static List<String> getWordsWithoutChars(String[] words, char chars) {
List<String> matchingStrings = new ArrayList<String>(Arrays.asList(words));

for(String word: words) {
if ( word.indexOf(chars) != -1) {
matchingStrings.remove(word);
continue;
}
}

return matchingStrings;
}

public char getLastCharGuessed() {
return lastCharGuessed;
}

public boolean getLastAttemptResult() {
return lastAttemptResult;
}

public boolean hasOneRightGuess() {
return hasOneRightGuess;
}

public static char getMostFrequentChar(String[] words, List<Character> attemptedCharacters) {
Map<Character,Integer> counter = new HashMap<Character, Integer>();

for(String word : words) {
for(char character: word.toCharArray()) {
try {
counter.get(character);
if ( counter.get(character) == null ) {
counter.put(character,0);
}
}
catch (Exception e){
counter.put(character, 0);
}

counter.put(character, counter.get(character) + 1);
}
}

char highest = 'a';
int highest_count = 0;
for(Character character: counter.keySet()) {
if(counter.get(character) > highest_count ) {
if(attemptedCharacters.indexOf(character) == -1 ) {
highest = character;
highest_count = counter.get(character);
}
}
}
return highest;
}
}
64 changes: 61 additions & 3 deletions src/test/java/com/hangman/players/YourPlayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,73 @@

import org.junit.Test;
import java.util.Arrays;
import java.util.List;

import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class YourPlayerTest {

@Test
public void testCanGetWordsThatMatchLengthSpecified() throws Exception {
String[] words = {"aboard", "accident"};
List<String> matches = YourPlayer.getWordMatchingLength(words, 6);
assertTrue(matches.contains("aboard"));
assertFalse(matches.contains("accident"));
}

@Test
public void testCanExcludeWordsThatContainMissedChar() throws Exception {
String[] words = {"aboard", "actual", "active"};
List<String> matches = YourPlayer.getWordsWithoutChars(words, 'b');
assertTrue(matches.contains("actual"));
assertFalse(matches.contains("aboard"));
}

@Test
public void testGivenAPartialMatchedStringFilterOutWordsThatDoNotQualify() throws Exception {
String[] words = {"aboard", "actual", "active"};
List<String> matches = YourPlayer.getWordMatchingPattern(words, Arrays.asList('_', 'c', '_', '_', '_', '_'));

assertTrue(matches.contains("actual"));
assertFalse(matches.contains("aboard"));
}

@Test
public void AlwaysGuessA() {
public void testLastGuessedCharIsReturned() throws Exception {
YourPlayer player = new YourPlayer();
char first = player.GetGuess(Arrays.asList('e', 'b', 'c'));
assertEquals(first, player.getLastCharGuessed());
}

@Test
public void testCanGetCorrectLastGuessResult() throws Exception {
YourPlayer player = new YourPlayer();
char first = player.GetGuess(Arrays.asList('_', '_', '_'));
char second = player.GetGuess(Arrays.asList('_', 'e', '_'));
assertEquals(YourPlayer.RIGHT_GUESS, player.getLastAttemptResult());
char third = player.GetGuess(Arrays.asList('_', 'e', '_'));
assertEquals(YourPlayer.WRONG_GUESS, player.getLastAttemptResult());
}

@Test
public void testCanCheckIfWeHaveMadeAtLeastOneRightGuess() throws Exception {
YourPlayer player = new YourPlayer();
char first = player.GetGuess(Arrays.asList('_', '_', '_'));
assertFalse(player.hasOneRightGuess());
char second = player.GetGuess(Arrays.asList('_', '_', '_'));
assertFalse(player.hasOneRightGuess());
char third = player.GetGuess(Arrays.asList('_', 'e', '_'));
assertTrue(player.hasOneRightGuess());
}

@Test
public void testCanGetTheMostFrequentAppearingCharInWordList() throws Exception {

char guess = player.GetGuess(Arrays.asList('a', 'b', 'c'));
String[] words = {"aboard", "actual", "active"};

assertEquals('a', guess);
char mostFrequent = YourPlayer.getMostFrequentChar(words, Arrays.asList('f', 'u', 't'));
assertEquals('a', mostFrequent);
}
}