-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
536 lines (443 loc) · 11.6 KB
/
main.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "liteList/linklist.h"
#include "mem/mem.h"
#include "grap/grap.h"
#include "opt/opt.h"
#include "hmg_engine.h"
#include "function.h"
#define BUFFSIZE 1024
#define GP_NLEN 32
#define GP_NCHAR HMC_HINTLEN+1
#define DLIVES 15
#define DSELECT 0
#define NL 10
#define ADDLIVESTEP 5
#define NROW 10
#define SUCID 0
#define FALID 1
#define GSEXTID 2
#define GPEXTID 3
#define WINID 4
#define LOSID 5
#define NGID 6
#define SKID 7
#define HINT '.'
#define EXIT '-'
#define DELIM ':'
#define NEWGAME '+'
#define ADDLIVES '*'
#define SKIP '/'
#define UNCH '_'
static char buff[BUFFSIZE + 1];
static char gr [GP_NLEN][GP_NCHAR];
static const char STR_HMG_NAME[] = "LivesMan Game";
static const char STR_HMG_VERSION[] = "2.28.3";
static const char STR_HMG_YEAR[] = "2014";
static char GFILE[BUFFSIZE];
#define DFILE GFILE
static const char GPREFIX[] = "g";
static const char WIN[] = "[^ _ ^] You win!";
static const char LOSE[] = "[T _ T] You lose!";
static int playhmg(int ng);
static char *grappath(char *path);
static void showhelp(const char *str);
static void
showscore(unsigned int score, int mlives, const char *gname,
const char *datafile);
static assem *gas, *das;
static hms_game *game;
static unsigned int lives = DLIVES, maxlives = DLIVES;
static unsigned int gcount = 1, newgame = 1;
static unsigned int gselected, dselected;
static unsigned int nstep, addlivestep = ADDLIVESTEP;
static memman mm;
static const char *param[] = {"-l:", "-s:", NULL};
static const char *paramdes[] = {"-l:{n} n lives", "-s:{n} n steps", NULL};
enum __param {
e_live, e_step
};
int
main(int argc, const char *argv[])
{
hms_group *gptr;
hms_data *dptr;
FILE *gfp, *dfp;
int i , j;
for (j = DSTART; (i =
opt_action(argc, argv, param, buff,
BUFFSIZE, DSTART)) != e_optend; j++) {
switch (i) {
case e_live:
if (!isUint(buff)) {
fprintf(stderr,
"ERR 1 ARG: %s is not positive number\n\n", buff);
showhelp(grappath((char *)argv[0]));
return 1;
} else if ((maxlives = s2ui(buff, 10)) < 1) {
fprintf(stderr, "ERR 1 ARG: %s is must >= 1\n\n", buff);
showhelp(grappath((char *)argv[0]));
return 1;
}
break;
case e_step:
if (!isUint(buff)) {
fprintf(stderr,
"ERR 2 ARG: %s is not positive number\n\n", buff);
showhelp(grappath((char *)argv[0]));
return 1;
} else if ((addlivestep = s2ui(buff, 10)) < 1) {
fprintf(stderr, "ERR 2 ARG: %s is must >= 1\n\n", buff);
showhelp(grappath((char *)argv[0]));
return 1;
}
break;
case e_optother:
strcpy(GFILE, buff);
goto _start;
}
}
fprintf(stderr, "ERR FILE: No input file\n\n");
showhelp(grappath((char *)argv[0]));
return 1;
/* =================== Start ================ */
_start:
mm_init(&mm);
#ifndef _DEVRAND_
hmf_srandom();
#endif
if (!(gas = hmf_init(0, &mm))) {
fprintf(stderr,
"ERR INIT GAS: Can not alloc memory for group assembly\n");
mm_freeall(&mm);
return 1;
}
if (!(gfp = fopen(GFILE, "r"))) {
perror(GFILE);
mm_freeall(&mm);
return 1;
}
while (fgets(buff, BUFFSIZE, gfp)) {
cutnl(buff, 0);
i = grapstring(cutcharbe(buff, 0x20), DELIM, GP_NLEN, GP_NCHAR, gr);
if (i < 0) {
mm_freeall(&mm);
fclose(gfp);
fprintf(stderr, "ERR FILE: %s\n", GFILE);
if (i == -1) {
fprintf(stderr,
"ERR FILE: %s :item is limited to <%u> key numbers\n",
GFILE, GP_NLEN - 1);
return 1;
} else if (i == -2) {
fprintf(stderr,
"ERR FILE: %s :item is limited to <%u> charecter numbers\n",
GFILE, GP_NCHAR - 1);
return 1;
}
}
if (i > 0) {
if (!(j = strcmp(gr[0], GPREFIX)) && (i == 2)) {
for (j = 1; j < i; j++) {
if (!(gptr = NEWGROUP())) {
fprintf(stderr,
"ERR NEWGRP: Can not alloc memory for group\n");
mm_freeall(&mm);
fclose(gfp);
return 1;
}
strcpy(gptr->name, gr[j]);
if (!hmf_add(gas, gptr, &mm)) {
fprintf(stderr,
"ERR ADDGRP: Can not add linklist for group\n");
mm_freeall(&mm);
fclose(gfp);
return 1;
}
GPTR(gas->end->ptr)->gid = gas->end->id;
}
} else if ((j && (i == 2)) || (!j && (i != 2))) {
fprintf(stderr,
"ERR ADDGROUP: File < %s >\n\t\tMSG: < %s > must be in first parameter\n\t\tMSG: numers of data must be 3 and numers of group must be 2\n\n",
DFILE, GPREFIX);
fprintf(stderr, "ERR DETAIL: ");
for (j = 0; j < i; j++) {
fprintf(stderr, "%s ", gr[j]);
}
fprintf(stderr, "\n");
mm_freeall(&mm);
fclose(gfp);
return 1;
} else if ((i > 3) || (i == 1)) {
fprintf(stderr,
"ERR ADDOBJECT: File < %s >\n\t\tMSG: Numers of data must be 3 and numers of group must be 2\n\n",
DFILE);
fprintf(stderr, "ERR DETAIL: ");
for (j = 0; j < i; j++) {
fprintf(stderr, "%s ", gr[j]);
}
fprintf(stderr, "\n");
mm_freeall(&mm);
fclose(gfp);
return 1;
}
}
}
fclose(gfp);
if (!hmf_getngroup(gas)) {
fprintf(stderr,
"ERR INITGAME: FILE <%s> :Nuber of group must'n be <0>\n",
grappath(GFILE));
mm_freeall(&mm);
return 1;
}
/*******************************************************************************************************************/
if (!(das = hmf_init(1, &mm))) {
mm_freeall(&mm);
fprintf(stderr,
"ERR INIT DAS: Can not alloc memory for data assembly\n");
return 1;
}
if (!(dfp = fopen(DFILE, "r"))) {
perror(DFILE);
mm_freeall(&mm);
return 1;
}
while (fgets(buff, BUFFSIZE, dfp)) {
cutnl(buff, 0);
i = grapstring(cutcharbe(buff, 0x20), DELIM, GP_NLEN, GP_NCHAR, gr);
if (i > 0) {
if (i == 3) {
if (!(dptr = NEWDATA())) {
fprintf(stderr,
"ERR NEWDATA: Can not alloc memory for data\n");
mm_freeall(&mm);
fclose(dfp);
return 1;
}
if (!isUint(gr[0])) {
fprintf(stderr,
"ERR ADDDATA: < %s > :First parameter of data is not a number\n",
DFILE);
fprintf(stderr, "ERR DETAIL: ");
for (j = 0; j < i; j++) {
fprintf(stderr, "%s ", gr[j]);
}
fprintf(stderr, "\n");
mm_freeall(&mm);
fclose(dfp);
return 1;
}
if (!strlen(gr[1])) {
fprintf(stderr,
"ERR ADDDATA: < %s > :String length of guess word must not be 0\n",
grappath(DFILE));
free(dptr);
mm_freeall(&mm);
fclose(dfp);
return 1;
}
dptr->gid = s2ui(gr[0], 10);
strcpy(dptr->name, gr[1]);
strcpy(dptr->hint, gr[2]);
if (!hmf_add(das, dptr, &mm)) {
fprintf(stderr,
"ERR ADDDATA: Can not add linklist for data\n");
mm_freeall(&mm);
fclose(dfp);
return 1;
}
}
}
}
fclose(dfp);
/**********************************************************************************************************************/
while ((j = playhmg(newgame)) > GPEXTID) {
if (j == WINID) { /* WIN */
gcount++;
lives = game->lives;
newgame = 0;
mm_free(&mm, game);
} else if (j == NGID) {
showscore(gcount - 1, maxlives,
hmf_getgroup(gas, gselected)->name, grappath(GFILE));
gcount = 1;
newgame = 1;
mm_free(&mm, game);
} else if (j == SKID) {
lives = game->lives;
newgame = 0;
mm_free(&mm, game);
} else if (j == LOSID) { /* LOSE */
showscore(gcount - 1, maxlives + nstep * addlivestep,
hmf_getgroup(gas, gselected)->name, grappath(GFILE));
do {
puts("\nDo you wnat to continue this game?");
printf("Answer->(Y/n) ");
fgets(buff, BUFFSIZE, stdin);
cutnl(buff, 0);
if (!strlen(buff)) {
buff[0] = 'y';
}
}
while ((strlen(buff) > 1) || (buff[0] != 'y' && buff[0] != 'n'));
if (buff[0] == 'n') { /* Press N */
break;
} else if (buff[0] == 'y') { /* Press Y */
gcount = 1;
newgame = 1;
mm_free(&mm, game);
}
}
}
if (j == GPEXTID) {
showscore(gcount - 1, maxlives + nstep * addlivestep,
hmf_getgroup(gas, gselected)->name, grappath(GFILE));
}
mm_freeall(&mm);
return j == FALID ? 1 : 0;
}
/***********************************************************************************************************************/
static int
playhmg(int ng)
{
linklist *ptr;
int i , hint = 0;
unsigned int n;
if (ng == 1) {
do {
puts
("\n********************* List of groups *********************");
printf(" [ %s ]\n\n", grappath(GFILE));
for (n = 1, ptr = gas->begin; ptr; ptr = ptr->forw, n++) {
printf("%u = %s (%u)\n", GPTR(ptr->ptr)->gid,
GPTR(ptr->ptr)->name, hmf_getndata(das,
GPTR
(ptr->ptr)->gid));
if (!(n % (NROW))) {
printf("Press Enter to continue");
fgets(buff, BUFFSIZE, stdin);
}
}
putchar(NL);
printf("Default = %s\n", hmf_getgroup(gas, DSELECT)->name);
printf("Which one ? or type (%c) to exit.\n", EXIT);
printf("Answer -> ");
fgets(buff, BUFFSIZE, stdin);
putchar(NL);
cutnl(buff, 0);
if ((strlen(buff) == 1) && (buff[0] == EXIT)) {
return GSEXTID;
} else if (!strlen(buff)) {
buff[0] = '0';
}
}
while ((!isUint(buff))
|| ((n = s2ui(buff, 10)) > (hmf_getngroup(gas) - 1))
|| (!hmf_getndata(das, n)));
gselected = n;
lives = maxlives;
nstep = 0;
}
/* End of new game procedure */
dselected = hmf_random(0, hmf_getndata(das, gselected) - 1);
game =
hmf_initgame(hmf_getdata(das, gselected, dselected), lives, UNCH, &mm);
if (!game) {
fprintf(stderr, "ERR INITGAME: Can not init for a game\n");
return FALID;
}
while (1) {
do {
printf
("\n********************* %u Game section *********************\n",
gcount);
puts(hmf_getgroup(gas, gselected)->name);
printf
("(%c)Hint (%c)New Game (%c)Add %u lives (%c)Skip (%c)Exit\n",
HINT, NEWGAME, ADDLIVES, addlivestep, SKIP, EXIT);
if (hint) {
printf("\t\t\t\tHint: %s", game->data->hint);
hint = 0;
}
printf("\nLives %u/%u\n\n", game->lives,
maxlives + nstep * addlivestep);
printf("%s\n\n", game->name);
printf("Type your char -> ");
fgets(buff, BUFFSIZE, stdin);
cutnl(buff, 0);
}
while (strlen(buff) != 1);
if (buff[0] == HINT) {
if (game->lives > 1) {
hint = 1;
game->lives--;
}
i = 0;
} else if (buff[0] == EXIT) {
return GPEXTID;
} else if (buff[0] == NEWGAME) {
return NGID;
} else if (buff[0] == ADDLIVES) {
nstep++;
game->lives += addlivestep;
game->win = 0;
i = 0;
} else if (buff[0] == SKIP) {
if (game->lives > 1) {
game->lives--;
return SKID;
}
i = 0;
} else {
i = hmf_gameplay(game, buff[0]);
}
if (i == 1) {
putchar(NL);
printf("%s\n\t\t\t< %s >\n", game->name, WIN);
return WINID;
} else if (i == -1) {
putchar(NL);
printf("%s\n\t\t\t< %s >\n", hmf_solve(game)->name, LOSE);
return LOSID;
}
}
return SUCID;
}
static char *
grappath(char *path)
{ /* return -1 if NULL ptr, 0 if sep was not
* found and position of filename if success */
int i , j = -1;
if (!path) {
return ((char *)0);
}
for (i = 0; path[i]; i++) {
if ((path[i] == '\\') || (path[i] == '/')) {
j = i;
}
}
return path + ((j == -1) ? 0 : j + 1);
}
static void
showhelp(const char *str)
{
unsigned int i;
fprintf(stderr, "%s version %s published in %s\n\n", STR_HMG_NAME,
STR_HMG_VERSION, STR_HMG_YEAR);
fprintf(stderr, "USAGE: %s options file\n", str);
fprintf(stderr, "OPTIONS\n");
for (i = 0; param[i]; i++) {
fprintf(stderr, "%s=\t%s\n", param[i], paramdes[i]);
}
fputc('\n', stderr);
}
static void
showscore(unsigned int score, int mlives, const char *gname,
const char *datafile)
{
printf("\n<< Game data: %s >>\n", datafile);
printf("<< You have selected %s group >>\n", gname);
printf("<< You have %u wins in %u lives >>\n", score, mlives);
}