commit 39973a334a3534a9e27f8b4d69b555ccec1ea59d Author: Matthew Slowe Date: Sun Nov 15 06:53:54 2015 +0000 working import diff --git a/main.java b/main.java new file mode 100644 index 0000000..2ab2e9a --- /dev/null +++ b/main.java @@ -0,0 +1,42 @@ +import java.io.*; +import java.util.*; +import uk.org.mafoo.wordbubbles.*; + +class main { + + public static void main(String[] args) throws IOException { + WordList words = WordList.loadWordListFromFile(args[0]); + System.out.println(words); + // String[] testwords = new String[]{"test", "amit", "ppp", "zebra"}; + // for (String w : testwords) { + // System.out.println(w + "? " + words.checkWord(w) + " / " + words.checkPrefix(w)); + // } + + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + String s; + ArrayList> grid = new ArrayList>(); + + while ((s = in.readLine()) != null && s.length() != 0) { + if(s.startsWith("#")) continue; + ArrayList row = new ArrayList(); + for ( char c : s.toLowerCase().toCharArray() ) { + row.add(new Character(c)); + } + grid.add(row); + } + + // for ( ArrayList row : grid ) { + // for (Character c : row) { + // System.out.print((c != null ? c : " ") + " "); + // } + // System.out.println("."); + // } + Prison prison = new Prison(grid); + System.out.println(prison); + + System.out.println(prison.search(words)); + + // System.out.println(prison.getNeighbours(prison.getCell(0,2))); + } + +} \ No newline at end of file diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..1006baa --- /dev/null +++ b/test.txt @@ -0,0 +1,6 @@ +# 5 5 6 6 + skct +ileii +ctvnn +eahrg +hcu \ No newline at end of file diff --git a/test2.txt b/test2.txt new file mode 100644 index 0000000..3ea6509 --- /dev/null +++ b/test2.txt @@ -0,0 +1,4 @@ +test +ar +rt +n \ No newline at end of file diff --git a/uk/org/mafoo/wordbubbles/Cell.java b/uk/org/mafoo/wordbubbles/Cell.java new file mode 100644 index 0000000..5b0e900 --- /dev/null +++ b/uk/org/mafoo/wordbubbles/Cell.java @@ -0,0 +1,22 @@ +package uk.org.mafoo.wordbubbles; + +import java.util.*; + +class Cell { + + protected final char inmate; + protected final int x, y; + 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"); + } + + public String toString() { + return "" + inmate; + } + +} \ No newline at end of file diff --git a/uk/org/mafoo/wordbubbles/ImpossibleException.java b/uk/org/mafoo/wordbubbles/ImpossibleException.java new file mode 100644 index 0000000..e73718e --- /dev/null +++ b/uk/org/mafoo/wordbubbles/ImpossibleException.java @@ -0,0 +1,7 @@ +package uk.org.mafoo.wordbubbles; + +class ImpossibleException extends Exception { + protected ImpossibleException() { + + } +} \ No newline at end of file diff --git a/uk/org/mafoo/wordbubbles/Prison.java b/uk/org/mafoo/wordbubbles/Prison.java new file mode 100644 index 0000000..9c8209b --- /dev/null +++ b/uk/org/mafoo/wordbubbles/Prison.java @@ -0,0 +1,102 @@ +package uk.org.mafoo.wordbubbles; + +import java.util.*; + +public class Prison { + + private ArrayList> cells = new ArrayList>(); + + public Prison(ArrayList> grid) { + // System.out.println("Initialising Prison"); + + for (int i=0; i row = new ArrayList(); + for (int j=0; j getNeighbours(Cell cell) { + ArrayList neighbours = new ArrayList(); + for(int i=-1; i<=1; i++) { + for(int j=-1; j<=1; j++) { + if( !(i==0 && j==0) ) { + try { + Cell c = cells.get(cell.x + i).get(cell.y + j); + if (c != null) neighbours.add(c); + } catch (NullPointerException e) { + // Swallow nulls as cell doesn't exist + } catch (ArrayIndexOutOfBoundsException e) { + + } catch (IndexOutOfBoundsException e) { + + } + } + } + } + return neighbours; + } + + public String toString() { + String s = new String(); + for ( ArrayList row : cells ) { + for (Cell c : row) { + s += (c != null ? c : " "); + } + s += "\n"; + } + return s; + } + + public Cell getCell(int x, int y) { + return cells.get(x).get(y); + } + + public ArrayList search(WordList words) { + ArrayList found = new ArrayList(); + // System.out.println("Started hunting"); + + for (int i=0; i(), "", words, found); + } catch (ImpossibleException e) { + + } + } + } + } + return found; + } + + private void rabbithole(Cell c, HashMap visited, String embryo, WordList words, ArrayList found) throws ImpossibleException { + // System.out.println("At " + c.x + "," + c.y); + + embryo += c.inmate; + + if(words.checkWord(embryo)) { + found.add(embryo); + // 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)) { + continue; + } else { + rabbithole(next, visited, embryo, words, found); + } + } + visited.remove(c); + } + } +} \ No newline at end of file diff --git a/uk/org/mafoo/wordbubbles/WordList.java b/uk/org/mafoo/wordbubbles/WordList.java new file mode 100644 index 0000000..34bdaa8 --- /dev/null +++ b/uk/org/mafoo/wordbubbles/WordList.java @@ -0,0 +1,52 @@ +package uk.org.mafoo.wordbubbles; + +import java.util.*; +import java.io.*; + +public class WordList { + + HashMap words = new HashMap(); + HashMap prefixes = new HashMap(); + static final int maxwordlength = 9; + + public WordList (ArrayList 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 " + 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 strings = new ArrayList(); + 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"; + } +} \ No newline at end of file