/
connect_4_optimized.c
352 lines (323 loc) · 8.02 KB
/
connect_4_optimized.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <stdio.h>
#include <stdlib.h>
//0th row is the first from top but displayed upside down
int mini(char [][7],int, int, int);
int max(char [][7], int,int, int);
int is_free(int col, char board[6][7])
{
int i;
for(i=0; i<6; i++)
if(board[i][col]=='0')
break;
if(i<6)
return 1;
return 0;
}
void display(char board[][7])
{
for(int i=5; i>=0; i--)
{
for(int j=0; j<7; j++)
printf("%d", board[i][j]-'0');
printf("\n");
}
printf("\n");
}
int check_win( char board[][7]) //1 for win, 2 for tie, 0 otherwise
{
int i, j;
//checks horizontal win
for(i=0;i<6;i++)
for(j=0;j<7-3;j++)
if(board[i][j] != '0' && board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3])
return 1;
//checks vertical win
for(i=0;i<6-3;i++)
for(j=0;j<7;j++)
if(board[i][j] != '0' && board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j])
return 1;
//return 2;
//checks rigth diagonal win
for(i=0;i<6-3;i++)
for(j=0;j<7-3;j++)
if(board[i][j] != '0' && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3])
return 1;
//checks left diagonal win
for(i=0;i<6-3;i++)
for(j=3;j<7;j++)
if(board[i][j] != '0' && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3])
return 1;
for(i=0;i<6;i++)
for(j=0; j<7; j++)
if(board[i][j]=='0')
break;
if(i==6 && j==7)
return 2;
return 0;
}
void update_board(int move, char choice, char board[][7])
{
if(choice=='c')
{
for(int i=0; i<6; i++)
{
if(board[i][move-1]=='0')
{
board[i][move-1]='1';
break;
}
}
return;
}
if(choice=='s')
{
for(int i=0; i<6; i++)
{
if(board[i][move-1]=='0')
{
board[i][move-1]='2';
break;
}
}
return;
}
{
int i;
for(i=0; i<6; i++)
{
if(board[i][move-1]=='0')
{
board[i-1][move-1]='0';
break;
}
}
if(i==6)
board[5][move-1]='0';
}
}
//check 4 in a row
int appx_score(char board[6][7])
{
int s_score=0, c_score=0;
int i, j, temp;
//checks horizontal win
for(i=0;i<6;i++)
for(j=0;j<7-3;j++)
{
temp=(int)board[i][j]+(int)board[i][j+1]+(int)board[i][j+2]+(int)board[i][j+3];
if(temp==195)
c_score=c_score+10;
else if(temp==198)
s_score=s_score+10;
}
//checks vertical win
for(i=0;i<6-3;i++)
for(j=0;j<7;j++)
{
temp=(int)board[i][j]+(int)board[i+1][j]+(int)board[i+2][j]+(int)board[i+3][j];
if(temp==195)
c_score=c_score+10;
else if(temp==198)
s_score=s_score+10;
}
//checks rigth diagonal win
for(i=0;i<6-3;i++)
for(j=0;j<7-3;j++)
{
temp=(int)board[i][j]+(int)board[i+1][j+1]+(int)board[i+2][j+2]+(int)board[i+3][j+3];
if(temp==195)
c_score=c_score+10;
else if(temp==198)
s_score=s_score+10;
}
//checks left diagonal win
for(i=0;i<6-3;i++)
for(j=0; j<7; j++)
{
temp=(int)board[i][j]+(int)board[i+1][j-1]+(int)board[i+2][j-2]+(int)board[i+3][j-3];
if(temp==195)
c_score=c_score+10;
else if(temp==198)
s_score=s_score+10;
}
return c_score-s_score;
}
int mini(char board[][7], int depth,int alpha, int beta)
{
if(depth==0)
{
return appx_score(board);
}
else if(check_win(board)==1)
return 100000+depth;
else if(check_win(board)==2)
return 0;
else
{
int temp=0, ctr=0;
int col_order[7]={3,2,4,1,5,0,6};
int score_order[7]={100000, 100000, 100000, 100000, 100000, 100000, 100000};
for(int i=0; i<7; i++)
{
if(is_free(i, board))
{
update_board(i+1, 's', board);
score_order[ctr]=appx_score(board);
col_order[ctr]=i;
update_board(i+1, 'a', board);
ctr=ctr+1;
}
}
for(int x=0; x<ctr; x++)
{
for(int y=0; y<ctr-x-1; y++)
{
if(score_order[y]>score_order[y+1])
{
int temp2 = col_order[y+1];
int temp1 = score_order[y+1];
col_order[y+1] = col_order[y];
col_order[y] = temp2;
score_order[y+1] = score_order[y];
score_order[y] = temp1;
}
}
}
for(int j=0; j<ctr; j++)
{
if(alpha<beta)
{
update_board(col_order[j]+1, 's', board);
temp=max(board, depth-1, alpha, beta);
if(temp<beta)
beta=temp;
update_board(col_order[j]+1, 'a', board);
}
else return beta;
}
return beta; //alpha becomes beta
}
}
int max(char board[][7], int depth, int alpha, int beta)
{
if(depth==0)
{
return appx_score(board);
}
else if(check_win(board)==1)
return -100000-depth;
else if(check_win(board)==2)
return 0;
else
{
int temp=0, ctr=0;
int col_order[7]={3,2,4,1,5,0,6};
int score_order[7]={-100000, -100000, -100000, -100000, -100000, -100000, -100000};
for(int i=0; i<7; i++)
{
if(is_free(i, board))
{
update_board(i+1, 'c', board);
score_order[ctr]=appx_score(board);
col_order[ctr]=i;
update_board(i+1, 'a', board);
ctr=ctr+1;
}
}
for(int x=0; x<ctr; x++)
{
for(int y=0; y<ctr-x-1; y++)
{
if(score_order[y]<score_order[y+1])
{
int temp2 = col_order[y+1];
int temp1 = score_order[y+1];
col_order[y+1] = col_order[y];
col_order[y] = temp2;
score_order[y+1] = score_order[y];
score_order[y] = temp1;
}
}
}
for(int j=0; j<ctr; j++)
{
if(alpha<beta)
{
update_board(col_order[j]+1, 'c', board);
temp=mini(board, depth-1, alpha, beta);
if(temp>alpha)
alpha=temp;
update_board(col_order[j]+1, 'a', board);
}
else return alpha;
}
return alpha; //beta becomes alpha
}
}
void play_server(char board[][7])
{
int move=1,i, temp=100100;
int col_order[7]={3,2,4,1,5,0,6};
int alpha=-100100;
int beta=+100100;
for(int j=0; j<7; j++)
{
if(is_free(col_order[j], board))
{
update_board(col_order[j]+1, 's', board);
temp=max(board,12,alpha, beta);
if(temp<beta)
{
beta=temp;
move=col_order[j]+1;
}
update_board(col_order[j]+1, 'a', board);
}
}
update_board(move, 's', board);
}
int main()
{
char board[6][7];
for(int i=0; i<6; i++)
for(int j=0; j<7; j++)
board[i][j]='0';
int move;
printf("enter first move\n");
scanf("%d", &move);
update_board(move, 'c', board);
display(board);
while(1)
{
play_server(board);
display(board);
if(check_win(board)==1)
{
printf("server wins");
break;
}
else if(check_win(board)==2)
{
printf("draw");
break;
}
else
{
printf("enter_move\n");
scanf("%d", &move);
update_board(move, 'c', board);
display(board);
if(check_win(board)==1)
{
printf("you win");
break;
}
else if(check_win(board)==2)
{
printf("draw");
break;
}
}
}
return 0;
}