First Tic Tac Toe

I am attempting to write a Tic Tac Toe program and this is my starting code. Any pointer on how to get this going? I can not get the function getGameState to work. I have been coding for about 4 weeks .. I am not sure what I am doing wrong here. Any help would be awesome.

[#include <iostream> // allows input and output.
#include <iomanip> // allows stream manipulators.
#include <cstdlib> // changes numbers to text.

using namespace std; //

const char initializeGame = 10; // constant variable for array size.
char ticTac[initializeGame] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

int getGameState(); // function prototype for getting the game state
void displayGameBoard(); // function prototype for displaying the gameboard
void playerXTurn();
void playerOTurn();

int main()
{
int x;
int o;

do
{
x = getGameState();
playerXTurn();
getGameState();

o = getGameState();
playerOTurn();
getGameState();

} while ( 'x' || 'o' == 0);

displayGameBoard();

if (x == 1) // if playerX has a complete row
{
cout << " Congratulations, X Won!!! " << endl;
cout << " \rWould you like to play again ? Y/N " << endl;
}

else if (o == 2) // if playerO has a complete row
{
cout << " Congratultions, O Won!!! " << endl;
cout << " \rWould you like to play again ? Y/N " << endl;
}

else // if the game is a draw
{
cout << " This game is a draw ! " << endl;
cout << "\rWould you like to play again ? Y/N " << endl;
}

}


//===========================================================================================
//
//
//
//
//
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv


void playerXTurn()
{

int playerX;

char markX = 'X';

displayGameBoard();

cout << " Enter a location for 'X' or enter <T> to terminate the game: " << endl
<< " Please enter your selection > ";
cin >> playerX;


if (playerX == 1 && ticTac[1] == '1')
{
ticTac[1] = markX;
} // end if playerX for ticTac[1]

else if (playerX == 2 && ticTac[2] == '2')
{
ticTac[2] = markX;
} // end if... else playerX for ticTac[2]

else if (playerX == 3 && ticTac[3] == '3')
{
ticTac[3] = markX;
} // end if... else playerX for ticTac[3]

else if (playerX == 4 && ticTac[4] == '4')
{
ticTac[4] = markX;
} // end if... else playerX for ticTac[4]

else if (playerX == 5 && ticTac[5] == '5')
{
ticTac[5] = markX;
} // end if... else playerX for ticTac[5]

else if (playerX == 6 && ticTac[6] == '6')
{
ticTac[6] = markX;
} // end if... else playerX for ticTac[6]

else if (playerX == 7 && ticTac[7] == '7')
{
ticTac[7] = markX;
} // end if... else playerX for ticTac[7]

else if (playerX == 8 && ticTac[8] == '8')
{
ticTac[8] = markX;
} // end if... else playerX for ticTac[8]

else if (playerX == 9 && ticTac[9] == '9')
{
ticTac[9] = markX;
} // end if... else playerX for ticTac[9]



else if (playerX != 1, 2, 3, 4, 5, 6, 7, 8, 9)
{
cout << " Selection is out of range, select a location between 1 and 9 " << endl;

}

else
{
cout << "This space is already occuppied, select another space." << endl;

}





} // end playerXTurn function

//===========================================================================================
//
//
//
//
//
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

void playerOTurn()

{

int playerO;
char markO = 'O';

displayGameBoard();

cout << " Enter a location for 'O' or enter <T> to terminate the game: " << endl
<< " Please enter your selection > ";
cin >> playerO;

if (playerO == 1 && ticTac[1] == '1')
{
ticTac[1] = markO;
} // end if... else playerO for ticTac[1]

else if (playerO == 2 && ticTac[2] == '2')
{
ticTac[2] = markO;
} // end if... else playerO for ticTac[2]

else if (playerO == 3 && ticTac[3] == '3')
{
ticTac[3] = markO;
} // end if... else playerO for ticTac[3]

else if (playerO == 4 && ticTac[4] == '4')
{
ticTac[4] = markO;
} // end if... else playerO for ticTac[4]

else if (playerO == 5 && ticTac[5] == '5')
{
ticTac[5] = markO;
} // end if... else playerO for ticTac[5]

else if (playerO == 6 && ticTac[6] == '6')
{
ticTac[6] = markO;
} // end if... else playerO for ticTac[6]

else if (playerO == 7 && ticTac[7] == '7')
{
ticTac[7] = markO;
} // end if... else playerO for ticTac[7]

else if (playerO == 8 && ticTac[8] == '8')
{
ticTac[8] = markO;
} // end if... else playerO for ticTac[8]

else if (playerO == 9 && ticTac[9] == '9')
{
ticTac[9] = markO;
} // end if... else playerO for ticTac[9]

else if (playerO != 1, 2, 3, 4, 5, 6, 7, 8, 9)
{
cout << " Selection is out of range, select a location between 1 and 9 " << endl;

}

else
{
cout << "This space is already occuppied, select another space." << endl;
}




} // end playerOTurn function

//==============================================================================================
// Function name: displayGameBoard
// Description : This function display the game board on a with a new clear screen everytime
// it is called.
//
// Input Parameters: none
//
// Return Value: none
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

void displayGameBoard() // shows gameboard and keeps track the players marked positions
{
system("cls"); // clears screen
cout << "\t\t\tTic-Tac-Toe\n"
<< "\t\t\t===========\n\r\r\r" << endl

<< " | | " << endl
<< " " << ticTac[1] << " | " << ticTac[2] << " | " << ticTac[3] << endl

<< "_____|_____|_____" << endl
<< " | | " << endl

<< " " << ticTac[4] << " | " << ticTac[5] << " | " << ticTac[6] << endl

<< "_____|_____|_____" << endl
<< " | | " << endl

<< " " << ticTac[7] << " | " << ticTac[8] << " | " << ticTac[9] << endl
<< " | | " << endl << endl << endl;

} // end displayGameBoard function

//==============================================================================================
// Function Name: getGameState
// Description: This function when called checks that status of the game
//
// Input Parameters:
//
// Return Value: integer that is either a 0,1,2, or -1.
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

int getGameState()
{

if (ticTac[1] == ticTac[2] && ticTac[2] == ticTac[3] && ticTac[1] == 'X')
{
return 1;
}

else if (ticTac[4] == ticTac[5] && ticTac[5] == ticTac[6] && ticTac[4] == 'X')
{
return 1;
}

else if (ticTac[7] == ticTac[8] && ticTac[8] == ticTac[9] && ticTac[7] == 'X')
{
return 1;
}

else if (ticTac[1] == ticTac[5] && ticTac[5] == ticTac[9] && ticTac[1] == 'X')
{
return 1;
}

else if (ticTac[3] == ticTac[5] && ticTac[5] == ticTac[7] && ticTac[3] == 'X')
{
return 1;
}

else if (ticTac[1] == ticTac[4] && ticTac[4] == ticTac[7] && ticTac[1] == 'X')
{
return 1;
}

else if (ticTac[2] == ticTac[5] && ticTac[5] == ticTac[8] & ticTac[2] == 'X')
{
return 1;
}

else if (ticTac[3] == ticTac[6] && ticTac[6] == ticTac[9] == ticTac[3] == 'X')
{
return 1;

}

else if (ticTac[1] == ticTac[2] && ticTac[2] == ticTac[3] && ticTac[1] == 'O')
{
return 2;
}

else if (ticTac[4] == ticTac[5] && ticTac[5] == ticTac[6] && ticTac[4] == 'O')
{
return 2;
}

else if (ticTac[7] == ticTac[8] && ticTac[8] == ticTac[9] && ticTac[7] == 'O')
{
return 2;
}

else if (ticTac[1] == ticTac[5] && ticTac[5] == ticTac[9] && ticTac[1] == 'O')
{
return 2;
}

else if (ticTac[3] == ticTac[5] && ticTac[5] == ticTac[7] && ticTac[3] == 'O')
{
return 2;
}

else if (ticTac[1] == ticTac[4] && ticTac[4] == ticTac[7] && ticTac[1] == 'O')
{
return 2;
}

else if (ticTac[2] == ticTac[5] && ticTac[5] == ticTac[8] & ticTac[2] == 'O')
{
return 2;
}

else if (ticTac[3] == ticTac[6] && ticTac[6] == ticTac[9] == ticTac[3] == 'O')
{
return 2;

}

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

{
return 0;
}

else
{
return -1;
}

} // end getGameState function
I have you tried to do this
 
if (ticTac[1] == ticTac[2] && ticTac[2] == ticTac[3] && ticTac[1] == ticTac[3]&& ticTac[1]=='O')

or
 
if (ticTac[1] == 'O' && ticTac[2] == 'O' && ticTac[3] == 'O')


?

._.) I've make one a few weeks ago, and the way i did it is, i store an occupied space in board for both player inside array. for example
1
2
3
X  2  3
4  O  6
O  X  9

