summaryrefslogtreecommitdiff
path: root/org/guru/boggle/Boggle.java
blob: b1dd13c525d2d4d679b6425eab90db8298330bde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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<Character> 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();
        }
    }
}