Your code is almost there. You just need to add a way to check whether or not the word is complete, and if it's not, to go back to where you were before.
The problem is, your current code terminates when there are no more "correct" letters available. This should only happen when there IS no such word in the grid. You need to make the function terminate when space == the length of the word. Get it?
[edited by - micepick on February 9, 2003 9:23:26 PM]
Word Search
here is a dumb question.
for bool functions returning 1 means true, 0 means false, right?
for bool functions returning 1 means true, 0 means false, right?
The problem is merely a basic 2D string search algorithm:
"I want to check Letter[X][Y] in my array and check if a particular word starts on that letter. Return TRUE if so, else FALSE." This is just a strcmp() with delusions of grandeur and recursion is really overkill for such a task. It's like trying to shell peas using an Exocet missile.
I'd go for a master "WordFound()" function that takes:
1. The search string,
2. The starting coordinate in the grid,
3. a 'direction' parameter (I suggest using deltas).
E.g. "WordFound(string, x, y, dx, dy)".
For NE, dx and dy would both be set to 1. For E, dx would be set to 1, dy to zero. For SE, dx would be 1 and dy -1. And so on around the compass.
All the function does is loop through each character in the string, checking if the current character in the string matches the character in the grid. If there's a match, increment x and y by dx and dy respectively and check the next character, and so on. Return FALSE if you get a mismatch or go out of bounds; return TRUE if you reach the end of the loop without hitting a mismatch.
(Recursion can still be used if you must.)
--
Sean Timarco Baggaley
[edited by - stimarco on February 9, 2003 9:43:06 PM]
"I want to check Letter[X][Y] in my array and check if a particular word starts on that letter. Return TRUE if so, else FALSE." This is just a strcmp() with delusions of grandeur and recursion is really overkill for such a task. It's like trying to shell peas using an Exocet missile.
I'd go for a master "WordFound()" function that takes:
1. The search string,
2. The starting coordinate in the grid,
3. a 'direction' parameter (I suggest using deltas).
E.g. "WordFound(string, x, y, dx, dy)".
For NE, dx and dy would both be set to 1. For E, dx would be set to 1, dy to zero. For SE, dx would be 1 and dy -1. And so on around the compass.
All the function does is loop through each character in the string, checking if the current character in the string matches the character in the grid. If there's a match, increment x and y by dx and dy respectively and check the next character, and so on. Return FALSE if you get a mismatch or go out of bounds; return TRUE if you reach the end of the loop without hitting a mismatch.
(Recursion can still be used if you must.)
--
Sean Timarco Baggaley
[edited by - stimarco on February 9, 2003 9:43:06 PM]
Sean Timarco Baggaley (Est. 1971.)Warning: May contain bollocks.
micepick,
i understand what you are telling me. now i just have to figure it all out and put it into code form which is going to be the tough part.
so i make recursive_word_finder a bool function.
inside the function do a string length or the word? (so the function knows when it has found the whole word and knows when to stop?)
when space == word it is done. when space != word it has to keep going.
something is wrong with that code but im not really sure what. i figure if i can get the code for one of the checks, the rest will be similar
i understand what you are telling me. now i just have to figure it all out and put it into code form which is going to be the tough part.
so i make recursive_word_finder a bool function.
inside the function do a string length or the word? (so the function knows when it has found the whole word and knows when to stop?)
when space == word it is done. when space != word it has to keep going.
int length = strlen(word) ; space++; //check to the right if (col < 7) { if (puzzle_board[row][col+1] == word[space]) { col++; recursive_word_finder(puzzle_board,row, col, word, space); return 0; } if (length == space) return 1; }
something is wrong with that code but im not really sure what. i figure if i can get the code for one of the checks, the rest will be similar
For one thing, if the call to recursive_word_finder returns one, the current function should also immediately break and return one.
I''m stummped, and my brain is now fried. Could you post some code for me?
why not write a small search function for each direction, then in a main wordsearch function you call each of them and whichever returns true you have your direction. if none return true then you haven''t found anything. That is how I would approach it.
Evillive2
Here is some code I wrote.. I can't guarantee it will work. for diagonals, its just a variation on the other check_ functions...
Hope that helps... yes, I was bored
edit: messed up the tags
[edited by - evillive2 on February 10, 2003 3:44:27 AM]
bool check_left_to_right( char *word, int row, int col ){ char buffer[10]; int i; int ilenght = strlen(word); if ( (ilength + col) > 7 ) return FALSE; buffer[0] = '\0'; for( i = 0; i<ilength; i++ ) buffer[i] = puzzle_board[row][col+i]; buffer[i] = '\0'; if ( !strcmp( word, buffer ) ) return TRUE; return FALSE;}bool check_right_to_left( char *word, int row, int col ){ char buffer[10]; int i; int ilenght = strlen(word); if ( (col - ilength) < 0 ) return FALSE; buffer[0] = '\0'; for( i = 0; i<ilength; i++ ) buffer[i] = puzzle_board[row][(col-ilength)+i]; buffer[i] = '\0'; if ( !strcmp( word, buffer ) ) return TRUE; return FALSE;}bool check_top_to_bottom( char *word, int row, int col ){ char buffer[10]; int i; int ilenght = strlen(word); if ( (row + ilength) > 7 ) return FALSE; buffer[0] = '\0'; for( i = 0; i<ilength; i++ ) buffer[i] = puzzle_board[row+i][col]; buffer[i] = '\0'; if ( !strcmp( word, buffer ) ) return TRUE; return FALSE;}bool check_bottom_to_top( char *word, int row, int col ){ char buffer[10]; int i; int ilenght = strlen(word); if ( (row - ilength) < 0 ) return FALSE; buffer[0] = '\0'; for( i = 0; i<ilength; i++ ) buffer[i] = puzzle_board[(row - ilength)+i][col]; buffer[i] = '\0'; if ( !strcmp( word, buffer ) ) return TRUE; return FALSE;}POINT *find_word_location( char *word, int direction ){ int row, col; bool found = FALSE; static POINT pt; for ( row = 0; row < 7; row++ ) { for ( col = 0; col < 7; col++ ) { if ( puzzle_board[row][col] == word[0] ) { if ( direction == DIR_R2L && check_right_to_left( word, row, col ) ) found = TRUE; else if ( direction == DIR_L2R && check_left_to_right( word, row, col ) ) found = TRUE; else if ( direction == DIR_T2B && check_top_to_bottom( word, row, col ) ) found = TRUE; else if ( direction == DIR_B2T && check_bottom_to_top( word, row, col ) ) found = TRUE; if ( found ) { pt.x = row; pt.y = col; return &pt } } } } return NULL;}void main_loop(){ //load the puzzle and words to search for ... //loop through the words and direction for ( iword = 0; iword < number_of_words; iword++ ) { for ( direction = 0; direction < 4; direction++ ) { board_location = find_word_direction( word_table[iword], direction ); if ( board_location != NULL ) { // do something with the board location and direction and word } } }}
Hope that helps... yes, I was bored
edit: messed up the tags
[edited by - evillive2 on February 10, 2003 3:44:27 AM]
Evillive2
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement