mirror of
https://github.com/fooflington/wordbubbles.git
synced 2025-04-04 18:19:08 +00:00
working!
This commit is contained in:
parent
5dced84c5c
commit
20bbb6c47e
@ -5,7 +5,7 @@ import uk.org.mafoo.wordbubbles.*;
|
||||
class main {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
WordList words = WordList.loadWordListFromFile(args[0]);
|
||||
Lexicon words = Lexicon.loadLexiconFromFile(args[0]);
|
||||
System.out.println(words);
|
||||
// String[] testwords = new String[]{"test", "amit", "ppp", "zebra"};
|
||||
// for (String w : testwords) {
|
||||
@ -42,10 +42,10 @@ class main {
|
||||
Prison prison = new Prison(grid);
|
||||
System.out.println(prison);
|
||||
|
||||
ArrayList<String> found = prison.search(words);
|
||||
for ( String w : found ) {
|
||||
ArrayList<LinkedHashSet<Cell>> found = prison.search(words);
|
||||
for ( LinkedHashSet<Cell> w : found ) {
|
||||
if(hunted != null) {
|
||||
if( hunted.containsKey(new Integer(w.length())) ) {
|
||||
if( hunted.containsKey(new Integer(w.size())) ) {
|
||||
System.out.println(w);
|
||||
}
|
||||
} else {
|
||||
|
@ -2,21 +2,24 @@ package uk.org.mafoo.wordbubbles;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class Cell {
|
||||
public class Cell {
|
||||
|
||||
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) {
|
||||
if(c >= 'a' && c <= 'z') {
|
||||
inmate = c;
|
||||
x = in_x;
|
||||
y = in_y;
|
||||
} else
|
||||
throw new RuntimeException("Unable to create a cell with no inmate");
|
||||
} else throw new RuntimeException("Unable to create a cell with no inmate");
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "" + inmate;
|
||||
}
|
||||
|
||||
public String toStringVerbose() {
|
||||
return "" + inmate + "@" + x + "," + y;
|
||||
}
|
||||
}
|
@ -56,8 +56,8 @@ public class Prison {
|
||||
return cells.get(x).get(y);
|
||||
}
|
||||
|
||||
public ArrayList<String> search(WordList words) {
|
||||
ArrayList<String> found = new ArrayList<String>();
|
||||
public ArrayList<LinkedHashSet<Cell>> search(Lexicon words) {
|
||||
ArrayList<LinkedHashSet<Cell>> found = new ArrayList<LinkedHashSet<Cell>>();
|
||||
// System.out.println("Started hunting");
|
||||
|
||||
for (int i=0; i<cells.size(); i++) {
|
||||
@ -65,7 +65,7 @@ public class Prison {
|
||||
if(cells.get(i).get(j) != null) {
|
||||
try {
|
||||
// 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) {
|
||||
|
||||
}
|
||||
@ -75,28 +75,36 @@ public class Prison {
|
||||
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);
|
||||
|
||||
embryo += c.inmate;
|
||||
visited.add(c);
|
||||
|
||||
if(words.checkWord(embryo)) {
|
||||
found.add(embryo);
|
||||
found.add(new LinkedHashSet<Cell>(visited));
|
||||
// System.out.println("Found a word: " + embryo);
|
||||
}
|
||||
|
||||
if(words.checkPrefix(embryo)) {
|
||||
// Then it's worth carrying on
|
||||
// System.out.println("Found valid embryo: " + embryo);
|
||||
visited.put(c, true);
|
||||
|
||||
for (Cell next : getNeighbours(c)) {
|
||||
if(visited.containsKey(next)) {
|
||||
if(visited.contains(next)) {
|
||||
continue;
|
||||
} else {
|
||||
rabbithole(next, visited, embryo, words, found);
|
||||
}
|
||||
}
|
||||
visited.remove(c);
|
||||
}
|
||||
|
||||
visited.remove(c);
|
||||
}
|
||||
}
|
@ -1,52 +1,14 @@
|
||||
package uk.org.mafoo.wordbubbles;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class WordList {
|
||||
ArrayList<LinkedHashSet<Cell>> list = new ArrayList<LinkedHashSet<Cell>>();
|
||||
|
||||
HashMap<String, Object> words = new HashMap<String, Object>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
WordList() {
|
||||
}
|
||||
|
||||
public boolean checkWord(String word) {
|
||||
// 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";
|
||||
public boolean add(E e) {
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user