Tic Tac Toe

I quickly made a tic tac toe program that works fine (some error handling isn't present but I'll put that in later) and exactly how I want it too. While I was coding it, I wasn't concerned with using the most efficient methods to implement my functions and just wrote really verbosely. Now that I have it working, is there a way I can introduce loops and stuff to make my code not as lengthy? I'm specifically looking at the symbol() and result() functions.

Also, I just started learning about classes and want to know how I could possibly rewrite my program using classes and a more object oriented approach. Any help would be 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
/*
    tictactoe.cpp
    
	This is a implementation of the popular game "Tic Tac Toe"
	in which players attempt to line up 3 X's or O's on a 3x3
	grid.
	
*/	
	


#include <iostream>


#define WIN 2
#define IN_PROGRESS 1
#define DRAW 0



using namespace std;



// The grid cells for which X or O will go into will be stored in an
// array is initially numbered for its position on 3x3 grid
char cells[10];

// Sets up a simple UI for the 3x3 grid required for play
void grid();

// Checks the state of the game. 
int result();

// resets the game board
void reset();

// if the move is valid, this updates the state of the cell
bool symbol( int player, int selection );


/********************************
 ****FUNCTION IMPLEMENTATIONS****
 ********************************/
 
bool symbol( int player, int selection ) {
	
	// if the selection is not a number between 1 and 9, return false
	if ( selection < 1 || selection > 9 )
	{
		return false;
	}
	
	char symbol;
	
		
	// determines symbol to be used based on the current player number
	if ( player == 1 )
	{
		symbol = 'X';
	}
	else
	{
		symbol = 'O';
	}
	
	
	// find the selected cell and check if it's a valid place to put
	// a the proper symbol
	
	if ( selection == 1 && cells[1] == '1' )
	{
			cells[1] = symbol;
			return true;
	}
	else if ( selection == 2 && cells[2] == '2' )
	{
			cells[2] = symbol;
			return true;
	}
	else if ( selection == 3 && cells[3] == '3' )
	{
			cells[3] = symbol;
			return true;
	}
	else if ( selection == 4 && cells[4] == '4' )
	{
			cells[4] = symbol;
			return true;
	}
	else if ( selection == 5 && cells[5] == '5' )
	{
			cells[5] = symbol;
			return true;
	}
	else if ( selection == 6 && cells[6] == '6' )
	{
			cells[6] = symbol;
			return true;
	}
	else if ( selection == 7 && cells[7] == '7' )
	{
			cells[7] = symbol;
			return true;
	}
	else if ( selection == 8 && cells[8] == '8' )
	{
			cells[8] = symbol;
			return true;
	}
	else if ( selection == 9 && cells[9] == '9' )
	{
			cells[9] = symbol;
			return true;
	}
	else
	{
			return false;
	}
	
	return false;
	
}
 
int result() {
	
	// These are all the possible winning combinations, if any of these
	// are satisfied, then it's a win
	if ( cells[1] == cells[2] && cells[2] == cells[3] )
	{
		return WIN;
	} 
	else if ( cells[4] == cells[5] && cells[5] == cells[6] ) 
	{
		return WIN;
	}
	else if ( cells[7] == cells[8] && cells[8] == cells[9] )
	{
		return WIN;
	}
	else if ( cells[1] == cells[4] && cells[4] == cells[7] )
	{
		return WIN;
	}
	else if ( cells[2] == cells[5] && cells[5] == cells[8] )
	{
		return WIN;
	}
	else if ( cells[3] == cells[6] && cells[6] == cells[9] )
	{
		return WIN;
	}
	else if ( cells[1] == cells[5] && cells[5] == cells[9] )
	{
		return WIN;
	}
	else if ( cells[3] == cells[5] && cells[5] == cells[7] )
	{
		return WIN;
	}
	// check for a draw in which every cell is occupied yet none of the possible winning combinations exist
	else if ( cells[1] != '1' && cells[2] != '2' && cells[3] != '3' && cells[4] != '4'
     && cells[5] != '5' && cells[6] != '6' && cells[7] != '7' && cells[8] != '8' && cells[9] != '9' )
	{
		return DRAW;
	}
	// if it's neither a win nor a draw, then it's still in progress
	else
	{
		return IN_PROGRESS;
	}
}

void reset() {
	
	cells[0] = '0';
	cells[1] = '1';
	cells[2] = '2';
	cells[3] = '3';
	cells[4] = '4';
	cells[5] = '5';
	cells[6] = '6';
	cells[7] = '7';
	cells[8] = '8';
	cells[9] = '9';
	
}

void grid() {
	
	
	cout << endl;

	cout << "     |     |     " << endl;
	cout << "  " << cells[1] << "  |  " << cells[2] << "  |  " << cells[3] << endl;

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

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

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

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

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

int main() {
	
	int player = 1;
	int selection = -1;
	int outcome;
	
	reset();
	
	
	do
	{	
		grid();
		player = ( player%2 )?1:2;
		
		cout << "Player " << player << ", pick a square: ";
		cin >> selection;
	
		symbol ( player, selection );
		
		outcome = result();
		
		player++;
		
	} while ( outcome == IN_PROGRESS );
	
	grid();
	
	player = player - 1;
	
	if ( outcome == WIN )
	{
		cout << "Player " << player << " wins!!!" << endl;
	}
	
	if ( outcome == DRAW )
	{
		cout << "The game is a draw!" << endl;
	}
	
}
Topic archived. No new replies allowed.