Newsgroups: comp.ai.games
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!spool.mu.edu!torn!watserv2.uwaterloo.ca!undergrad.math.uwaterloo.ca!eadengle
From: eadengle@watcgl.uwaterloo.ca (Ed "Cynwrig" Dengler)
Subject: Re: Boggle
Message-ID: <D7AK77.6v1@undergrad.math.uwaterloo.ca>
Sender: news@undergrad.math.uwaterloo.ca (news spool owner)
Nntp-Posting-Host: cgl.uwaterloo.ca
Organization: University of Waterloo
References: <3n1vsr$l6p@mudraker.mtholyoke.edu>
Date: Wed, 19 Apr 1995 16:52:18 GMT
Lines: 73

Greetings!

In article <3n1vsr$l6p@mudraker.mtholyoke.edu>,
 <rsznicer@mtholyoke.edu> wrote:
>Hiya,
>
>   Does anyone know of, or can anyone come up with an algorithm to find 
>solutions (words) in the game boggle? Basically, A Boggle program would 
>generate random (I think) letters on a grid. Like:
>
>ABCR
>WFAL
>NXTD
>FISH
>
>Fish, Cat, and Cats are three visable solutions... Just explaining 
>because I'm sure this game has been given many different ones since it 
>has been invented. Anyway, what do ya think?
>
>Ryan

This shouldn't be too hard.  My first approximation of an algorithm would
be as follows:

        Generate a dictionary of words using some form of tree structure
        where the next letter can be the possible continuation of at least
        one word.  Mark a complete word using a single bit so that you
        know when a valid word is found.  For example:

               *-c-a-P-a-b-l-E
                    -T-a-p-u-l-T
                      -f-i-s-H
                      -S
                -f-i-s-H-e-S

        Here, the capitals represent a valid ending of a word.  In the tree
        structure, only unique letters can continue a possible word.

        Next, use the following algorithm:

        for <each position in the box>
            let <x,y> be <the position>
            let <entry> be <the dictionary entry starting with this letter>
            call find(x,y,entry)
        endfor

        proc find(x,y,entry)
            <mark letter at x,y as used>
            if <current entry is a capital letter>
            and <the current word is not on the list of words>
                <add current word to the list of words>
            endif
            for <each direction>
                if <x,y + direction is still on the board>
                and <the new letter is unmarked>
                and <there is a continuation using the new letter>
                    let <x',y'> be <x,y + direction>
                    let <entry'> be <the new dictionary entry>
                    call find(x',y',entry')
                endif
            endfor
            <unmark letter at x,y>
        endproc

This should generate all valid words available in the dictionary.  I am
assuming that Boggle does not allow a letter to be used more than once.
If it does, simply remove the marking and unmarking code.

There might be more efficient means of doing this, but this is just off
the top of my head.

Ed

