-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdealer.cpp
1693 lines (1560 loc) · 58.5 KB
/
dealer.cpp
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
static int quiet = 0;
#include <netinet/in.h>
#include <unistd.h>
#include <sys/time.h>
#include "getopt.h"
#include "tree.h"
#include "pointcount.h"
#include "dealer.h"
#include "c4.h"
#include "pbn.h"
#ifdef FRANCOIS
static const int TWO_TO_THE_13 = (1 << 13);
#endif
static const int RANDBITS = 16;
static const int NRANDVALS = (1 << RANDBITS);
/* Global variables */
enum { STAT_MODE, EXHAUST_MODE };
static const int DEFAULT_MODE = STAT_MODE;
static int computing_mode = DEFAULT_MODE;
int uppercase = 0;
char lcrep[] = "23456789tjqka";
char ucrep[] = "23456789TJQKA";
#define representation (uppercase ? ucrep : lcrep);
static const int imparr[24] = {10, 40, 80, 120, 160, 210, 260, 310, 360, 410, 490, 590,
740, 890, 1090, 1190, 1490, 1740, 1990, 2240, 2490, 2990, 3490, 3990};
static Deal fullpack;
static Deal stacked_pack;
static int swapping = 0;
static int swapindex = 0;
static int loading = 0;
static int loadindex = 0;
/* Various handshapes can be asked for. For every shape the user is
interested in a number is generated. In every distribution that fits that
shape the corresponding bit is set in the distrbitmaps 4-dimensional array.
This makes looking up a shape a small constant cost.
*/
static int ***distrbitmaps[14];
static int results[2][5][14];
static int nprod;
static int ngen;
static Tree defaulttree = {TreeType::Number, NIL, NIL, 1, 0};
static Action defaultaction = {(Action *)0, ActionType::PrintAll};
static unsigned char zero52[NRANDVALS];
static Deal *deallist;
/* Function definitions */
static void fprintcompact(FILE *, Deal, int);
static int trix(char);
static void error(const char *);
static void printew(Deal d);
static int true_dd(Deal d, int l, int c);
extern void yyparse();
/* globals */
Deal curdeal;
Handstat hs[4];
const char *input_file = 0;
int maxgenerate;
int maxdealer;
int maxproduce;
int maxvuln;
long seed = 0;
bool use_compass[NSUITS];
bool will_print;
Tree *decisiontree = &defaulttree;
Action *actionlist = &defaultaction;
const char *player_name[] = {"North", "East", "South", "West"};
const Tree *NIL = nullptr;
#ifdef FRANCOIS
/* Special variables for exhaustive mode
Exhaustive mode created by Francois DELLACHERIE, 01-1999 */
int vectordeal;
/* a vectordeal is a binary *word* of at most 26 bits: there are at most
26 cards to deal between two players. Each card to deal will be
affected to a given position in the vector. Example : we need to deal
the 26 minor cards between north and south (what a double fit !!!)
Then those cards will be represented in a binary vector as shown below:
Card : DA DK ... D2 CA CK ... C4 C3 C2
Bit-Pos : 25 24 13 12 11 2 1 0
The two players will be respectively represented by the bit values 0 and
1. Let's say north gets the 0, and south the 1. Then the vector
11001100000001111111110000 represents the following hands :
North: D QJ8765432 C 5432
South: D AKT9 C AKQJT9876
A suitable vectordeal is a vector deal with hamming weight equal to 13.
For computing those vectors, we use a straightforward
meet in the middle approach. */
int exh_predealt_vector = 0;
char exh_card_map[256];
/* exh_card_map[card] is the coordinate of the card in the vector */
Card exh_card_at_bit[26];
/* exh_card_at_bit[i] is the card pointed by the bit #i */
unsigned char exh_player[2];
/* the two players that have unknown cards */
unsigned char exh_empty_slots[2];
/* the number of empty slots of those players */
unsigned char exh_suit_length_at_map[NSUITS][26];
/* exh_suit_length_at_map[SUIT][POS] = 1 if the card at
position POS is from the suit SUIT */
unsigned char exh_suit_points_at_map[NSUITS][26];
/* same as above with the hcp-value instead of 1 */
unsigned char exh_msb_suit_length[NSUITS][TWO_TO_THE_13];
/* we split the the vector deal into 2 sub-vectors of length 13. Example for
the previous vector: 1100110000000 and 11111111110000
msb lsb
exh_msb_suit_length[SUIT][MSB_VECTOR] represents the msb-contribution of
the MSB_VECTOR for the length of the hand for suit SUIT, and for player 0.
In the trivial above example, we have: exh_msb_suit_length[SUIT][MSBs] = 0
unless SUIT == DIAMOND, = the number of 0 in MSB, otherwise.*/
unsigned char exh_lsb_suit_length[NSUITS][TWO_TO_THE_13];
/* same as above for the lsb-sub-vector */
unsigned char exh_msb_suit_points[NSUITS][TWO_TO_THE_13];
/* same as above for the hcp's in the suits */
unsigned char exh_lsb_suit_points[NSUITS][TWO_TO_THE_13];
/* same as above for the lsb-sub-vector */
unsigned char exh_msb_totalpoints[TWO_TO_THE_13];
/* the total of the points of the cards represented by the
13 most significant bits in the vector deal */
unsigned char exh_lsb_totalpoints[TWO_TO_THE_13];
/* same as above for the lsb */
unsigned char exh_total_cards_in_suit[NSUITS];
/* the total of cards remaining to be dealt in each suit */
unsigned char exh_total_points_in_suit[NSUITS];
/* the total of hcps remaining to be dealt in each suit */
unsigned char exh_total_points;
/* the total of hcps remaining to be dealt */
int *HAM_T[14], Tsize[14];
/* Hamming-related tables. See explanation below... */
int completely_known_hand[4] = {0, 0, 0, 0};
#endif /* FRANCOIS */
static void initevalcontract() {
int i, j, k;
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++)
for (k = 0; k < 14; k++)
results[i][j][k] = 0;
}
static int imps(int scorediff) {
int i, j;
j = abs(scorediff);
for (i = 0; i < 24; i++)
if (imparr[i] >= j)
return scorediff < 0 ? -i : i;
return scorediff < 0 ? -i : i;
}
static int score(int vuln, int suit, int level, int tricks) {
int total = 0;
/* going down */
if (tricks < 6 + level)
return -50 * (1 + vuln) * (6 + level - tricks);
/* Tricks */
total = total + ((suit >= SUIT_HEART) ? 30 : 20) * (tricks - 6);
/* NT bonus */
if (suit == SUIT_NT)
total += 10;
/* part score bonus */
total += 50;
/* game bonus for NT */
if ((suit == SUIT_NT) && level >= 3)
total += 250 + 200 * vuln;
/* game bonus for major-partscore bns */
if ((suit == SUIT_HEART || suit == SUIT_SPADE) && level >= 4)
total += 250 + 200 * vuln;
/* game bonus for minor-partscore bns */
if ((suit == SUIT_CLUB || suit == SUIT_DIAMOND) && level >= 5)
total += 250 + 200 * vuln;
/* small slam bonus */
if (level == 6)
total += 500 + 250 * vuln;
/* grand slam bonus */
if (level == 7)
total += 1000 + 500 * vuln;
return total;
}
static void showevalcontract(int nh) {
int s, l, i, v;
for (v = 0; v < 2; v++) {
printf("%sVulnerable\n", v ? "" : "Not ");
printf(" ");
for (l = 1; l < 8; l++)
printf(" %d ", l);
printf("\n");
for (s = 0; s < 5; s++) {
printf("%c: ", "cdhsn"[s]);
for (l = 1; l < 8; l++) {
int t = 0, tn = 0;
for (i = 0; i < 14; i++) {
t += results[0][s][i] * score(v, s, l, i);
tn += results[1][s][i] * score(v, s, l, i);
}
printf("%4d/%4d ", t / nh, tn / nh);
}
printf("\n");
}
printf("\n");
}
}
static int dd(Deal d, int l, int c) {
/* results-cached version of dd() */
/* the dd cache, and the ngen it refers to */
static int cached_ngen = -1;
static char cached_tricks[4][5];
/* invalidate cache if it's another deal */
if (ngen != cached_ngen) {
memset(cached_tricks, -1, sizeof(cached_tricks));
cached_ngen = ngen;
}
if (cached_tricks[l][c] == -1) {
/* cache the costly computation's result */
cached_tricks[l][c] = true_dd(d, l, c);
}
/* return the cached value */
return cached_tricks[l][c];
}
static struct tagLibdeal {
unsigned long suits[4];
unsigned short tricks[5];
int valid;
} libdeal;
static int get_tricks(int pn, int dn) {
int tk = libdeal.tricks[dn];
int resu;
resu = (pn ? (tk >> (4 * pn)) : tk) & 0x0F;
return resu;
}
int true_dd(Deal d, int l, int c) {
if (loading && libdeal.valid) {
int resu = get_tricks((l + 1) % 4, (c + 1) % 5);
/* This will get the number of tricks EW can get. If the user wanted NW,
we have to subtract 13 from that number. */
return ((l == 0) || (l == 2)) ? 13 - resu : resu;
} else {
FILE *f;
char cmd[1024];
char tn1[256], tn2[256];
char res;
f = fopen(tn1, "w+");
if (f == 0)
error("Can't open temporary file");
fprintcompact(f, d, 0);
fprintf(f, "%c %c\n", "eswn"[l], "cdhsn"[c]);
fclose(f);
tmpnam(tn2);
sprintf(cmd, "bridge -d -q %s >%s", tn1, tn2);
system(cmd);
f = fopen(tn2, "r");
if (f == 0)
error("Can't read output of analysis");
fscanf(f, "%*[^\n]\n%c", &res);
fclose(f);
remove(tn1);
remove(tn2);
/* This will get the number of tricks EW can get. If the user wanted NW,
we have to subtract 13 from that number. */
return ((l == 1) || (l == 3)) ? 13 - trix(res) : trix(res);
}
}
// FIXME: no caller and incomplete!
void evalcontract() {
int s;
for (s = 0; s < 5; s++) {
results[1][s][dd(curdeal, 3, s)]++; /* south declarer */
results[0][s][dd(curdeal, 1, s)]++; /* north declarer */
}
}
void error(const char *s) {
fprintf(stderr, "%s\n", s);
exit(10);
}
/* implementations of regular & alternate pointcounts */
static int countindex = -1;
static void zerocount(int points[13]) {
int i;
for (i = 12; i >= 0; i--)
points[i] = 0;
}
void clearpointcount() {
zerocount(tblPointcount[idxHcp]);
countindex = -1;
}
void clearpointcount_alt(int cin) {
zerocount(tblPointcount[cin]);
countindex = cin;
}
void pointcount(int index, int value) {
assert(index <= 12);
if (index < 0) {
yyerror("too many pointcount values");
}
if (countindex < 0)
tblPointcount[idxHcp][index] = value;
else
tblPointcount[countindex][index] = value;
}
void *mycalloc(unsigned nel, size_t siz) {
void *p = calloc(nel, siz);
if (p)
return p;
fprintf(stderr, "Out of memory\n");
exit(-1); /*NOTREACHED */
}
static void initdistr() {
int ***p4, **p3, *p2;
int clubs, diamonds, hearts;
/* Allocate the four dimensional pointer array */
for (clubs = 0; clubs <= 13; clubs++) {
p4 = (int ***)mycalloc((unsigned)14 - clubs, sizeof(*p4));
distrbitmaps[clubs] = p4;
for (diamonds = 0; diamonds <= 13 - clubs; diamonds++) {
p3 = (int **)mycalloc((unsigned)14 - clubs - diamonds, sizeof(*p3));
p4[diamonds] = p3;
for (hearts = 0; hearts <= 13 - clubs - diamonds; hearts++) {
p2 = (int *)mycalloc((unsigned)14 - clubs - diamonds - hearts, sizeof(*p2));
p3[hearts] = p2;
}
}
}
}
void setshapebit(int cl, int di, int ht, int sp, int msk, int excepted) {
if (excepted)
distrbitmaps[cl][di][ht][sp] &= ~msk;
else
distrbitmaps[cl][di][ht][sp] |= msk;
}
static void newpack(Deal d) {
int suit, rank, place;
place = 0;
for (suit = SUIT_CLUB; suit <= SUIT_SPADE; suit++)
for (rank = 0; rank < 13; rank++)
d[place++] = make_card(suit, rank);
}
#ifdef FRANCOIS
int hascard(Deal d, int player, Card onecard, int vectordeal) {
int i;
int who;
switch (computing_mode) {
case STAT_MODE:
for (i = player * 13; i < (player + 1) * 13; i++)
if (d[i] == onecard)
return 1;
return 0;
break;
case EXHAUST_MODE:
if (exh_card_map[onecard] == -1)
return 0;
who = 1 & (vectordeal >> exh_card_map[onecard]);
return (exh_player[who] == player);
}
return 0;
#else
int hascard(Deal d, int player, Card onecard) {
int i;
for (i = player * 13; i < (player + 1) * 13; i++)
if (d[i] == onecard)
return 1;
return 0;
#endif /* FRANCOIS */
}
Card make_card(char rankchar, char suitchar) {
int rank, suit = 0;
for (rank = 0; rank < 13 && ucrep[rank] != rankchar; rank++)
;
assert(rank < 13);
switch (suitchar) {
case 'C':
suit = 0;
break;
case 'D':
suit = 1;
break;
case 'H':
suit = 2;
break;
case 'S':
suit = 3;
break;
default:
assert(0);
}
return make_card(suit, rank);
}
int make_contract(char suitchar, char trickchar) {
int trick, suit;
trick = (int)trickchar - '0';
switch (suitchar) {
case 'C':
suit = 0;
break;
case 'D':
suit = 1;
break;
case 'H':
suit = 2;
break;
case 'S':
suit = 3;
break;
case 'N':
suit = 4;
break;
default:
suit = 0;
printf("%c", suitchar);
assert(0);
}
return make_contract(suit, trick);
}
static void analyze(Deal d, Handstat *hsbase) {
/* Analyze a hand. Modified by HU to count controls and losers. */
/* Further mod by AM to count several alternate pointcounts too */
int player, next, c, r, s, t;
Card curcard;
Handstat *hs;
/* for each player */
for (player = COMPASS_NORTH; player <= COMPASS_WEST; ++player) {
/* If the expressions in the input never mention a player
we do not calculate his hand statistics. */
if (!use_compass[player]) {
#ifdef _DEBUG
/* In debug mode, blast the unused handstat, so that we can recognize it
as garbage should if we accidently read from it */
hs = hsbase + player;
memset(hs, 0xDF, sizeof(Handstat));
#endif /* _DEBUG_ */
continue;
}
/* where are the handstats for this player? */
hs = hsbase + player;
/* Initialize the handstat structure */
memset(hs, 0x00, sizeof(Handstat));
#ifdef _DEBUG
/* To debug, blast it with garbage.... */
memset(hs, 0xDF, sizeof(Handstat));
/* then overwrite those specific counters which need to be incremented */
for (t = idxHcp; t < idxEnd; ++t) {
/* clear the points for each suit */
for (s = SUIT_CLUB; s <= SUIT_SPADE; s++) {
hs->hs_counts[t][s] = 0;
}
/* and the total points as well */
hs->hs_totalcounts[t] = 0;
}
/* clear the length and distribution for each suit */
for (s = SUIT_CLUB; s <= SUIT_SPADE; s++) {
hs->hs_length[s] = 0;
hs->hs_dist[s] = 0;
}
/* clear the total distribution */
hs->hs_totaldist = 0;
/* clear the total losers */
hs->hs_totalloser = 0;
#endif /* _DEBUG_ */
/* start from the first card for this player, and walk through them all -
use the player offset to jump to the first card. Can't just increment
through the deck, because we skip those players who are not part of the
analysis */
next = 13 * player;
for (c = 0; c < 13; c++) {
curcard = d[next++];
s = card_suit(curcard);
r = card_rank(curcard);
/* Enable this #if to dump a visual look at the hands as they are
analysed. Best bet is to use test.all, or something else that
generates only one hand, lest you quickly run out of screen
realestate */
#if 0
#ifdef _DEBUG
#define VIEWCOUNT
#endif /* _DEBUG_ */
#endif /* 0 */
#ifdef VIEWCOUNT
printf("%c%c", "CDHS"[s], "23456789TJQKA"[r]);
#endif /* VIEWCOUNT */
hs->hs_length[s]++;
for (t = idxHcp; t < idxEnd; ++t) {
#ifdef VIEWCOUNT
printf(" %d ", tblPointcount[t][r]);
#endif /* VIEWCOUNT */
hs->hs_counts[t][s] += tblPointcount[t][r];
}
#ifdef VIEWCOUNT
printf("\n");
#endif /* VIEWCOUNT */
}
#ifdef VIEWCOUNT
printf("---\n");
#undef VIEWCOUNT
#endif /* VIEWCOUNT */
for (s = SUIT_CLUB; s <= SUIT_SPADE; s++) {
assert(hs->hs_length[s] < 14);
assert(hs->hs_length[s] >= 0);
switch (hs->hs_length[s]) {
case 0: {
/* A void is 0 losers */
hs->hs_loser[s] = 0;
break;
}
case 1: {
/* Singleton A 0 losers, K or Q 1 loser */
int losers[] = {1, 1, 0};
assert(hs->hs_control[s] < 3);
hs->hs_loser[s] = losers[hs->hs_counts[idxControls][s]];
break;
}
case 2: {
/* Doubleton AK 0 losers, Ax or Kx 1, Qx 2 */
int losers[] = {2, 1, 1, 0};
assert(hs->hs_control[s] <= 3);
hs->hs_loser[s] = losers[hs->hs_counts[idxControls][s]];
break;
}
default: {
/* Losers, first correct the number of losers */
assert(hs->hs_counts[idxWinners][s] < 4);
assert(hs->hs_counts[idxWinners][s] >= 0);
hs->hs_loser[s] = 3 - hs->hs_counts[idxWinners][s];
break;
}
}
/* Now add the losers to the total. */
hs->hs_totalloser += hs->hs_loser[s];
/* total up the other flavors of points */
for (t = idxHcp; t < idxEnd; ++t) {
hs->hs_totalcounts[t] += hs->hs_counts[t][s];
}
/* Now, using the values calculated already, load those pointcount
values which are common enough to warrant a non array lookup */
hs->hs_hcp[s] = hs->hs_counts[idxHcp][s];
hs->hs_control[s] = hs->hs_counts[idxControls][s];
hs->hs_dist[s] = hs->hs_length[s] > 4 ? hs->hs_length[s] - 4 : 0;
hs->hs_totaldist += hs->hs_dist[s];
} /* end for each suit */
hs->hs_totalhcp = hs->hs_totalcounts[idxHcp];
hs->hs_totalcontrol = hs->hs_totalcounts[idxControls];
hs->hs_bits = distrbitmaps[hs->hs_length[SUIT_CLUB]][hs->hs_length[SUIT_DIAMOND]][hs->hs_length[SUIT_HEART]]
[hs->hs_length[SUIT_SPADE]];
} /* end for each player */
}
void fprintcompact(FILE *f, Deal d, int ononeline) {
char pt[] = "nesw";
int s, p, r;
for (p = COMPASS_NORTH; p <= COMPASS_WEST; p++) {
fprintf(f, "%c ", pt[p]);
for (s = SUIT_SPADE; s >= SUIT_CLUB; s--) {
for (r = 12; r >= 0; r--)
if (HAS_CARD(d, p, make_card(s, r)))
fprintf(f, "%c", ucrep[r]);
if (s > 0)
fprintf(f, ".");
}
/* OK to use \n as this is mainly intended for internal dealer use. */
fprintf(f, ononeline ? " " : "\n");
}
}
static void printdeal(Deal d) {
int suit, player, rank, cards;
printf("%4d.\n", (nprod + 1));
for (suit = SUIT_SPADE; suit >= SUIT_CLUB; suit--) {
cards = 10;
for (player = COMPASS_NORTH; player <= COMPASS_WEST; player++) {
while (cards < 10) {
printf(" ");
cards++;
}
cards = 0;
for (rank = 12; rank >= 0; rank--) {
if (HAS_CARD(d, player, make_card(suit, rank))) {
printf("%c ", ucrep[rank]);
cards++;
}
}
if (cards == 0) {
printf("- ");
cards++;
}
}
printf("\n");
}
printf("\n");
}
static void setup_deal() {
int i, j;
j = 0;
for (i = 0; i < 52; i++) {
if (stacked_pack[i] != NO_CARD) {
curdeal[i] = stacked_pack[i];
} else {
while (fullpack[j] == NO_CARD)
j++;
curdeal[i] = fullpack[j++];
assert(j <= 52);
}
}
}
void predeal(int player, Card onecard) {
int i, j;
for (i = 0; i < 52; i++) {
if (fullpack[i] == onecard) {
fullpack[i] = NO_CARD;
for (j = player * 13; j < (player + 1) * 13; j++)
if (stacked_pack[j] == NO_CARD) {
stacked_pack[j] = onecard;
return;
}
yyerror("More than 13 cards for one player");
}
}
yyerror("Card predealt twice");
}
static void initprogram() {
int i, i_cycle;
int val;
/* Now initialize array zero52 with numbers 0..51 repeatedly. This whole
charade is just to prevent having to do divisions. */
val = 0;
for (i = 0, i_cycle = 0; i < NRANDVALS; i++) {
while (stacked_pack[val] != NO_CARD) {
/* this slot is predealt, do not use it */
val++;
if (val == 52) {
val = 0;
i_cycle = i;
}
}
zero52[i] = val++;
if (val == 52) {
val = 0;
i_cycle = i + 1;
}
}
/* Fill the last part of the array with 0xFF, just to prevent
that 0 occurs more than 51. This is probably just for hack value */
while (i > i_cycle) {
zero52[i - 1] = 0xFF;
i--;
}
}
static void swap2(Deal d, int p1, int p2) {
/* functions to assist "simulated" shuffling with player
swapping or loading from Ginsberg's library.dat -- AM990423 */
Card t;
int i;
p1 *= 13;
p2 *= 13;
for (i = 0; i < 13; ++i) {
t = d[p1 + i];
d[p1 + i] = d[p2 + i];
d[p2 + i] = t;
}
}
static FILE *find_library(const char *basename, const char *openopt) {
static const char *prefixes[] = {
"", "./", "../", "../../", "c:/", "c:/data/", "d:/myprojects/dealer/", "d:/arch/games/gib/", 0};
int i;
char buf[256];
FILE *result = 0;
for (i = 0; prefixes[i]; ++i) {
strcpy(buf, prefixes[i]);
strcat(buf, basename);
result = fopen(buf, openopt);
if (result)
break;
}
return result;
}
static int shuffle(Deal d) {
int i, j, k;
Card t;
if (loading) {
static FILE *lib = 0;
if (!lib) {
lib = find_library("library.dat", "rb");
if (!lib) {
fprintf(stderr, "Cannot find or open library file\n");
exit(-1);
}
fseek(lib, 26 * loadindex, SEEK_SET);
}
if (fread(&libdeal, 26, 1, lib)) {
int ph[4], i, suit, rank, pn;
unsigned long su;
libdeal.valid = 1;
for (i = 0; i < 4; ++i)
ph[i] = 13 * i;
for (i = 0; i <= 4; ++i) {
libdeal.tricks[i] = ntohs(libdeal.tricks[i]);
}
for (suit = 0; suit < 4; ++suit) {
su = libdeal.suits[suit];
su = ntohl(su);
for (rank = 0; rank < 13; ++rank) {
pn = su & 0x03;
su >>= 2;
d[ph[pn]++] = make_card(suit, 12 - rank);
}
}
return 1;
} else {
libdeal.valid = 0;
return 0;
}
}
if (swapindex) {
switch (swapindex) {
case 1:
swap2(d, 1, 3);
break;
case 2:
swap2(d, 2, 3);
break;
case 3:
swap2(d, 1, 2);
break;
case 4:
swap2(d, 1, 3);
break;
case 5:
swap2(d, 2, 3);
break;
}
} else {
/* Algorithm according to Knuth. For each card exchange with a random
other card. This is supposed to be the perfect shuffle algorithm.
It only depends on a valid random number generator. */
for (i = 0; i < 52; i++) {
if (stacked_pack[i] == NO_CARD) {
/* Thorvald Aagaard 14.08.1999 don't switch a predealt card */
do {
do {
#ifdef STD_RAND
k = RANDOM();
#else
/* Upper bits most random */
k = (RANDOM() >> (31 - RANDBITS));
#endif /* STD_RAND */
j = zero52[k];
} while (j == 0xFF);
} while (stacked_pack[j] != NO_CARD);
t = d[j];
d[j] = d[i];
d[i] = t;
}
}
}
if (swapping) {
++swapindex;
if ((swapping == 2 && swapindex > 1) || (swapping == 3 && swapindex > 5))
swapindex = 0;
}
return 1;
}
#ifdef FRANCOIS
/* Specific routines for EXHAUST_MODE */
void exh_get2players() {
/* Just finds who are the 2 players for whom we make exhaustive dealing */
int player, player_bit;
for (player = COMPASS_NORTH, player_bit = 0; player <= COMPASS_WEST; player++) {
if (completely_known_hand[player]) {
if (use_compass[player]) {
/* We refuse to compute anything for a player who has already his (her)
13 cards */
fprintf(stderr, "Exhaust-mode error: cannot compute anything for %s (known hand)\n",
player_name[player]);
exit(-1); /*NOTREACHED */
}
} else {
if (player_bit == 2) {
/* Exhaust mode only if *exactly* 2 hands have unknown cards */
fprintf(stderr, "Exhaust-mode error: more than 2 unknown hands...\n");
exit(-1); /*NOTREACHED */
}
exh_player[player_bit++] = player;
}
}
if (player_bit < 2) {
/* Exhaust mode only if *exactly* 2 hands have unknown cards */
fprintf(stderr, "Exhaust-mode error: less than 2 unknown hands...\n");
exit(-1); /*NOTREACHED */
}
}
void exh_set_bit_values(int bit_pos, Card onecard) {
/* only sets up some tables (see table definitions above) */
int suit, rank;
int suitloop;
suit = card_suit(onecard);
rank = card_rank(onecard);
for (suitloop = SUIT_CLUB; suitloop <= SUIT_SPADE; suitloop++) {
exh_suit_points_at_map[suitloop][bit_pos] = (suit == suitloop ? tblPointcount[0][rank] : 0);
exh_suit_length_at_map[suitloop][bit_pos] = (suit == suitloop ? 1 : 0);
}
exh_card_map[onecard] = bit_pos;
exh_card_at_bit[bit_pos] = onecard;
}
void exh_setup_card_map() {
int i;
for (i = 0; i < 256; i++) {
exh_card_map[i] = -1; /* undefined */
}
}
void exh_map_cards() {
int i, i_player;
int bit_pos;
for (i = 0, bit_pos = 0; i < 52; i++) {
if (fullpack[i] != NO_CARD) {
exh_set_bit_values(bit_pos, fullpack[i]);
bit_pos++;
}
}
/* Some cards may also have been predealt for the exh-players. In that case,
those cards are "put" in the msb positions of the vectordeal. The value
of the exh_predealt_vector is the constant part of the vector deal. */
for (i_player = 0; i_player < 2; i_player++) {
int player = exh_player[i_player];
exh_empty_slots[i_player] = 0;
for (i = 13 * player; i < 13 * (player + 1); i++) {
if (stacked_pack[i] == NO_CARD) {
exh_empty_slots[i_player]++;
} else {
exh_set_bit_values(bit_pos, stacked_pack[i]);
exh_predealt_vector |= (i_player << bit_pos);
bit_pos++;
}
}
}
}
void exh_print_stats(Handstat *hs) {
int s;
for (s = SUIT_CLUB; s <= SUIT_SPADE; s++) {
printf(" Suit %d: ", s);
printf("Len = %2d, Points = %2d\n", hs->hs_length[s], hs->hs_hcp[s]);
}
printf(" Totalpoints: %2d\n", hs->hs_totalhcp);
}
void exh_print_vector(Handstat *hs) {
int i, s, r;
int onecard;
Handstat *hsp;
printf("Player %d: ", exh_player[0]);
for (i = 0; i < 26; i++) {
if (!(1 & (vectordeal >> i))) {
onecard = exh_card_at_bit[i];
s = card_suit(onecard);
r = card_rank(onecard);
printf("%c%d ", ucrep[r], s);
}
}
printf("\n");
hsp = hs + exh_player[0];
exh_print_stats(hsp);
printf("Player %d: ", exh_player[1]);
for (i = 0; i < 26; i++) {
if ((1 & (vectordeal >> i))) {
onecard = exh_card_at_bit[i];
s = card_suit(onecard);
r = card_rank(onecard);
printf("%c%d ", ucrep[r], s);
}
}
printf("\n");
hsp = hs + exh_player[1];
exh_print_stats(hsp);
}
void exh_precompute_analyse_tables(void) {
/* This routine precomputes the values of the tables exh_lsb_... and
exh_msb_... These tables will allow very fast computation of
hand-primitives given the value of the vector deal.
Example: the number of hcp in diamonds of the player 0 will be :
exh_lsb_suit_length[vectordeal & (TWO_TO_THE_13-1)]
+ exh_msb_suit_length[vectordeal >> 13];
The way those table are precomputed may seem a little bit ridiculous.
This may be the reminiscence of Z80-programming ;-) FD-0499
*/
int vec_13;
int i_bit;
int suit;
unsigned char *elsp, *emsp, *elt, *emt, *elsl, *emsl;
unsigned char *mespam, *meslam;
unsigned char *lespam, *leslam;
for (vec_13 = 0; vec_13 < TWO_TO_THE_13; vec_13++) {
elt = exh_lsb_totalpoints + vec_13;
emt = exh_msb_totalpoints + vec_13;
*elt = *emt = 0;
for (suit = SUIT_CLUB; suit <= SUIT_SPADE; suit++) {
elsp = exh_lsb_suit_points[suit] + vec_13;
emsp = exh_msb_suit_points[suit] + vec_13;
elsl = exh_lsb_suit_length[suit] + vec_13;
emsl = exh_msb_suit_length[suit] + vec_13;
lespam = exh_suit_points_at_map[suit] + 12;
leslam = exh_suit_length_at_map[suit] + 12;
mespam = exh_suit_points_at_map[suit] + 25;
meslam = exh_suit_length_at_map[suit] + 25;
*elsp = *emsp = *elsl = *emsl = 0;
for (i_bit = 13; i_bit--; lespam--, leslam--, mespam--, meslam--) {
if (!(1 & (vec_13 >> i_bit))) {