Hi all.
I am building a program to emulate a game 'Boggle', and have hit a dead end.
I have tried to search for hints online as to what I need to do from here, but everything I find is either just a block of code sparingly commented or doesn't help.
So far I have got it to find words in straight lines from any edge letter; i.e.:
Letters:
R V G E
N T W U
S D H I
V L H O
Found Words:
RVGE
RNSV
RTHO
NTWU
SDHI
VLHO
VSNR
VDWE
VTDL
LDTV
GWHH
HHWG
EUIO
EGVR
EWDV
UWTN
IHDS
OHLV
OIUE
OHTR
However, I need it to search from each letter in all directions as well.
Any hints on what I need to alter?
I stand by my signature, so no walls of code please :)
The search code:
static bool FindWord(string word = null) {
int width = 4; // Grid width
int height = 4; // Grid Height
int cX = 0; // x coordinate
int cY = 0; // y coordinate
int d = 0; // Search direction
for (cX = 0; cX < width; cX++) {
for (cY = 0; cY < height; cY++) {
for (d = 0; d < 8; d++) {
Search(cX, cY, d, "", width, height);
}
}
}
return true;
}
static string Search(int cX, int cY, int d, string word, int width, int height) {
string oneLetter;
// Stay within bounds
if (cY >= width || cY < 0 || cX >= height || cX < 0) {
return null;
}
oneLetter = dieFaces[cY, cX];
string newWord = word + oneLetter;
switch (d) {
case 0:
if(newWord.Length > 3) Console.WriteLine(newWord);
Search(cX + 1, cY, d, newWord, width, height);
break;
case 1:
if (newWord.Length > 3) Console.WriteLine(newWord);
Search(cX, cY + 1, d, newWord, width, height);
break;
case 2:
if (newWord.Length > 3) Console.WriteLine(newWord);
Search(cX - 1, cY, d, newWord, width, height);
break;
case 3:
if (newWord.Length > 3) Console.WriteLine(newWord);
Search(cX, cY - 1, d, newWord, width, height);
break;
case 4:
if (newWord.Length > 3) Console.WriteLine(newWord);
Search(cX + 1, cY + 1, d, newWord, width, height);
break;
case 5:
if (newWord.Length > 3) Console.WriteLine(newWord);
Search(cX - 1, cY + 1, d, newWord, width, height);
break;
case 6:
if (newWord.Length > 3) Console.WriteLine(newWord);
Search(cX - 1, cY - 1, d, newWord, width, height);
break;
case 7:
if (newWord.Length > 3) Console.WriteLine(newWord);
Search(cX + 1, cY -1, d, newWord, width, height);
break;
}
return newWord;
}
Hopefully someone will have a clue :)