Help with scopes/function not called in tic tac toe code?

Hi! I've been having trouble all semester with knowing the scopes of different functions/variables and I have a particular question on one instance in my tic tac toe code. (Warning: I know it's not finished! I still have more to add, but I wanted to get this part correct before moving on)

I have a 2D vector named box, and I am trying to modify this vector throughout my code and have a bunch of functions that use the vector box. And all of these functions are changing it. How would I go about the function/variable prototypes in the beginning of my code in order to achieve this?

For the way I have it in the beginning of my code, I figured each function would be modifying the 2D box vector since the & operator has the address of the 2D vector. But it's most likely not correct since I'm getting the error in my prototype for box: 'box' declared as reference but not initialized'
I never had to initialize a 2D vector in the prototype before, so I don't understand the error.


Here is my code:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Lab 10: Tic-Tac-Toe Game */

/*		NOTES:
			- Needs to check if all the positions are full (Put inside win() function)

*/

#include <iostream>
#include <vector>

using namespace std;


// Function prototypes:

vector< vector<char> >& box;
void grid(vector< vector<char> >& box);
void play(vector< vector<char> >& box);
void print_win(vector< vector<char> >& box);
bool win(vector< vector<char> >& box);
char winner;


// Main:

int main(int argc, char** argv){

	// Initializing board:
	vector< vector<char> > box(3, vector<char> (3, ' '));

	grid(box);

	cout << "Player 1 is X, Player 2 is O\n";

	play(box);

	return 0;
}


// Playing moves:

void play(vector< vector<char> >& box){

	int row, column;

	cout << "Player 1, what is the coordinate of your mark?\n";
	cin >> row >> column;
	box[row][column] = 'X';
	grid(box);

	if (win(box) == true){
		print_win();
		return;
	}

	cout << "Player 2, what is the coordinate of your mark?\n";
	cin >> row >> column;
	box[row][column] = 'O';
	grid(box);

	if(win(box) == true){
		print_win();
		return;
	} else {
		return play(box);
	}


}


// Finds if anyone won

bool win(vector< vector<char> >& box){

	if(box[0][0] == box[1][0] == box[2][0]){
		char winner = box[0][0];
		return true;
	}else if(box[0][0] == box[0][1] == box[0][2]){
		char winner = box[0][0];
		return true;
	}else if(box[0][1] == box[1][1] == box[2][1]){
		char winner = box[0][1];
		return true;
	}else if(box[0][2] == box[1][2] == box[2][2]){
		char winner = box[0][2];
		return true;
	}else if(box[1][0] == box[1][1] == box[1][2]){
		char winner = box[1][0];
		return true;
	}else if(box[2][0] == box[2][1] == box[2][2]){
		char winner = box[2][0];
		return true;
	}else if(box[0][0] == box[1][1] == box[2][2]){
		char winner = box[0][0];
		return true;
	}else if(box[0][2] == box[1][1] == box[2][0]){
		char winner = box[0][2];
		return true;
	}else{
		return false;	// No one won.
	}
	return false;
}

// Prints the winning screen

void print_win(vector< vector<char> >& box){

	if(winner == 'X'){
		cout <<"Congratulations! Player 1 won! You're the bombdiggity best!\n";
	}else{
		cout <<"Congratulations! Player 2 won! O snap!\n";
	}

	char answer;
	cout << "Would you like to play again? Y/N"<< '\n';
	cin >> answer;
	if(answer == 'Y'){
		
		for(int row = 0; row < 3; row++){
			for(int column = 0; column < 3; column++){
				box[row][column] = ' ';
			}
		}

		play(box);
	}else{
		cout << "Bye.\n";
	}

}


// The tic-tac-toe grid:

void grid(vector< vector<char> >& box){
	cout << "     |     |     \n";
	cout << "  "<< box[0][0] <<"  |  "<< box[0][1] << "  |  " << box[0][2] <<'\n';
	cout << "_____|_____|_____\n";
	cout << "     |     |     \n";
	cout << "  "<< box[1][0] <<"  |  "<< box[1][1] << "  |  " << box[1][2] <<'\n';
	cout << "_____|_____|_____\n";
	cout << "     |     |     \n";
	cout << "  "<< box[2][0] <<"  |  "<< box[2][1] << "  |  " << box[2][2] <<'\n';
	cout << "     |     |     \n";
}
Last edited on
box on line 16 is a reference. A reference has to always refer to an object and it's not possible to change which object it refers to later on, so for that reason you'll have to state what object it refers to right away when it's defined. You are creating a variable named box inside main that you pass to all the functions so it doesn't look like you actually need line 16 so you can probably just remove it.
Ah, I see. Thanks! That's a lot clearer now.

