TiC tAc ToE game in C++

hi! i have made my very first game just last night. ^___^ it is a simple tic tac toe game that can be played by either 2 players or one against the computer. It worked fine on well... my opinion. but i really need comments and suggestions on how to improve it. tnx.

PS. in ai(), I set the value of x and y to rand() thus making the computer opponent less challenging. can this be improve? how?

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
#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>

using namespace std;
int x,y,a,b,c;
int playerOne_win[4];
int playerTwo_win[4];
string ttt[3][3];
// THss will display the game situation ;P
void ticTacToe(){
	cout<<" _ _ _\n";
	for(a=0;a<3;a++){
		for (b=0;b<3;b++){
			cout<<"|"<<ttt[a][b];
		}
		cout<<"|\n";
	}
}

// This will handle and declare the winner
void check(){
	ticTacToe();
	for(a=0;a<3;a++){
		for(b=0;b<4;b++){
			playerOne_win[b]=0;
			playerTwo_win[b]=0;
		}
		for (b=0;b<3;b++){
			if(ttt[a][b]=="O"){
				playerOne_win[0]++;
			}
			if(ttt[b][a]=="O"){
				playerOne_win[1]++;
			}
			if(ttt[a][b]=="X"){
				playerTwo_win[0]++;
			}
			if(ttt[b][a]=="X"){
				playerTwo_win[1]++;
			}
		}
		b=0;
		for(c=2;c>=0;c--){
			
			if(ttt[c][c]=="O"){
				playerOne_win[2]++;
			}
			if(ttt[c][c]=="X"){
				playerTwo_win[2]++;
			}
			if(ttt[b][c]=="O"){
				playerOne_win[3]++;
			}
			if(ttt[b][c]=="X"){
				playerTwo_win[3]++;
			}
			b++;
		}
		for(b=0;b<4;b++){
			if(playerOne_win[b]==3){
				cout<<"\nCONGRATULATIONS!\nPlayer one won!";
				exit(EXIT_SUCCESS);
			}
			if(playerTwo_win[b]==3){
				cout<<"\nCONGRATULATIONS!\nPlayer two/Computer won!";
				exit(EXIT_SUCCESS);
			}
		}
	}
	
}
// This one handles player one
void player1(){
	check();
	cout<<endl;
	cout<<"\nPlayer one's turn: ";
	cin>>x>>y;
	
	if((ttt[x][y]=="X")||(ttt[x][y]=="O")){
		cout<<"\nIllegal move! you missed a turn.\n";
	}
	else{
		ttt[x][y]="O";
	}
}
// This one handles player two
void player2(){
	check();
	cout<<endl;
	cout<<"\nPlayer two's turn: ";
	cin>>x>>y;
	
	if((ttt[x][y]=="X")||(ttt[x][y]=="O")){
		cout<<"Illegal move! you missed a turn.\n";
	}
	else{
		ttt[x][y]="X";
	}
}
// this handles the computer
void ai(){
	int aiTerminator;
	aiTerminator=0;
	check();
	cout<<endl;
	cout<<"\nComputer's turn: ";

	while(aiTerminator==0){
		srand(time(NULL));
		x=rand() % 3+0;
		y=rand() % 3+0;
	
		if((ttt[x][y]=="X")||(ttt[x][y]=="O")){
			aiTerminator=0;
		}
		else{
			cout<<x<<" "<<y<<endl;
			ttt[x][y]="X";
			aiTerminator++;
		}
	}
}
// 
int main(){
	char choice;
	for(a=0;a<3;a++){
		for(int b=0;b<3;b++){
			ttt[a][b]="_";
		}
	}
	cout<<"Play TiCtAcToE!\n\n";
	
	cout<<"Instruction: \n\n";
	for(int a=0;a<3;a++){
		cout<<"     ";
		for(int b=0;b<3;b++){
			cout<<"| "<<a<<" "<<b<<" ";
		}
		cout<<"|\n\n";
	}
	cout<<"NEW GAME!\n\n";
	cout<<"    A.) Play with a friend.\n";
	cout<<"    B.) Play against the computer.\n\n";
	
	cout<<"Your call! ";
	
	while(true){
		cin>>choice;
		switch(choice){
			case 'A':
				while(true){
					player1();
					player2();
				}
					break;
			case 'B':
				while(true){
					player1();
					ai();
				}
					break;
			default:
				cout<<"what? ";
				break;
		}
	}
	return 0;
}


It is a random position ai.
It doesn't know the best move.
You could brute force a spanning tree of the best possible move the AI can make.
(Takes a while)
But then the ai will be unbeatable if it goes first.

e.g.
if first, go to a random corner.
based on where your moves are, and enemy move is, go somewhere else.
Write out all possible locations.

Or you could make a semi-smart AI.
If x[0] && x[1] == "X", put X in x[2] (or 0) depending on his piece.
DO that or all rows/colums/diagonals, should be 20 different possibilities just for 2 in a row and blocking/finishing the 3rd.
Last edited on
Topic archived. No new replies allowed.