tic tac toe

Pages: 12
My first attempt at a tic tac toe game. Is there a way to make this more efficient, mainly the determine winner functions?

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class ticTacToe
{
public:
	void printBoard();
	void getMove1(int);
	void getMove2(int);
	bool isValidMove(int,int);
	bool determineWinner1();
	bool determineWinner2();
	void reset();
	ticTacToe();
private:
	char board[3][3];
};

void instructions();

int main()
{
	int choice, num;
	static ticTacToe board;
	instructions();
	cout<<"To play, press 1, to exit press 9: "<<endl;
	cin>>choice;
	do
	{
		bool status = false;
		while(status == false)
		{
		    cout<<"Enter play (player 1): "<<endl;
		    cin>>num;
		    board.getMove1(num);
		    board.printBoard();
		    if(board.determineWinner1() == true)
			{
				status = true;
				cout<<"\nPlayer 1 wins!!!"<<endl;
			}
			else
			{
				cout<<"Enter play (player 2): "<<endl;
				cin>>num;
				board.getMove2(num);
				board.printBoard();
				if(board.determineWinner2() == true)
				{
					status = true;
					cout<<"\nPlayer 2 wins!!!"<<endl;
				}
			}
		}
		board.reset();
		cout<<"\nPress 1 to play again, press 9 to exit: "<<endl;
		cin>>choice;
	}while(choice != 9);
	return 0;
}

void instructions()
{
	cout<<"Tic Tac Toe Rules:"<<endl;
	cout<<"The object of the game is to get 3 X's or 3 O's in a row."<<endl;
	cout<<"The X, player 1 gets to move first."<<endl;
	cout<<"The board locations are as follows:"<<endl;
	cout<<"    1    2    3"<<endl;
	cout<<"    4    5    6"<<endl;
	cout<<"    7    8    9"<<endl<<endl;
}

void ticTacToe::reset()
{
	for(int row = 0; row < 3; row++)
		for(int col = 0; col <3; col++)
			board[row][col] = '*';
}
void ticTacToe::printBoard()
{
	for(int row = 0; row < 3; row++)
	{
		for(int col = 0; col < 3; col++)
			cout<<setw(3)<<board[row][col]<<" ";
		cout<<endl<<endl;
	}
}

ticTacToe::ticTacToe()
{
	for(int row = 0; row < 3; row++)
		for(int col = 0; col < 3; col++)
			board[row][col] = '*';
}

void ticTacToe::getMove1(int num)
{
	bool validate = false;
	switch(num)
	{
	case 1:
		validate = isValidMove(0,0);
		if(validate == true)
			board[0][0] = 'X';
		break;
	case 2:
        validate = isValidMove(0,1);
		if(validate == true)
			board[0][1] = 'X';
		break;
	case 3:
		validate = isValidMove(0,2);
		if(validate == true)
			board[0][2] = 'X';
		break;
	case 4:
		validate = isValidMove(1,0);
		if(validate == true)
			board[1][0] = 'X';
		break;
	case 5:
        validate = isValidMove(1,1);
		if(validate == true)
			board[1][1] = 'X';
		break;
	case 6:
        validate = isValidMove(1,2);
		if(validate == true)
			board[1][2] = 'X';
		break;
	case 7:
        validate = isValidMove(2,0);
		if(validate == true)
			board[2][0] = 'X';
		break;
	case 8:
        validate = isValidMove(2,1);
		if(validate == true)
			board[2][1] = 'X';
		break;
	case 9:
        validate = isValidMove(2,2);
		if(validate == true)
			board[2][2] = 'X';
		break;
	default:
		cout<<"\nInvalid input."<<endl;
	}
}

bool ticTacToe::isValidMove(int row, int col)
{
	if(board[row][col] == 'X' || board[row][col] == 'O')
		return false;
    return true;
}

void ticTacToe::getMove2(int num)
{
    bool validate = false;
	switch(num)
	{
	case 1:
		validate = isValidMove(0,0);
		if(validate == true)
			board[0][0] = 'O';
		break;
	case 2:
        validate = isValidMove(0,1);
		if(validate == true)
			board[0][1] = 'O';
		break;
	case 3:
		validate = isValidMove(0,2);
		if(validate == true)
			board[0][2] = 'O';
		break;
	case 4:
		validate = isValidMove(1,0);
		if(validate == true)
			board[1][0] = 'O';
		break;
	case 5:
        validate = isValidMove(1,1);
		if(validate == true)
			board[1][1] = 'O';
		break;
	case 6:
        validate = isValidMove(1,2);
		if(validate == true)
			board[1][2] = 'O';
		break;
	case 7:
        validate = isValidMove(2,0);
		if(validate == true)
			board[2][0] = 'O';
		break;
	case 8:
        validate = isValidMove(2,1);
		if(validate == true)
			board[2][1] = 'O';
		break;
	case 9:
        validate = isValidMove(2,2);
		if(validate == true)
			board[2][2] = 'O';
		break;
	default:
		cout<<"\nInvalid input."<<endl;
	}
}

