Skip to content
Snippets Groups Projects
Commit e7ae16a0 authored by Josue Lopez's avatar Josue Lopez
Browse files

Passed all tests

parent 0fb5ebe4
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -8,10 +8,11 @@ import java.util.*;
public class EvilHangmanGame implements IEvilHangmanGame {
private SortedSet<Character> guessedLetters = new TreeSet<Character>();
private SortedSet<String> words = new TreeSet<String>();
private SortedSet<String> words = null;
@Override
public void startGame(File dictionary, int wordLength) throws IOException, EmptyDictionaryException {
words = new TreeSet<String>();
if (dictionary.length() == 0 || wordLength == 0) throw new EmptyDictionaryException("File is empty");
//Setup for loading dictionary
FileReader fr = new FileReader(dictionary);
......@@ -37,6 +38,16 @@ public class EvilHangmanGame implements IEvilHangmanGame {
HashMap<Integer, SortedSet<String>> partition = new HashMap<Integer, SortedSet<String>>();
groupWords(partition, guess);
int finalKey = getGroupKey(partition);
words = partition.get(finalKey);
return words;
}
private void groupWords(HashMap<Integer, SortedSet<String>> partition, char guess){
for (String word : words) {
int count = 0;
int hash = 1;
......@@ -52,47 +63,22 @@ public class EvilHangmanGame implements IEvilHangmanGame {
);
partition.get(hash).add(word);
}
}
private int getGroupKey(HashMap<Integer, SortedSet<String>> partition){
int maxSize = 0;
int finalKey = 0;
for (Map.Entry<Integer, SortedSet<String>> entry: partition.entrySet()){
int currSize = entry.getValue().size();
SortedSet<String> currGroup = entry.getValue();
int currSize = currGroup.size();
int currKey = entry.getKey();
if(currSize == maxSize){
// 1. Choose the group in which the letter does not appear at all.
if(currKey < finalKey) finalKey = currKey;
// 2. If each group has the guessed letter, choose the one with the
// fewest letters.
// 3. If this still has not resolved the issue, choose the one with the
// rightmost guessed letter.
// 4. If there is still more than one group, choose the one with the next
// rightmost letter. Repeat this step (step 4) until a group is
// chosen.
}
if(currSize == maxSize) if (currKey < finalKey) finalKey = currKey;
if(currSize > maxSize) {
maxSize = currSize;
finalKey = entry.getKey();
finalKey = currKey;
}
}
words = partition.get(finalKey);
//Add letter to the set
return words;
}
static void print(String out){
System.out.println(out);
}
int charCount(String word, char guess){
int count = 0;
for (int i = 0; i < word.length(); i++){
if (guess == word.charAt(i)) count++;
}
return count;
return finalKey;
}
@Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment