tic tac toe question

Hi everyone!

I'm nearing completion with this tic tac toe program, except I need a little help.

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>

using namespace std;

const int R=3;
const int C=3;
char space[R][C] = {'1','2','3','4','5','6','7','8','9'};

int main() {

	bool win;
	int count = 0;
	int currentPlayer;
	int xPlayer = 1;
	int oPlayer = 2;	
	
	void displayBoard();
	int makeMove(int, int, int);
	int checkWin(bool);
	int displayWin(bool);
	int newGame(int);

	currentPlayer = xPlayer;
	
	cout << "\nWelcome to my Tic Tac Toe game! \n";

	do{
		cout << "It is player " << currentPlayer << "'s turn.\n";

		displayBoard();			
		makeMove(currentPlayer, xPlayer, oPlayer);
		checkWin(win);
		displayWin(win);

		if (currentPlayer == xPlayer){
			currentPlayer = oPlayer;
		}else
		if(currentPlayer == oPlayer){
			currentPlayer = xPlayer;
		}
		count++;		

		if(win == true){
			count = 8;
		}

	}while(count <=8);

	displayBoard();
	
}

void displayBoard(){

	for(int i=0; i<3; i++){
		for(int j=0; j<3; j++){
			cout << space[i][j];
			if(j<2)
				cout << " | ";
		}
		cout << endl;
		if(i<2){
			cout << "_________" << endl;
		}
	}

}

int makeMove(int currentPlayer, int xPlayer, int oPlayer) {

	int choice;
	int r, c;
	char playerSymbol;

	if(currentPlayer == xPlayer){
		playerSymbol = 'X';
	}else
	if(currentPlayer == oPlayer){
		playerSymbol = 'O';
	}
	
		cout << "Player " << currentPlayer << "'s move:\n";
		cin >> choice;

		if(choice == 1 && space[0][0] == '1'){
			r = 0;
			c = 0;
			space[r][c] = playerSymbol;
		}else
		if(choice == 2 && space[0][1] == '2'){
			r = 0;
			c = 1;
			space[r][c] = playerSymbol;
		}else
		if(choice == 3 && space[0][2] == '3'){
			r = 0;
			c = 2;
			space[r][c] = playerSymbol;
		}else
		if(choice == 4 && space[1][0] == '4'){
			r = 1;
			c = 0;
			space[r][c] = playerSymbol;
		}else
		if(choice == 5 && space[1][1] == '5'){
			r = 1;
			c = 1;
			space[r][c] = playerSymbol;
		}else
		if(choice == 6 && space[1][2] == '6'){
			r = 1;
			c = 2;
			space[r][c] = playerSymbol;
		}else
		if(choice == 7 && space[2][0] == '7'){
			r = 2;
			c = 0;
			space[r][c] = playerSymbol;
		}else
		if(choice == 8 && space[2][1] == '8'){
			r = 2;
			c = 1;
			space[r][c] = playerSymbol;
		}else
		if(choice == 9 && space[2][2] == '9'){
			r = 2;
			c = 2;
			space[r][c] = playerSymbol;
		} else
			r = c = -1;

		if ( r == -1 || c == -1 || space[r][c] == xPlayer || space[r][c] == oPlayer )
			cout << "Invalid move. Please enter another value.";
}


int checkWin(bool win){

	// 1 2 3 horizontal wins
	if(space[0][0] == space[0][1] && space[0][0] == space[0][2]){
		win = true;
	}else
	//4 5 6 
	if(space[1][0] == space[1][1] && space[1][0] == space[1][2]){
		win = true;	
	}else
	//7 8 9
	if(space[2][0] == space[2][1] && space[2][0] == space[2][2]){
		win = true;
	}else
	
	
	// 1 4 7 vertical wins
	if(space[0][0] == space [1][0] && space[0][0] == space[2][0]){
		win = true;
	}else
	//2 5 8
	if(space[0][1] == space[1][1] && space[0][1] == space[2][1]){
		win = true;
	}else
	//3 6 9
	if(space[0][2] == space[1][2] && space[0][2] == space[2][2]){
		win = true;
	}else
	
	//1 5 9 diagonal wins
	if(space[0][0] == space[1][1] && space[0][0] == space[2][2]){
		win = true;
	}
	//3 5 7
	if(space[0][2] == space[1][1] && space[0][2] == space[2][0]){
		win = true;
	}else
		win = false;


return win;

}

void displayWin(bool win) {


	if(win == true){
		cout << "Player  won!\n";
		
	}
}


My checkWin function doesn't seem to be operating properly. Mainly, when the requirements are met for a win, it doesn't seem to recognize the win and activate displayWin. I'm pretty sure it's a simple matter of me incorrectly passing the win variable, but no matter what I try to do with it I can't figure it out.

Any help will be greatly appreciated!
The problem is you are returning a bool from the checkWin function but you are not setting the value that is returned equal to anything, so it just gets lost. You would want to change it to something like win = checkWin(win); so that the value returned gets stored in win. However in this case there would be no point in passing win into the function (you would just set up a local variable inside the function and then return that.

The other way you could do it is to pass the variable by reference instead of by value. void checkWin(bool &win){ . This means that when you pass the value it actually passes the address not just a copy so it changes the original copy of the variable (and so you don't need to return anything anymore). Your choice which way to go.

Just for other side notes, makeMove is defined to return an int but it isn't returning anything right now, you may need some function definitions above main so it can see they exist. I suggest you keep your else if statements on one line or else they can be much harder for people to understand.
Last edited on
Topic archived. No new replies allowed.