From e3a192fd3a5b535e246deba540164e69cfdfa689 Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Thu, 2 Jun 2016 11:43:53 -0700 Subject: Initial boggle code checkin. --- org/guru/boggle/Boggle.java | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 org/guru/boggle/Boggle.java (limited to 'org/guru/boggle/Boggle.java') diff --git a/org/guru/boggle/Boggle.java b/org/guru/boggle/Boggle.java new file mode 100644 index 0000000..b1dd13c --- /dev/null +++ b/org/guru/boggle/Boggle.java @@ -0,0 +1,52 @@ +package org.guru.boggle; + +import java.io.IOException; +import java.util.Set; + +public class Boggle implements Runnable { + + private final SimpleBoard board; + private final DictionaryTrie dict; + + public Boggle() throws IOException { + this.dict = new DictionaryTrie("/Users/scott/words"); + this.board = new SimpleBoard(); + } + + @Override + public void run() { + if (dict.containsWord("BUDDG")) { + System.out.println("wtf?"); + } + + for (BoardPosition p : board.allPositions()) { + System.out.println("Starting position " + p); + recurse(p, ""); + } + } + + public void recurse(BoardPosition p, String soFar) { + char letter = board.letterAt(p); + board.markUsed(p); + soFar = soFar + letter; + if (dict.containsWord(soFar)) { + System.out.println(soFar); + } + Set possibleSuccessors = dict.possibleSuccessorLetters(soFar); + for (BoardPosition q : board.adjacentUnusedNeighborPosition(p)) { + char potentialSuccessor = board.letterAt(q); + if (possibleSuccessors.contains(potentialSuccessor)) { + recurse(q, soFar); + } + } + board.clearUsed(p); + } + + public static void main(String args[]) { + try { + new Boggle().run(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} -- cgit v1.3