-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe
More file actions
130 lines (116 loc) · 2.49 KB
/
TicTacToe
File metadata and controls
130 lines (116 loc) · 2.49 KB
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
Problem Description:
/*
Design TicTacToe Game for two players.
*/
Solution:
#include <bits/stdc++.h>
using namespace std;
class init{
public:
char board[3][3];
static int turn;
init(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
board[i][j] = '-';
}
}
}
void printBoard(){
cout<<"Turn "<<turn++<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<board[i][j]<<" ";
}
cout<<endl;
}
}
};
int init:: turn =1;
class User{
public:
string name;
bool win;
User(string name){
this->name = name;
this->win = false;
}
};
bool userWin(init obj,int r,int c,char ch){
//check row
bool row = true,col = true, d1 = true,d2 = true;
for(int i=0;i<3;i++){
if(obj.board[i][c]!=ch){
row = false;
}
}
//check col
for(int i=0;i<3;i++){
if(obj.board[r][i]!=ch){
col = false;
}
}
for(int i=0;i<3;i++){
if(obj.board[i][i]!=ch){
d1 = false;
}
}
for(int i=2;i>=0;i--){
if(obj.board[i][i]!=ch){
d2 = false;
}
}
return (row || col || d1 || d2);
}
string PlayGame(User u1,User u2,init obj){
int row,col;
srand(time(NULL));
int turn = 0;
int cnt = 0;
while(cnt<9 || !u1.win || !u2.win){
row = 0+rand()%3;
col = 0+rand()%3;
if(turn == 0){
if(obj.board[row][col]=='-'){
cout<<row<<" "<<col<<endl;
obj.board[row][col] = 'X';
obj.printBoard();
if(userWin(obj,row,col,'X')){
u1.win = true;
return u1.name + " wins!!!";
//break;
}
turn = !turn;
cnt++;
}
}
else{
if(obj.board[row][col]=='-'){
cout<<row<<" "<<col<<endl;
obj.board[row][col] = 'O';
obj.printBoard();
if(userWin(obj,row,col,'O')){
u2.win = true;
return u2.name + " wins!!!";
//break;
}
turn = !turn;
cnt++;
}
}
if(cnt == 9){
return "Draw";
}
}
//return "Draw";
}
int main() {
init obj;
//obj.printBoard();
string user1,user2;
cin>>user1>>user2;
User u1(user1);
User u2(user2);
cout<<PlayGame(u1,u2,obj)<<endl;
return 0;
}