bool ticTacToe::determineWinner1()
{
	for(int row = 0; row < 3; row++)
	{
		int count = 0;
		for(int col = 0; col < 3; col++)
		{
			if(board[row][col] == 'X')
				count = count + 1;
			if(count >= 3)
				return true;
		}
	}
	for(int cols = 0; cols < 3; cols++)
	{
		int counter = 0;
		for(int rows = 0; rows < 3; rows++)
		{
			if(board[rows][cols] == 'X')
				counter = counter + 1;
			if(counter >= 3)
				return true;
		}
	}
	for(int row1 = 0; row1 < 3; row1++)
		for(int col1 = 0; col1 < 3; col1++)
		{
			if(board[0][0] == 'X' && board [1][1] == 'X')
				if(board[2][2] == 'X')
					return true;
			if(board[0][2] == 'X' && board[1][1] == 'X')
				if(board[2][0] == 'X')
					return true;
		}
	return false;
}

bool ticTacToe::determineWinner2()
{
	for(int row = 0; row < 3; row++)
	{
		int count = 0;
		for(int col = 0; col < 3; col++)
		{
			if(board[row][col] == 'O')
				count = count + 1;
			if(count >= 3)
				return true;
		}
	}
	for(int cols = 0; cols < 3; cols++)
	{
		int counter = 0;
		for(int rows = 0; rows < 3; rows++)
		{
			if(board[rows][cols] == 'O')
				counter = counter + 1;
			if(counter >= 3)
				return true;
		}
	}
	for(int row1 = 0; row1 < 3; row1++)
		for(int col1 = 0; col1 < 3; col1++)
		{
			if(board[0][0] == 'O' && board [1][1] == 'O')
				if(board[2][2] == 'O')
					return true;
			if(board[0][2] == 'O' && board[1][1] == 'O')
				if(board[2][0] == 'O')
					return true;
		}
	return false;
}
closed account (j3bk4iN6)
that program is awesome, one thing i did notice is that when you enter an empty line it goes into and indefinite loop. I ran it if thats ok, but I won't save it. You need to put a by: at the top in comments.
Last edited on
it just so big. What can it all do?

my little flimsy one.
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
// simple tic tac toe

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int cwin (char bb[],char *win);

int main() {
	char b[9] = {'1','2','3','4','5','6','7','8','9'};
	char win, pick;
	int run = 1, apick = 0;
	int num ,comp , tick;
	
	srand((unsigned)time(0));
	
	// game loop
	do {
		// computer play
		tick = 0; apick = 1;
		do {
			tick++;
			apick = 2;
			comp = rand() % 9;
			pick = comp + 49;
			if (b[comp] == pick) {
				b[comp] = 'o';
				apick = 0;
				}
			if (tick > 10 ) { apick = 0; cout << "computer pass\n"; }
		} while (apick == 2);
	run = cwin( b , &win );
	
	// human player	
	if (run == 1)  {
		for (int n=0; n<9; n++) { 
		  cout << b[n] ;
		  if ((n+1) % 3 == 0) {cout << "\n"; } else { cout << " | "; }
		}
		do {
			cout << "enter position: ";
			cin >> pick;
			cin.ignore(1,'\n');
		
			if (pick >= '1' && pick <= '9') {
		  		num = pick - 49;
		  		apick = 2;
		  		if (b[num] == pick) { b[num] = 'x';} else {apick = 1;}		  
	    	}
	    	else { cout << "Invalid Pick !" << "\n"; apick = 0;}
		  
			if (apick == 1) {cout << "already pick: \n"; apick = 0;}
		} while ( apick == 0 ) ;
		
		run = cwin( b , &win );
	}	
			
	} while (run == 1);
	  for (int n=0; n<9; n++) { 
		cout << b[n] ;
		if ((n+1) % 3 == 0) {cout << "\n"; } else { cout << " | "; }
	  }
	cout << "\n" << win << " wins the game\n";
	
	return 0;
}

int cwin (char bb[],char *win) {
	int run = 1;
	if (bb[0] == bb[1] && bb[0] == bb[2]) { *win = bb[0]; run = 0;}
	if (bb[3] == bb[4] && bb[3] == bb[5]) { *win = bb[3]; run = 0;}
	if (bb[6] == bb[7] && bb[6] == bb[8]) { *win = bb[6]; run = 0;}
	if (bb[0] == bb[3] && bb[0] == bb[6]) { *win = bb[0]; run = 0;}
	if (bb[1] == bb[4] && bb[1] == bb[7]) { *win = bb[1]; run = 0;}
	if (bb[2] == bb[5] && bb[2] == bb[8]) { *win = bb[2]; run = 0;}
	if (bb[0] == bb[4] && bb[0] == bb[8]) { *win = bb[0]; run = 0;}
	if (bb[2] == bb[4] && bb[2] == bb[6]) { *win = bb[2]; run = 0;}
	
	return run;
}