playerX[]={8,1}
playerO[]={5,7}

after that inside getGameState I just need to search the number combination to win the game inside that array, its something like "if playerX have 1,2,3 inside his/her array then he/she win"
Last edited on
// Return Value: integer that is either a 0,1,2, or -1.
You should indicate what these values mean. It looks like 1 means X won, 2 means O won, 0 means it's a draw and -1 means nobody has won. The rest of my post will assume this is right.

The consistent problem I see in your code is that you're not taking advantage of variables. For example, the problem in getGameState() is that somewhere in that mass of if/then/else, there's a bug. It only takes one misplaced number to screw it up. You can simplify this stuff by using loops instead:
1
2
3
4
5
6
7
8
// Check if X won on a row:
for (int start = 1; start <= 9; start += 3) {
    if (ticTac[start] == ticTac[start+1] &&
        ticTac[start] == ticTac[start+2] &&
        ticTac[start] == 'X') {
           return 1;
    }
}


The first time through the loop start is 1 so it compares squares 1,2 and 3.
The second time through start is 4 and it compares 4,5,6. Then it compares 7 8 9.

Similar code can check for a column:
1
2
3
4
5
6
7
8
// Check if X won on a column:
for (int start = 1; start <= 3; ++start) {
    if (ticTac[start] == ticTac[start+3] &&
        ticTac[start] == ticTac[start+6] &&
        ticTac[start] == 'X') {
           return 1;
    }
}


