Here's my initial schema. I had to do this to think things through myself.
Code:
Direction Table coordinate changes from index char
N = 0 0, -1
NE = 1 1, -1 N
E = 2 1, 0 W-|-E
SE = 3 1, 1 S
S = 4 0, 1
SW = 5 -1, 0
W = 6 -1, 0
NW = 7 -1, -1
dx , dy
x increases left to right
y increases going down
when I think of the size of the grid I think of the total number of squares, so the number of rows would be sqrt(size) as would the number of columns, given that the grid is always a square. The grid could then be declared using dynamic memory and filled from data in the file or whatever.
My search would then start with something like this:
for(x = 0; x < rows; ++x)
for(y = 0; y < cols; ++y)
if grid[x][y] == str[0] //first letter matches grid char
//screen all 8 directions for str
if(finWords(str, grid, size, x, y, z))
//output x and y coord and direction.
else
//indicate str not found
//terminate program or restart search for another word or let run til end looking for other copies of current word
with finWords() something like this:
bool finWords(char * str, char ** grid, int size, int& loc_x, int& loc_y, int & loc_direct)
{
for(z = 0; z < 8; ++z)
{
switch(loc_direct)
{
case 0;
dx = 0;
dy = -1;
break;
//repeat for cases 1-7
}
if(checkFurther(str, grid, loc_x, loc_y, dx, dy))
return true;
}
//if no case returned true
return false;
}
and checkFurther() could be something like this:
bool checkFurther(char * str, char** grid, int size, int& loc_x, int& loc_y, int dx, int dy)
{
length = strlen(str);
for(i = 0 ; i < length; ++i)
curX = loc_x + (i * dx);
curY = loc_y + (i * dy);
if(!boundsCheck(size, curX, curY))
return false;
if grid[curX][curY] != str[i]
return false
return true;
}
bool boundsCheck(int size, int curX, curY)
{
upper = sqrt(size)
result = true
if curX < 0 or curX >= upper
result = false
if curY < 0 or curY >= upper
result = false
return result;
}
My schema varies from yours in:
1) how I interpret the meaning of size
2) how I implement the legend (N/S/E/W)
3) I don't recopy anything from grid, I use the whole grid all the time
4) tabGrille isn't needed because strlen() doesn't count the terminating null char of a C style string.
5) I use a switch statement rather than an array of structs to implement the direction to check.
6) I do bounds checking before checking chars beyond the first char for equality
7) Somehow I managed to get my functions to do be very short and do just one or two things each (not something I'm usually very good at!) which makes evaluating logic and error checking easier.