Your way of determining the winner is ok.
And with the added function to determine a draw...heh..oops.

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
int main()
{
	int choice, num;
	static ticTacToe board;
	instructions();
	cout<<"To play, press 1, to exit press 9: "<<endl;
	cin>>choice;
	do
	{
		bool status = false;
		while(status == false)
		{
		    cout<<"Enter play (player 1): "<<endl;
		    cin>>num;
		    board.getMove1(num);
		    board.printBoard();
			if(board.determineDraw() == true)
			{
				status = true;
				cout<<"\nDraw!!!"<<endl;
				break;
			}
		    if(board.determineWinner1() == true)
			{
				status = true;
				cout<<"\nPlayer 1 wins!!!"<<endl;
			}
			else
			{
				cout<<"Enter play (player 2): "<<endl;
				cin>>num;
				board.getMove2(num);
				board.printBoard();
				if(board.determineDraw() == true)
				{
					status = true;
					cout<<"\nDraw!!!"<<endl;
					break;
				}
				if(board.determineWinner2() == true)
				{
					status = true;
					cout<<"\nPlayer 2 wins!!!"<<endl;
				}
			}
		}
		board.reset();
		cout<<"\nPress 1 to play again, press 9 to exit: "<<endl;
		cin>>choice;
	}while(choice != 9);
	return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
bool ticTacToe::determineDraw()
{
	for(int row = 0; row < 3; row++)
	{
		for(int col = 0; col < 3; col++)
		{
			if(board[row][col] == '*')
				return false;
		}
	}
	return true;
}
closed account (j3bk4iN6)
The way it is indented is an art in and of itself.
ROFL most people don't use the tab button.
You lost me..where does the tab button come in? Sorry, I'm still a noob here, 4th week in C++.
closed account (j3bk4iN6)
maybe its just the formatting of the code tag.
Maybe, lol. I did notice that my structure is a little off, thanks a lot copy and paste, and the indentations are 8 spaces instead of 4.
Yah I meant formatting.
closed account (j3bk4iN6)
You'll notice I say alot of dumb things. I like the formating that its in on the page because its almost an art form. Like a style that could be passed on. Or a signature.
Not to say I recommend it, it was just more pleasing to the eye.
Last edited on
This weirdly spaced formatting is do do with the tab character.
If you actually use the tab character to indent your code and copy and paste into the forum this is what happens (very spread out across the page).
1
2
3
4
5
6
7
8
9
10
11
12
bool ticTacToe::determineDraw()
{
	for(int row = 0; row < 3; row++)
	{
		for(int col = 0; col < 3; col++)
		{
			if(board[row][col] == '*')
				return false;
		}
	}
	return true;
}



All programming editors that I know of has the option to substitute spaces for the tab character
when indenting.
So the code when copied and pasted now looks like this (using 4 spaces as substitute for tab)

1
2
3
4
5
6
7
8
9
10
11
12
bool ticTacToe::determineDraw()
{
    for(int row = 0; row < 3; row++)
    {
        for(int col = 0; col < 3; col++)
        {
            if(board[row][col] == '*')
                return false;
        }
    }
    return true;
}


closed account (j3bk4iN6)
sorry to take up space about this yoke88
that is the longest TicTacToe program i've ever seen.. anyway there is a TicTacToe program in the source section of this site..
closed account (j3bk4iN6)
have you run it? Its very sophisticated. The more I understand the code, the less I'm able to come up with a similiar method of how to create it. I wouldn't even know where to start with the functions

void printBoard();
void getMove1(int);
void getMove2(int);
bool isValidMove(int,int);
bool determineWinner1();
bool determineWinner2();
void reset();
ticTacToe();

and I don't see one pointer.
Last edited on
yes i understand it.. what part don't you understand?
closed account (jLNv0pDG)
Is there a way to make this more efficient

You could probably get rid of the class and just have this all take place inside main, calliing a few functions from there.

Also, because getMove and getMove2 are virtually identical functions, you could merge both into the one by expanding the parameters to include the symbol you are going to be putting on the board:

1
2
3
4
5
6
void getMove (int num, char X_or_O) {
...
		validate = isValidMove(0,0);
		if(validate == true) {
		    board[0][0] = X_or_O;
                    break;



You need to fix a gameplay bug in void ticTacToe::getMove. At the moment, trying to put a piece on a square that already has a piece on it will still cost a player his or her turn without changing the board or outputting the message "invalid move".

You'll need some way to re-call the function, (stealing the line from your main function):

1
2
3
4
5
6
7
8
9
10
11
	case 1:
		validate = isValidMove(0,0);
		if(validate == true) {
		    board[0][0] = 'X';
                }
//		if(validate == false) {
//		    cout<<"Enter play (player 1): "<<endl;
//		    cin>>num;
//		    board.getMove1(num);
//             }
               break;

Last edited on
closed account (j3bk4iN6)
Give me a break, 4 weeks into it and thats your first tic tac toe program.
That makes perfect sense, I was trying to figure out a way to implement getMove just once; it never occured to me to include another parameter. Thank you. The exercise called for the implementation of a class and certain member functions, otherwise I wouldn't have used a class for it either.

Yes this is only my fourth week, 5th now in C++, but I've been studying Software Engineering for a year and it helps with learning the syntax and semantics. Plus I have about 9 weeks of a C# course under my belt. I wish I could just focus on programming but I still have four other courses this term including trigonometry and Literature 415; TONS of reading.
Pages: 12