mirror of
				https://github.com/fooflington/wordbubbles.git
				synced 2025-11-03 14:29:02 +00:00 
			
		
		
		
	working!
This commit is contained in:
		@@ -5,7 +5,7 @@ import uk.org.mafoo.wordbubbles.*;
 | 
				
			|||||||
class main {
 | 
					class main {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public static void main(String[] args) throws IOException {
 | 
						public static void main(String[] args) throws IOException {
 | 
				
			||||||
		WordList words = WordList.loadWordListFromFile(args[0]);
 | 
							Lexicon words = Lexicon.loadLexiconFromFile(args[0]);
 | 
				
			||||||
		System.out.println(words);
 | 
							System.out.println(words);
 | 
				
			||||||
		// String[] testwords = new String[]{"test", "amit", "ppp", "zebra"};
 | 
							// String[] testwords = new String[]{"test", "amit", "ppp", "zebra"};
 | 
				
			||||||
		// for (String w : testwords) {
 | 
							// for (String w : testwords) {
 | 
				
			||||||
@@ -42,10 +42,10 @@ class main {
 | 
				
			|||||||
		Prison prison = new Prison(grid);
 | 
							Prison prison = new Prison(grid);
 | 
				
			||||||
		System.out.println(prison);
 | 
							System.out.println(prison);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		ArrayList<String> found = prison.search(words);
 | 
							ArrayList<LinkedHashSet<Cell>> found = prison.search(words);
 | 
				
			||||||
		for ( String w : found ) {
 | 
							for ( LinkedHashSet<Cell> w : found ) {
 | 
				
			||||||
			if(hunted != null) {
 | 
								if(hunted != null) {
 | 
				
			||||||
				if( hunted.containsKey(new Integer(w.length())) ) {
 | 
									if( hunted.containsKey(new Integer(w.size())) ) {
 | 
				
			||||||
					System.out.println(w);
 | 
										System.out.println(w);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,21 +2,24 @@ package uk.org.mafoo.wordbubbles;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.util.*;
 | 
					import java.util.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Cell {
 | 
					public class Cell {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	protected final char inmate;
 | 
						protected final char inmate;
 | 
				
			||||||
	protected final int x, y;
 | 
						protected int x = -1;
 | 
				
			||||||
 | 
						protected int y = -1;
 | 
				
			||||||
	protected Cell (final char c, final int in_x, final int in_y) {
 | 
						protected Cell (final char c, final int in_x, final int in_y) {
 | 
				
			||||||
		if(c >= 'a' && c <= 'z') {
 | 
							if(c >= 'a' && c <= 'z') {
 | 
				
			||||||
			inmate = c;
 | 
								inmate = c;
 | 
				
			||||||
			x = in_x;
 | 
								x = in_x;
 | 
				
			||||||
			y = in_y;
 | 
								y = in_y;
 | 
				
			||||||
		} else 
 | 
							} else throw new RuntimeException("Unable to create a cell with no inmate");
 | 
				
			||||||
			throw new RuntimeException("Unable to create a cell with no inmate");
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public String toString() {
 | 
						public String toString() {
 | 
				
			||||||
		return "" + inmate;
 | 
							return "" + inmate;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public String toStringVerbose() {
 | 
				
			||||||
 | 
							return "" + inmate + "@" + x + "," + y;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -56,8 +56,8 @@ public class Prison {
 | 
				
			|||||||
		return cells.get(x).get(y);
 | 
							return cells.get(x).get(y);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public ArrayList<String> search(WordList words) {
 | 
						public ArrayList<LinkedHashSet<Cell>> search(Lexicon words) {
 | 
				
			||||||
		ArrayList<String> found = new ArrayList<String>();
 | 
							ArrayList<LinkedHashSet<Cell>> found = new ArrayList<LinkedHashSet<Cell>>();
 | 
				
			||||||
		// System.out.println("Started hunting");
 | 
							// System.out.println("Started hunting");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for (int i=0; i<cells.size(); i++) {
 | 
							for (int i=0; i<cells.size(); i++) {
 | 
				
			||||||
@@ -65,7 +65,7 @@ public class Prison {
 | 
				
			|||||||
				if(cells.get(i).get(j) != null) {
 | 
									if(cells.get(i).get(j) != null) {
 | 
				
			||||||
					try {
 | 
										try {
 | 
				
			||||||
						// System.out.println("Starting down a new rabbit hole at " + i + "," + j);
 | 
											// System.out.println("Starting down a new rabbit hole at " + i + "," + j);
 | 
				
			||||||
						rabbithole(cells.get(i).get(j), new HashMap<Cell, Object>(), "", words, found);
 | 
											rabbithole(cells.get(i).get(j), new LinkedHashSet<Cell>(), "", words, found);
 | 
				
			||||||
					} catch (ImpossibleException e) {
 | 
										} catch (ImpossibleException e) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
@@ -75,28 +75,36 @@ public class Prison {
 | 
				
			|||||||
		return found;
 | 
							return found;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private void rabbithole(Cell c, HashMap<Cell, Object> visited, String embryo, WordList words, ArrayList<String> found) throws ImpossibleException {
 | 
						private void rabbithole(
 | 
				
			||||||
 | 
							Cell c, 
 | 
				
			||||||
 | 
							LinkedHashSet<Cell> visited, 
 | 
				
			||||||
 | 
							String embryo, 
 | 
				
			||||||
 | 
							Lexicon words, 
 | 
				
			||||||
 | 
							ArrayList<LinkedHashSet<Cell>> found
 | 
				
			||||||
 | 
							) throws ImpossibleException {
 | 
				
			||||||
		// System.out.println("At " + c.x + "," + c.y);
 | 
							// System.out.println("At " + c.x + "," + c.y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		embryo += c.inmate;
 | 
							embryo += c.inmate;
 | 
				
			||||||
 | 
							visited.add(c);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(words.checkWord(embryo)) {
 | 
							if(words.checkWord(embryo)) {
 | 
				
			||||||
			found.add(embryo);
 | 
								found.add(new LinkedHashSet<Cell>(visited));
 | 
				
			||||||
			// System.out.println("Found a word: " + embryo);
 | 
								// System.out.println("Found a word: " + embryo);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(words.checkPrefix(embryo)) {
 | 
							if(words.checkPrefix(embryo)) {
 | 
				
			||||||
			// Then it's worth carrying on
 | 
								// Then it's worth carrying on
 | 
				
			||||||
			// System.out.println("Found valid embryo: " + embryo);
 | 
								// System.out.println("Found valid embryo: " + embryo);
 | 
				
			||||||
			visited.put(c, true);
 | 
								
 | 
				
			||||||
			for (Cell next : getNeighbours(c)) {
 | 
								for (Cell next : getNeighbours(c)) {
 | 
				
			||||||
				if(visited.containsKey(next)) {
 | 
									if(visited.contains(next)) {
 | 
				
			||||||
					continue;
 | 
										continue;
 | 
				
			||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					rabbithole(next, visited, embryo, words, found);
 | 
										rabbithole(next, visited, embryo, words, found);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		visited.remove(c);
 | 
							visited.remove(c);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,52 +1,14 @@
 | 
				
			|||||||
package uk.org.mafoo.wordbubbles;
 | 
					package uk.org.mafoo.wordbubbles;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.*;
 | 
					import java.util.*;
 | 
				
			||||||
import java.io.*;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class WordList {
 | 
					public class WordList {
 | 
				
			||||||
 | 
						ArrayList<LinkedHashSet<Cell>> list = new ArrayList<LinkedHashSet<Cell>>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	HashMap<String, Object> words = new HashMap<String, Object>();
 | 
						WordList() {
 | 
				
			||||||
	HashMap<String, Object> prefixes = new HashMap<String, Object>();
 | 
					 | 
				
			||||||
	static final int maxwordlength = 9;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public WordList (ArrayList<String> wordlist) {
 | 
					 | 
				
			||||||
		for (String word : wordlist) {
 | 
					 | 
				
			||||||
			if(word.length() > maxwordlength) continue;
 | 
					 | 
				
			||||||
			word = word.toLowerCase();
 | 
					 | 
				
			||||||
			words.put(word, true);
 | 
					 | 
				
			||||||
			char[] word_a = word.toCharArray();
 | 
					 | 
				
			||||||
			String prefix = "";
 | 
					 | 
				
			||||||
			for(int i=0; i<maxwordlength && i<word_a.length; i++) {
 | 
					 | 
				
			||||||
				prefix += word_a[i];
 | 
					 | 
				
			||||||
				prefixes.put(prefix, true);
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public boolean checkWord(String word) {
 | 
						public boolean add(E e) {
 | 
				
			||||||
		// System.out.println("Checking word: " + word + " -> " + words.containsKey(word));
 | 
					 | 
				
			||||||
		return words.containsKey(word);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
	public boolean checkPrefix(String prefix) {
 | 
					 | 
				
			||||||
		// System.out.println("Checking prefix: " + prefix + " -> " + prefixes.containsKey(prefix));
 | 
					 | 
				
			||||||
		return prefixes.containsKey(prefix);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public static WordList loadWordListFromFile(String file) throws FileNotFoundException, IOException {
 | 
					 | 
				
			||||||
		FileReader fr = new FileReader(file); 
 | 
					 | 
				
			||||||
		BufferedReader br = new BufferedReader(fr); 
 | 
					 | 
				
			||||||
		String s; 
 | 
					 | 
				
			||||||
		ArrayList<String> strings = new ArrayList<String>();
 | 
					 | 
				
			||||||
		while((s = br.readLine()) != null) { 
 | 
					 | 
				
			||||||
			if(s.length() <= maxwordlength)
 | 
					 | 
				
			||||||
				strings.add(s);
 | 
					 | 
				
			||||||
		} 
 | 
					 | 
				
			||||||
		fr.close(); 
 | 
					 | 
				
			||||||
		return new WordList(strings);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public String toString() {
 | 
					 | 
				
			||||||
		return "WordList object with " + words.size() + " words and " + prefixes.size() + " word prefixes";
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user