My current issue with the tic tac toe board now is that when I play through the game, whenever a player wins, the game continues and print_win() doesn't seem to be called at all.
In the code above you are not passing box as argument to print_win. Have you changed it?
Last edited on
Yes, I changed it so box is passed into it.

Updated code:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Lab 10: Tic-Tac-Toe Game */

/*		NOTES:
			- Needs to check if all the positions are full (Put inside win() function)

*/

#include <iostream>
#include <vector>

using namespace std;


// Function prototypes:

//vector< vector<char> >& box;
void grid(vector< vector<char> >& box);
void play(vector< vector<char> >& box);
void print_win(vector< vector<char> >& box);
bool win(vector< vector<char> >& box);
char winner;


// Main:

int main(int argc, char** argv){

	// Initializing board:
	vector< vector<char> > box(3, vector<char> (3, ' '));

	grid(box);

	cout << "Player 1 is X, Player 2 is O\n";

	play(box);

	return 0;
}


// Playing moves:

void play(vector< vector<char> >& box){

	int row, column;

	cout << "Player 1, what is the coordinate of your mark?\n";
	cin >> row >> column;
	box[row][column] = 'X';
	grid(box);

	if (win(box) == true){
		print_win(box);
		return;
	}

	cout << "Player 2, what is the coordinate of your mark?\n";
	cin >> row >> column;
	box[row][column] = 'O';
	grid(box);

	if(win(box) == true){
		print_win(box);
		return;
	} else {
		return play(box);
	}


}


// Finds if anyone won

bool win(vector< vector<char> >& box){

	if(box[0][0] == box[1][0] == box[2][0]){
		char winner = box[0][0];
		return true;
	}else if(box[0][0] == box[0][1] == box[0][2]){
		char winner = box[0][0];
		return true;
	}else if(box[0][1] == box[1][1] == box[2][1]){
		char winner = box[0][1];
		return true;
	}else if(box[0][2] == box[1][2] == box[2][2]){
		char winner = box[0][2];
		return true;
	}else if(box[1][0] == box[1][1] == box[1][2]){
		char winner = box[1][0];
		return true;
	}else if(box[2][0] == box[2][1] == box[2][2]){
		char winner = box[2][0];
		return true;
	}else if(box[0][0] == box[1][1] == box[2][2]){
		char winner = box[0][0];
		return true;
	}else if(box[0][2] == box[1][1] == box[2][0]){
		char winner = box[0][2];
		return true;
	}else{
		return false;	// No one won.
	}
	return false;
}

// Prints the winning screen

void print_win(vector< vector<char> >& box){

	if(winner == 'X'){
		cout <<"Congratulations! Player 1 won! You're the bombdiggity best!\n";
	}else{
		cout <<"Congratulations! Player 2 won! O snap!\n";
	}

	char answer;
	cout << "Would you like to play again? Y/N"<< '\n';
	cin >> answer;
	if(answer == 'Y'){
		
		for(int row = 0; row < 3; row++){
			for(int column = 0; column < 3; column++){
				box[row][column] = ' ';
			}
		}

		play(box);
	}else{
		cout << "Bye.\n";
	}

}


// The tic-tac-toe grid:

void grid(vector< vector<char> >& box){
	cout << "     |     |     \n";
	cout << "  "<< box[0][0] <<"  |  "<< box[0][1] << "  |  " << box[0][2] <<'\n';
	cout << "_____|_____|_____\n";
	cout << "     |     |     \n";
	cout << "  "<< box[1][0] <<"  |  "<< box[1][1] << "  |  " << box[1][2] <<'\n';
	cout << "_____|_____|_____\n";
	cout << "     |     |     \n";
	cout << "  "<< box[2][0] <<"  |  "<< box[2][1] << "  |  " << box[2][2] <<'\n';
	cout << "     |     |     \n";
}
My compiler spits out a bunch of warnings in the win function.

warning: suggest parentheses around comparison in operand of ‘==’

The == operator takes two arguments and returns a bool (true or false).

a == b == c is equivalent to((a == b) == c).

(a == b) will return a bool so c will be compared to a bool.

If you want to compare if a, b and c are equal you'll have to split it into two comparisons and combine the result using the && (and) operator a == b && b == c.


warning: unused variable ‘winner’


 
char winner = box[0][0];

This creates a local variable that only exists within that if statement. If your intention was to assign the global winner variable just remove char from the beginning of the line.

Last edited on
Ah, I see! Thanks! I've updated my code to fix that.
However, now whenever I run the code, player 2 "wins" after player 1 plays one turn.
Make sure three empty boxes in row does not give a win.
Thank you so much, Peter! :D
It is much appreciated.
Topic archived. No new replies allowed.