Back in high school, I played A LOT of WordRacer (the Yahoo Games version of Boggle). After taking computer programming courses in college, it seemed fun to write a little program that would automatically solve those puzzles by finding every word. So here's a little python program that does exactly that.

Note that this was written A LONG time ago (maybe 2002?) and the code could be a lot cleaner . It requires an external dictionary text file (one word per line). Here goes:

import os, sys

# grab puzzle letters from STDIN
letters = sys.argv[1:]

# build a hash of words
# 't' - terminal (a word)
# 'n' - nonterminal
dict = {}
words = open('dict.txt','r').readlines()
for word in words:
    for i in range(len(word)-1):
        if i == len(word)-2 and len(word) > 3:
            dict[word[0:i+1]] = 't'
        else:
            if not dict.has_key(word[0:i+1]):
                dict[word[0:i+1]] = 'n'

# the solution
soln = {}

class Node:
    def __init__(self, let):
        self.l = let
        self.v = 0
        self.n = []
    def visit(self, word):
        self.v = 1
        if dict.has_key(word+self.l):
            if dict[word+self.l] == 't':
                soln[word+self.l] = word+self.l
            for n in self.n:
                if not n.v:
                    n.v = 1
                    n.visit(word+self.l)
                    n.v = 0
        self.v = 0

# build the puzzle as a list of nodes
nodes = []
for l in letters:
    nodes.append(Node(l))

# must have exactly 16 nodes
if len(nodes) != 16:
 print 'error determining letters - check screen'
 exit()

# link nodes into a 4x4 grid
#
#  0  1  2  3
#  4  5  6  7
#  8  9 10 11
# 12 13 14 15
#

nodes[0].n  = [nodes[1],nodes[4],nodes[5]]
nodes[1].n  = [nodes[0],nodes[2],nodes[4],nodes[5],nodes[6]]
nodes[2].n  = [nodes[1],nodes[3],nodes[5],nodes[6],nodes[7]]
nodes[3].n  = [nodes[2],nodes[6],nodes[7]]

nodes[4].n  = [nodes[0],nodes[1],nodes[5],nodes[8],nodes[9]]
nodes[5].n  = [nodes[0],nodes[1],nodes[2],nodes[4],nodes[6],nodes[8],nodes[9],nodes[10]]
nodes[6].n  = [nodes[1],nodes[2],nodes[3],nodes[5],nodes[7],nodes[9],nodes[10],nodes[11]]
nodes[7].n  = [nodes[2],nodes[3],nodes[6],nodes[10],nodes[11]]

nodes[8].n  = [nodes[4],nodes[5],nodes[9],nodes[12],nodes[13]]
nodes[9].n  = [nodes[4],nodes[5],nodes[6],nodes[8],nodes[10],nodes[12],nodes[13],nodes[14]]
nodes[10].n = [nodes[5],nodes[6],nodes[7],nodes[9],nodes[11],nodes[13],nodes[14],nodes[15]]
nodes[11].n = [nodes[6],nodes[7],nodes[10],nodes[14],nodes[15]]

nodes[12].n = [nodes[8],nodes[9],nodes[13]]
nodes[13].n = [nodes[8],nodes[9],nodes[10],nodes[12],nodes[14]]
nodes[14].n = [nodes[9],nodes[10],nodes[11],nodes[13],nodes[15]]
nodes[15].n = [nodes[10],nodes[11],nodes[14]]

# solve the puzzle
for n in nodes:
    n.visit('')

# compare two strings by length
def lensort(x,y):
    return cmp(len(x),len(y))

# print the solution in order of ascending length
soln2 = soln.keys()
soln2.sort(lensort)
for w in soln2:
    print w

Using it is pretty simple - just call the program from the command line with 16 letters as arguments, like so:

python wracer.py a b c d e f g h i j k l m n o p

That would correspond to a puzzle that looks like this:

A B C D
E F G H
I J K L
M N O P

And here's the solution:

nim
jin
pol
fie
ink
kop
lop
fin
mink
knop
jink
fink
fino
glop
plonk
knife

You don't have to stick to 4x4 puzzles. The solution algorithm is nice and generic and will work for any shape or size if you just link up the nodes differently.

It's a funny thing about programs like this. I got a lot more enjoyment out of writing the program than actually using it. It's fun to blow people out of the water with a huge score, but it gets boring pretty quick.