Problems with variable initilization

Pages: 12
Yes post your code - i notice that on line 114 in the move function you have a return with 2 values. This doesn't do what you think, you can only return 1 value & it will be b because of the comma operator. Perhaps the move function should return a bool.

The snippet I posted was only 2 simple lines, which I know works, you must have something else going on.
Last edited on
Yea the move function only returns one value now that was me being frustrated trying to get it to work and yea i know the current section to make the comptuer make a move is not right at all I am still working/figureing that out

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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <iostream>
#include <math.h>
#include <cmath>

using namespace std;

char square[3][3] = {'1', '2', '3', '4', '5' ,'6', '7', '8', '9'};
bool CheckWin(int a, int b, int Turn);
void Board();
int Geta(int Turn);
int Getb(int row, int Turn);
int Turn;
int move(int row, int col, int Turn, char Player);

int main()
{	
	int a = 0;
	int b = 0;
	int	row;
	int col;
	int Turn;
	int Valid = 0;
	int CPUCheck = 1;
	bool gamewon = false;
	int charsplaced = 0;
	char Player = 'O';


	Board();
	cout << "Above is how the various moves are identified" << endl;
	system("pause");

	for(int a = 0; a <= 3; a++)
	{
		for(int b = 0; b <= 3; b++)
			square[a][b] = ' ';
	}
	Board();
	while(!gamewon)
	{
		if (Valid != -1)
		{
			if(Player == 'O')
			{
				--CPUCheck;
				cout << "Your' move" << endl;
				Player = 'X';

			}
			else
			{
				cout << "My Turn";
				system("pause");
				Player = 'O';

				int vertical = 1;
				int horizontal = 1;
				int diagonal1 = 1;
				int diagonal2 = 1;
				int i;
				int ii;
			
				CPUCheck = 1;

				for(i = a; square[i][b] == Player && i <= 2; i++, vertical++);
				for(i = a; square[i][b] == Player && i >= 0; i--, vertical++);
					if(vertical >= 2)
					{
						if (square[0][0] == ' ')
							Turn = 1;
						else if(square[0][1] == ' ')
							Turn = 2;
						else if(square[0][3] == ' ')
							Turn = 3;
						else if(square[1][0] == ' ')
							Turn = 4;
						else if(square[1][1] == ' ')
							Turn = 5;
						else if(square[1][2] == ' ')
							Turn = 6;
						else if(square[2][0] == ' ')
							Turn = 7;
						else if(square[2][1] == ' ')
							Turn = 8;
						else if(square[2][2] == ' ')
							Turn = 9;
					}
				else
				for(i = b - 1; square[a][i] == Player && i >= 0; i--, horizontal++);
				for(i = b + 1; square[a][i] == Player && i <= 2; i++, horizontal++);
					if(vertical >= 2)
					{
									if (square[0][0] == ' ')
							Turn = 1;
						else if(square[0][1] == ' ')
							Turn = 2;
						else if(square[0][3] == ' ')
							Turn = 3;
						else if(square[1][0] == ' ')
							Turn = 4;
						else if(square[1][1] == ' ')
							Turn = 5;
						else if(square[1][2] == ' ')
							Turn = 6;
						else if(square[2][0] == ' ')
							Turn = 7;
						else if(square[2][1] == ' ')
							Turn = 8;
						else if(square[2][2] == ' ')
							Turn = 9;
					}
				else
				for(i = a - 1, ii = b - 1; square[i][ii] == Player && i >= 0 && ii >=0; i--, ii--, diagonal1++);
				for(i = a + 1, ii = b + 1; square[i][ii] == Player && i <= 2 && ii <=2; i++, ii++, diagonal1++);
					if(diagonal1 >= 3)
					{
										if (square[0][0] == ' ')
							Turn = 1;
						else if(square[0][1] == ' ')
							Turn = 2;
						else if(square[0][3] == ' ')
							Turn = 3;
						else if(square[1][0] == ' ')
							Turn = 4;
						else if(square[1][1] == ' ')
							Turn = 5;
						else if(square[1][2] == ' ')
							Turn = 6;
						else if(square[2][0] == ' ')
							Turn = 7;
						else if(square[2][1] == ' ')
							Turn = 8;
						else if(square[2][2] == ' ')
							Turn = 9;
					}
				else	
				for(i = a - 1, ii = b + 1; square[i][ii] == Player && i >= 0 && ii <=2; i--, ii++, diagonal2++);
				for(i = a + 1, ii = b - 1; square[i][ii] == Player && i <= 2 && ii <=0; i++, ii--, diagonal2++);
					if(diagonal2 >= 3)
					{
							if (square[0][0] == ' ')
							Turn = 1;
						else if(square[0][1] == ' ')
							Turn = 2;
						else if(square[0][3] == ' ')
							Turn = 3;
						else if(square[1][0] == ' ')
							Turn = 4;
						else if(square[1][1] == ' ')
							Turn = 5;
						else if(square[1][2] == ' ')
							Turn = 6;
						else if(square[2][0] == ' ')
							Turn = 7;
						else if(square[2][1] == ' ')
							Turn = 8;
						else if(square[2][2] == ' ')
							Turn = 9;
					}

				else
					Turn = 9;

			}
		}
		while(true)
		{
		if(charsplaced == 9)
			{
				break;
			}
		
			if(CPUCheck == 0)
			{
				cin >> Turn;
			}
		 if(Turn <=9 && Turn >=1)
			{
				break;
			}
			else
			{
				cout << "\nPlease enter a value between 1 and 9: ";
				if(cin.fail())
				{
					cin.clear();
					char c;
					cin >> c;
				}
			}
			
		}
		if(charsplaced == 9)
		{
			break;
		}
		row = Geta(Turn);
		col = Getb(row, Turn);
		Valid = move(row, col, Turn, Player); 
			if(Valid == -1)
			{
				cout << "That is an invalid move try again";
			}
			else
			{
				gamewon = CheckWin(row, col, Turn);
				charsplaced ++;
				system("cls");
				Board();
			}
}
	system("cls");
	if(charsplaced == 9)
	{
		cout << "Draw\n";
		system("pause");
		return 0;
	}

	if (Player == 'X')
	{
		cout << "You win";
	}
	else
	{
		cout << "I win";
	}
	system("pause");
	return 0;
}
int Getb(int row, int Turn)
{
		int col = (Turn - row*3)-1;
		return col;
}

