Envisaging Wyatt Lyon Preul

Those are my eyes staring at you. They allow me to see you clearly through this screen. No, don't click there, it will take you away from my online space.

Let me start again, I am Wyatt and I am a software engineer. I am also a process theologian, author, music hipster, struggling artist who enjoys thinking outside of your finitude. I am not as pretentious as that line makes me sound. If you have some spare time read more about me.

Boggle Solver Source

A A A

There has been interest in the simple algorithm I used for the boggle solver I blogged about previously.

  Here is the source for the code behind.  The dictionary file is just a list of words, delimited by a line feed.

protected List<string> _words;
protected List<string> _foundWords;

protected override void OnInit(EventArgs e)
{
this.Submit.Click += new EventHandler(Submit_Click);
base.OnInit(e);
}

void Submit_Click(object sender, EventArgs e)
{
LoadWords();
FoundWords.Text
= string.Empty;
if (_words != null)
{
_foundWords
= new List<string>();

char[,] wordMatrix = new char[5, 5];
wordMatrix[
0, 0] = Char.Parse(a1.Text);
wordMatrix[
0, 1] = Char.Parse(a2.Text);
wordMatrix[
0, 2] = Char.Parse(a3.Text);
wordMatrix[
0, 3] = Char.Parse(a4.Text);
wordMatrix[
0, 4] = Char.Parse(a5.Text);
wordMatrix[
1, 0] = Char.Parse(b1.Text);
wordMatrix[
1, 1] = Char.Parse(b2.Text);
wordMatrix[
1, 2] = Char.Parse(b3.Text);
wordMatrix[
1, 3] = Char.Parse(b4.Text);
wordMatrix[
1, 4] = Char.Parse(b5.Text);
wordMatrix[
2, 0] = Char.Parse(c1.Text);
wordMatrix[
2, 1] = Char.Parse(c2.Text);
wordMatrix[
2, 2] = Char.Parse(c3.Text);
wordMatrix[
2, 3] = Char.Parse(c4.Text);
wordMatrix[
2, 4] = Char.Parse(c5.Text);
wordMatrix[
3, 0] = Char.Parse(d1.Text);
wordMatrix[
3, 1] = Char.Parse(d2.Text);
wordMatrix[
3, 2] = Char.Parse(d3.Text);
wordMatrix[
3, 3] = Char.Parse(d4.Text);
wordMatrix[
3, 4] = Char.Parse(d5.Text);
wordMatrix[
4, 0] = Char.Parse(e1.Text);
wordMatrix[
4, 1] = Char.Parse(e2.Text);
wordMatrix[
4, 2] = Char.Parse(e3.Text);
wordMatrix[
4, 3] = Char.Parse(e4.Text);
wordMatrix[
4, 4] = Char.Parse(e5.Text);

a1.Text
= string.Empty;
a2.Text
= string.Empty;
a3.Text
= string.Empty;
a4.Text
= string.Empty;
a5.Text
= string.Empty;
b1.Text
= string.Empty;
b2.Text
= string.Empty;
b3.Text
= string.Empty;
b4.Text
= string.Empty;
b5.Text
= string.Empty;
c1.Text
= string.Empty;
c2.Text
= string.Empty;
c3.Text
= string.Empty;
c4.Text
= string.Empty;
c5.Text
= string.Empty;
d1.Text
= string.Empty;
d2.Text
= string.Empty;
d3.Text
= string.Empty;
d4.Text
= string.Empty;
d5.Text
= string.Empty;
e1.Text
= string.Empty;
e2.Text
= string.Empty;
e3.Text
= string.Empty;
e4.Text
= string.Empty;
e5.Text
= string.Empty;

foreach (string possibleWord in _words)
{
if (possibleWord == null || possibleWord.Length < 4)
continue;
char startChar = possibleWord[0];
for (int x = 0; x < 5; x++)
for (int y = 0; y < 5; y++)
if (wordMatrix[x, y] == startChar)
{
CheckWord(wordMatrix, x, y, possibleWord);
}
}

foreach (string foundWord in _foundWords)
{
if (string.IsNullOrEmpty(FoundWords.Text))
FoundWords.Text
= foundWord;
else
FoundWords.Text
= string.Format("{0}, {1}", FoundWords.Text, foundWord);
}
}
}

void CheckWord(char[,] wordMatrix, int x, int y, string possibleWord)
{
char[,] used = new char[5, 5];
used[x, y]
= possibleWord[0];
for (int i = 1; i < possibleWord.Length; i++)
{
if (x + 1 < 5 && used[x + 1, y] != possibleWord[i] && possibleWord[i] == wordMatrix[x + 1, y])
{
used[x
+ 1, y] = possibleWord[i];
x
= x + 1;
continue;
}
else if (x - 1 > -1 && used[x - 1, y] != possibleWord[i] && possibleWord[i] == wordMatrix[x - 1, y])
{
used[x
- 1, y] = possibleWord[i];
x
= x - 1;
continue;
}
else if (y + 1 < 5 && used[x, y + 1] != possibleWord[i] && possibleWord[i] == wordMatrix[x, y + 1])
{
used[x, y
+ 1] = possibleWord[i];
y
= y + 1;
continue;
}
else if (y - 1 > -1 && used[x, y - 1] != possibleWord[i] && possibleWord[i] == wordMatrix[x, y - 1])
{
used[x, y
- 1] = possibleWord[i];
y
= y - 1;
continue;
}
else if (x + 1 < 5 && y + 1 < 5 && used[x + 1, y + 1] != possibleWord[i] && possibleWord[i] == wordMatrix[x + 1, y + 1])
{
used[x
+ 1, y + 1] = possibleWord[i];
x
= x + 1;
y
= y + 1;
continue;
}
else if (x - 1 > -1 && y + 1 < 5 && used[x - 1, y + 1] != possibleWord[i] && possibleWord[i] == wordMatrix[x - 1, y + 1])
{
used[x
- 1, y + 1] = possibleWord[i];
x
= x - 1;
y
= y + 1;
continue;
}
else if (x + 1 < 5 && y - 1 > -1 && used[x + 1, y - 1] != possibleWord[i] && possibleWord[i] == wordMatrix[x + 1, y - 1])
{
used[x
+ 1, y - 1] = possibleWord[i];
x
= x + 1;
y
= y - 1;
continue;
}
else if (x - 1 > -1 && y - 1 > -1 && used[x - 1, y - 1] != possibleWord[i] && possibleWord[i] == wordMatrix[x - 1, y - 1])
{
used[x
- 1, y - 1] = possibleWord[i];
x
= x - 1;
y
= y - 1;
continue;
}
else
return;
}

_foundWords.Add(possibleWord);
}

void LoadWords()
{
if (File.Exists(Server.MapPath("dictionary.txt")))
using (FileStream fs = File.OpenRead(Server.MapPath("dictionary.txt")))
{
byte[] b = new byte[1024];
UTF8Encoding temp
= new UTF8Encoding(true);
_words
= new List<string>();

while (fs.Read(b, 0, b.Length) > 0)
{
foreach (string word in temp.GetString(b).Split('\n'))
{
string updatedWord = word.Replace("\r", "");
_words.Add(updatedWord);
}
}
}
}

Tagged as: