Help with tic tac toe problem

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include<iostream>
#include<string>

using namespace std;



int main() {
	char keeplaying = 'y';
	while (keeplaying == 'y') {
		char tic[3][3];
		const int fill0 = 0;
		const int fill1 = 1;
		const int fill2 = 2;
		cout << "New Game: X goes first." << endl;
		cout << "------------------" << endl;;
		cout << "|R/C|   0  |  1  |   2  | " << endl;
		cout << "| 0 |   " << tic[fill0][fill0] << "  |  " << tic[fill0][fill1] << "  |  " << tic[fill0][fill2] << "   |" << endl;
		cout << "| 1 |   " << tic[fill1][fill0] << "  |  " << tic[fill1][fill1] << "  |  " << tic[fill1][fill2] << "   |" << endl;
		cout << "| 2 |   " << tic[fill2][fill0] << "  |  " << tic[fill2][fill1] << "  |  " << tic[fill2][fill2] << "   |" << endl;
		cout << "X's turn" << endl;
		cout << "Where do you want your X placed?" << endl;
		cout << "Please select a number and column number seperate by a space." << endl;
		char space;
		int row;
		int column;
		bool player = true;
		
		while (player == true) {
			cin >> row >> column;
			
			cout << "You have entered row#" << row << endl;
			cout << "And column #" << column << endl;
			cout << "Thankyou for your selection." << endl;

			
			if (tic[row][column] == 'X' || tic[row][column] == 'Y') {
					cout << "choose a different value" << endl;
					player = true;
			}
			else if (row == 0 && column == 0) {
				player = false;
				tic[fill0][fill0] = { 'X' };
			}
			else if (row == 1 && column == 0) {
				player = false;
				tic[fill1][fill0] = { 'X' };
			}
			else if (row == 2 && column == 0) {
				player = false;
				tic[fill2][fill0] = { 'X' };
			}
			else if (row == 0 && column == 1) {
				player = false;
				tic[fill0][fill1] = { 'X' };
			}
			else if (row == 1 && column == 1) {
				player = false;
				tic[fill1][fill1] = { 'X' };
			}
			else if (row == 2 && column == 1) {
				player = false;
				tic[fill2][fill1] = { 'X' };
			}
			else if (row == 0 && column == 2) {
				player = false;
				tic[fill0][fill2] = { 'X' };
			}
			else if (row == 1 && column == 2) {
				player = false;
				tic[fill1][fill2] = { 'X' };
			}
			else if (row == 2 && column == 2) {
				tic[fill2][fill2] = { 'X' };
				player = false;
			}
			else if (row>2||row<0||column>2||column<0) {
				cout << "enter a correct value" << endl;
				player = true;

			}
			
		}
		if ('X' == tic[fill0][fill0] && 'X' == tic[fill1][fill0] && 'X' == tic[fill2][fill0]) {
			cout << "X wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('X' == tic[fill0][fill1] && 'X' == tic[fill1][fill1] && 'X' == tic[fill2][fill1]) {
			cout << "X wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('X' == tic[fill0][fill2] && 'X' == tic[fill1][fill2] && 'X' == tic[fill2][fill2]) {
			cout << "X wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('X' == tic[fill0][fill0] && 'X' == tic[fill0][fill1] && 'X' == tic[fill0][fill2]) {
			cout << "X wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('X' == tic[fill1][fill0] && 'X' == tic[fill1][fill1] && 'X' == tic[fill0][fill2]) {
			cout << "X wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('X' == tic[fill2][fill0] && 'X' == tic[fill2][fill1] && 'X' == tic[fill2][fill2]) {
			cout << "X wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('X' == tic[fill0][fill0] && 'X' == tic[fill1][fill1] && 'X' == tic[fill2][fill2]) {
			cout << "X wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('X' == tic[fill2][fill0] && 'X' == tic[fill1][fill1] && 'X' == tic[fill0][fill2]) {
			cout << "X wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}

		cout << "------------------" << endl;;
		cout << "|R/C|   0  |  1  |   2  | " << endl;
		cout << "| 0 |   " << tic[fill0][fill0] << "  |  " << tic[fill0][fill1] << "  |  " << tic[fill0][fill2] << "   |" << endl;
		cout << "| 1 |   " << tic[fill1][fill0] << "  |  " << tic[fill1][fill1] << "  |  " << tic[fill1][fill2] << "   |" << endl;
		cout << "| 2 |   " << tic[fill2][fill0] << "  |  " << tic[fill2][fill1] << "  |  " << tic[fill2][fill2] << "   |" << endl;
		cout << "O's turn." << endl;
		cout << "Where do you want your O placed" << endl;
		cout << "Please enter row number and column number seperated by a space." << endl;

		while (player == false) {
			cin >> row >> column;
			
			cout << "You have entered row#" << row << endl;
			cout << "and column" << column << endl;
			if (tic[row][column] == 'X' || tic[row][column] == 'Y') {
				cout << "choose a different value" << endl;
				player = false;
			}
			else if (row == 0 && column == 0) {
				player = true;
				tic[fill0][fill0] = { 'O' };
			}
			else if (row == 1 && column == 0) {
				player = true;
				tic[fill1][fill0] = { 'O' };
			}
			else if (row == 2 && column == 0) {
				player = true;
				tic[fill2][fill0] = { 'O' };
			}
			else if (row == 0 && column == 1) {
				player = true;
				tic[fill0][fill1] = { 'O' };
			}
			else if (row == 1 && column == 1) {
				player = true;

				tic[fill1][fill1] = { 'O' };
			}
			else if (row == 2 && column == 1) {
				player = true;
				tic[fill2][fill1] = { 'O' };
			}
			else if (row == 0 && column == 2) {
				player = true;
				tic[fill0][fill2] = { 'O' };
			}
			else if (row == 1 && column == 2) {
				player = true;
				tic[fill1][fill2] = { 'O' };
			}
			else if (row == 2 && column == 2) {
				player = true;
				tic[fill2][fill2] = { 'O' };
			}
			else if (row > 2 || row < 0 || column>2 || column < 0) {
				cout << "Invalid Entry: try again." << endl;
				player = false;
			}
		}
		if ('O' == tic[fill0][fill0] && 'O' == tic[fill1][fill0] && 'O' == tic[fill2][fill0]) {
			cout << "O wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('O' == tic[fill0][fill1] && 'O' == tic[fill1][fill1] && 'O' == tic[fill2][fill1]) {
			cout << "O wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('O' == tic[fill0][fill2] && 'O' == tic[fill1][fill2] && 'O' == tic[fill2][fill2]) {
			cout << "O wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('O' == tic[fill0][fill0] && 'O' == tic[fill0][fill1] && 'O' == tic[fill0][fill2]) {
			cout << "O wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('O' == tic[fill1][fill0] && 'O' == tic[fill1][fill1] && 'O' == tic[fill0][fill2]) {
			cout << "O wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('O' == tic[fill2][fill0] && 'O' == tic[fill2][fill1] && 'O' == tic[fill2][fill2]) {
			cout << "O wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('O' == tic[fill0][fill0] && 'O' == tic[fill1][fill1] && 'O' == tic[fill2][fill2]) {
			cout << "O wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}
		else if ('O' == tic[fill2][fill0] && 'O' == tic[fill1][fill1] && 'O' == tic[fill0][fill2]) {
			cout << "O wins the game!, keep playing(y/n)?" << endl;
			cin >> keeplaying;
		}

		}

		return(0);
}

Clearly this is a bad way of doing this assignment. Is there a way to call a 2d array or make the inputs variables? I couldn't figure out how to do either so i just put everything inside main. Every time i tried to make a void function to call, there was always a problem with the inputs because they need to be constant values. I also dont know how to restart the program so it starts with a clean slate when played again. Any help would be appreciated.
Last edited on
Hello MAZambelli4353,

You are correct "this is a bad way of doing this assignment".

Lines 11 - 14 should be defined outside the while loop. The array needs to be initialized otherwise it will print garbage until the array is filled. The two inner while loops will only exit the program when "O" wins and does nothing when "X" wins. Or maybe when "X" is in row 1 it does not catch this as a win. Have not tracked that down yet.

If you choose to continue the program you need a nested for loop to zero out the array before the next game. But only if you continue for another game.

I would take the lines that print the board and put them in a function.

Line 15 prints every time it is "X's" turn. It only needs to print once at the beginning of the game.

It will take awhile and a lot of work to fix what you have, but it could eventually be made to work. It might be easier to rethink the program breaking up some parts into functions instead of trying to do everything in main.

Is there a way to call a 2d array or make the inputs variables?

Not sure what you mean here? You do not call a 2D array you use it. You are already inputting to "row" and "column" which you use. I do not understand what "make the inputs variables" means.

Every time i tried to make a void function to call, there was always a problem with the inputs because they need to be constant values.

Be more specific about what you mean and show or explain what you have tried.

Hope that helps,

Andy
Topic archived. No new replies allowed.