forked from thomasoa/andrews-deal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdds.cpp
4536 lines (4174 loc) · 160 KB
/
dds.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
/*
*
* DDS 1.1.9 A bridge double dummy solver.
* Copyright (C) 2006-2008 by Bo Haglund
* Cleanups and porting to Linux and MacOSX (C) 2006 by Alex Martelli
*
* This program 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.
*
* This program 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
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc, 51 Franklin Street, 5th Floor, Boston MA 02110-1301, USA.
*
*/
#include "dds.h"
const int ContractInfo::nextSuitArray[4][4] = {
{ 1, 2, 3, 4},
{ 2, 0, 3, 4},
{ 1, 3, 0, 4},
{ 1, 2, 4, 0}
};
GLOBALS Globals;
int nodeTypeStore[4];
unsigned short int lowestWin[50][4];
int nodes;
int trickNodes;
int no[50];
int iniDepth;
int seatToPlay;
int payOff, val;
struct pos lookAheadPos; /* Is initialized for starting alpha-beta search */
struct moveType forbiddenMoves[14];
struct moveType initialMoves[4];
struct moveType cd;
struct movePlyType movePly[50];
int tricksTarget;
struct gameInfo game;
int estTricks[4];
FILE *fp2, *fp7, *fp11;
struct moveType * bestMove;
struct winCardType * temp_win;
int hiwinSetSize=0, hinodeSetSize=0;
int hilenSetSize=0;
int MaxnodeSetSize=0;
int MaxwinSetSize=0;
int MaxlenSetSize=0;
int nodeSetSizeLimit=0;
int winSetSizeLimit=0;
int lenSetSizeLimit=0;
LONGLONG maxmem, allocmem, summem;
int wmem, nmem, lmem;
int maxIndex;
int wcount, ncount, lcount;
int clearTTflag=FALSE, windex=-1;
int ttCollect=FALSE;
int suppressTTlog=FALSE;
unsigned char cardRank[15], cardSuit[5], cardSeat[4];
LONGLONG suitLengths=0;
struct posSearchType *rootnp[14][4];
struct winCardType **pw;
struct nodeCardsType **pn;
struct posSearchType **pl;
#ifdef CANCEL
int cancelOrdered=FALSE;
int cancelStarted=FALSE;
int threshold=CANCELCHECK;
#endif
#ifdef _MANAGED
#pragma managed(push, off)
#endif
extern "C" inline holding_t distinctUnplayedCards(holding_t origHolding, holding_t played,holding_t *sequence) {
holding_t bitRank;
holding_t unplayed = origHolding & (~played);
holding_t result = 0;
int inSequence=0;
*sequence = 0;
if (unplayed) {
for (bitRank = (1<<12); bitRank; bitRank >>= 1) {
if (unplayed & bitRank) {
if (inSequence) {
*sequence |= bitRank;
} else {
result |= bitRank;
inSequence = 1;
}
} else {
if (!(played & bitRank)) {
inSequence = 0;
}
}
}
}
return result;
}
struct UnplayedCardsFinder {
protected:
struct diagram starting;
public:
UnplayedCardsFinder() {}
void initialize(const struct diagram &diagram) {
for (int seat=0; seat<4; seat++) {
for (int suit=0; suit<4; suit++) {
starting.cards[seat][suit] = diagram.cards[seat][suit];
}
}
}
inline holding_t getUnplayed(int seat, int suit, holding_t played,holding_t &sequence) {
holding_t holding = starting.cards[seat][suit];
return distinctUnplayedCards(holding,played,&sequence);
}
} unplayedFinder;
#ifdef _MANAGED
#pragma managed(pop)
#endif
extern "C" int SolveBoard(struct deal dl, int target,
int solutions, int mode, struct futureTricks *futp) {
int k, n, cardCount, found, totalTricks, tricks, last, checkRes;
int g, upperbound, lowerbound, first, i, j, forb, ind, flag, noMoves;
int seat, suit;
int mcurr;
int noStartMoves;
int seatRelFirst;
int latestTrickSuit[4];
int latestTrickRank[4];
int maxSeat=0, maxSuit=0, maxRank;
struct pos iniPosition;
struct movePlyType temp;
struct moveType mv;
/*FILE *fp;*/
/*InitStart();*/ /* Include InitStart() if inside SolveBoard,
but preferable InitStart should be called outside
SolveBoard like in DllMain for Windows. */
for (k=0; k<=13; k++) {
forbiddenMoves[k].rank=0;
forbiddenMoves[k].suit=0;
}
if (target<-1) {
DumpInput(-5, dl, target, solutions, mode);
return -5;
}
if (target>13) {
DumpInput(-7, dl, target, solutions, mode);
return -7;
}
if (solutions<1) {
DumpInput(-8, dl, target, solutions, mode);
return -8;
}
if (solutions>3) {
DumpInput(-9, dl, target, solutions, mode);
return -9;
}
if (target==-1) {
tricksTarget=99;
} else {
tricksTarget=target;
}
cardCount=0;
for (seat=0; seat<=3; seat++) {
for (suit=0; suit<=3; suit++) {
game.diagram.cards[seat][suit]=dl.remaining.cards[seat][suit];
cardCount+=CountOnes(game.diagram.cards[seat][suit]);
}
}
if (dl.currentTrickRank[2]) {
if ((dl.currentTrickRank[2]<2)||(dl.currentTrickRank[2]>14)
||(dl.currentTrickSuit[2]<0)||(dl.currentTrickSuit[2]>3)) {
DumpInput(-12, dl, target, solutions, mode);
return -12;
}
seatToPlay=rho(dl.first);
seatRelFirst=3;
noStartMoves=3;
if (cardCount<=4) {
for (k=0; k<=3; k++) {
if (game.diagram.cards[seatToPlay][k]!=0) {
latestTrickSuit[seatToPlay]=k;
latestTrickRank[seatToPlay]=
InvBitMapRank(game.diagram.cards[seatToPlay][k]);
break;
}
}
latestTrickSuit[partner(dl.first)]=dl.currentTrickSuit[2];
latestTrickRank[partner(dl.first)]=dl.currentTrickRank[2];
latestTrickSuit[lho(dl.first)]=dl.currentTrickSuit[1];
latestTrickRank[lho(dl.first)]=dl.currentTrickRank[1];
latestTrickSuit[dl.first]=dl.currentTrickSuit[0];
latestTrickRank[dl.first]=dl.currentTrickRank[0];
}
} else if (dl.currentTrickRank[1]) {
if ((dl.currentTrickRank[1]<2)||(dl.currentTrickRank[1]>14)
||(dl.currentTrickSuit[1]<0)||(dl.currentTrickSuit[1]>3)) {
DumpInput(-12, dl, target, solutions, mode);
return -12;
}
seatToPlay=partner(dl.first);
seatRelFirst=2;
noStartMoves=2;
if (cardCount<=4) {
for (k=0; k<=3; k++) {
if (game.diagram.cards[seatToPlay][k]!=0) {
latestTrickSuit[seatToPlay]=k;
latestTrickRank[seatToPlay]=
InvBitMapRank(game.diagram.cards[seatToPlay][k]);
break;
}
}
for (k=0; k<=3; k++) {
if (game.diagram.cards[rho(dl.first)][k]!=0) {
latestTrickSuit[rho(dl.first)]=k;
latestTrickRank[rho(dl.first)]=
InvBitMapRank(game.diagram.cards[rho(dl.first)][k]);
break;
}
}
latestTrickSuit[lho(dl.first)]=dl.currentTrickSuit[1];
latestTrickRank[lho(dl.first)]=dl.currentTrickRank[1];
latestTrickSuit[dl.first]=dl.currentTrickSuit[0];
latestTrickRank[dl.first]=dl.currentTrickRank[0];
}
} else if (dl.currentTrickRank[0]) {
if ((dl.currentTrickRank[0]<2)||(dl.currentTrickRank[0]>14)
||(dl.currentTrickSuit[0]<0)||(dl.currentTrickSuit[0]>3)) {
DumpInput(-12, dl, target, solutions, mode);
return -12;
}
seatToPlay=lho(dl.first);
seatRelFirst=1;
noStartMoves=1;
if (cardCount<=4) {
for (k=0; k<=3; k++) {
if (game.diagram.cards[seatToPlay][k]!=0) {
latestTrickSuit[seatToPlay]=k;
latestTrickRank[seatToPlay]=
InvBitMapRank(game.diagram.cards[seatToPlay][k]);
break;
}
}
for (k=0; k<=3; k++) {
if (game.diagram.cards[rho(dl.first)][k]!=0) {
latestTrickSuit[rho(dl.first)]=k;
latestTrickRank[rho(dl.first)]=
InvBitMapRank(game.diagram.cards[rho(dl.first)][k]);
break;
}
}
for (k=0; k<=3; k++) {
if (game.diagram.cards[partner(dl.first)][k]!=0) {
latestTrickSuit[partner(dl.first)]=k;
latestTrickRank[partner(dl.first)]=
InvBitMapRank(game.diagram.cards[partner(dl.first)][k]);
break;
}
}
latestTrickSuit[dl.first]=dl.currentTrickSuit[0];
latestTrickRank[dl.first]=dl.currentTrickRank[0];
}
} else {
seatToPlay=dl.first;
seatRelFirst=0;
noStartMoves=0;
if (cardCount<=4) {
for (k=0; k<=3; k++) {
if (game.diagram.cards[seatToPlay][k]!=0) {
latestTrickSuit[seatToPlay]=k;
latestTrickRank[seatToPlay]=
InvBitMapRank(game.diagram.cards[seatToPlay][k]);
break;
}
}
for (k=0; k<=3; k++) {
if (game.diagram.cards[rho(dl.first)][k]!=0) {
latestTrickSuit[rho(dl.first)]=k;
latestTrickRank[rho(dl.first)]=
InvBitMapRank(game.diagram.cards[rho(dl.first)][k]);
break;
}
}
for (k=0; k<=3; k++) {
if (game.diagram.cards[partner(dl.first)][k]!=0) {
latestTrickSuit[partner(dl.first)]=k;
latestTrickRank[partner(dl.first)]=
InvBitMapRank(game.diagram.cards[partner(dl.first)][k]);
break;
}
}
for (k=0; k<=3; k++) {
if (game.diagram.cards[lho(dl.first)][k]!=0) {
latestTrickSuit[lho(dl.first)]=k;
latestTrickRank[lho(dl.first)]=
InvBitMapRank(game.diagram.cards[lho(dl.first)][k]);
break;
}
}
}
}
game.contract=100+10*(dl.trump+1);
game.first=dl.first;
first=dl.first;
game.noOfCards=cardCount;
if (dl.currentTrickRank[0]!=0) {
game.leadSeat=dl.first;
game.leadSuit=dl.currentTrickSuit[0];
game.leadRank=dl.currentTrickRank[0];
} else {
game.leadSeat=0;
game.leadSuit=0;
game.leadRank=0;
}
for (k=0; k<=2; k++) {
initialMoves[k].suit=255;
initialMoves[k].rank=255;
}
for (k=0; k<noStartMoves; k++) {
initialMoves[noStartMoves-1-k].suit=dl.currentTrickSuit[k];
initialMoves[noStartMoves-1-k].rank=dl.currentTrickRank[k];
}
if (cardCount % 4) {
totalTricks=(cardCount-4)/4+2;
} else {
totalTricks=(cardCount-4)/4+1;
}
checkRes=CheckDeal(&cd);
if (game.noOfCards<=0) {
DumpInput(-2, dl, target, solutions, mode);
return -2;
}
if (game.noOfCards>52) {
DumpInput(-10, dl, target, solutions, mode);
return -10;
}
if (totalTricks<target) {
DumpInput(-3, dl, target, solutions, mode);
return -3;
}
if (checkRes) {
DumpInput(-4, dl, target, solutions, mode);
return -4;
}
if (cardCount<=4) {
maxSuit = latestTrickSuit[dl.first];
maxRank = 0;
for (k=0; k<=3; k++) {
if ((dl.trump != maxSuit && dl.trump == latestTrickSuit[k]) ||
(maxSuit == latestTrickSuit[k] && maxRank<latestTrickRank[k])) {
maxRank=latestTrickRank[k];
maxSuit=dl.trump;
maxSeat=k;
}
}
futp->nodes=0;
#ifdef BENCH
futp->totalNodes=0;
#endif
futp->cards=1;
futp->suit[0]=latestTrickSuit[seatToPlay];
futp->rank[0]=latestTrickRank[seatToPlay];
futp->equals[0]=0;
if ((target==0)&&(solutions<3)) {
futp->score[0]=0;
} else if ((seatToPlay==maxSeat)||
(partner(seatToPlay)==maxSeat)) {
futp->score[0]=1;
} else {
futp->score[0]=0;
}
return 1;
}
if (mode!=2) {
Wipe();
winSetSizeLimit=WINIT;
nodeSetSizeLimit=NINIT;
lenSetSizeLimit=LINIT;
allocmem=(WINIT+1)*sizeof(struct winCardType);
allocmem+=(NINIT+1)*sizeof(struct nodeCardsType);
allocmem+=(LINIT+1)*sizeof(struct posSearchType);
winCards=pw[0];
nodeCards=pn[0];
posSearch=pl[0];
wcount=0; ncount=0; lcount=0;
InitGame(0, FALSE, first, seatRelFirst,iniPosition);
} else {
InitGame(0, TRUE, first, seatRelFirst,iniPosition);
}
unplayedFinder.initialize(game.diagram);
nodes=0; trickNodes=0;
iniDepth=cardCount-4;
hiwinSetSize=0;
hinodeSetSize=0;
if (mode==0) {
MoveGen(&lookAheadPos, iniDepth);
if (movePly[iniDepth].last==0) {
futp->nodes=0;
#ifdef BENCH
futp->totalNodes=0;
#endif
futp->cards=1;
futp->suit[0]=movePly[iniDepth].move[0].suit;
futp->rank[0]=movePly[iniDepth].move[0].rank;
futp->equals[0]=
movePly[iniDepth].move[0].sequence<<2;
futp->score[0]=-2;
return 1;
}
}
if ((target==0)&&(solutions<3)) {
MoveGen(&lookAheadPos, iniDepth);
futp->nodes=0;
#ifdef BENCH
futp->totalNodes=0;
#endif
for (k=0; k<=movePly[iniDepth].last; k++) {
futp->suit[k]=movePly[iniDepth].move[k].suit;
futp->rank[k]=movePly[iniDepth].move[k].rank;
futp->equals[k]=
movePly[iniDepth].move[k].sequence<<2;
futp->score[k]=0;
}
if (solutions==1)
futp->cards=1;
else
futp->cards=movePly[iniDepth].last+1;
return 1;
}
if ((target!=-1)&&(solutions!=3)) {
val=ABsearch(&lookAheadPos, tricksTarget, iniDepth);
#ifdef CANCEL
if (cancelStarted) {
cancelOrdered=FALSE;
cancelStarted=FALSE;
return 2;
}
#endif
temp=movePly[iniDepth];
last=movePly[iniDepth].last;
noMoves=last+1;
hiwinSetSize=winSetSize;
hinodeSetSize=nodeSetSize;
hilenSetSize=lenSetSize;
if (nodeSetSize>MaxnodeSetSize)
MaxnodeSetSize=nodeSetSize;
if (winSetSize>MaxwinSetSize)
MaxwinSetSize=winSetSize;
if (lenSetSize>MaxlenSetSize)
MaxlenSetSize=lenSetSize;
if (val==1)
payOff=tricksTarget;
else
payOff=0;
futp->cards=1;
ind=2;
if (payOff<=0) {
futp->suit[0]=movePly[game.noOfCards-4].move[0].suit;
futp->rank[0]=movePly[game.noOfCards-4].move[0].rank;
futp->equals[0]=(movePly[game.noOfCards-4].move[0].sequence)<<2;
if (tricksTarget>1)
futp->score[0]=-1;
else
futp->score[0]=0;
} else {
futp->suit[0]=bestMove[game.noOfCards-4].suit;
futp->rank[0]=bestMove[game.noOfCards-4].rank;
futp->equals[0]=(bestMove[game.noOfCards-4].sequence)<<2;
futp->score[0]=payOff;
}
} else {
g=estTricks[seatToPlay];
upperbound=(game.noOfCards+3)/4;
lowerbound=0;
do {
if (g==lowerbound) {
tricks=g+1;
} else {
tricks=g;
}
val=ABsearch(&lookAheadPos, tricks, iniDepth);
#ifdef CANCEL
if (cancelStarted) {
cancelOrdered=FALSE;
cancelStarted=FALSE;
return 2;
}
#endif
if (val==TRUE) {
mv=bestMove[game.noOfCards-4];
}
hiwinSetSize=Max(hiwinSetSize, winSetSize);
hinodeSetSize=Max(hinodeSetSize, nodeSetSize);
hilenSetSize=Max(hilenSetSize, lenSetSize);
if (nodeSetSize>MaxnodeSetSize)
MaxnodeSetSize=nodeSetSize;
if (winSetSize>MaxwinSetSize)
MaxwinSetSize=winSetSize;
if (lenSetSize>MaxlenSetSize)
MaxlenSetSize=lenSetSize;
if (val==FALSE) {
upperbound=tricks-1;
g=upperbound;
} else {
lowerbound=tricks;
g=lowerbound;
}
InitSearch(&iniPosition, game.noOfCards-4,
initialMoves, first, TRUE);
} while (lowerbound<upperbound);
payOff=g;
temp=movePly[iniDepth];
last=movePly[iniDepth].last;
noMoves=last+1;
ind=2;
bestMove[game.noOfCards-4]=mv;
futp->cards=1;
if (payOff<=0) {
futp->score[0]=0;
futp->suit[0]=movePly[game.noOfCards-4].move[0].suit;
futp->rank[0]=movePly[game.noOfCards-4].move[0].rank;
futp->equals[0]=(movePly[game.noOfCards-4].move[0].sequence)<<2;
} else {
futp->score[0]=payOff;
futp->suit[0]=bestMove[game.noOfCards-4].suit;
futp->rank[0]=bestMove[game.noOfCards-4].rank;
futp->equals[0]=(bestMove[game.noOfCards-4].sequence)<<2;
}
tricksTarget=payOff;
}
if ((solutions==2)&&(payOff>0)) {
forb=1;
ind=forb;
while ((payOff==tricksTarget)&&(ind<(temp.last+1))) {
forbiddenMoves[forb].suit=bestMove[game.noOfCards-4].suit;
forbiddenMoves[forb].rank=bestMove[game.noOfCards-4].rank;
forb++; ind++;
/* All moves before bestMove in the move list shall be
moved to the forbidden moves list, since none of them reached
the target */
mcurr=movePly[iniDepth].current;
for (k=0; k<=movePly[iniDepth].last; k++)
if ((bestMove[iniDepth].suit==movePly[iniDepth].move[k].suit)
&&(bestMove[iniDepth].rank==movePly[iniDepth].move[k].rank))
break;
for (i=0; i<k; i++) { /* All moves until best move */
flag=FALSE;
for (j=0; j<forb; j++) {
if ((movePly[iniDepth].move[i].suit==forbiddenMoves[j].suit)
&&(movePly[iniDepth].move[i].rank==forbiddenMoves[j].rank)) {
/* If the move is already in the forbidden list */
flag=TRUE;
break;
}
}
if (!flag) {
forbiddenMoves[forb]=movePly[iniDepth].move[i];
forb++;
}
}
if (1/*(winSetSize<winSetFill)&&(nodeSetSize<nodeSetFill)*/) {
InitSearch(&iniPosition, game.noOfCards-4,
initialMoves, first, TRUE);
} else {
InitSearch(&iniPosition, game.noOfCards-4,
initialMoves, first, FALSE);
}
val=ABsearch(&lookAheadPos, tricksTarget, iniDepth);
#ifdef CANCEL
if (cancelStarted) {
cancelOrdered=FALSE;
cancelStarted=FALSE;
return 2;
}
#endif
hiwinSetSize=winSetSize;
hinodeSetSize=nodeSetSize;
hilenSetSize=lenSetSize;
if (nodeSetSize>MaxnodeSetSize)
MaxnodeSetSize=nodeSetSize;
if (winSetSize>MaxwinSetSize)
MaxwinSetSize=winSetSize;
if (lenSetSize>MaxlenSetSize)
MaxlenSetSize=lenSetSize;
if (val==TRUE) {
payOff=tricksTarget;
futp->cards=ind;
futp->suit[ind-1]=bestMove[game.noOfCards-4].suit;
futp->rank[ind-1]=bestMove[game.noOfCards-4].rank;
futp->equals[ind-1]=(bestMove[game.noOfCards-4].sequence)<<2;
futp->score[ind-1]=payOff;
}
else
payOff=0;
}
} else if ((solutions==2)&&(payOff==0)&&((target==-1)||(tricksTarget==1))) {
futp->cards=noMoves;
/* Find the cards that were in the initial move list
but have not been listed in the current result */
n=0;
for (i=0; i<noMoves; i++) {
found=FALSE;
if ((temp.move[i].suit==futp->suit[0])&&
(temp.move[i].rank==futp->rank[0])) {
found=TRUE;
}
if (!found) {
futp->suit[1+n]=temp.move[i].suit;
futp->rank[1+n]=temp.move[i].rank;
futp->equals[1+n]=(temp.move[i].sequence)<<2;
futp->score[1+n]=0;
n++;
}
}
}
if ((solutions==3)&&(payOff>0)) {
forb=1;
ind=forb;
for (i=0; i<last; i++) {
forbiddenMoves[forb].suit=bestMove[game.noOfCards-4].suit;
forbiddenMoves[forb].rank=bestMove[game.noOfCards-4].rank;
forb++; ind++;
g=payOff;
upperbound=payOff;
lowerbound=0;
if (0) {
InitSearch(&iniPosition, game.noOfCards-4,
initialMoves, first, FALSE);
} else {
InitSearch(&iniPosition, game.noOfCards-4,
initialMoves, first, TRUE);
}
do {
if (g==lowerbound) {
tricks=g+1;
} else {
tricks=g;
}
val=ABsearch(&lookAheadPos, tricks, iniDepth);
#ifdef CANCEL
if (cancelStarted) {
cancelOrdered=FALSE;
cancelStarted=FALSE;
return 2;
}
#endif
if (val==TRUE)
mv=bestMove[game.noOfCards-4];
hiwinSetSize=Max(hiwinSetSize, winSetSize);
hinodeSetSize=Max(hinodeSetSize, nodeSetSize);
hilenSetSize=Max(hilenSetSize, lenSetSize);
if (nodeSetSize>MaxnodeSetSize)
MaxnodeSetSize=nodeSetSize;
if (winSetSize>MaxwinSetSize)
MaxwinSetSize=winSetSize;
if (lenSetSize>MaxlenSetSize)
MaxlenSetSize=lenSetSize;
if (val==FALSE) {
upperbound=tricks-1;
g=upperbound;
} else {
lowerbound=tricks;
g=lowerbound;
}
if (0) {
InitSearch(&iniPosition, game.noOfCards-4,
initialMoves, first, FALSE);
} else {
InitSearch(&iniPosition, game.noOfCards-4,
initialMoves, first, TRUE);
}
} while (lowerbound<upperbound);
payOff=g;
if (payOff==0) {
last=movePly[iniDepth].last;
futp->cards=temp.last+1;
for (j=0; j<=last; j++) {
futp->suit[ind-1+j]=movePly[game.noOfCards-4].move[j].suit;
futp->rank[ind-1+j]=movePly[game.noOfCards-4].move[j].rank;
futp->equals[ind-1+j]=(movePly[game.noOfCards-4].move[j].sequence)<<2;
futp->score[ind-1+j]=payOff;
}
break;
} else {
bestMove[game.noOfCards-4]=mv;
futp->cards=ind;
futp->suit[ind-1]=bestMove[game.noOfCards-4].suit;
futp->rank[ind-1]=bestMove[game.noOfCards-4].rank;
futp->equals[ind-1]=(bestMove[game.noOfCards-4].sequence)<<2;
futp->score[ind-1]=payOff;
}
}
} else if ((solutions==3)&&(payOff==0)) {
futp->cards=noMoves;
/* Find the cards that were in the initial move list
but have not been listed in the current result */
n=0;
for (i=0; i<noMoves; i++) {
found=FALSE;
if ((temp.move[i].suit==futp->suit[0])&&
(temp.move[i].rank==futp->rank[0])) {
found=TRUE;
}
if (!found) {
futp->suit[1+n]=temp.move[i].suit;
futp->rank[1+n]=temp.move[i].rank;
futp->equals[1+n]=(temp.move[i].sequence)<<2;
futp->score[1+n]=0;
n++;
}
}
}
for (k=0; k<=13; k++) {
forbiddenMoves[k].suit=0;
forbiddenMoves[k].rank=0;
}
futp->nodes=trickNodes;
#ifdef BENCH
futp->totalNodes=nodes;
#endif
/*if ((wcount>0)||(ncount>0)||(lcount>0)) {
fp2=fopen("dyn.txt", "a");
fprintf(fp2, "wcount=%d, ncount=%d, lcount=%d\n",
wcount, ncount, lcount);
fprintf(fp2, "winSetSize=%d, nodeSetSize=%d, lenSetSize=%d\n",
winSetSize, nodeSetSize, lenSetSize);
fprintf(fp2, "\n");
fclose(fp2);
}*/
return 1;
}
const RelativeRanksFinder &rel=Globals.rel;
struct ttStoreType * ttStore;
struct nodeCardsType * nodeCards;
struct winCardType * winCards;
struct posSearchType * posSearch;
holding_t iniRemovedRanks[4];
int nodeSetSize=0; /* Index with range 0 to nodeSetSizeLimit */
int winSetSize=0; /* Index with range 0 to winSetSizeLimit */
int lenSetSize=0; /* Index with range 0 to lenSetSizeLimit */
int lastTTstore=0;
int _initialized=0;
extern "C" void DDSInitStart(void) {
InitStart();
}
void InitStart(void) {
int k;
if (_initialized)
return;
_initialized = 1;
nodeSetSizeLimit=NINIT;
winSetSizeLimit=WINIT;
lenSetSizeLimit=LINIT;
maxmem=(6000001*sizeof(struct nodeCardsType)+
18000001*sizeof(struct winCardType)+
240001*sizeof(struct posSearchType));
//maxmem = maxmem * 3 /2 ;
//cerr << maxmem << endl;
bestMove = (struct moveType *)calloc(50, sizeof(struct moveType));
/*bestMove = new moveType [50];*/
if (bestMove==NULL) {
exit(1);
}
cardRank[2]='2'; cardRank[3]='3'; cardRank[4]='4'; cardRank[5]='5';
cardRank[6]='6'; cardRank[7]='7'; cardRank[8]='8'; cardRank[9]='9';
cardRank[10]='T'; cardRank[11]='J'; cardRank[12]='Q'; cardRank[13]='K';
cardRank[14]='A';
cardSuit[0]='S'; cardSuit[1]='H'; cardSuit[2]='D'; cardSuit[3]='C';
cardSuit[4]='N';
cardSeat[0]='N'; cardSeat[1]='E'; cardSeat[2]='S'; cardSeat[3]='W';
temp_win = (struct winCardType *)calloc(5, sizeof(struct winCardType));
if (temp_win==NULL)
exit(1);
summem=(WINIT+1)*sizeof(struct winCardType)+
(NINIT+1)*sizeof(struct nodeCardsType)+
(LINIT+1)*sizeof(struct posSearchType);
wmem=(WSIZE+1)*sizeof(struct winCardType);
nmem=(NSIZE+1)*sizeof(struct nodeCardsType);
lmem=(LSIZE+1)*sizeof(struct posSearchType);
maxIndex=(int)(maxmem-summem)/((WSIZE+1) * sizeof(struct winCardType));
pw = (struct winCardType **)calloc(maxIndex+1, sizeof(struct winCardType *));
if (pw==NULL)
exit(1);
pn = (struct nodeCardsType **)calloc(maxIndex+1, sizeof(struct nodeCardsType *));
if (pn==NULL)
exit(1);
pl = (struct posSearchType **)calloc(maxIndex+1, sizeof(struct posSearchType *));
if (pl==NULL)
exit(1);
for (k=0; k<=maxIndex; k++) {
if (pw[k]) {
free(pw[k]);
}
pw[k]=NULL;
}
for (k=0; k<=maxIndex; k++) {
if (pn[k]) {
free(pn[k]);
}
pn[k]=NULL;
}
for (k=0; k<=maxIndex; k++) {
if (pl[k]) {
free(pl[k]);
}
pl[k]=NULL;
}
pw[0] = (struct winCardType *)calloc(winSetSizeLimit+1, sizeof(struct winCardType));
if (pw[0]==NULL)
exit(1);
allocmem=(winSetSizeLimit+1)*sizeof(struct winCardType);
winCards=pw[0];
pn[0] = (struct nodeCardsType *)calloc(nodeSetSizeLimit+1, sizeof(struct nodeCardsType));
if (pn[0]==NULL)
exit(1);
allocmem+=(nodeSetSizeLimit+1)*sizeof(struct nodeCardsType);
nodeCards=pn[0];
pl[0] = (struct posSearchType *)calloc(lenSetSizeLimit+1, sizeof(struct posSearchType));
if (pl[0]==NULL)
exit(1);
allocmem+=(lenSetSizeLimit+1)*sizeof(struct posSearchType);
posSearch=pl[0];
wcount=0; ncount=0; lcount=0;
ttStore = (struct ttStoreType *)calloc(SEARCHSIZE, sizeof(struct ttStoreType));
/*ttStore = new ttStoreType[SEARCHSIZE];*/
if (ttStore==NULL) {
exit(1);
}
initializeDDSLookup();
return;
}
void InitGame(int gameNo, int moveTreeFlag, int first, int seatRelFirst, struct pos &iniPosition) {
int k, temp1, temp2,m;
/*int points[4], tricks;
int addNS, addEW, addMAX, trumpNS, trumpEW;
struct gameInfo gm;*/
#ifdef STAT
fp2=fopen("stat.txt","w");
#endif
#ifdef TTDEBUG
if (!suppressTTlog) {
fp7=fopen("storett.txt","w");
fp11=fopen("rectt.txt", "w");
fclose(fp11);
ttCollect=TRUE;
}
#endif
for (k=0; k<=3; k++) {
for (m=0; m<=3; m++) {
iniPosition.diagram.cards[k][m]=game.diagram.cards[k][m];
}
}
iniPosition.stack[game.noOfCards-4].first=first;
iniPosition.seatRelFirst=seatRelFirst;
lookAheadPos=iniPosition;
Globals.rel.initialize(game.diagram);
temp1=game.contract/100;
temp2=game.contract-100*temp1;
int trump=temp2/10-1;
Globals.setContract(trump);
estTricks[1]=6;
estTricks[3]=6;
estTricks[0]=7;
estTricks[2]=7;
/*end: tricksest */
#ifdef STAT
fprintf(fp2, "Estimated tricks for seat to play:\n");
fprintf(fp2, "seat=%d est tricks=%d\n",
seatToPlay, estTricks[seatToPlay]);
#endif
InitSearch(&lookAheadPos, game.noOfCards-4, initialMoves, first,
moveTreeFlag);
return;
}
void InitSearch(struct pos * posPoint, int depth, struct moveType startMoves[], int first, int mtd) {
int s, d, h, seatRelFirst;
int k, noOfStartMoves; /* Number of start moves in the 1st trick */
int seat[3], suit[3], rank[3];
struct moveType move;
unsigned short int startMovesBitMap[4][4]; /* Indices are seat and suit */
const ContractInfo contract = Globals.getContract();
for (h=0; h<=3; h++)
for (s=0; s<=3; s++)
startMovesBitMap[h][s]=0;
seatRelFirst=posPoint->seatRelFirst;
noOfStartMoves=seatRelFirst;
for (k=0; k<=2; k++) {
seat[k]=RelativeSeat(first,k);
suit[k]=startMoves[k].suit;
rank[k]=startMoves[k].rank;
if (k<noOfStartMoves)
startMovesBitMap[seat[k]][suit[k]] |= BitRank(rank[k]);
}
for (d=0; d<=49; d++) {
/*bestMove[d].suit=0;*/
bestMove[d].rank=0;
/*bestMove[d].weight=0;
bestMove[d].sequence=0; 0315 */
}
if ((RelativeSeat(first,seatRelFirst)==0)||
(RelativeSeat(first,seatRelFirst)==2)) {
nodeTypeStore[0]=MAXNODE;
nodeTypeStore[1]=MINNODE;
nodeTypeStore[2]=MAXNODE;
nodeTypeStore[3]=MINNODE;
} else {