-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc.c
429 lines (352 loc) · 8.49 KB
/
sc.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
/*
class_sc.c
This is the class of object which constitute the play
*/
#define EOG (-1)
#include "sc.h"
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "perm.h"
#include "sf.h"
extern int games;
/* Neal and Jack play the game. In Win we gather the cards
yet-to-assign to Neal or Jack
*/
Player Neal, Jack;
Card *win[CARDNUM]; /* a stack and its stack-pointer */
int winp;
#define RESETSTACK winp=0
#define PUSH(c) win[winp++]=c
#define TOPOFSTACK win[winp-1]
#define POP --winp
#define EMPTYSTACK (winp==0)
/* We can refer to the players as Neal and Jack, or we can use
player[0] and player[1], or player[NEAL] and player[JACK];
*/
Player *player[2] = { &Neal, &Jack };
/* the deck */
Card deck[CARDNUM];
#ifdef ANSI
void sc(char values[],int threshold)
#else
void sc(values, threshold) char values[];int threshold;
#endif
{
int x;
games++;
Init(values);
x=Play(threshold,M);
if( x>threshold)
stampa_pack("RESULTS/not_finished_in_",threshold,values);
}
#ifdef ANSI
int Init(char values[])
#else
int Init(values) char values[];
#endif
{
int i;
/* Initialization of deck[] */
for (i=0; i<cardM; i++)
{
deck[i].value = M[i];
deck[i].next = deck + i + 1;
}
deck[cardM/2-1].next = deck[cardM-1].next = NULL;
/* now, (say cardM is 40),
deck[0] -> deck[1] -> ... deck[19] -> NULL
and
deck[20] -> deck[21] -> ... deck[39] -> NULL
*/
/* Initializes both Players */
Neal.top = deck;
Neal.bottom = deck + cardM/2 -1;
Neal.n = cardM/2;
Jack.top = deck + cardM/2;
Jack.bottom = deck + cardM -1;
Jack.n = cardM/2;
RESETSTACK;
}
#ifdef ANSI
int Play(int threshold, char values[])
#else
int Play(threshold, values) int threshold; char values[];
#endif
{
int2 who_turns;
int turns;
int c=-1;
extern int z;
int stop_the_game(int);
/* INITFLIP initializes flipping, FLIP performs it */
#define INITFLIP who_turns = JACK, turns = -1
#define OTHER 1-who_turns
#define FLIP who_turns = OTHER
turns=0;
/* a 2-state automata! */
INITFLIP;
F_state:
if(turns>threshold) return turns;
FLIP;
if ( ! (c=Turn(who_turns,&turns)) ) goto F_state;
S_state:
if(turns>threshold) return turns;
FLIP;
switch (c)
{
case EOG:
stop_the_game(who_turns);
DEBUG("out of Play -- EOG condition found");
return turns;
case 5:
if (c=Turn(who_turns,&turns))
goto S_state;
case 4:
if (c=Turn(who_turns,&turns))
goto S_state;
case 3:
if (c=Turn(who_turns,&turns))
goto S_state;
case 2:
if (c=Turn(who_turns,&turns))
goto S_state;
case 1:
if (c=Turn(who_turns,&turns))
goto S_state;
break;
default:
printf("strange condition!default case in switch!\n");
printf("c=%d\n", c);
fflush(stdout);exit(-1);
}
push_this_match(OTHER);
goto F_state;
}
/* Turn() re-Turns ( :-) ) EOG if the current player's deck is over,
or the card on top of the current player's deck. This results in
a shortening of the current user's deck and in a growing for
None's deck
*/
#ifdef ANSI
int Turn(int2 index,int *t)
#else
int Turn(index, t) int2 index; int *t;
#endif
{
Card *c = player[index]->top;
(*t)++;
if (c == NULL)
return EOG;
if (player[index]->top == NULL)
return EOG;
if (player[index]->top == player[index]->bottom)
return EOG;
player[index]->top = player[index]->top->next;
PUSH(c);
/*
DEBUGFMT1("out of Turn() (return=%d)\n", c->value);
*/
dump();
return (int) c->value;
}
/* pushes all Cards in the win stack on bottom of the winner,
re-initializes the stack pointer
*/
#ifdef ANSI
void push_this_match(int2 winner)
#else
void push_this_match(winner) int2 winner;
#endif
{
int i;
Player *p = player[winner];
int thedeck[100];
/*DEBUGFMT2("within push_this_match():%s wins %d cards\n",
(winner?"Jack":"Neal"), winp);*/
for (i=0; i<winp; i++)
{
p->bottom->next = win[i];
p->bottom = win[i];
}
p->bottom->next = NULL;
RESETSTACK;
/*
DEBUG("out of push_this_match()");
*/
dump();
}
int save_winner(int winner)
{
FILE *f;
static int J,N;
if(winner!=2)
winner?J++:N++;
if (games % 20000 == 0 || winner==2)
{
f=fopen("RESULTS/winnings","w");
if(f==NULL) { perror("can't open RESULTS/winnings"); exit(-6); }
fprintf(f,"Jack's winnings = %d\nNeal's winnings = %d\n",J,N);
fclose(f);
}
}
#ifdef ANSI
int stop_the_game(int winner)
#else
int stop_the_game(winner) int winner;
#endif
{
save_winner(winner);
}
#ifdef STRINGCMP
void dump()
{
int i;
sf_t njm[3];
int thedeck[100];
Card *t;
static FILE *f;
static int sizenjm=3*sizeof(sf_t);
#ifdef ANSI
sf_t goedelize(int,int,int4*);
#else
sf_t goedelize();
#endif
if (analize)
{
if (f==NULL)
if ( (f=fopen("dump", "w")) == NULL )
exit(COULDNT_DUMP);
/*
for (i=0, t=Neal.top; t!=NULL; t=t->next, i++)
thedeck[i] = t->value;
njm[0] = goedelize (cardA, i, thedeck);
for (i=0, t=Jack.top; t!=NULL; t=t->next, i++)
thedeck[i] = t->value;
njm[1] = goedelize (cardA, i, thedeck);
for (i=0; i<winp; i++)
thedeck[i]=win[i]->value;
njm[2] = goedelize (cardA, i, thedeck);
fwrite(njm, sizenjm, 1, f);
*/
for (t=Neal.top; t!=NULL; t=t->next)
fprintf(f, "%d", t->value);
fprintf(f, " ");
for (t=Jack.top; t!=NULL; t=t->next)
fprintf(f, "%d", t->value);
fprintf(f, " ");
for (i=0; i<winp; i++)
fprintf(f, "%d", win[i]->value);
if (winp == 0)
fprintf(f, "-");
fprintf(f, "\n");
}
}
#else /* NON STRINGCMP */
void dump()
{
int i;
sf_t njm[3];
int thedeck[100];
Card *t;
static FILE *f;
static int sizenjm=3*sizeof(sf_t);
#ifdef ANSI
sf_t goedelize(int,int,int4*);
#else
sf_t goedelize();
#endif
if (analize)
{
if (f==NULL)
if ( (f=fopen("dump", "w")) == NULL )
exit(COULDNT_DUMP);
#ifndef ASCIICONF
for (i=0, t=Neal.top; t!=NULL; t=t->next, i++)
thedeck[i] = t->value;
njm[0] = goedelize (cardA, i, thedeck);
for (i=0, t=Jack.top; t!=NULL; t=t->next, i++)
thedeck[i] = t->value;
njm[1] = goedelize (cardA, i, thedeck);
for (i=0; i<winp; i++)
thedeck[i]=win[i]->value;
njm[2] = goedelize (cardA, i, thedeck);
fwrite(njm, sizenjm, 1, f);
#else
for (t=Neal.top; t!=NULL; t=t->next)
fprintf(f, "%d", t->value);
fprintf(f, " ");
for (t=Jack.top; t!=NULL; t=t->next)
fprintf(f, "%d", t->value);
fprintf(f, " ");
for (i=0; i<winp; i++)
fprintf(f, "%d", win[i]->value);
if (winp == 0)
fprintf(f, "-");
fprintf(f, "\n");
#endif
}
}
#endif /* STRINGCMP */
/********************************************************************/
#ifdef ANSI
void stampa_pack(char nome[],int sec,int4 deck[])
#else
void stampa_pack(nome, sec, deck) char nome[]; int sec; int4 deck[];
#endif
/********************************************************************/
{
int i;
FILE *file_ris;
char nome_file[100];
char char_sec[50];
extern int offset;
strcpy(nome_file,nome);
sprintf(char_sec,"%d",sec);
strcat(nome_file,char_sec);
file_ris=fopen(nome_file,"a");
if(file_ris==NULL) {
perror("stampa_pack");
exit(-6);
}
for (i=0;i<cardM;i++)
fprintf(file_ris,"%c",M[i]+offset);
fprintf(file_ris,"\n");
fclose(file_ris);
}
/*
interpretes a deck as a base-cardA number and converts it
into decimal
*/
#ifdef ANSI
int base10(int cardA, int cardM, int values[])
#else
int base10(cardA, cardM, values) int cardA; int cardM; int values[];
#endif
{
int i, res, mul;
res=0;
mul=1;
for (i=cardM-1; i>=0; i--, mul *= cardA)
{
res += mul*values[i];
}
return res;
}
#ifdef ANSI
sf_t goedelize(int cardA, int cardM, int values[])
#else
sf_t goedelize(cardA, cardM, values) int cardA; int cardM; int values[];
#endif
{
int i;
sf_t res, mul;
res=0.0;
mul=1.0;
for (i=cardM-1; i>=0; i--, mul *= (sf_t)cardA)
{
res += (sf_t) mul*(values[i]+1.0);
}
return res;
}
/* eof class_sc.c */