working import

This commit is contained in:
Matthew Slowe 2015-11-15 06:53:54 +00:00
commit 39973a334a
7 changed files with 235 additions and 0 deletions

42
main.java Normal file

@ -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<ArrayList<Character>> grid = new ArrayList<ArrayList<Character>>();
while ((s = in.readLine()) != null && s.length() != 0) {
if(s.startsWith("#")) continue;
ArrayList<Character> row = new ArrayList<Character>();
for ( char c : s.toLowerCase().toCharArray() ) {
row.add(new Character(c));
}
grid.add(row);
}
// for ( ArrayList<Character> 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)));
}
}

6
test.txt Normal file

@ -0,0 +1,6 @@
# 5 5 6 6
skct
ileii
ctvnn
eahrg
hcu

4
test2.txt Normal file

@ -0,0 +1,4 @@
test
ar
rt
n

@ -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;
}
}

@ -0,0 +1,7 @@
package uk.org.mafoo.wordbubbles;
class ImpossibleException extends Exception {
protected ImpossibleException() {
}
}

@ -0,0 +1,102 @@
package uk.org.mafoo.wordbubbles;
import java.util.*;
public class Prison {
private ArrayList<ArrayList<Cell>> cells = new ArrayList<ArrayList<Cell>>();
public Prison(ArrayList<ArrayList<Character>> grid) {
// System.out.println("Initialising Prison");
for (int i=0; i<grid.size(); i++) {
ArrayList<Cell> row = new ArrayList<Cell>();
for (int j=0; j<grid.get(i).size(); j++) {
// System.out.println("[" + i + "][" + j + "]");
Character c = grid.get(i).get(j);
row.add((c.charValue() != ' ' ? new Cell(c, i, j) : null));
}
cells.add(row);
}
}
public ArrayList<Cell> getNeighbours(Cell cell) {
ArrayList<Cell> neighbours = new ArrayList<Cell>();
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<Cell> 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<String> search(WordList words) {
ArrayList<String> found = new ArrayList<String>();
// System.out.println("Started hunting");
for (int i=0; i<cells.size(); i++) {
for (int j=0; j<cells.get(i).size(); j++) {
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);
} catch (ImpossibleException e) {
}
}
}
}
return found;
}
private void rabbithole(Cell c, HashMap<Cell, Object> visited, String embryo, WordList words, ArrayList<String> 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);
}
}
}

@ -0,0 +1,52 @@
package uk.org.mafoo.wordbubbles;
import java.util.*;
import java.io.*;
public class WordList {
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);
}
}
}
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";
}
}