-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
369 lines (362 loc) · 9.39 KB
/
main.cc
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
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <curses.h>
#include <stdlib.h>
#include "woorden.h"
#include "bord.h"
#include "extio.h"
#include "grafisch.h"
#include "volledig.h"
#include "enkel.h"
#include "debug.h"
#define NOBREAK
enum TMode { MODGEEN, MODGRF, MODENK, MODVOL, MODCOM, MODLOG, MODDB, MODX, MODQ };
enum TError { ERR_GEEN, ERR_MODESET, ERR_UNKNOWN, ERR_PRGCNT };
void printhelp(char *progname)
{
printf("\
BUBBLES v1.0\nCodecup 2004 LuckyWords\n\n");
printf("\
Usage: %s -[SGCALsvmwba] [prog1 prog2 ...] [-l logfile]\n", progname);
printf("\n\
Options:\n\n");
printf("\
S plays a single game, asks for starting letter (or random)\n\n");
printf("\
G plays a single game in a graphical environment\n\
asks for starting letter (or random)\n\n");
printf("\
C plays 52 games in a graphical environment (competition)\n\
outputs statistics afterwards\n\n");
printf("\
A writes a shell file which will let all programs play 52 games\n\
against every opponent (all)\n\
outputs statistics afterwards\n\n");
printf("\
s no graphical ouput (silent)\n\
use with -C\n\n");
printf("\
v extra output (verbose)\n\
use with -G or -C\n\n");
printf("\
m print matrix\n\
with -C this option prints a matrix when a game is completed\n\
with -G this option prints a matrix each time when a programm places a letter\n\n");
}
int main(int argc, char *argv[])
{
int result=0;
int i, j, n;
TMode mode=MODGEEN;
TError error=ERR_GEEN;
bool reverse=false;
bool matrix=false;
bool wait=false;
bool verbose=false;
bool log=false;
bool shell=false;
bool shelllog=false;
bool shelllog2=false;
bool fulllog=false;
bool silent=false;
bool nextlog=false;
bool nextshell=false;
bool nextshelllog=false;
bool nextshelllog2=false;
char prog[PRGMAX][256];
char logfile[256]="log";
char shellfile[256]="competitie.sh";
char shelllogfile[256]="shelllog";
char shelllogfile2[256]="shelllog2";
char errormsg[256];
int prgptr=0;
for (i=1; i<argc; i++)
{
if (argv[i][0]=='-')
{
n=strlen(argv[i]);
for (j=1; j<n; j++)
{
switch (argv[i][j])
{
case 'D':
if (mode!=MODGEEN)
{
error=ERR_MODESET;
sprintf(errormsg, "Too many Capitals (S)");
break;
}
mode=MODDB;
break;
case 'S':
if (mode!=MODGEEN)
{
error=ERR_MODESET;
sprintf(errormsg, "Too many Capitals (S)");
break;
}
mode=MODENK;
break;
case 'G':
if (mode!=MODGEEN)
{
error=ERR_MODESET;
sprintf(errormsg, "Too many Capitals (G)");
break;
}
mode=MODGRF;
break;
case 'C':
if (mode!=MODGEEN)
{
error=ERR_MODESET;
sprintf(errormsg, "Too many Capitals (C)");
break;
}
mode=MODVOL;
break;
case 'X':
if (mode!=MODGEEN)
{
error=ERR_MODESET;
sprintf(errormsg, "Too many Capitals (X)");
break;
}
mode=MODX;
break;
case 'Q':
if (mode!=MODGEEN)
{
error=ERR_MODESET;
sprintf(errormsg, "Too many Capitals (Q)");
break;
}
mode=MODQ;
break;
case 'A':
if (mode!=MODGEEN)
{
error=ERR_MODESET;
sprintf(errormsg, "Too many Capitals (A)");
break;
}
mode=MODCOM;
break;
case 'L':
if (mode!=MODGEEN)
{
error=ERR_MODESET;
sprintf(errormsg, "Too many Capitals (L)");
break;
}
mode=MODLOG;
break;
case 'r':
reverse=true;
break;
case 's':
silent=true;
break;
case 'v':
verbose=true;
break;
case 'w':
wait=true;
break;
case 'm':
matrix=true;
break;
case 'c':
if (nextlog || nextshell || nextshelllog) break;
nextshelllog2=true;
break;
case 'a':
if (nextlog || nextshell || nextshelllog2) break;
nextshelllog=true;
break;
case 'b':
if (nextlog || nextshelllog || nextshelllog2) break;
nextshell=true;
break;
case 'l':
if (nextshell || nextshelllog || nextshelllog2) break;
if (argv[i][j+1]=='+')
{
fulllog=true;
j++;
}
nextlog=true;
break;
case '-':
if (!strcmp(argv[i], "--help"))
{
printhelp(argv[0]);
break;
}
NOBREAK;
default:
error=ERR_UNKNOWN;
sprintf(errormsg, "Unkown option %c", argv[i][j]);
}
if (error) break;
}
}
else
{
if (prgptr==PRGMAX)
{
error=ERR_PRGCNT;
break;
}
if (nextlog==true)
{
strcpy(logfile, argv[i]);
log=true;
nextlog=false;
}
else if (nextshelllog2==true)
{
strcpy(shelllogfile2, argv[i]);
shelllog2=true;
nextshelllog2=false;
}
else if (nextshelllog==true)
{
strcpy(shelllogfile, argv[i]);
shelllog=true;
nextshelllog=false;
}
else if (nextshell==true)
{
strcpy(shellfile, argv[i]);
shell=true;
nextshell=false;
}
else
{
strcpy(prog[prgptr], argv[i]);
prgptr++;
}
}
if (error) break;
}
switch (error)
{
case ERR_GEEN:
if (silent && verbose)
{
printf("Silent option on: switching to non-verbose...\n");
verbose=false;
}
if (silent && matrix)
{
printf("Silent option on: switching to non-matrix...\n");
matrix=false;
}
if (silent && wait)
{
printf("Silent option on: switching to non-waiting...\n");
wait=false;
}
switch (mode)
{
case MODX:
if (prgptr<2)
{
printhelp(argv[0]);
printf("\nERROR: Not enough programs were given...\n");
result=1;
break;
}
result=alllog2(prog, prgptr, log, logfile, shelllog, shelllog2, shelllogfile, shelllogfile2);
break;
case MODLOG:
if (prgptr<2)
{
printhelp(argv[0]);
printf("\nERROR: Not enough programs were given...\n");
result=1;
break;
}
result=alllog(prog, prgptr, log, logfile, shelllog, shelllogfile);
break;
case MODQ:
if (prgptr<2)
{
printhelp(argv[0]);
printf("\nERROR: Not enough programs were given...\n");
result=1;
break;
}
result=all2(prog, prgptr, silent, verbose, fulllog, log, logfile, shell, shellfile, shelllog2, shelllogfile2);
break;
case MODCOM:
if (prgptr<2)
{
printhelp(argv[0]);
printf("\nERROR: Not enough programs were given...\n");
result=1;
break;
}
result=all(prog, prgptr, silent, verbose, fulllog, log, logfile, shell, shellfile, shelllog, shelllogfile);
break;
case MODVOL:
if (prgptr<2)
{
printhelp(argv[0]);
printf("\nERROR: Not enough programs were given...\n");
result=1;
break;
}
if (!matrix && wait) wait=false;
result=volledig(prog[0], prog[1], silent, verbose, log, logfile, matrix, wait, shelllog, shelllogfile, fulllog);
break;
case MODDB:
if (prgptr<2)
{
printhelp(argv[0]);
printf("\nERROR: Not enough programs were given...\n");
result=1;
break;
}
result=debug(prog[0], prog[1], matrix, log, fulllog, logfile, wait, reverse);
break;
case MODGRF:
if (prgptr<2)
{
printhelp(argv[0]);
printf("\nERROR: Not enough programs were given...\n");
result=1;
break;
}
result=grafisch(prog[0], prog[1], matrix, log, fulllog, logfile);
break;
case MODENK:
break;
case MODGEEN:
printhelp(argv[0]);
result=1;
break;
}
break;
case ERR_PRGCNT:
printhelp(argv[0]);
printf("\nERROR: Too many programs...\n");
break;
case ERR_MODESET:
printhelp(argv[0]);
printf("\nERROR: Too many Capitals given as option...\n");
break;
case ERR_UNKNOWN:
printhelp(argv[0]);
printf("\nERROR: %s...\n", errormsg);
break;
}
if (error) return error;
else if (result) return result+10;
else return 0;
}