GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Feb-2005, 09:24
The_Kingpin The_Kingpin is offline
New Member
 
Join Date: Oct 2004
Posts: 13
The_Kingpin is on a distinguished road

Finding a word in a 2d grid


Hi all,

I was asked to make a mystery word game (find some word in a grid to reveal tthe hidden word) and to do this I need to make a function that search for a word in my grid and returns his coordinates and the direction it was found in the grid.

Once the first letter of the searched word matches the grid's letter, the word can be in the 8 directions so we need to check this out.

I have made a function but I'm having problems returning the correct coordinates and direction. If anyone could give a little help pointing me what I'm doing wrong, I would really appreciate !

Here it is:


CPP / C++ / C Code:

typedef struct {
	   int x;
	   int y;
	} TDirection;
	
	// Directions for N, NE, E, SE, S, SW, W, NW, assumming North and West are negative
	TDirection DIRECTION[8] = { { 0,-1 }, {1,-1}, {1,0}, {1,1}, {0,1}, {-1,1}, {-1,0}, {-1,-1} };


// The parameters fro this function are the following:
// char* word is the word we are searching for
// char** array is the grid. Each item of the array are string and 
//                       represents a line of the grid (note that the grid
//                       will always be square)
// int size is the size of the grid
// int &loc_x, int &loc_y, int &loc_dir are the default coordinates 
//  and were initialized to 0 before calling the function
//
bool finWord(char *word, char **array, int size, int &loc_x, int &loc_y, int &loc_dir)
{
           // Get the length of the searched word
          int len = strlen(word);
          
           // A word cannot be only 1 letter
          if(len>1)
          for (int x=0; x < size - 1; x++) {
               
              // We build a new array containing the current line of the grid
               char* arrGrid = new char [strlen(array[x])];
               arrGrid = array[x];
               
               // We go trough the entire line, until we reach a null value
               for (unsigned int y=0; y < strlen(array[x]); y++) {
                    if(tabGrille[y] == '\0') break;
                       
                   // Compare the word's lfirst letter with the current letter in the grid
                    if (toupper(tabGrille[y])!=toupper(word[0]))
                         continue;
                 
                   // I think the problem is in this part. I can't seems to make it work properly
                    for (int dir=0; dir<8; dir++) {
                         int cur_x = x;
                         int cur_y = y;
                     
                         // We check the rest of the word in every direction
                         for (int l=0; l<len; l++)  {

                              if (toupper(arrGrid[y])!=toupper(word[0]))
                                   break;
                              
                              if (l==len-1) {
                                   loc_x = x;
                                   loc_y = y;
                                   loc_dir = dir;
                                   cout << "Found the word: " << word << " | position: (" <<
                                   loc_x << "," << loc_y << ") | Direction: " << getDirection(loc_dir) << endl;  // getDirection converts the number into string
                                   return true;
                              }

                              // advance in current direction
                               cur_x += DIRECTION[dir].x;    
                               cur_y += DIRECTION[dir].y;
                               
                                                 // ends of the grid
                               if (cur_x<0 || cur_y<0 || cur_x>=size || cur_y>=size)
                                   break;  
                         }  
                    }
               }
          }
     return false;
}


Thanks a lot for the help,

Frank
Last edited by LuciWiz : 25-Feb-2005 at 01:01. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 24-Feb-2005, 10:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by The_Kingpin
Hi all,

If anyone could give a little help pointing me what I'm doing wrong, I would really appreciate !


There's not much chance that I will be able to read through your code to check functionality. One glaring error that must be fixed:

CPP / C++ / C Code:
      char* arrGrid = new char [strlen(array[x])];
      arrGrid = array[x];
       

You allocate an array (with new) and set the pointer arrGrid to the address of its first element. Then you immediately set the pointer arrGrid to the value of an address passed in as an argument. (So you can never free the memory --- by the way, where do you delete it? You know that you must delete everything that new gave you: right?)

You probably want strcpy() to copy whatever into the memory that you got from new. (But it's kind of hard to tell since I have nothing that I can actually compile and execute.)

You have a comment that indicates that you think you know where your problem is. Great! Now, put some output statements in and around the suspected block to see what's really happening

Regards,

Dave
  #3  
Old 24-Feb-2005, 16:57
The_Kingpin The_Kingpin is offline
New Member
 
Join Date: Oct 2004
Posts: 13
The_Kingpin is on a distinguished road
Thanks Dave for the comments.

As you thaught, I didn't knew we had to delete a created object . I'm still freshly new in C++ and with the previous language, this wasn't necessary.

Like you said, I know where the problem is but don't know how to solve it. I was given a function (that i can't touch) that fill an array with each line of the grid as a different item.

I'll continue working on this, if anyone has suggestions please let me knoe...

Thanks guys,

Frank
  #4  
Old 24-Feb-2005, 17:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by The_Kingpin
Thanks Dave for the comments.

As you thaught, I didn't knew we had to delete a created object . I'm still freshly new in C++ and with the previous language, this wasn't necessary.

Like you said, I know where the problem is but don't know how to solve it. I was given a function (that i can't touch) that fill an array with each line of the grid as a different item.

I'll continue working on this, if anyone has suggestions please let me knoe...

Thanks guys,

Frank

Do you have any control over program parameters? I mean: can you create a test case with a small grid or what? You seem to indicate that you can't control what gets put into the grid --- is this the case?

If you can control the input in some way, then put in a small grid and a small word and run it through your function. If not, then I suggest you print out whatever grid you are given, find a word (by inspection) and call your program to see if it can find the word. Put output statements that show how you are tracking through the grid while looking for the word. Something like that.

Regards,

Dave
  #5  
Old 24-Feb-2005, 19:53
The_Kingpin The_Kingpin is offline
New Member
 
Join Date: Oct 2004
Posts: 13
The_Kingpin is on a distinguished road
Actually I'm able to find the first letter of a word in the grid and return it's coordinates. It's the testing after that that cause the problem

I don't know, from there, how to check every direction to find the second letter of the word. I know to solve the current issue with my function I should create and call a new function called findNextLetter that would check every direction for a possible match, but I simply don't know how to do it...

Search for "word search" everywhere on the web but looks like I'm having no luck tonight...

Desperate dude.
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
won't read file from disk, dunno why wbsquared03 C++ Forum 3 29-Nov-2004 11:19
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 22-Nov-2004 23:26
How to do word comparison in C++ ? mail_as C++ Forum 19 22-Jun-2004 17:39
testing word file "need help" alexandro C Programming Language 2 03-Jun-2004 14:38
coding a word with a givin factor funnyf C++ Forum 2 13-Jan-2004 08:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:05.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.