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

almost there

parent 0ab922b9
No related branches found
No related tags found
No related merge requests found
No preview for this file type
File added
......@@ -4,19 +4,15 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.*;
public class EvilHangmanGame implements IEvilHangmanGame {
SortedSet<Character> guessedLetters;
HashSet<String> dictionaryWords = new HashSet<String>();
private SortedSet<Character> guessedLetters = new TreeSet<Character>();
private SortedSet<String> words = new TreeSet<String>();
@Override
public void startGame(File dictionary, int wordLength) throws IOException, EmptyDictionaryException {
if (dictionary.length() == 0 || wordLength == 0) throw new EmptyDictionaryException("File is empty");
//Setup for loading dictionary
FileReader fr = new FileReader(dictionary);
BufferedReader br = new BufferedReader(fr);
......@@ -25,17 +21,78 @@ public class EvilHangmanGame implements IEvilHangmanGame {
//Add word if it matches the wordLength
while(dictionaryScanner.hasNext()) {
String word = dictionaryScanner.next();
if (word.length() == wordLength) dictionaryWords.add(word);
if (word.length() == wordLength) words.add(word);
}
if (dictionaryWords.size() == 0) throw new EmptyDictionaryException("File is empty");
if (words.size() == 0) throw new EmptyDictionaryException("File is empty");
}
@Override
public Set<String> makeGuess(char guess) throws GuessAlreadyMadeException {
if(guessedLetters.contains(guess)) throw new GuessAlreadyMadeException("Something");
guessedLetters.add(guess);
return null;
//Check if letter has been used
Character lowerGuess = Character.toLowerCase(guess);
if(!guessedLetters.add(lowerGuess)) {
throw new GuessAlreadyMadeException("Letter already used");
}
HashMap<Integer, SortedSet<String>> partition = new HashMap<Integer, SortedSet<String>>();
for (String word : words) {
int count = 0;
int hash = 1;
for (int i = 0; i < word.length(); i++){
if(guess == word.charAt(i)) {
hash *= (word.length() - i);
count++;
}
}
hash *= count;
if(!partition.containsKey(hash)) partition.put(
hash, new TreeSet<>()
);
partition.get(hash).add(word);
}
int maxSize = 0;
int finalKey = 0;
for (Map.Entry<Integer, SortedSet<String>> entry: partition.entrySet()){
int currSize = entry.getValue().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) {
maxSize = currSize;
finalKey = entry.getKey();
}
}
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;
}
@Override
......
package hangman;
import java.util.SortedSet;
import java.util.TreeSet;
public class WordGroup {
private int letterCount;
private SortedSet<String> words = null;
private SortedSet<Integer> indexList = null;
public WordGroup(int letterCount, SortedSet<String> words, SortedSet<Integer> indexList) {
this.letterCount = letterCount;
this.words = words;
this.indexList = indexList;
}
public int getLetterCount() {
return letterCount;
}
public SortedSet<String> getWords() {
return words;
}
public SortedSet<Integer> getIndexList() {
return indexList;
}
public int getSize(){
return words.size();
}
@Override
public String toString() {
return "WordGroup{\n" +
"\tletterCount=" + letterCount +
",\n\twords=" + words.toString() +
",\n\tindexList=" + indexList.toString() +
"\n}";
}
}
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