int Geta(int Turn)
{
		int row =  floor(float (Turn/3));
		return row;
}

int move(int row, int col, int Turn, char Player)
{
	if (Turn >=0 && Turn <= 9)
	{
		if(square[row][col] == ' ')
			{
				square[row][col] = Player;
				return Turn;
			}
			else
			{
				return -1;
			}
	}
	
	else
	{
		cout << "please enter a value between 1 and 9: ";
		return -1;
	}
}


bool CheckWin(int a, int b, int Turn)
{
	int vertical = 1;
	int horizontal = 1;
	int diagonal1 = 1;
	int diagonal2 = 1;
	char Player = square[a][b];
	int i;
	int ii;


	for(i = a + 1; square[i][b] == Player && i <= 2; i++, vertical++);
	for(i = a - 1; square[i][b] == Player && i >= 0; i--, vertical++);
		if(vertical >= 3)
			return true;
	for(i = b - 1; square[a][i] == Player && i >= 0; i--, horizontal++);
	for(i = b + 1; square[a][i] == Player && i <= 2; i++, horizontal++);
		if(vertical >= 3)
			return true;
	for(i = a - 1, ii = b - 1; square[i][ii] == Player && i >= 0 && ii >=0; i--, ii--, diagonal1++);
	for(i = a + 1, ii = b + 1; square[i][ii] == Player && i <= 2 && ii <=2; i++, ii++, diagonal1++);
		if(diagonal1 >= 3)
			return true;
	for(i = a - 1, ii = b + 1; square[i][ii] == Player && i >= 0 && ii <=2; i--, ii++, diagonal2++);
	for(i = a + 1, ii = b - 1; square[i][ii] == Player && i <= 2 && ii <=0; i++, ii--, diagonal2++);
		if(diagonal2 >= 3)
			return true;
	return false;
}

void Board()
{
	system("cls");
	cout << "Player 1 (X) - Computer (O)" << endl << endl;
	cout << endl;

	cout << "     |     |     " << endl;
	cout << "  " << square[0][0] << "  |  " << square[0][1] << "  |  " << square[0][2] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;
	
	cout << "  " << square[1][0] << "  |  " << square[1][1] << "  |  " << square[1][2] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << square[2][0] << "  |  " << square[2][1] << "  |  " << square[2][2] << endl;

	cout << "     |     |     " << endl << endl;
}
Topic archived. No new replies allowed.
Pages: 12