Repository navigation
Expand file tree
/
Copy pathtic-tac-toe.cpp
More file actions
167 lines (156 loc) · 4.51 KB
/
Copy pathtic-tac-toe.cpp
File metadata and controls
167 lines (156 loc) · 4.51 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
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
// Author: Nguyen Quoc Phu Dep Trai
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define int long long
#define db long double
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define ve vector
#define uni(a) sort(all(a)),a.resize(unique(all(a))-a.begin())
#define str string
#define fi first
#define se second
#define pb push_back
#define pk pop_back
#define pt pop_front
#define pf push_front
#define LB lower_bound
#define UB upper_bound
#define sz(x) (int)((x).size())
#define el '\n'
#define f(i,a,b) for(int i=(a);i<=(b);i++)
#define fd(i,a,b) for(int i=(a);i>=(b);i--)
#define FORN(i,a,b) for(int i=(a);i<(b);i++)
#define all(a) a.begin(),a.end()
#define btpc(a) __builtin_popcount(a)
#define btctz(a) __builtin_ctz(a) // bit 1 MIN
#define btclz(a) __builtin_clz(a) // bit 1 MAX
#define tct template<typename T>
#define BIT(x,i) ((x>>i)&1)
#define MASK(x) (1<<(x))
ll lg(ll a){return __lg(a);}
ll sq(ll a){return a*a;}
ll gcd(ll a , ll b){return __gcd(a,b);}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}
tct bool mini(T& a,const T& b){return (a>b)?a=b,1:0;}
tct bool maxi(T& a,const T& b){return (a<b)?a=b,1:0;}
ll rd(ll l,ll r){return l+1ll*rand()*rand()%(r-l+1);}
tct void prt(T a[] ,int n ){f(i,1,n)cout<<a[i]<<" ";cout<<el;}
#define print(a) for(auto _v : a )cout<<_v<<" ";cout<<'\n';
const int N = 1e6+5 , maxN=20, CH = 26 ;
const ll sm = 1e9+7 , base=311, inf = (int)1e9 , llinf = (ll)1e18 + 7LL , LG=__lg(N);
const db PI = acos(-1), eps = 1e-9 ;
void add(ll &a,ll b){a+=b; if(a>=sm)a-=sm;}
void del(ll &a,ll b){a-=b; if(a<0)a+=sm;}
int xx[] = {0,0,0,-1,1};
int yy[] = {0,-1,1,0,0};
vector<char>board;
char PLAYER_X = 'X';
char PLAYER_O = 'O';
char EMPTY = '.';
void display_board(){
f(i,1,3) cout<<board[i]<<" ";
cout<<el;
f(i,4,6) cout<<board[i]<<" ";
cout<<el;
f(i,7,9) cout<<board[i]<<" ";
cout<<el;
}
int WhoWin(){
f(x,0,2){
if(board[1+3*x]==board[2+3*x]&&board[2+3*x]==board[3+3*x]&&board[3+3*x]!=EMPTY){
if(board[1+3*x]==PLAYER_X) return 1;
return -1;
}
}
f(x,1,3){
if(board[x]==board[x+3]&&board[x+3]==board[x+6]&&board[x]!=EMPTY){
if(board[x]==PLAYER_X) return 1;
return -1;
}
}
if(board[1]==board[5]&&board[5]==board[9]&&board[1]!=EMPTY){
if(board[1]==PLAYER_X) return 1;
return -1;
}
if(board[3]==board[5]&&board[5]==board[7]&&board[7]!=EMPTY){
if(board[3]==PLAYER_X) return 1;
return -1;
}
f(i,1,9) if(board[i]==EMPTY) return 0;
return 2;
}
vector<int> MOVE(){
vll SAVE;
f(i,1,9)if(board[i]==EMPTY)SAVE.pb(i);
return SAVE;
}
int Point(vector<int>&move, int depth,int player){
int result = WhoWin();
if(result!=0){
if(result==2) return 0;
if(result==1) return 10-depth;
return -1*(depth+1);
}
int cost;
if(player){
int MIN=0;
for(auto step:move){
board[step]=PLAYER_X;
vll newmove=MOVE();
maxi(MIN,Point(newmove,depth+1,player^1));
board[step]=EMPTY;
}
cost=MIN;
}
else {
int MAX=inf;
for(auto step:move){
board[step]=PLAYER_O;
vll newmove=MOVE();
mini(MAX,Point(newmove,depth+1,player^1));
board[step]=EMPTY;
}
cost=MAX;
}
return cost;
}
signed main(){
board.resize(10,EMPTY);
display_board();
while(!WhoWin()){
cout<<"Please enter the number from 1 to 9: ";
int number; cin>>number;
if(board[number]!=EMPTY){
cout<<"Your step error! Please try again.";
continue;
}
board[number]=PLAYER_X;
vll move=MOVE();
int bestmove=-1;
int cost=inf;
for(auto step:move){
board[step]=PLAYER_O;
vll newmove=MOVE();
if(mini(cost,Point(newmove,0,1))){
bestmove=step;
}
board[step]=EMPTY;
}
board[bestmove]=PLAYER_O;
display_board();
}
if(WhoWin()==1){
cout<<"Congratulation you won!"<<el;
}
else if(WhoWin()==-1){
cout<<"Sorry you lose!"<<el;
}
else cout<<"Draw!"<<el;
return 0;
}