I started doing a program that is supposed to solve crosswords ( search for words in all directions ).
So basicaly I have a field [X:Y] , there is a letter on every location ( '0' for empty ) and I have to keep track if that letter was used in a word or is left and that's why I made that a struct outside the class and I use it as a variable in the main class.
I also add new words that I'm searching for. I made a struct for that too because I have to keep track if the word was found or not and it also has to contain the word itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
struct csItem
{
char item;
bool state;
};
struct csSearch
{
std::string word;
bool found;
};
class csField
{
public:
csField(unsigned short sizeXY);
csField(unsigned short sizeX, unsigned short sizeY);
void initialize();
//code...
private:
csItem **item;
csSearch *search;
|
It looks like this , csField is the class , the whole field. csItem is the smallest part of the field, it has coordinates [x:y].
csSearch is the struct that contains the words that I'm looking for.
I add new words with the method pushWord :
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
|
void pushWord(std::string word)
{
csSearch *temp = NULL;
if ( words != 0 )
{
temp = new csSearch[words];
for ( int i = 0 ; i < words ; i++ )
{
temp[i].word = search[i].word;
temp[i].found = search[i].found;
}
delete[] search;
words++;
search = new csSearch[words];
for ( int i = 0 ; i < words-1 ; i++ )
{
search[i].word = temp[i].word;
search[i].found = temp[i].found;
}
delete[] temp;
search[words-1].word = word;
}
else
{
words++;
search = new csSearch[words];
search[words-1].word = word;
}
}
|
words is a integer variable inside the class csField that keeps track of how many words are there.
The algoritm that is searching for the words is not wrong, actually I was suprised that it is working... The problem is something i've never met before and I don't understand what is happening , it's working just partially and I tried to solve it but without success.
If I show the whole field that is read from a text file it is working without any problem, the item[][] contains the right letters and the state variable is initialized to false.
The same applies for search[], I add all the words and the string inside the struct contains all of them without problem.
But when I need to change the value of any boolean of the struct, be it search, indicating that the word was found or be it item, indicating that the letter was used in a word, it won't change the value to true.
Here is the code in which I change the value of search :
(It is inside a method of the class csField)
1 2 3 4
|
if ( found == true )
{
this->search[w].found = true;
}
|
And here is the part where I change the item value :
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
|
bool matchRIGHT(std::string word,int x,int y)
{
int length = word.length();
bool match = true;
char letter = '0';
for ( int sx = 0 ; sx < sizeX && sx < length ; sx++ )
{
letter = this->getItemValue(x+sx,y);
if ( word[sx] != this->getItemValue(x+sx,y) )
match = false;
}
if ( match == true )
{
std::cout << word << " was found![matchRIGHT]" << std::endl;
for ( int sx = 0 ; sx < sizeX && sx < length ; sx++ )
{
//change state of all items used
this->setItemState(x+sx,y,true);
}
}
return match;
}
|
1 2 3 4
|
char getItemValue(int x, int y) { return item[x][y].item ; }
bool getItemState(int x, int y) { return item[x][y].state ; }
void setItemValue(int x, int y, char value) { item[x][y].item = value; }
void setItemState(int x, int y, bool state) { item[x][y].state = state; }
|
Both of the variables should change it's value from FALSE to TRUE but when I put them to the output they are still false.
But if I have an object of the class, let's say csField Alpha(30);
if I change the value in main() like, Alpha.setItemState(0,0,true);
It will change the variable, but if I do it in the class using this-> it won't change anything. I was using the debugger to make sure it goes to the line where it is supposed to change it and it goes to the line, no error, but the value is left unchanged. Why is this so ?