Most of the time, this function (below) works to find a word in a Boggle type grid. The times it doesn't, is when the same letter from the searched word, is located more than once in the search, and the searched word isn't found following a false, dead-end, letter search.
How can I search each of the directions, until a match is found, then exit function, or all directions were searched and no match was found?
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
bool Check_Word(char grid[10][10], string Search_Word, int z, int j)
{
int Ck_Grid[10][10]={{0},{0}};
bool Found = false;
int go=1;
int zz = z, jj = j;
int len = Search_Word.length();
for (int x = 1;x < len; x++)
{
if(grid[zz-1][jj] == Search_Word[x] && Ck_Grid[zz-1][jj] == 0)
{
Ck_Grid[zz-1][jj] = 1;
go++;
zz--;
}
if(grid[zz+1][jj] == Search_Word[x] && Ck_Grid[zz+1][jj] == 0)
{
Ck_Grid[zz+1][jj] = 1;
go++;
zz++;
}
if(grid[zz][jj-1] == Search_Word[x] && Ck_Grid[zz][jj-1] == 0)
{
Ck_Grid[zz][jj-1] = 1;
go++;
jj--;
}
if(grid[zz][jj+1] == Search_Word[x] && Ck_Grid[zz][jj+1] == 0)
{
Ck_Grid[zz][jj+1] = 1;
go++;
jj++;
}
if(grid[zz-1][jj-1] == Search_Word[x] && Ck_Grid[zz-1][jj-1] == 0)
{
Ck_Grid[zz-1][jj-1] = 1;
go++;
zz--;
jj--;
}
if(grid[zz+1][jj+1] == Search_Word[x] && Ck_Grid[zz+1][jj+1] == 0)
{
Ck_Grid[zz+1][jj+1] = 1;
go++;
zz++;
jj++;
}
if(grid[zz+1][jj-1] == Search_Word[x] && Ck_Grid[zz+1][jj-1] == 0)
{
Ck_Grid[zz+1][jj-1] = 1;
go++;
zz++;
jj--;
}
if(grid[zz-1][jj+1] == Search_Word[x] && Ck_Grid[zz-1][jj+1] == 0)
{
Ck_Grid[zz-1][jj+1] = 1;
go++;
zz--;
jj++;
}
}
if(go==len)
Found = true;
return Found;
}
|
example:
1 2 3 4
|
S L D M
V C L O
J O O B
N M A C
|
(This program is being written with user urfa2 criteria, in a letter search can be diagonal)
If the searched for word is 'MOOD', then I could get a false result since the search may stop after 'MOOB', 'MOLD', 'MOOJ, etc. I'm not sure how to check if there is more than one word path, after finding a false word. Any ideas??