2D Array Passing to function - only reads first line

Hi guys,
Hoping someone has some insight to what my issue is. Any help would be greatly appreciated.

I have an array of char 4x4 that is being passed into a function.
When I create the array with my own letters (debugging purposes), everything works like a champ. The function sees the entire 2d array.
When I create the array with random letters, the array is created fine with random letters but the function only takes in the first 4 characters a[0][0] to a[0][3].

The only difference in my code, is the array is set as static when debugging.

Here are the relevant bits of code. If you need to see more to clarify, please let me know.

h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class boggleGame  {
public:
	boggleGame(bool debug);
	void printBoggle();
	//char letters[SIZE][SIZE];
	static char letters[SIZE][SIZE]; //for debug

	friend class boggleCheater; //so solver can access letters[][]
};

class boggleCheater {
public:
	boggleCheater();
	void solve (int size, boggleGame board, ifstream &words);
	
private:
	bool matchWord(const string &word, int row, int col, int index, int activeLetters, char board[][SIZE] );
};



cpp
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
36
37
38
39
40
41
42
boggleGame::boggleGame(bool debug) {
	if (debug == false)
	{
		int row, col;
		unsigned seed = (unsigned) time (0);
		srand (seed);
		for (row = 0; row < SIZE; row++) {     
			for (col = 0; col < SIZE; col++) {
				letters[row][col] = rand () % 26 + 'a';
			}
		}

	}
}

//FOR DEBUG ONLY
char boggleGame::letters[4][4] =
{
	{'y','o','x','f'},
	{'r','b','a','k'},
	{'v','e','d','i'},
	{'u','y','t','r'}
};

void boggleCheater::solve(int size, boggleGame board, ifstream &words) {
	string word;

	//go through word in wordlist
	while (!words.eof())
	{
		getline (words, word);
		//kick out if not 3 or more letters or more than 17 (16 unless you have Q/Qu so 17)
		if ((word.length() < 3 ) || (word.length() > 17)) continue;

		for (int row = 0; row < 4; row++)
			for (int col = 0; col < 4; col++)
				if (board.letters[ row ][ col ] == word[ 0 ])
					if (matchWord( word, row, col, (word[ 0 ] == 'q') ? 2 : 1, (1 << ((row *4) +col)), board.letters ))
						cout << word << '\n'; //output the word
	}

}


its this matchWord function that doesnt see the entire 2d array (board.letters)
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool boggleCheater::matchWord(const string &word, int row, int col, int index, int activeLetters, char board[][SIZE]) {
	if (index == word.length()) return true; 
	for (int r = -1; r < 2; r++)
		for (int c = -1; c < 2; c++)
		{
			if (((row +r) < 0) || ((row +r) > 3) || ((col +c) < 0) || ((col +c) > 3)) continue;
		
			int mask = 1 << (((row +r) *4) +col +c);
			if (((activeLetters & mask) == 0) &&  (board[ row +r ][ col +c ] == word[ index ]))
				if (matchWord( word, row +r, col +c, (word[ index ] == 'q') ? index +2 : index +1, activeLetters | mask, board ))
					return true;
		}
		return false;
}


so when im not using my static array with pre-determined letters, the array is created when you make a boggleGame object.

any ideas why?
Thanks,
-G
Do you expect boggleCheater::solve to modify the contents in the boggleGame received as an argument?

Then you might consider passing it as pointer or reference instead...

This also explains why a static array works => all instances (even copies from your original instance) modify the same letters member!
The game board never changes (or gets modified).
Since I never made a copy constructor, the board.letters only "copied" the first line when passing it to the function.
I ended up passing the board by reference and is working as intended now.
Since the static var was just for testing, im planning on removing it.
Thanks for the help !!

-G
Topic archived. No new replies allowed.