You'll have to check the diagonals the hard way:
1
2
3
if (ticTac[1] == ticTac[5] && ticTac[1] == ticTac[9] && ticTac[1] == 'X')
    return 'X';
// and similar code for the other diagonal. 


You can also use a loop to see if it's a draw. After you've determined that nobody has won:
1
2
3
4
for (int i=1; i<10; ++i) {
    if (ticTac[i] != i) return -1;   // board not full
}
return 1;  // board is full 

See how this works? Rather than explicityly checking ticTac[1], ticTac[2] etc., I use different values of i to check each square.

In your main program, you have variables x and o, but really what you want is the a variable that records the state of the board. Just call it state. This will make some bugs in the main loop more obvious. In your main loop you want to do this:
1
2
3
4
5
6
7
8
9
10
playerXTurn();
if (getGameState() == 1) {
    // say X won
    break;
}
playerOTurn()
if (getGameState() == 2) {
   // say O won
   break;
}


On the same theme of not using variables effectively, look at playerXTurn(). You can get rid of almost all the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void playerXTurn()
{
int square;
char mark = 'X';
displayGameBoard();

cout << " Enter a location for 'X' or enter <T> to terminate the game: " << endl
<< " Please enter your selection > ";
cin >> square;

if (square <1 || square > 9)
{
cout << " Selection is out of range, select a location between 1 and 9 " << endl;
} else if (ticTac[square] != square)
{
cout << "This space is already occuppied, select another space." << endl;
} else {
  ticTac[square] = mark;
}


Do the same thing with playerOMove. When you're done, notice that playerXMove() and playerOMove() are nearly identical. They should differ by just one line of code. Can you create a single function called playerMove() that can replace them both? Hint: pass a parameter to playerMove() and call it with different values of the parameter.

I hope this helps.
Thank you both for the insight and help with this. I will post my finished result when it is complete.
Alright, so here is the updated code. Everything thing seems to work except for the exits. I am trying to use a bool value to get the loops in playerXTurn to work, but I am not sure if that is even the correct way to go about it. They seem to be working if some enters in the wrong value or a spot that someone has already marked but I can not get it to work if the some hits 'T' to exit after someone has won. Also, what would you suggest to exit out of the game completely?


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
#include <iostream> // allows input and output.
#include <iomanip> // allows stream manipulators.
#include <cstdlib> // changes numbers to text. 

using namespace std; // 

const char ticTacBoard = 10; // constant variable for array size.
char ticTac[ticTacBoard] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

int getGameState(); // function prototype for getting the game state
void initializeGame(); // clears the board for a new game
void displayGameBoard(); // function prototype for displaying the gameboard
void playerXTurn();  // function prototype for playerXTurn
void playerOTurn(); // function prototype for playerOTurn

int main()
{
	int state;
	char playAgain;
	state = getGameState();
	do
	{
		do
		{

			playerXTurn();
			getGameState();
			if (getGameState() == 1)
			{
				cout << " Congratulations, X Won!!! " << endl;
				break;

			}

			playerOTurn();
			getGameState();
			if (getGameState() == 2)
			{
				cout << " Congratulations, O Won!!! " << endl;
				break;
			}

			else (getGameState() == 0); // if the game is a draw
			{
				cout << " This game is a draw ! " << endl;

			}

			displayGameBoard();

		} while (state == -1);


		cout << "\r\rWould you like to play again ? Y/N ";
		cin >> playAgain;

		if (playAgain == 'Y' || 'y')
		{
			initializeGame();
			displayGameBoard();
		}


	} while (playAgain =! 'N'|| 'n');
	

	cout << "Goodbye!! Thanks for playing";

}


//===========================================================================================
// Function name : playerXTurn()
// Description : This function prompts playerX to enter place to mark 'X'.
//				 
// Input Parameters: none
//
// Return Value: none 
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv


void playerXTurn()
{

	bool term = true;

	do
	{

		int playerX;

		char markX = 'X';

		displayGameBoard();

		cout << " Enter a location for 'X' or enter <T> to terminate the game: " << endl
			<< " Please enter your selection > ";
		cin >> playerX;

		if (playerX <1 || playerX > 9)
		{
			cout << " Selection is out of range, select a location between 1 and 9 " << endl;

			term = false;
		}

		else if (ticTac[playerX] == 'X' || ticTac[playerX] == 'O')
		{
			cout << "This space is already occuppied, select another space." << endl;
			term = false;
		}

		else
		{
			ticTac[playerX] = markX;
		}

		displayGameBoard();

	} while (term == false);

} // end playerXTurn function 

//===========================================================================================
// Function name : playerOTurn()
// Description : This function prompts playerX to enter place to mark 'O'.
//				 
// Input Parameters: none
//
// Return Value: none
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

void playerOTurn()

{
	bool term = true;
	do
	{

		int playerO;

		char markO = 'O';

		displayGameBoard();

		cout << " Enter a location for 'O' or enter <T> to terminate the game: " << endl
			<< " Please enter your selection > ";
		cin >> playerO;

		if (playerO <1 || playerO > 9)
		{
			cout << " Selection is out of range, select a location between 1 and 9 " << endl;
			 term = false;
		}

		else if (ticTac[playerO] == 'X' || ticTac[playerO] == 'O')
		{
			cout << "This space is already occuppied, select another space." << endl;
			term = false;
		}

		else
		{
			ticTac[playerO] = markO;
			term = true;
		}

		displayGameBoard();
	} while (term == false);
} // end playerOTurn function

//==============================================================================================
// Function name: displayGameBoard
// Description : This function display the game board on a with a new clear screen everytime 
//				 it is called.
// 
// Input Parameters: none
//
// Return Value: none 
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

void initializeGame()
{
	ticTac[1] = '1';
	ticTac[2] = '2';
	ticTac[3] = '3';
	ticTac[4] = '4';
	ticTac[5] = '5';
	ticTac[6] = '6';
	ticTac[7] = '7';
	ticTac[8] = '8';
	ticTac[9] = '9';
}

void displayGameBoard() // shows gameboard and keeps track the players marked positions
{
	system("cls"); // clears screen 
	cout << "\t\t\tTic-Tac-Toe\n"
		<< "\t\t\t===========\n\r\r\r" << endl

		<< "     |     |     " << endl
		<< "  " << ticTac[1] << "  |  " << ticTac[2] << "  |  " << ticTac[3] << endl

		<< "_____|_____|_____" << endl
		<< "     |     |     " << endl

		<< "  " << ticTac[4] << "  |  " << ticTac[5] << "  |  " << ticTac[6] << endl

		<< "_____|_____|_____" << endl
		<< "     |     |     " << endl

		<< "  " << ticTac[7] << "  |  " << ticTac[8] << "  |  " << ticTac[9] << endl
		<< "     |     |     " << endl << endl << endl;

} // end displayGameBoard function

//==============================================================================================
// Function Name: getGameState
// Description: This function when called checks that status of the game
//
// Input Parameters:
//
// Return Value: Integer that is either a 0,1,2, or -1.
//               1 = X has won, 2 = O has won, 0 = draw, and -1 = game in progress.
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

int getGameState()
{
	for (int start = 1; start <= 9; start += 3)
	{
		if (ticTac[start] == ticTac[start + 1] &&
			ticTac[start] == ticTac[start + 2] &&
			ticTac[start] == 'X')
		{
			return 1;
		}
	}

	for (int start = 1; start <= 3; ++start)
	{
		if (ticTac[start] == ticTac[start + 3] &&
			ticTac[start] == ticTac[start + 6] &&
			ticTac[start] == 'X')
		{
			return 1;
		}
	}

	for (int start = 1; start <= 3; ++start)
	{
		if (ticTac[start] == ticTac[start + 3] &&
			ticTac[start] == ticTac[start + 6] &&
			ticTac[start] == 'O')
		{
			return 2;
		}
	}

	for (int start = 1; start <= 9; start+=3)
	{
		if (ticTac[start] == ticTac[start + 1] &&
			ticTac[start] == ticTac[start + 2] &&
			ticTac[start] == 'O')
		{
			return 2;
		}
	}

	if (ticTac[1] == ticTac[5] && ticTac[1] == ticTac[9] && ticTac[1] == 'X')
	{
		return 1;
	}

	else if (ticTac[3] == ticTac[5] && ticTac[3] == ticTac[7] && ticTac[3] == 'X')
	{
		return 1;
	}

	else if (ticTac[1] == ticTac[5] && ticTac[1] == ticTac[9] && ticTac[1] == 'O')
	{
		return 2;
	}

	else if (ticTac[3] == ticTac[5] && ticTac[3] == ticTac[7] && ticTac[3] == 'O')
	{
		return 2;
	}

	for (char i = 1; i < 10; ++i)
	{
		if (ticTac[i] != i)
		{
			return -1;  // game board is not full, game in progress
		}
	} return 0; //  game board is full with no winner... Draw

} // end getGameState function 
Last edited on
Please put code inside code tags so we get line numbers and the formatting. See the right side of the edit window.

I can not get it to work if the some hits 'T' to exit

Change playerXTurn() to return a bool. Return true if the player wants to continue and false if they want to quit. Then the call to playerXTurn() becomes sometime like:
1
2
3
4
if (!playerXTurn()) {
    cout << "Good bye!\n";
    return 0;
}


Your main loop has some unnecessary code in it:
1
2
3
4
5
6
	    playerXTurn();
	    getGameState();
	    if (getGameState() == 1) {
		cout << " Congratulations, X Won!!! " << endl;
		break;
	    }
Line 2 isn't necessary.

Here's a nasty bug: you forgot an if in main() and unfortunately the result is still legal C++. The correct code is:
1
2
3
4
...
else if (getGateState() == 0) {
    cout << "This game is a draw!" << end;
}


The comment for function displayGameBoard is above function initializeGame() instead of displayGameBoard()
Alright, so I got most of the program working except for the 'for' loop that checks for a draw. I do not see what I am missing. Any hints?

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
int getGameState()
{
	int drawCounter = 1;

		for (int start = 1; start <= 9; start += 3)
		{
			if (ticTac[start] == ticTac[start + 1] &&
				ticTac[start] == ticTac[start + 2] &&
				ticTac[start] == 'X')
			{
				return 1;
			}
		}

		for (int start = 1; start <= 3; ++start)
		{
			if (ticTac[start] == ticTac[start + 3] &&
				ticTac[start] == ticTac[start + 6] &&
				ticTac[start] == 'X')
			{
				return 1;
			}
		}

		for (int start = 1; start <= 3; ++start)
		{
			if (ticTac[start] == ticTac[start + 3] &&
				ticTac[start] == ticTac[start + 6] &&
				ticTac[start] == 'O')
			{
				return 2;
			}
		}

		for (int start = 1; start <= 9; start += 3)
		{
			if (ticTac[start] == ticTac[start + 1] &&
				ticTac[start] == ticTac[start + 2] &&
				ticTac[start] == 'O')
			{
				return 2;
			}
		}

		if (ticTac[1] == ticTac[5] && ticTac[1] == ticTac[9] && ticTac[1] == 'X')
		{
			return 1;
		}

		else if (ticTac[3] == ticTac[5] && ticTac[3] == ticTac[7] && ticTac[3] == 'X')
		{
			return 1;
		}

		else if (ticTac[1] == ticTac[5] && ticTac[1] == ticTac[9] && ticTac[1] == 'O')
		{
			return 2;
		}

		else if (ticTac[3] == ticTac[5] && ticTac[3] == ticTac[7] && ticTac[3] == 'O')
		{
			return 2;
		}

		for (int i = 1; i < 9; i++)
		{
			if (ticTac[i] != i)
			{
				return -1;  // game board is not full, game in progress
			}
		}

		for (int x = 1; x < 10; x++)
		{
			if (ticTac[x] == 'X' || ticTac[x] == 'O')
			{
				drawCounter++;
			}
		}

		if (drawCounter == 9)
		{
			return 3;  // game board is full.. game is a draw
		}

} // end getGameState function [output][/output] 
You initialize drawCounter to 1 at line 3. So when all squares are taken, you increment it 9 times to a final value of 10.

The loop at line 65 should go until i <= 9, not i < 9.

Actually once you fix line 65, you can replace lines 73-84 with return 3;. If you don't return at line 69 then the board must be full and the game is a draw.

You could improve the code further by noticing that many of the loops are nearly identical. For example, lines 5-13 are almost the same as lines 35-43. If you redefine the function to return:
1
2
3
4
// Return Value: Integer:
//               'X' = X has won, 'O' = O has won, zero = draw, and -1 = game in progress.
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
 


Then the two loops can be combined as:
1
2
3
4
5
6
7
8
for (int start = 1; start <= 9; start += 3)
{
	if (ticTac[start] == ticTac[start + 1] &&
		ticTac[start] == ticTac[start + 2])
	{
		return ticTac[start];  // This will be either 'X' or 'O'
	}
}


Remember that you'll need to change the code that calls getGameState to deal with the new return values.
Ahhh, I see the light. I actually just found mistake and was getting on to say thanks for the help. I will start on improving the code now and post the finish product a bit later in the week. Thank you again for the insight, it has help tremendously.
Any time you find yourself writing similar code over and and over, step back and ask yourself how combine them. Sometimes you need a function, sometimes you can combine it like this.
Topic archived. No new replies allowed.