C++ for Tic Tac Toe, Programming Trouble

very new to this so bear with me, but the code runs the cpu move fine and after you input a move for the human it just closes out, any input would be much appreciated

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
#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;
void programFlow();
void menu();
void gameFlow();
void gameboard();
void startingmove();
void humanmove();
void computermove();
char analyzer();
char grids[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char a = 'a';
char b = 'b';
char c = 'c';
char d = 'd';
char e = 'e';
char f = 'f';
char g = 'g';
char h = 'h';
char i = 'i';
int moves=0;


int _tmain(int argc, _TCHAR* argv[])
{
	programFlow();

	return 0;
}

void programFlow()
{
	menu();
	system("cls");
	gameFlow();
}
void gameFlow()
{
	startingmove();


	
	
		while (moves <= 9)
		{
			computermove();
			gameboard();
			if (analyzer())
			{
				std::cout << "CPU WIN!\n";
				break;
			}
			if (moves != 9) humanmove();
			gameboard();
			if (analyzer)
			{
				std::cout << "HUMAN WIN!!\n";
				break;
			}
		}
		if ((!analyzer))cout << "It is a draw\n";
	}




void menu()
{
	int x;
	char yeh;
	cout << "---------------------------------------\n";
	cout << "-======X======Tic-Tac-Toe======O======-\n";
	cout << "---------------------------------------\n";
	cout << "        Welcome to Tic Tac Toe\n\n\n\n";
	cout << "press x to play game.........";
	cin >> x;
	cout << "To veiw game records press 1\n\n\n";
	// come back after game works to add in file out
}
void gameboard()
{
	cout << "     |     |     " << endl;
	cout << "  " << grids[1] << "  |  " << grids[2] << "  |  " << grids[3] << endl;

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

	cout << "  " << grids[4] << "  |  " << grids[5] << "  |  " << grids[6] << endl;

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

	cout << "  " << grids[7] << "  |  " << grids[8] << "  |  " << grids[9] << endl;

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

}

void startingmove()
{
	grids[5] = 'X';
	gameboard();
	humanmove();
}

void humanmove()
{
	int humanchoice;
	cout << "Enter your desired grid number to mark board\n";
	cin >> humanchoice;
	if (humanchoice == 1)grids[0] = 'O';
	else if (humanchoice == 2)grids[1] = 'O';
	else if (humanchoice == 3)grids[2] = 'O';
	else if (humanchoice == 4)grids[3] = 'O';
	else if (humanchoice == 5)grids[4] = 'O';
	else if (humanchoice == 6)grids[5] = 'O';
	else if (humanchoice == 7)grids[6] = 'O';
	else if (humanchoice == 8)grids[7] = 'O';
	else if (humanchoice == 9)grids[8] = 'O';

}

void computermove()
{
	int computerchoice;
	computerchoice = analyzer();
	if (computerchoice == a) grids[0] = 'X';
	else if (computerchoice == b) grids[1] = 'X';
	else if (computerchoice == c) grids[2] = 'X';
	else if (computerchoice == d) grids[3] = 'X';
	else if (computerchoice == e) grids[4] = 'X';
	else if (computerchoice == f) grids[5] = 'X';
	else if (computerchoice == g) grids[6] = 'X';
	else if (computerchoice == h) grids[7] = 'X';
	else if (computerchoice == i) grids[8] = 'X';

}

char analyzer()
{
	//check and place for two in a row
	if (grids[1] == 'O' && grids[2] == grids[1] && grids[3] != 'X')//Horizontal 1-3
		return c;
	else if (grids[1] == 'O' && grids[3] == grids[1] && grids[2] != 'X')//Horizontal 1-2
		return b;
	else if (grids[2] == 'O' && grids[3] == grids[2] && grids[1] != 'X')//Horizontal 1-1
		return a;
	else if (grids[7] == 'O' && grids[8] == grids[7] && grids[9] != 'X')//Horizontal 3-3
		return i;
	else if (grids[7] == 'O' && grids[9] == grids[7] && grids[8] != 'X')//Horizontal 3-2
		return h;
	else if (grids[8] == 'O' && grids[9] == grids[8] && grids[7] != 'X')//Horizontal 3-1
		return g;
	else if (grids[1] == 'O' && grids[4] == grids[1] && grids[7] != 'X')//Vertical 1-3
		return g;
	else if (grids[1] == 'O' && grids[7] == grids[1] && grids[4] != 'X')//Vertical 1-2
		return d;
	else if (grids[4] == 'O' && grids[7] == grids[4] && grids[1] != 'X')//Vertical 1-1
		return a;
	else if (grids[3] == 'O' && grids[6] == grids[3] && grids[9] != 'X')//Vertical 3-3
		return i;
	else if (grids[3] == 'O' && grids[9] == grids[3] && grids[6] != 'X')//Vertical 3-2
		return f;
	else if (grids[6] == 'O' && grids[9] == grids[6] && grids[3] != 'X')//Vertical 3-1
		return c;

	//dont fall into traps
	else if (grids[5] == 'X' && grids[1] == 'O' && grids[3] != 'O' && grids[3] != 'X')//play corner3
		return c;
	else if (grids[5] == 'X' && grids[3] == 'O' && grids[1] != 'O' && grids[1] != 'X')//play corner1
		return a;
	else if (grids[5] == 'X' && grids[9] == 'O' && grids[7] != 'O' && grids[7] != 'X')//play corner7
		return g;
	else if (grids[5] == 'X' && grids[7] == 'O' && grids[9] != 'O' && grids[9] != 'X')//play corner9
		return i;
	//try to win
	else if (grids[1] == 'X' && grids[2] == grids[1] && grids[3] != 'O')//Horizontal 1-3
		return c;
	else if (grids[1] == 'X' && grids[3] == grids[1] && grids[2] != 'O')//Horizontal 1-2
		return b;
	else if (grids[2] == 'X' && grids[3] == grids[2] && grids[1] != 'O')//Horizontal 1-1
		return a;
	else if (grids[4] == 'X' && grids[5] == grids[4] && grids[6] != 'O')//Horizontal 2-3
		return f;
	else if (grids[6] == 'X' && grids[5] == grids[6] && grids[4] != 'O')//Horizontal 2-2
		return d;
	else if (grids[7] == 'X' && grids[8] == grids[7] && grids[9] != 'O')//Horizontal 3-3
		return i;
	else if (grids[7] == 'X' && grids[9] == grids[7] && grids[8] != 'O')//Horizontal 3-2
		return h;
	else if (grids[8] == 'X' && grids[9] == grids[8] && grids[7] != 'O')//Horizontal 3-1
		return g;
	else if (grids[1] == 'X' && grids[4] == grids[1] && grids[7] != 'O')//Vertical 1-3
		return g;
	else if (grids[1] == 'X' && grids[7] == grids[1] && grids[4] != 'O')//Vertical 1-2
		return d;
	else if (grids[4] == 'X' && grids[7] == grids[4] && grids[1] != 'O')//Vertical 1-1
		return a;
	else if (grids[2] == 'X' && grids[5] == grids[2] && grids[8] != 'O')//Vertical 1-2
		return h;
	else if (grids[8] == 'X' && grids[5] == grids[8] && grids[2] != 'O')//Vertical 1-1
		return b;
	else if (grids[3] == 'X' && grids[6] == grids[3] && grids[9] != 'O')//Vertical 3-3
		return i;
	else if (grids[3] == 'X' && grids[9] == grids[3] && grids[6] != 'O')//Vertical 3-2
		return f;
	else if (grids[6] == 'X' && grids[9] == grids[6] && grids[3] != 'O')//Vertical 3-1
		return c;
	else if (grids[1] == 'X' && grids[5] == grids[1] && grids[9] != 'O')//Diagonal 1-3
		return i;
	else if (grids[9] == 'X' && grids[5] == grids[9] && grids[1] != 'O')//Diagonal 1-1
		return a;
	else if (grids[3] == 'X' && grids[5] == grids[3] && grids[7] != 'O')//Diagonal 2-1
		return g;
	else if (grids[7] == 'X' && grids[5] == grids[7] && grids[3] != 'O')//Diagonal 2-3
		return c;
	// place move regardless
	else if (grids[1] != 'O' && grids[1] != 'X') //1
		return a;
	else if (grids[2] != 'O' && grids[2] != 'X') //2
		return b;
	else if (grids[3] != 'O' && grids[3] != 'X') //3
		return c;
	else if (grids[4] != 'O' && grids[4] != 'X') //4
		return d;
	else if (grids[5] != 'O' && grids[5] != 'X') //5
		return e;
	else if (grids[6] != 'O' && grids[6] != 'X') //6
		return f;
	else if (grids[7] != 'O' && grids[7] != 'X') //7
		return g;
	else if (grids[8] != 'O' && grids[8] != 'X') //8
		return h;
	else if (grids[9] != 'O' && grids[9] != 'X') //9
		return i;
}

int checkwin()
{
	if (grids[1] == grids[2] && grids[2] == grids[3])

		return 1;
	else if (grids[4] == grids[5] && grids[5] == grids[6])

		return 1;
	else if (grids[7] == grids[8] && grids[8] == grids[9])

		return 1;
	else if (grids[1] == grids[4] && grids[4] == grids[7])

		return 1;
	else if (grids[2] == grids[5] && grids[5] == grids[8])

		return 1;
	else if (grids[3] == grids[6] && grids[6] == grids[9])

		return 1;
	else if (grids[1] == grids[5] && grids[5] == grids[9])

		return 1;
	else if (grids[3] == grids[5] && grids[5] == grids[7])

		return 1;
	else if (grids[1] != '1' && grids[2] != '2' && grids[3] != '3'
		&& grids[4] != '4' && grids[5] != '5' && grids[6] != '6'
		&& grids[7] != '7' && grids[8] != '8' && grids[9] != '9')

		return 0;
	else
		return -1;
}
Morning.
Only had a quick look but, on lines 58 and 64 you're not calling your analyser function properly.
on line 57, is your call to gameboard supposed to be part of the 'if'? it's difficult to tell as you've no braces around statements. i tend to put braces on if's even when there's only one line in it.

edit:
wait a sec.. You're calling analyser like this:
if (analyzer())
etc etc..
which implies to me that the function returns a bool, but it's actually returning a char??
Last edited on
OK I see my main function was all kinda of crazy so I am attempting to correct it, but I am at a loss. I have been looking at other codes to try and understand it, but I just can not seem to grasp it. However, the program just runs forever without stopping and I am trying to figure out how to make the game pause so that a cpu and human move is made everytime

So I am trying to reformulate the main into something along the lines of

int main()
{
gameflow()
}

void gameflow()
{
startingmove();

while (checkwin == false)
{
computermove();
checkwin();
humanmove();
checkwin();
gameboard();
}
}
Topic archived. No new replies allowed.