forked from dds-bridge/ddd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddd.cpp
2219 lines (1998 loc) · 72.4 KB
/
ddd.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
/* **************************************************************************
ddd.cpp bridge double dummy driver
PM Cronje June 2006
Copyright 2006 P.M.Cronje
This file is part of the Double Dummer Driver (DDD).
DDD is a driver for the double dummy solver DDS,
released separately under the GPL by Bo Haglund.
DDD is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
DDD is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DDD; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
************************************************************************** */
// Changes in cleanSB 07-10-11 by Bo Haglund due to changes in dds 1.1.7 */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
// -----------------------------------------------------------------------------
// HOW TO COMPILE AND LINK THIS WITH THE LATEST VERSION OF DDS
//
// The following is suggested:
//
// - obtain the latest dds11x.cpp and dds11x.h file for version 1.1x
// of Bo Haglund's DDS and copy them into this directory
//
// Linux: compile and link as follows:
// g++ -O2 -Wall -o ./ddd ddd.cpp dds11x.cpp defs.cpp timer.cpp giblib.cpp rng.cpp
//
// Windows: compile and link as follows:
// g++ -O2 -Wall -o ddd.exe ddd.cpp dds11x.cpp defs.cpp timer.cpp giblib.cpp rng.cpp
//
// for debugging change the switch '-O2' to '-g'
//
// Note: on MingW you must have _WIN32 defined to compile code in timer.cpp
//
// -----------------------------------------------------------------------------
#include "dds/include/dll.h"
#include "portab_DDD.h"
#include "giblib.h"
#include "timer.h"
#define szVERSION "1.05"
#define szVERSION_DATE "25-Jan-2007"
/* v1.03 10-Jul-2006 add set tricks to -giblib option
add -tricks option
v1.04 12-Jul-2006 changed giblib to C++ class cGIBLib
(not struct sGIBLIb anymore)
13-jul-2006 add generateDeal(. ..) in class cGibLib
14-Jul-2006 add optional flag 'all' for option
-giblib=d1-d2[-all]
changes to szTricks etc. in class cGibLib
15-Jul-2006 fix 0 elapsed times for -gen
25-Jan-2007 changes to use dds11x
*/
// -----------------------------------------------------------------------------
// prototypes defined in this file
// -----------------------------------------------------------------------------
void cleanSB();
void getSBScore(const struct futureTricks & fut, int * pmaxscore,
bool bscore[14], ushort m[14][4]);
bool generate(int gen, unsigned int genseed, int gencards, int gentricks);
bool giblib(char * pszfile, int target, int sol, int mode, char * pszgiblib);
FILE * openFile(char * pszfile);
void playDD(cGIBLib * pgib, int target, int sol);
void printSBScore(int target, int solutions, struct futureTricks * pfut,
double elapsed);
void printSBScore(bool bscore[14], ushort m[14][4]);
bool setDDS(cGIBLib * pgib, struct deal * pdl);
bool testSBCode(int sbcode);
bool timeAll(char * pszfile, int trumps, int leader);
bool timeg(char * pszfile, int target, int sol, int mode,
char * pszxcn, int leader, bool bverbose);
bool tricks(cGIBLib * pgib, int ideal, int target, int sol, int mode);
// -----------------------------------------------------------------------------
// macros to convert to/from DDS players/cards
// -----------------------------------------------------------------------------
#define PLAYER2DDS(pl) ((pl-1)&3)
#define CARD2DDS(card) (14-card)
#define DDS2PLAYER(ddspl) ((ddspl+1)&3)
#define DDS2CARD(ddscard) (14-ddscard)
#define szGENFILE "gen.txt"
// *****************************************************************************
// DDD definitions
// *****************************************************************************
int main(int argc, char * argv[])
{
cGIBLib gib;
char * pszfile = 0, *pszname = 0, *pszxcn = 0, *pszgiblib = 0;
int iarg, target = -1, solutions = 3, mode = 1, deal = 1, trumps = -1, leader = -1;
int sbcode;
bool bok = false, btimeall = false, bplaydd = false, bverbose = false, btricks = false;
int gen = 0, gencards = 52, gentricks = 1;
unsigned int genseed = 0;
FILE * fp;
cTimer timer;
struct deal dl;
struct futureTricks fut;
printf("\nDouble Dummy Driver (DDD) version %s (%s)\n",
szVERSION, szVERSION_DATE);
if (argc < 2)
{
printf(
"\n"
"DDD usage:\n"
" ddd_executable file [opts]\n"
"where:\n"
" file : path for 'giblib' input file \n"
"optional arguments [opts] are one or more of: \n"
" -v : verbose where applicable \n"
" -target=d : (default -1), see dll reference \n"
" -sol=d : solution 1/2/3 (default 3), see dll reference \n"
" -mode=d : 0/1 (default 1), see dll reference \n"
" -trumps=t : s/h/d/c/n, this overrides the file (default=n) \n"
" -leader=l : w/n/e/s, this overrides the file (default=w) \n"
" but used only when no cards have been played \n"
" -deal=d : 1/2/... deal number in giblib file \n"
" only one of -deal or -name should be specified \n"
" -name=str : deal with 'name=str' in giblib file \n"
" only one of -deal or -name should be specified \n"
" -playdd : play deal choosing between DDS alternatives \n"
" -timeall : time all deals in file for sol=1/2/3, print stats \n"
" -timeg=xcn : x - hex digit, total tricks by n-s \n"
" c - contract s/h/d/c/n \n"
" n - number of deals \n"
" time the first n deals in the giblib file, \n"
" having total tricks x at contract c, \n"
" for target=-1 sol=1 mode=1 \n"
" and for the specified/default leader,\n"
" each deal is validated \n"
" -giblib=d1-d2[-all] : validate all deals from d1 to d2 in giblib file \n"
" for target=-1 sol=1 mode=1 \n"
" 1. if '-all' is given, this is done for all of \n"
" the 20 trick values even if some of them are '-' \n"
" 2. if '-all' is not given, this is done only for \n"
" those trick values which are not '-' \n"
" -tricks : like -giblib, but for single deal specified \n"
" by -name=str -deal=d or option \n"
" generate deals: \n"
" -gen=n : (required) n=number of deals to generate \n"
" output is written to a file (see below) \n"
" -genseed=s : (default 0) seed for random generator \n"
" -gencards=c : (default=52) number of cards generated per deal,\n"
" must be multiple of 4 \n"
" -gentricks=t : 0,1,...,20 (default 1), number of tricks values \n"
" to set randomly \n"
" generate output is written to a file: \n"
" gen-'genseed'-'ndeal'-'gencards'-'gentricks'.txt \n"
"\n"
);
return -1;
}
for (iarg = 1; iarg < argc; iarg++)
{
if (argv[iarg][0] == '-')
{
if (strncasecmp(argv[iarg], "-target=", 8) == 0)
target = atol(argv[iarg] + 8);
else if (strcasecmp(argv[iarg], "-timeall") == 0)
btimeall = true;
else if (strncasecmp(argv[iarg], "-timeg=", 7) == 0)
pszxcn = argv[iarg] + 7;
else if (strncasecmp(argv[iarg], "-giblib=", 8) == 0)
pszgiblib = argv[iarg] + 8;
else if (strncasecmp(argv[iarg], "-tricks", 7) == 0)
btricks = true;
else if (strcasecmp(argv[iarg], "-playdd") == 0)
bplaydd = true;
else if (strncasecmp(argv[iarg], "-gen=", 5) == 0)
gen = atol(argv[iarg] + 5);
else if (strncasecmp(argv[iarg], "-genseed=", 9) == 0)
genseed = static_cast<unsigned int>(atol(argv[iarg] + 9));
else if (strncasecmp(argv[iarg], "-gencards=", 10) == 0)
gencards = atol(argv[iarg] + 10);
else if (strncasecmp(argv[iarg], "-gentricks=", 11) == 0)
gentricks = atol(argv[iarg] + 11);
else if (strncasecmp(argv[iarg], "-sol=", 5) == 0)
solutions = atol(argv[iarg] + 5);
else if (strncasecmp(argv[iarg], "-mode=", 6) == 0)
{
mode = atol(argv[iarg] + 6);
if (mode < 0)
mode = 0;
else if (mode > 0)
mode = 1;
}
else if (strncasecmp(argv[iarg], "-trumps=", 8) == 0)
{
if (tolower(argv[iarg][8]) == 's') trumps = 0;
else if (tolower(argv[iarg][8]) == 'h') trumps = 1;
else if (tolower(argv[iarg][8]) == 'd') trumps = 2;
else if (tolower(argv[iarg][8]) == 'c') trumps = 3;
else if (tolower(argv[iarg][8]) == 'n') trumps = 4;
else
{
printf("*** error: invalid trumps option '%s'\n", argv[iarg]);
return -1;
}
}
else if (strncasecmp(argv[iarg], "-leader=", 8) == 0)
{
if (tolower(argv[iarg][8]) == 'w') leader = 0;
else if (tolower(argv[iarg][8]) == 'n') leader = 1;
else if (tolower(argv[iarg][8]) == 'e') leader = 2;
else if (tolower(argv[iarg][8]) == 's') leader = 3;
else
{
printf("*** error: invalid leader option '%s'\n", argv[iarg]);
return -1;
}
}
else if (strncasecmp(argv[iarg], "-deal=", 6) == 0)
{
deal = atol(argv[iarg] + 6);
if (deal < 1)
deal = 1;
}
else if (strncasecmp(argv[iarg], "-name=", 6) == 0)
pszname = argv[iarg] + 6;
else if (strcasecmp(argv[iarg], "-v") == 0)
bverbose = true;
else
{
printf("*** error: invalid option '%s'\n", argv[iarg]);
return -1;
}
}
else
pszfile = argv[iarg];
}
if (gen > 0)
{
generate(gen, genseed, gencards, gentricks);
goto cleanup;
}
if (btimeall)
{
timeAll(pszfile, trumps, leader);
goto cleanup;
}
else if (pszxcn)
{
timeg(pszfile, -1, 1, 1, pszxcn, (leader == -1 ? 0 : leader), bverbose);
goto cleanup;
}
else if (pszgiblib)
{
giblib(pszfile, -1, 1, 1, pszgiblib);
goto cleanup;
}
// open file
fp = openFile(pszfile);
if (fp == 0)
return -1;
// read giblib deal
bok = gib.readFile(deal - 1, pszname, fp);
fclose(fp);
if (!bok)
{
printf("%s", gib.szErrMsg);
return -1;
}
if (btricks)
{
tricks(&gib, deal, -1, 1, 1);
goto cleanup;
}
// if overridden from command line, set trumps/leader
if (trumps != -1)
gib.Trumps = trumps;
if ((gib.nPlayed == 0) && (leader != -1))
{
// override leader/player only when no cards have been played
gib.Leader = gib.Player = leader;
}
if (bplaydd)
{
playDD(&gib, target, solutions);
goto cleanup;
}
// set up dds10
if (setDDS(&gib, &dl) == false)
return -1;
printf("\n");
gib.print();
printf("\n");
gib.printHands();
gib.printInfo();
printf("\n");
fflush(stdout);
timer.start();
sbcode = SolveBoard(dl, target, solutions, mode, &fut, 0);
timer.check();
if (testSBCode(sbcode) == false)
exit(-1);
printSBScore(target, solutions, &fut, timer.dblElapsed());
cleanup:
cleanSB();
return 0;
} // main
// *****************************************************************************
void cleanSB()
{
}
// *****************************************************************************
bool generate(int gen, unsigned int genseed, int gencards, int gentricks)
{
// generate deals
// write giblib extended format to specified file
cGIBLib gib;
int ntrick, sbcode, ideal, trickpos, trumps, leader;
int settrick[20], setpos[20], nsettrick, itrick;
int maxscore, nerror, score, ntotal;
bool bscore[14];
ushort m[14][4];
FILE * fp;
cTimer timer;
struct deal dl;
struct futureTricks fut;
double elapsed, totalelapsed;
unsigned long long nodes, totalnodes;
char sz1[32], szfile[256];
int target = -1, sol = 1, mode = 1;
// check arguments
sprintf(szfile, "gen-%u-%d-%d-%d.txt", genseed, gen, gencards, gentricks);
// if existing file, warn the user
fp = fopen(szfile, "r");
if (fp)
{
fclose(fp);
printf("\n*** WARNING: generate deals\n"
" the file '%s' is an existing file\n"
" and it may be overwritten\n"
"do you want to continue? (y/n)):", szfile);
fflush(stdout);
char buf[255], *pch;
pch = fgets(buf, 255, stdin);
if (pch == 0)
return false;
if (tolower(*pch) != 'y')
return false;
}
if (gen < 0)
{
printf("*** error: gen=%d invalid number of deals to generate\n", gen);
return false;
}
if ((gencards < 4) || (gencards > 52))
{
printf("*** error: gencards=%d must be >=4 and <=52\n", gencards);
return false;
}
if ((gencards % 4) != 0)
{
printf("*** error: gencards=%d not a multiple ofg 4\n", gencards);
return false;
}
if ((gentricks < 0) || (gentricks > 20))
{
printf("*** error: gentricks=%d must be >=0 and <=20\n", gentricks);
return false;
}
fp = fopen(szfile, "w");
if (fp == 0)
{
printf("*** error: : cannot open gen file %s for writing\n", szfile);
return false;
}
printf("\ngenerate: seed=%u deals=%d cards=%d tricks=%d file=%s\n",
genseed, gen, gencards, gentricks, szfile);
fflush(stdout);
gib.setRNGSeed(genseed);
timer.start();
nerror = ntotal = 0;
elapsed = totalelapsed = 0;
nodes = totalnodes = 0;
for (ideal = 0; ideal < gen; ideal++)
{
// loop over generated deal
// generate the deal
if (gib.generateDeal(gencards / 4) == false)
return false;
if (gib.setGeneratedDeal() == false)
break;
// set tricks
if (gentricks > 0)
{
// initialize trick positions
for (trickpos = 0; trickpos < 20; trickpos++)
settrick[trickpos] = trickpos;
nsettrick = 20;
// get sorted trick positions
for (itrick = 0; itrick < gentricks; itrick++)
{
// get a random trick position
trickpos = static_cast<int>(gib.pRNG->randomUint
(static_cast<unsigned>(nsettrick)));
setpos[itrick] = settrick[trickpos];
if (trickpos < nsettrick - 1)
memmove(settrick + trickpos, settrick + trickpos + 1,
static_cast<size_t>(nsettrick - 1 - trickpos)*sizeof(int));
nsettrick--;
}
// compute trick values
for (itrick = 0; itrick < gentricks; itrick++)
{
timer.check();
trickpos = setpos[itrick];
gib.getTricks(trickpos, &leader, &trumps, &ntrick);
// set up dds10
gib.Trumps = trumps;
gib.Leader = leader;
if (setDDS(&gib, &dl) == false)
return false;
sbcode = SolveBoard(dl, target, sol, mode, &fut, 0);
if (testSBCode(sbcode) == false)
return false;
getSBScore(fut, &maxscore, bscore, m);
if (maxscore < 0)
{
printf("*** error: deal=%d leader=%c : no score\n",
ideal + 1, chPLAYER[gib.Leader]);
return false;
}
score = maxscore;
if (score >= 0)
score = ((gib.Leader & 1) ? score : gib.numCard() / 4 - score);
timer.check();
nodes = static_cast<unsigned long long>(fut.nodes);
totalnodes += nodes;
elapsed = timer.dblDeltaElapsed();
printf(" deal=%d leader=%c %d%c nodes=%s elapsed=%0.2f\n",
ideal + 1, chPLAYER[gib.Leader], score, (trumps == 4) ? 'N' : chSUIT[trumps],
format64(nodes, sz1), elapsed);
if (score < 10)
gib.szTricks[trickpos] = static_cast<char>(score + '0');
else
gib.szTricks[trickpos] = static_cast<char>(score - 10 + 'A');
}
}
// write the deal to file
fprintf(fp, "%s:%s\n", gib.szDeal, gib.szTricks);
// flush file, so that cancel does not lose data
fflush(fp);
// print the deal
printf("%d %s:%s\n", ideal + 1, gib.szDeal, gib.szTricks);
}
timer.check();
totalelapsed = timer.dElapsed;
printf("deals=%d nodes=%s elapsed=%0.2f\n"
"output written to file %s\n",
gen, format64(totalnodes, sz1),
totalelapsed, szfile);
fclose(fp);
return true;
} // generate
// *****************************************************************************
void getSBScore(const struct futureTricks & fut, int * pmaxscore,
bool bscore[14], ushort m[14][4])
{
// get DDS 'score' after SolveBoard(..) has been run
int iscore, suit, alt;
for (iscore = 0; iscore < 14; iscore++)
bscore[iscore] = false;
memset(m, 0, 14 * 4 * sizeof(ushort));
*pmaxscore = -1;
for (alt = 0; alt < fut.cards; alt++)
{
iscore = fut.score[alt];
if (iscore == -1)
continue;
if (fut.rank[alt])
{
bscore[iscore] = true;
if (*pmaxscore < iscore)
*pmaxscore = iscore;
suit = fut.suit[alt];
setBit(m[iscore][suit], 14 - fut.rank[alt]);
ushort malt = static_cast<ushort>(fut.equals[alt]);
while (malt)
{
int c = leastSignificant1Bit(malt);
clearBit(malt, c);
setBit(m[iscore][suit], 14 - c);
}
}
}
} // getSBScore
// *****************************************************************************
bool giblib(char * pszfile, int target, int sol, int mode, char * pszgiblib)
{
// run deals in giblib file for all possible tricks,
// collect and print the stats
cGIBLib gib;
int sbcode, ideal1, ideal2, ideal, trickpos, trumps,
tricks = 0, leader;
int maxscore, nerror, score, ntotal;
char * pch;
bool ball, bscore[14];
ushort m[14][4];
bool bok = false;
FILE * fp;
cTimer timer;
struct deal dl;
struct futureTricks fut;
double elapsed, dealelapsed, totalelapsed;
unsigned long long nodes, dealnodes, totalnodes;
char sz1[32], sz2[32];
// get/test arguments
pch = strchr(pszgiblib, '-');
if (pch == 0)
{
printf("*** error: giblib=%s not 'deal1-deal2[-all]'\n", pszgiblib);
return false;
}
ideal1 = atol(pszgiblib);
if ((ideal1 < 1) || (strlen(pszgiblib) == 0))
{
printf("*** error: giblib=%s invalid deal1\n", pszgiblib);
return false;
}
pch++;
ideal2 = atol(pch);
if ((ideal2 < 1) || (strlen(pch) == 0))
{
printf("*** error: giblib=%s invalid deal2\n", pszgiblib);
return false;
}
if (ideal1 > ideal2)
{
printf("*** error: giblib=%s ideal1 > ideal2\n", pszgiblib);
return false;
}
ball = false;
pch = strchr(pch, '-');
if (pch)
{
if (strcmp(pch, "-all") != 0)
{
printf("*** error: giblib=%s expected '-all' after ideal2\n", pszgiblib);
return false;
}
ball = true;
}
fp = openFile(pszfile);
if (fp == 0)
return false;
printf("\n"
"giblib=%s target=%d sol=%d mode=%d\n", pszgiblib, target, sol, mode);
fflush(stdout);
timer.start();
nerror = ntotal = 0;
elapsed = totalelapsed = 0;
nodes = totalnodes = 0;
// skip to startdeal
for (ideal = 1; ideal < ideal1; ideal++)
{
bok = gib.readDeal(fp);
if (!bok)
return false;
}
for (ideal = ideal1; ideal <= ideal2; ideal++)
{
// loop over deals in file
// read giblib deal
bok = gib.readDeal(fp);
if (!bok)
break;
if (gib.setDeal() == false)
break;
if ((gib.numCard() % 4) != 0)
{
printf(" warning: deal=%d cards=%d not multiple of 4, skipping deal ...\n",
ideal, gib.numCard());
continue;
}
// Note that we are using gib.szTricks internally,
// - if not read, we are setting it,
// - in addition we are correcting any errors.
dealnodes = 0;
dealelapsed = 0.0;
for (trickpos = 0; trickpos < 20; trickpos++)
{
if (gib.getTricks(trickpos, &leader, &trumps, &tricks) == false)
{
if (ball == false)
continue;
}
// set up dds10
gib.Trumps = trumps;
gib.Leader = leader;
if (setDDS(&gib, &dl) == false)
return false;
sbcode = SolveBoard(dl, target, sol, mode, &fut, 0);
timer.check();
if (testSBCode(sbcode) == false)
return false;
getSBScore(fut, &maxscore, bscore, m);
if (maxscore < 0)
{
printf("*** error: deal=%d leader=%c no score\n",
ideal, chPLAYER[gib.Leader]);
return false;
}
score = maxscore;
if (score >= 0)
score = ((gib.Leader & 1) ? score : gib.numCard() / 4 - score);
nodes = static_cast<unsigned long long>(fut.nodes);
dealnodes += nodes;
totalnodes += nodes;
elapsed = timer.dblDeltaElapsed();
dealelapsed += elapsed;
totalelapsed += elapsed;
if (gib.szTricks[trickpos] != '-')
{
printf(" deal=%d leader=%c %d%c score=%d nodes=%s elapsed=%0.2f\n",
ideal, chPLAYER[gib.Leader], tricks, (trumps == 4) ? 'N' : chSUIT[trumps],
score, format64(nodes, sz1), elapsed);
bok = true;
// test that tricks and score agree
if ((score < 0) || score != tricks)
{
nerror++;
bok = false;
printf(" error: deal=%d leader=%c %d%c score=%d\n",
ideal, chPLAYER[gib.Leader], tricks, (trumps == 4) ? 'N' : chSUIT[trumps],
score);
}
}
else
{
printf(" deal=%d leader=%c %d%c nodes=%s elapsed=%0.2f\n",
ideal, chPLAYER[gib.Leader], score, (trumps == 4) ? 'N' : chSUIT[trumps],
format64(nodes, sz1), elapsed);
bok = false;
}
if (bok == false)
{
if (score < 10)
gib.szTricks[trickpos] = static_cast<char>
(score + static_cast<int>('0'));
else
gib.szTricks[trickpos] = static_cast<char>
(score - 10 + static_cast<int>('A'));
}
ntotal++;
}
printf(" deal=%d nodes=%s elapsed=%0.2f totalelapsed=%0.2f\n"
" ",
ideal, format64(dealnodes, sz1), dealelapsed, totalelapsed);
if (strlen(gib.pszName))
printf("name=%s ", gib.pszName);
printf("tricks=%s (max=%d)\n",
gib.szTricks, gib.numCard() / 4);
}
printf("deals=%s nodes=%s avg=%s elapsed=%0.2f avg=%.02f\n",
pszgiblib, format64(totalnodes, sz1),
ntotal ? format64(totalnodes / static_cast<unsigned>(ntotal), sz2) :
format64(totalnodes, sz2),
totalelapsed,
ntotal ? totalelapsed / static_cast<double>(ntotal) :
static_cast<double>(totalelapsed));
if (nerror)
printf("*** ERROR: nerror=%d, tricks and score different\n", nerror);
printf("\n");
fclose(fp);
return true;
} // giblib
// *****************************************************************************
FILE * openFile(char * pszfile)
{
if (pszfile == 0)
{
printf("*** error: no 'giblib' file specified\n");
return 0;
}
/*if(access(pszfile,F_OK) != 0)
{ printf("*** error: non-existing file %s\n",pszfile);
return 0;
}
*/
FILE * fp = fopen(pszfile, "r");
if (fp == 0)
{
printf("*** : cannot open file %s for reading\n", pszfile);
return 0;
}
return fp;
} // openFile
// *****************************************************************************
void playDD(cGIBLib * pgib, int target, int sol)
{
cTimer timer;
double elapsed = 0;
struct deal dl;
struct futureTricks fut;
int sbcode, suit, card, ntotalcard;
char * pch, *pchend, buf[255];
bool bhelp = false, bcompute = true;
char szplayer[4][6] = {"west", "north", "east", "south"};
printf("\n");
pgib->print();
printf("\n");
timer.start();
for (;;)
{
pgib->printHands();
pgib->printInfo();
printf("\n");
fflush(stdout);
ntotalcard = pgib->numCard();
if (ntotalcard)
{
// DDS only when there are cards left to play
if (bcompute)
{
// set up dds10
if (setDDS(pgib, &dl) == false)
return;
timer.check();
sbcode = SolveBoard(dl, target, sol, 1, &fut, 0);
timer.check();
if (testSBCode(sbcode) == false)
exit(-1);
elapsed += timer.dblDeltaElapsed();
}
printSBScore(target, sol, &fut, elapsed);
}
if (bhelp)
{
printf("help for options:\n"
" 'q' - quit \n"
" 'h' - print this help \n"
" 'u' - unplay previous card (if available) \n"
" 'sc' - card/suit to play, e.g. dq/ht/s3\n"
" must be valid for current player \n"
);
bhelp = false;
}
bcompute = false;
if (ntotalcard > 0)
printf("enter options for %s ", szplayer[pgib->Player]);
else
printf("enter options ");
if (ntotalcard <= 0)
printf("(q/h/u): ");
else if (pgib->nPlayed > 0)
printf("(q/h/u/sc): ");
else
printf("(q/h/sc): ");
fflush(stdout);
pch = fgets(buf, 255, stdin);
if (pch == 0)
continue;
pchend = strchr(buf, '\n');
if (pchend)
*pchend = '\0';
pchend = strchr(buf, '\r');
if (pchend)
*pch = '\0';
while (*pch == ' ')
pch++;
if (*pch == '\0')
continue;
if (*pch == 'q')
break;
else if ((pch[0] == 'h') && ((pch[1] == ' ') || (pch[1] == '\0')))
{
bhelp = true;
continue;
}
else if ((pgib->nPlayed > 0) && (*pch == 'u'))
{
if (pgib->unplayCard())
bcompute = true;
}
else if (ntotalcard)
{
if (tolower(pch[0]) == 's') suit = 0;
else if (tolower(pch[0]) == 'h') suit = 1;
else if (tolower(pch[0]) == 'd') suit = 2;
else if (tolower(pch[0]) == 'c') suit = 3;
else
continue;
pch++;
card = static_cast<int>(cGIBLib::getCard(pch[0]));
if (card == eCARD_NONE)
continue;
// play the card
pgib->SuitPlayed[pgib->nPlayed] = suit;
pgib->CardPlayed[pgib->nPlayed] = card;
if (pgib->playCard(suit, card))
bcompute = true;
}
}
} // playDD
// *****************************************************************************
void printSBScore(int target, int solutions, struct futureTricks * pfut,
double elapsed)
{
// print DDS 'score' after SolveBoard(..) has been run
char sz1[32];
bool bscore[14];
ushort m[14][4];
int maxscore;
printf("-- sb completed: nodes=%s tgt=%d sol=%d alt=%d elapsed=%0.2f\n",
format(static_cast<unsigned>(pfut->nodes), sz1), target, solutions, pfut->cards, elapsed);
getSBScore(*pfut, &maxscore, bscore, m);
printSBScore(bscore, m);
} // printSBScore
// *****************************************************************************
void printSBScore(bool bscore[14], ushort m[14][4])
{
int iscore, suit, nscore = 0;
for (iscore = 0; iscore < 14; iscore++)
{
if (bscore[iscore])
{
nscore++;
printf(" %2d: ", iscore);
for (suit = 0; suit < 4; suit++)
{
if (m[iscore][suit])
{
printf("%c ", chSUIT[suit]);
while (m[iscore][suit])
{
int c = leastSignificant1Bit(m[iscore][suit]);
clearBit(m[iscore][suit], c);
printf("%c", chCARD[c]);
}
printf(" ");
}
}
printf("\n");
}
}
if (nscore == 0)
printf(" none\n");
} // printSBScore
// *****************************************************************************
bool setDDS(cGIBLib * pgib, struct deal * pdl)
{
// setup deal structure for DDS
int pl, suit, ntrick, ntrickcard;
int lastsuit, lastcard, card;
pdl->trump = pgib->Trumps; // s=0,h=1,d=2,c=3,nt=4
// remaining cards from partial trick
ntrick = (pgib->nPlayed / 4);
ntrickcard = (pgib->nPlayed - 4 * ntrick);
memset(pdl->currentTrickSuit, 0, 3 * sizeof(int));
memset(pdl->currentTrickRank, 0, 3 * sizeof(int));
if (ntrickcard > 0)
{
for (pl = 0; pl < ntrickcard; pl++)
{
lastsuit = pgib->SuitPlayed[4 * ntrick + pl];
lastcard = pgib->CardPlayed[4 * ntrick + pl];
pdl->currentTrickSuit[pl] = lastsuit;
pdl->currentTrickRank[pl] = CARD2DDS(lastcard);
}
}
// DDS leader
pdl->first = PLAYER2DDS(pgib->Leader); // n=0,e=1,s=2,w=3
// DDS remaining cards
for (pl = 0; pl < 4; pl++)
{
int ddspl = PLAYER2DDS(pl);
for (suit = 0; suit < 4; suit++)
{
ushort m = 0, mp = pgib->mPlayerSuit[pl][suit];