diff --git a/EvilHangman/out/production/EvilHangman/hangman/EvilHangmanGame.class b/EvilHangman/out/production/EvilHangman/hangman/EvilHangmanGame.class index 5525408dcad0c2320c7ec95c2f7b692b95e9db1c..5bd98c55c023c7875603510940561d94a3ceb8f0 100644 Binary files a/EvilHangman/out/production/EvilHangman/hangman/EvilHangmanGame.class and b/EvilHangman/out/production/EvilHangman/hangman/EvilHangmanGame.class differ diff --git a/EvilHangman/src/hangman/EvilHangmanGame.java b/EvilHangman/src/hangman/EvilHangmanGame.java index b018e201ec748f1960efc54c65e3ed295a556c09..5861ca3c0f489319b70a9a20fdc3477d621c2ecd 100644 --- a/EvilHangman/src/hangman/EvilHangmanGame.java +++ b/EvilHangman/src/hangman/EvilHangmanGame.java @@ -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