-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
399 lines (373 loc) · 9.51 KB
/
Copy pathmain.c
File metadata and controls
399 lines (373 loc) · 9.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"
#include "ai.h"
int main(void)
{
struct history *historyptr,*startptr;/*all structs have been declared in functions.h*/
unsigned char boardsize=9, newboardsize, bwalls=10, wwalls=10,i,j,**board,cmd[50], *token, *tempp, color, wall,bpi,bpj,wpi,wpj;
const unsigned char *commands[]={"name","known_command","list_commands","quit","boardsize","clear_board","walls","playmove","playwall","genmove","undo","winner","showboard"};
int temp;
int outwall,outx,outy,outmove;//for genmove
if((board=malloc(boardsize*sizeof(unsigned char *)))==NULL) /*default allocation*/
return -1;
for(i=0;i<boardsize;i++)
if((board[i]=malloc(boardsize*sizeof(unsigned char)))==NULL) return -1;
if((startptr=malloc(sizeof(struct history)))==NULL) return -1;
startptr->num=0;
startptr->next=NULL;
historyptr=startptr;
//WELCOME
printf("Welcome to kostis' Quoridor! List of commands below:\n\n");
for(i=0;i<13;i++) printf("%s\n",commands[i]);
printf("\n");
do
{
fgets(cmd,sizeof(cmd),stdin);/*get the line*/
if((tempp=strchr(cmd,'#'))!=NULL)/*discard the comments*/
*tempp=0;
token=strtok(cmd," \t\n");/*keep the first word*/
if(token==NULL)/*discard empty lines(or only-comment lines)*/
continue;
else if(!strcmp(token,"boardsize")) /*****boardsize command*****/
{
token=strtok(NULL," \t\n");/*keep the size*/
if(token==NULL || (newboardsize=atoi(token))<3 || newboardsize>25 || newboardsize%2==0)
{
printf("? unacceptable size\n\n");
fflush(stdout);
continue;
}
for(i=0;i<boardsize;i++)/*free the last board*/
free(board[i]);
free(board);
if((board=malloc(newboardsize*sizeof(unsigned char *)))==NULL) /*allocate for new size*/
return -1;
for(i=0;i<newboardsize;i++)
if((board[i]=malloc(newboardsize*sizeof(unsigned char)))==NULL)
return -1;
boardsize=newboardsize;
printf("=\n\n");
fflush(stdout);
if(clear_board(boardsize,board,&bpi,&bpj,&wpi,&wpj,&startptr)==-1)
return -1;
historyptr=startptr;
}
else if(!strcmp(token,"clear_board")) /*****clear_board command*****/
{
if(clear_board(boardsize,board,&bpi,&bpj,&wpi,&wpj,&startptr)==-1)
return -1;
historyptr=startptr;
printf("=\n\n");
fflush(stdout);
}
else if(!strcmp(token,"walls")) /*****walls command*****/
{
token=strtok(NULL," \t\n");/*keep the number of walls*/
if(token==NULL || (bwalls=wwalls=atoi(token))==0)
{
printf("? unacceptable number of walls given\n\n");
fflush(stdout);
continue;
}
printf("=\n\n");
fflush(stdout);
}
else if(!strcmp(token,"name")) /*****name command*****/
{
printf("= kostis\n\n");
fflush(stdout);
}
else if(!strcmp(token,"known_command")) /*****known_command command*****/
{
token=strtok(NULL," \t\n");/*keep the command*/
if(token==NULL)
{
printf("= false\n\n");
fflush(stdout);
continue;
}
for(i=0;i<13;i++)
if(!strcmp(token,commands[i]))
{
printf("= true\n\n");
fflush(stdout);
break;
}
if(i==13)
printf("= false\n\n");
fflush(stdout);
}
else if(!strcmp(token,"list_commands")) /*****list_commands command*****/
{
printf("= \n");
for(i=0;i<13;i++)
printf("%s\n",commands[i]);
printf("\n");
fflush(stdout);
}
else if(!strcmp(token,"quit")) /*****quit command*****/
{
printf("=\n\n");
fflush(stdout);
break;
}
else if(!strcmp(token,"playmove")) /*****playmove command*****/
{
token=strtok(NULL," \t\n");/*keep the color*/
if(token==NULL)
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
convert(token);
if(!strcmp(token,"w") || !strcmp(token,"white"))
color=2;
else if(!strcmp(token,"b") || !strcmp(token,"black"))
color=1;
else
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
token=strtok(NULL," \t\n");/*keep the vertex*/
if(token==NULL)
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
convert(token);
if(token[0] >= 'a' && token[0] <= 'y' - (25-boardsize))
{
j = token[0] - 'a';
token++;
}
else
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
if((i=atoi(token))<=0 || i>boardsize)
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
i = boardsize - i; /*i,j express the pawns' wanted position in the board*/
if(move(boardsize,board,color,i,j,&historyptr,&bpi,&bpj,&wpi,&wpj,0)==-1)
return -1;
}
else if(!strcmp(token,"playwall")) /*****playwall command*****/
{
token=strtok(NULL," \t\n");/*keep the color*/
if(token==NULL)
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
convert(token);
if(!strcmp(token,"w") || !strcmp(token,"white"))
color=2;
else if(!strcmp(token,"b") || !strcmp(token,"black"))
color=1;
else
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
token=strtok(NULL," \t\n");/*keep the vertex*/
if(token==NULL)
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
convert(token);
if(token[0] >= 'a' && token[0] <= 'y' - (25-boardsize))
{
j = token[0] - 'a';
token++;
}
else
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
if((i=atoi(token))<=0 || i>boardsize)
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
i = boardsize - i; /*i,j express the wall's wanted position in the board*/
token=strtok(NULL," \t\n");/*keep the orientation*/
if(token==NULL)
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
convert(token);
if(!strcmp(token,"horizontal") || !strcmp(token,"h"))
wall=1;
else if(!strcmp(token,"vertical") || !strcmp(token,"v"))
wall=2;
else
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
if(wall_placement(boardsize,board,color,wall,&bwalls,&wwalls,i,j,&historyptr,bpi,bpj,wpi,wpj,0)==-1)
return -1;
}
else if(!strcmp(token,"genmove")) /*****genmove command*****/
{
token=strtok(NULL," \t\n");/*keep the color*/
if(token==NULL)
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
convert(token);
if(!strcmp(token,"w") || !strcmp(token,"white"))
color=2;
else if(!strcmp(token,"b") || !strcmp(token,"black"))
color=1;
else
{
printf("? syntax error\n\n");
fflush(stdout);
continue;
}
outmove=genmove(boardsize, board, bpi,bpj,wpi,wpj,bwalls,wwalls,color);
outy=outmove&0xff;
outmove=outmove>>8;
outx=outmove&0xff;
outwall=outmove>>8;
if(outwall==0)
{
if(move(boardsize,board,color,outy,outx,&historyptr,&bpi,&bpj,&wpi,&wpj,1)==-1)
return -1;
printf("= %c%i\n\n",'A'+outx,boardsize-outy);
fflush(stdout);
}
else
{
if(wall_placement(boardsize,board,color,outwall,&bwalls,&wwalls,outx,outy,&historyptr,bpi,bpj,wpi,wpj,1)==-1)
return -1;
printf("= %c%i %c\n\n",'A'+outy,boardsize-outx,(outwall==1)?('h'):('v'));
fflush(stdout);
}
}
else if(!strcmp(token,"undo")) /*****undo command*****/
{
int times;
token=strtok(NULL," \t\n");
if(token==NULL)
times=1;
else
times=atoi(token);
if(times>historyptr->num)
printf("? cannot undo\n\n");
else
{
for(i=1;i<=times;i++)
{
undo(boardsize,board,&bwalls,&wwalls,&historyptr,startptr,&bpi,&bpj,&wpi,&wpj);
}
printf("=\n\n");
}
fflush(stdout);
}
else if(!strcmp(token,"winner")) /*****winner command*****/
{
if(bpi==boardsize-1)
printf("= true black\n\n");
else if(wpi==0)
printf("= true white\n\n");
else
printf("= false\n\n");
fflush(stdout);
}
else if(!strcmp(token,"showboard")) /*****showboard command*****/
{
printf("=\n\n ");
for(i=0;i<boardsize;i++)
printf(" %c ",'A'+i);
printf(" \n +");
for(i=0;i<boardsize;i++)
printf("---+");
printf(" \n");
for(i=boardsize;i>0;i--)
{
printf(" %d |",i);
for(j=0;j<boardsize;j++)
{
if(board[boardsize-i][j]!=2 && (i==boardsize || board[boardsize-i-1][j]!=2))
{
if((bpi!=boardsize-i || bpj!=j)&&(wpi!=boardsize-i || wpj!=j))
printf(" |");
else if(bpi==boardsize-i && bpj==j)
printf(" B |");
else if(wpi==boardsize-i && wpj==j)
printf(" W |");
}
else
{
if((bpi!=boardsize-i || bpj!=j)&&(wpi!=boardsize-i || wpj!=j))
printf(" H");
else if(bpi==boardsize-i && bpj==j)
printf(" B H");
else if(wpi==boardsize-i && wpj==j)
printf(" W H");
}
}
if(i==boardsize)
printf(" %d black walls: %d\n +",i,bwalls);
else if(i==boardsize-1)
printf(" %d white walls: %d\n +",i,wwalls);
else
printf(" %d \n +",i);
for(j=0;j<boardsize;j++)
{
if(board[boardsize-i][j]!=1 && (j==0 || board[boardsize-i][j-1]!=1))
printf("---");
else
printf("===");
if(board[boardsize-i][j]==1)
printf("=");
else if(board[boardsize-i][j]==2)
printf("H");
else
printf("+");
}
printf(" \n");
}
printf(" ");
for(i=0;i<boardsize;i++)
printf(" %c ",'A'+i);
printf(" \n\n");
fflush(stdout);
}
else
printf("? unknown command\n\n");
}while(1);
fflush(stdout);
for(i=0;i<boardsize;i++)/*free the last board*/
free(board[i]);
free(board);
free_history(startptr);/*free history list*/
//////
//////
return 0;
}