Reputation: 19
This is code to solve a Boggle board game using recursion. After I find all the words that are in the dictionary, when I check the size of the vector I have been adding the words to, the correct number of words are found, but afterwards when I access the vector outside the function, I get that the size of the vector is zero. I have sent the address of the vector using the “&” but it seems like even then the vector is not being updated outside the function.
Here is the code for my function wordCheck, where I send a copy of the puzzle, a row and a column and an empty string called finalWord. As you can see I keep track of the size of the vector as words are added, however once the function has gone through the all the possible words that can be formed with the first letter being the letter at r,c the vector size is correct but once I try to access it outside the loop like in computerPlay() the size is back to 0 and so nothing is printed at the end. In this example I am just testing for the first position (0,0). I have also copied the code for computerPlay().
void Boggle::computerPlay(){
//copy of boggle board
char boggleCopy[SET_ROWS][SET_COLUMNS];
vector<string> computerWords;
for (int i = 0; i < SET_ROWS; i++) {
for (int j = 0; j < SET_COLUMNS; j++) {
boggleCopy[i][j] = theBoard[i][j];
}
}
string finalWord;
wordCheck(boggleCopy, 0, 0, finalWord, computerWords);
// here the vector size printed is Zero when in fact it should be 7 like it is inside the loop
cout << "Vector size is " << computerWords.size() << endl;
for (vector<string>::iterator i = computerWords.begin(); i != computerWords.end(); i++){
cout << *i << endl;
}
}
void Boggle::wordCheck(char puzzle[SET_ROWS][SET_COLUMNS], int r, int c, string finalWord, vector<string>& v){
char letter = puzzle[r][c];
finalWord = finalWord + letter;
puzzle[r][c] = ' ';
if (finalWord.length() > 2){
if(dictionary.binarySearch(finalWord)){
v.push_back(finalWord);
cout << v.size() << " is the size" << endl;
}
}
for(int dr = -1 ; dr <= 1 ; dr++ ){
for(int dc = -1 ; dc <= 1 ; dc++){
if (dr != 0 || dc != 0){
if(finalWord.length() <= maxLength){
if (!outOfBounds(r + dr, c + dc) && !(puzzle [r + dr][c + dc] == ' ')){
if (finalWord.length() == 3){
if(!(dictionary.isPrefix(finalWord))){
break;
}
}
wordCheck(puzzle, r + dr, c + dc, finalWord, computerWords);
}
}
}
}
}
puzzle[r][c] = finalWord.at(finalWord.length() - 1);
int size = v.size();
cout << "Last vector size is " << size << endl;
// here my code prints out 7 as the size of the vector, which is the correct size that the vector should be.
cout << "Last vector size is " << computerWords.size() << endl;
}
Upvotes: 0
Views: 809
Reputation: 141586
vector<string> computerWords;
is a local variable to the computerPlay
function. It no longer exists when that function exits, and its name is not visible outside the function.
If your code in wordCheck
compiles, it means you have a different vector (also called computerWords
) somewhere else in your code.
Perhaps you meant to not have this other vector, and for the wordCheck
function to use the parameter v
rather than computerWords
.
Upvotes: 3