~ubuntu-branches/ubuntu/utopic/dds/utopic-proposed

« back to all changes in this revision

Viewing changes to dll.h

  • Committer: Package Import Robot
  • Author(s): Christoph Berg
  • Date: 2014-05-26 22:23:44 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20140526222344-y7xuceqamrdb00qu
Tags: 2.5.2+ddd105-1
New dds upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* portability-macros header prefix */
2
 
 
3
 
/* Windows requires a __declspec(dllexport) tag, etc */
4
 
#if defined(_WIN32)
5
 
#    define DLLEXPORT __declspec(dllexport)
6
 
#    define STDCALL __stdcall
7
 
#else
8
 
#    define DLLEXPORT
9
 
#    define STDCALL
10
 
#    define INT8 char
11
 
#endif
12
 
 
13
 
#ifdef __cplusplus
14
 
#    define EXTERN_C extern "C"
15
 
#else
16
 
#    define EXTERN_C
17
 
#endif
18
 
 
19
 
#if defined(_WIN32) && defined(__MINGW32__) 
20
 
#define WINVER 0x500    /* Dirty trick, but it works. */
21
 
#    include <windows.h>
22
 
#    include <process.h>
23
 
#endif
24
 
 
25
 
#if defined(_WIN32) && !defined(__MINGW32__)
26
 
#    include <windows.h>
27
 
#    include <process.h>
28
 
#endif
29
 
 
30
 
#if defined(_MSC_VER)
31
 
#    include <intrin.h>
32
 
#else
33
 
#    include <omp.h>
34
 
#endif
35
 
 
36
 
#ifdef __linux__   
37
 
#    include <unistd.h>
38
 
#endif
39
 
 
40
 
/* end of portability-macros section */
41
 
 
42
 
#define DDS_VERSION             20402   /* Version 2.4.2. Allowing for 2 digit
43
 
                                        minor versions */
44
 
 
45
 
#define PBN
46
 
 
47
 
#define PBN_PLUS
48
 
 
49
 
/*#define BENCH*/
50
 
 
51
 
#include <stdio.h>
52
 
/*#define _CRTDBG_MAP_ALLOC */ /* MEMORY LEAK? */
53
 
#include <stdlib.h>
54
 
/*#include <crtdbg.h> */  /* MEMORY LEAK? */
55
 
#include <string.h>
56
 
#include <time.h>
57
 
#include <assert.h>
58
 
#include <math.h>
59
 
 
60
 
#ifdef __linux__
61
 
typedef long long __int64;
62
 
#endif
63
 
 
64
 
/*#define STAT*/        /* Define STAT to generate a statistics log, stat.txt */
65
 
/*#define TTDEBUG*/     /* Define TTDEBUG to generate transposition table debug information.
66
 
                                                Only for a single thread! */
67
 
 
68
 
#ifdef  TTDEBUG
69
 
#define SEARCHSIZE  20000
70
 
#else
71
 
#define SEARCHSIZE  1
72
 
#endif
73
 
 
74
 
#if defined(INFINITY)
75
 
#    undef INFINITY
76
 
#endif
77
 
#define INFINITY    32000
78
 
 
79
 
#define MAXNOOFTHREADS  16
80
 
 
81
 
#define MAXNODE     1
82
 
#define MINNODE     0
83
 
 
84
 
#define TRUE        1
85
 
#define FALSE       0
86
 
 
87
 
#define MOVESVALID  1
88
 
#define MOVESLOCKED 2
89
 
 
90
 
#define NSIZE   100000
91
 
#define WSIZE   100000
92
 
#define LSIZE   20000
93
 
#define NINIT   250000
94
 
#define WINIT   700000
95
 
#define LINIT   50000
96
 
 
97
 
#define SIMILARDEALLIMIT        5
98
 
#define SIMILARMAXWINNODES  700000
99
 
 
100
 
#define MAXNOOFBOARDS           200/*100*/
101
 
 
102
 
#define Max(x, y) (((x) >= (y)) ? (x) : (y))
103
 
#define Min(x, y) (((x) <= (y)) ? (x) : (y))
104
 
 
105
 
/* "hand" is leading hand, "relative" is hand relative leading
106
 
hand.
107
 
The handId macro implementation follows a solution 
108
 
by Thomas Andrews. 
109
 
All hand identities are given as
110
 
0=NORTH, 1=EAST, 2=SOUTH, 3=WEST. */
111
 
 
112
 
#define handId(hand, relative) (hand + relative) & 3
113
 
#define CMP_SWAP(i, j) if (a[i].weight < a[j].weight)  \
114
 
  { struct moveType tmp = a[i]; a[i] = a[j]; a[j] = tmp; } 
115
 
 
116
 
 
117
 
struct gameInfo  {          /* All info of a particular deal */
118
 
  int declarer;
119
 
  int leadHand;
120
 
  int leadSuit;
121
 
  int leadRank;
122
 
  int first;
123
 
  int noOfCards;
124
 
  unsigned short int suit[4][4];
125
 
    /* 1st index is hand id, 2nd index is suit id */
126
 
};
127
 
 
128
 
 
129
 
struct moveType {
130
 
  unsigned char suit;
131
 
  unsigned char rank;
132
 
  unsigned short int sequence;          /* Whether or not this move is
133
 
                                        the first in a sequence */
134
 
  short int weight;                     /* Weight used at sorting */
135
 
};
136
 
 
137
 
struct movePlyType {
138
 
  struct moveType move[14];             
139
 
  int current;
140
 
  int last;
141
 
};
142
 
 
143
 
struct highCardType {
144
 
  int rank;
145
 
  int hand;
146
 
};
147
 
 
148
 
struct futureTricks {
149
 
  int nodes;
150
 
  #ifdef BENCH
151
 
  int totalNodes;
152
 
  #endif
153
 
  int cards;
154
 
  int suit[13];
155
 
  int rank[13];
156
 
  int equals[13];
157
 
  int score[13];
158
 
};
159
 
 
160
 
struct deal {
161
 
  int trump;
162
 
  int first;
163
 
  int currentTrickSuit[3];
164
 
  int currentTrickRank[3];
165
 
  unsigned int remainCards[4][4];
166
 
};
167
 
 
168
 
 
169
 
struct dealPBN {
170
 
  int trump;
171
 
  int first;
172
 
  int currentTrickSuit[3];
173
 
  int currentTrickRank[3];
174
 
  char remainCards[80];
175
 
};
176
 
 
177
 
 
178
 
struct pos {
179
 
  unsigned short int rankInSuit[4][4];   /* 1st index is hand, 2nd index is
180
 
                                        suit id */
181
 
  int orderSet[4];
182
 
  int winOrderSet[4];
183
 
  int winMask[4];
184
 
  int leastWin[4];
185
 
  unsigned short int removedRanks[4];    /* Ranks removed from board,
186
 
                                        index is suit */
187
 
  unsigned short int winRanks[50][4];  /* Cards that win by rank,
188
 
                                       indices are depth and suit */
189
 
  unsigned char length[4][4];
190
 
  char ubound;
191
 
  char lbound;
192
 
  char bestMoveSuit;
193
 
  char bestMoveRank;
194
 
  int first[50];                 /* Hand that leads the trick for each ply*/
195
 
  int high[50];                  /* Hand that is presently winning the trick */
196
 
  struct moveType move[50];      /* Presently winning move */              
197
 
  int handRelFirst;              /* The current hand, relative first hand */
198
 
  int tricksMAX;                 /* Aggregated tricks won by MAX */
199
 
  struct highCardType winner[4]; /* Winning rank of the trick,
200
 
                                    index is suit id. */
201
 
  struct highCardType secondBest[4]; /* Second best rank, index is suit id. */
202
 
};
203
 
 
204
 
struct posSearchType {
205
 
  struct winCardType * posSearchPoint; 
206
 
  long long suitLengths;
207
 
  struct posSearchType * left;
208
 
  struct posSearchType * right;
209
 
};
210
 
 
211
 
 
212
 
struct nodeCardsType {
213
 
  char ubound;  /* ubound and
214
 
                        lbound for the N-S side */
215
 
  char lbound;
216
 
  char bestMoveSuit;
217
 
  char bestMoveRank;
218
 
  char leastWin[4];
219
 
};
220
 
 
221
 
struct winCardType {
222
 
  int orderSet;
223
 
  int winMask;
224
 
  struct nodeCardsType * first;
225
 
  struct winCardType * prevWin;
226
 
  struct winCardType * nextWin;
227
 
  struct winCardType * next;
228
 
}; 
229
 
 
230
 
 
231
 
struct evalType {
232
 
  int tricks;
233
 
  unsigned short int winRanks[4];
234
 
};
235
 
 
236
 
struct absRankType {
237
 
  char rank;
238
 
  char hand;
239
 
};
240
 
 
241
 
struct relRanksType {
242
 
  int aggrRanks[4];
243
 
  int winMask[4];
244
 
  char relRank[15][4];
245
 
  struct absRankType absRank[15][4];
246
 
};
247
 
 
248
 
struct adaptWinRanksType {
249
 
  unsigned short int winRanks[14];
250
 
};
251
 
 
252
 
 
253
 
struct ttStoreType {
254
 
  struct nodeCardsType * cardsP;
255
 
  char tricksLeft;
256
 
  char target;
257
 
  char ubound;
258
 
  char lbound;
259
 
  unsigned char first;
260
 
  unsigned short int suit[4][4];
261
 
};
262
 
 
263
 
struct card {
264
 
  int suit;
265
 
  int rank;
266
 
};
267
 
 
268
 
struct boards {
269
 
  int noOfBoards;
270
 
  struct deal deals[MAXNOOFBOARDS];
271
 
  int target[MAXNOOFBOARDS];
272
 
  int solutions[MAXNOOFBOARDS];
273
 
  int mode[MAXNOOFBOARDS];
274
 
};
275
 
 
276
 
struct boardsPBN {
277
 
  int noOfBoards;
278
 
  struct dealPBN deals[MAXNOOFBOARDS];
279
 
  int target[MAXNOOFBOARDS];
280
 
  int solutions[MAXNOOFBOARDS];
281
 
  int mode[MAXNOOFBOARDS];
282
 
};
283
 
 
284
 
struct solvedBoards {
285
 
  int noOfBoards;
286
 
  struct futureTricks solvedBoard[MAXNOOFBOARDS];
287
 
};
288
 
 
289
 
struct paramType {
290
 
  int noOfBoards;
291
 
  struct boards *bop;
292
 
  struct solvedBoards *solvedp;
293
 
  int error;
294
 
};
295
 
 
296
 
struct ddTableDeal {
297
 
  unsigned int cards[4][4];
298
 
};
299
 
 
300
 
struct ddTableDeals {
301
 
  int noOfTables;
302
 
  struct ddTableDeal deals[MAXNOOFBOARDS>>2];
303
 
};
304
 
 
305
 
struct ddTableDealPBN {
306
 
  char cards[80];
307
 
};
308
 
 
309
 
struct ddTableDealsPBN {
310
 
  int noOfTables;
311
 
  struct ddTableDealPBN deals[MAXNOOFBOARDS>>2];
312
 
};
313
 
 
314
 
struct ddTableResults {
315
 
  int resTable[5][4];
316
 
};
317
 
 
318
 
struct ddTablesRes {
319
 
  int noOfBoards;
320
 
  struct ddTableResults results[MAXNOOFBOARDS>>2];
321
 
};
322
 
 
323
 
 
324
 
struct parResults {
325
 
  char parScore[2][16]; /* index = 0 is NS view and index = 1 is EW view. */
326
 
  char parContractsString[2][128]; /* index = 0 is NS view and index = 1 
327
 
                                      is EW view. By �view� is here meant 
328
 
                                      which side that starts the bidding. */
329
 
};
330
 
 
331
 
 
332
 
struct allParResults {
333
 
  struct parResults presults[MAXNOOFBOARDS / 20];
334
 
};
335
 
 
336
 
struct par_suits_type {
337
 
  int suit;
338
 
  int tricks;
339
 
  int score;
340
 
}; 
341
 
 
342
 
 
343
 
struct localVarType {
344
 
  int nodeTypeStore[4];
345
 
  int trump;
346
 
  unsigned short int lowestWin[50][4];
347
 
  int nodes;
348
 
  int trickNodes;
349
 
  int no[50];
350
 
  int iniDepth;
351
 
  int handToPlay;
352
 
  int payOff;
353
 
  int val;
354
 
  struct pos iniPosition;
355
 
  struct pos lookAheadPos; /* Is initialized for starting
356
 
                              alpha-beta search */
357
 
  struct moveType forbiddenMoves[14];
358
 
  struct moveType initialMoves[4];
359
 
  struct moveType cd;
360
 
  struct movePlyType movePly[50];
361
 
  int tricksTarget;
362
 
  struct gameInfo game;
363
 
  int newDeal;
364
 
  int newTrump;
365
 
  int similarDeal;
366
 
  unsigned short int diffDeal;
367
 
  unsigned short int aggDeal;
368
 
  int estTricks[4];
369
 
  FILE *fp2;
370
 
  FILE *fp7;
371
 
  FILE *fp11;
372
 
  /*FILE *fdp */
373
 
  struct moveType bestMove[50];
374
 
  struct moveType bestMoveTT[50];
375
 
  struct winCardType temp_win[5];
376
 
  int nodeSetSizeLimit;
377
 
  int winSetSizeLimit;
378
 
  int lenSetSizeLimit;
379
 
  unsigned long long maxmem;            /* bytes */
380
 
  unsigned long long allocmem;
381
 
  unsigned long long summem;
382
 
  int wmem;
383
 
  int nmem; 
384
 
  int lmem;
385
 
  int maxIndex;
386
 
  int wcount;
387
 
  int ncount;
388
 
  int lcount;
389
 
  int clearTTflag;
390
 
  int windex;
391
 
  /*int ttCollect;
392
 
  int suppressTTlog;*/
393
 
  struct relRanksType * rel;
394
 
  struct adaptWinRanksType * adaptWins;
395
 
  struct posSearchType *rootnp[14][4];
396
 
  struct winCardType **pw;
397
 
  struct nodeCardsType **pn;
398
 
  struct posSearchType **pl;
399
 
  struct nodeCardsType * nodeCards;
400
 
  struct winCardType * winCards;
401
 
  struct posSearchType * posSearch;
402
 
  /*struct ttStoreType * ttStore;
403
 
  int lastTTstore;*/
404
 
  unsigned short int iniRemovedRanks[4];
405
 
 
406
 
  int nodeSetSize; /* Index with range 0 to nodeSetSizeLimit */
407
 
  int winSetSize;  /* Index with range 0 to winSetSizeLimit */
408
 
  int lenSetSize;  /* Index with range 0 to lenSetSizeLimit */
409
 
};
410
 
 
411
 
extern int noOfThreads;
412
 
extern int noOfCores;
413
 
extern struct localVarType localVar[MAXNOOFTHREADS];
414
 
extern int * highestRank;
415
 
extern int * counttable;
416
 
extern struct adaptWinRanksType * adaptWins;
417
 
extern unsigned short int bitMapRank[16];
418
 
extern unsigned short int relRankInSuit[4][4];
419
 
extern int sum;
420
 
extern int score1Counts[50], score0Counts[50];
421
 
extern int c1[50], c2[50], c3[50], c4[50], c5[50], c6[50], c7[50],
422
 
  c8[50], c9[50];
423
 
extern int nodeTypeStore[4];            /* Look-up table for determining if
424
 
                                        node is MAXNODE or MINNODE */
425
 
extern int lho[4], rho[4], partner[4];                                        
426
 
extern int nodes;                       /* Number of nodes searched */
427
 
extern int no[50];                      /* Number of nodes searched on each
428
 
                                        depth level */
429
 
extern int payOff;
430
 
extern int iniDepth;
431
 
extern int treeDepth;
432
 
extern int tricksTarget;                /* No of tricks for MAX in order to
433
 
                                        meet the game goal, e.g. to make the
434
 
                                        contract */
435
 
extern int tricksTargetOpp;             /* Target no of tricks for MAX
436
 
                                        opponent */
437
 
extern int targetNS;
438
 
extern int targetEW;
439
 
extern int handToPlay;
440
 
extern int searchTraceFlag;
441
 
extern int countMax;
442
 
extern int depthCount;
443
 
extern int highHand;
444
 
extern int nodeSetSizeLimit;
445
 
extern int winSetSizeLimit;
446
 
extern int lenSetSizeLimit;
447
 
extern int estTricks[4];
448
 
extern int recInd; 
449
 
extern int suppressTTlog;
450
 
extern unsigned char suitChar[4];
451
 
extern unsigned char rankChar[15];
452
 
extern unsigned char handChar[4];
453
 
extern unsigned char cardRank[15], cardSuit[5], cardHand[4];
454
 
extern int totalNodes;
455
 
extern struct futureTricks fut, ft;
456
 
extern struct futureTricks *futp;
457
 
extern char stri[128];
458
 
 
459
 
extern FILE *fp, /**fp2, *fp7, *fp11,*/ *fpx;
460
 
  /* Pointers to logs */
461
 
 
462
 
extern struct ttStoreType * ttStore;
463
 
extern int lastTTstore;
464
 
extern int ttCollect;
465
 
extern int suppressTTlog;
466
 
 
467
 
EXTERN_C DLLEXPORT int STDCALL SolveBoard(struct deal dl, 
468
 
  int target, int solutions, int mode, struct futureTricks *futp, int threadIndex);
469
 
EXTERN_C DLLEXPORT int STDCALL CalcDDtable(struct ddTableDeal tableDeal, 
470
 
  struct ddTableResults * tablep);
471
 
 
472
 
#ifdef PBN
473
 
EXTERN_C DLLEXPORT int STDCALL SolveBoardPBN(struct dealPBN dlpbn, int target, 
474
 
    int solutions, int mode, struct futureTricks *futp, int thrId);
475
 
EXTERN_C DLLEXPORT int STDCALL CalcDDtablePBN(struct ddTableDealPBN tableDealPBN, 
476
 
  struct ddTableResults * tablep);
477
 
#endif
478
 
 
479
 
#ifdef PBN_PLUS
480
 
EXTERN_C DLLEXPORT int STDCALL SolveAllBoards(struct boardsPBN *bop, struct solvedBoards *solvedp);
481
 
EXTERN_C DLLEXPORT int STDCALL CalcAllTables(struct ddTableDeals *dealsp, int mode, 
482
 
        int trumpFilter[5], struct ddTablesRes *resp, struct allParResults *presp);
483
 
EXTERN_C DLLEXPORT int STDCALL CalcAllTablesPBN(struct ddTableDealsPBN *dealsp, int mode, 
484
 
        int trumpFilter[5], struct ddTablesRes *resp, struct allParResults *presp);
485
 
EXTERN_C DLLEXPORT int STDCALL CalcPar(struct ddTableDeal tableDeal, int vulnerable, 
486
 
    struct ddTableResults * tablep, struct parResults *presp);
487
 
EXTERN_C DLLEXPORT int STDCALL CalcParPBN(struct ddTableDealPBN tableDealPBN, 
488
 
  struct ddTableResults * tablep, int vulnerable, struct parResults *presp);
489
 
#endif
490
 
 
491
 
#ifdef __linux
492
 
EXTERN_C void InitStart(int gb_ram, int ncores);   /* For usage with ctypes in Linux. */ 
493
 
#endif
494
 
 
495
 
EXTERN_C void InitStart(int gb_ram, int ncores);
496
 
void InitGame(int gameNo, int moveTreeFlag, int first, int handRelFirst, int thrId);
497
 
void InitSearch(struct pos * posPoint, int depth,
498
 
  struct moveType startMoves[], int first, int mtd, int thrId);
499
 
int ABsearch(struct pos * posPoint, int target, int depth, int thrId);
500
 
void Make(struct pos * posPoint, unsigned short int trickCards[4], 
501
 
  int depth, int trump, struct movePlyType *mply, int thrId);
502
 
int MoveGen(struct pos * posPoint, int depth, int trump, struct movePlyType *mply, int thrId);
503
 
void MergeSort(int n, struct moveType *a);
504
 
inline int WinningMove(struct moveType * mvp1, struct moveType * mvp2, int trump, int thrId);
505
 
inline int WinningMoveNT(struct moveType * mvp1, struct moveType * mvp2, int thrId);
506
 
int AdjustMoveList(int thrId);
507
 
int QuickTricks(struct pos * posPoint, int hand, 
508
 
        int depth, int target, int trump, int *result, int thrId);
509
 
int LaterTricksMIN(struct pos *posPoint, int hand, int depth, int target, int trump, int thrId); 
510
 
int LaterTricksMAX(struct pos *posPoint, int hand, int depth, int target, int trump, int thrId);
511
 
struct nodeCardsType * CheckSOP(struct pos * posPoint, struct nodeCardsType
512
 
  * nodep, int target, int tricks, int * result, int *value, int thrId);
513
 
struct nodeCardsType * UpdateSOP(struct pos * posPoint, struct nodeCardsType
514
 
  * nodep);  
515
 
struct nodeCardsType * FindSOP(struct pos * posPoint,
516
 
  struct winCardType * nodeP, int firstHand, 
517
 
        int target, int tricks, int * valp, int thrId);  
518
 
struct nodeCardsType * BuildPath(struct pos * posPoint, 
519
 
  struct posSearchType *nodep, int * result, int thrId);
520
 
void BuildSOP(struct pos * posPoint, int tricks, int firstHand, int target,
521
 
  int depth, int scoreFlag, int score, int thrId);
522
 
struct posSearchType * SearchLenAndInsert(struct posSearchType
523
 
        * rootp, long long key, int insertNode, int *result, int thrId);  
524
 
void Undo(struct pos * posPoint, int depth, struct movePlyType *mply, int thrId);
525
 
int CheckDeal(struct moveType * cardp, int thrId);
526
 
int InvBitMapRank(unsigned short bitMap);
527
 
int InvWinMask(int mask);
528
 
void ReceiveTTstore(struct pos *posPoint, struct nodeCardsType * cardsP, int target, 
529
 
  int depth, int thrId);
530
 
int NextMove(struct pos *posPoint, int depth, struct movePlyType *mply, int thrId); 
531
 
int DumpInput(int errCode, struct deal dl, int target, int solutions, int mode); 
532
 
void Wipe(int thrId);
533
 
void AddNodeSet(int thrId);
534
 
void AddLenSet(int thrId);
535
 
void AddWinSet(int thrId);
536
 
 
537
 
void PrintDeal(FILE *fp, unsigned short ranks[4][4]);
538
 
 
539
 
int SolveAllBoards4(struct boards *bop, struct solvedBoards *solvedp);
540
 
int SolveAllBoards1(struct boards *bop, struct solvedBoards *solvedp);
541
 
 
542
 
int IsCard(char cardChar);
543
 
void UpdateDealInfo(int card);
544
 
int IsVal(char cardChar);
545
 
 
546
 
 
 
1
/* portability-macros header prefix */
 
2
 
 
3
/* Windows requires a __declspec(dllexport) tag, etc */
 
4
#if defined(_WIN32)
 
5
#    define DLLEXPORT __declspec(dllexport)
 
6
#    define STDCALL __stdcall
 
7
#else
 
8
#    define DLLEXPORT
 
9
#    define STDCALL
 
10
#    define INT8 char
 
11
#endif
 
12
 
 
13
#ifdef __cplusplus
 
14
#    define EXTERN_C extern "C"
 
15
#else
 
16
#    define EXTERN_C
 
17
#endif
 
18
 
 
19
#if defined(_WIN32) && defined(__MINGW32__) 
 
20
#define WINVER 0x500    
 
21
#    include <windows.h>
 
22
#    include <process.h>
 
23
#endif
 
24
 
 
25
#if defined(_WIN32) && !defined(__MINGW32__)
 
26
#    include <windows.h>
 
27
#    include <process.h>
 
28
#endif
 
29
 
 
30
#ifdef __linux__   
 
31
#    include <unistd.h>
 
32
#endif
 
33
 
 
34
 
 
35
#if defined(_MSC_VER)
 
36
#    include <intrin.h>
 
37
#else
 
38
#    include <omp.h>
 
39
#endif
 
40
 
 
41
/* end of portability-macros section */
 
42
 
 
43
#define DDS_VERSION     20502   /* Version 2.5.2. Allowing for 2 digit minor versions */
 
44
 
 
45
#define PBN
 
46
 
 
47
#define PBN_PLUS
 
48
 
 
49
/*#define BENCH*/
 
50
 
 
51
#include <stdio.h>
 
52
/*#define _CRTDBG_MAP_ALLOC */ /* MEMORY LEAK? */
 
53
#include <stdlib.h>
 
54
/*#include <crtdbg.h> */  /* MEMORY LEAK? */
 
55
#include <string.h>
 
56
#include <time.h>
 
57
#include <assert.h>
 
58
#include <math.h>
 
59
 
 
60
#ifdef __linux__
 
61
typedef long long __int64;
 
62
#endif
 
63
 
 
64
/*#define STAT*/        /* Define STAT to generate a statistics log, stat.txt */
 
65
/*#define TTDEBUG*/     /* Define TTDEBUG to generate transposition table debug information.
 
66
                                                Only for a single thread! */
 
67
 
 
68
#ifdef  TTDEBUG
 
69
#define SEARCHSIZE  20000
 
70
#else
 
71
#define SEARCHSIZE  1
 
72
#endif
 
73
 
 
74
#if defined(INFINITY)
 
75
#    undef INFINITY
 
76
#endif
 
77
#define INFINITY    32000
 
78
 
 
79
#define MAXNOOFTHREADS  16
 
80
 
 
81
#define MAXNODE     1
 
82
#define MINNODE     0
 
83
 
 
84
#define TRUE        1
 
85
#define FALSE       0
 
86
 
 
87
#define MOVESVALID  1
 
88
#define MOVESLOCKED 2
 
89
 
 
90
#define NSIZE   100000
 
91
#define WSIZE   100000
 
92
#define LSIZE   20000
 
93
#define NINIT   250000
 
94
#define WINIT   700000
 
95
#define LINIT   50000
 
96
 
 
97
#define SIMILARDEALLIMIT        5
 
98
#define SIMILARMAXWINNODES      700000
 
99
 
 
100
#define MAXNOOFBOARDS           200/*100*/
 
101
 
 
102
#define Max(x, y) (((x) >= (y)) ? (x) : (y))
 
103
#define Min(x, y) (((x) <= (y)) ? (x) : (y))
 
104
 
 
105
/* "hand" is leading hand, "relative" is hand relative leading
 
106
hand.
 
107
The handId macro implementation follows a solution 
 
108
by Thomas Andrews. 
 
109
All hand identities are given as
 
110
0=NORTH, 1=EAST, 2=SOUTH, 3=WEST. */
 
111
 
 
112
#define handId(hand, relative) (hand + relative) & 3
 
113
#define CMP_SWAP(i, j) if (a[i].weight < a[j].weight)  \
 
114
  { struct moveType tmp = a[i]; a[i] = a[j]; a[j] = tmp; } 
 
115
 
 
116
 
 
117
struct gameInfo  {          /* All info of a particular deal */
 
118
  int declarer;
 
119
  int leadHand;
 
120
  int leadSuit;
 
121
  int leadRank;
 
122
  int first;
 
123
  int noOfCards;
 
124
  unsigned short int suit[4][4];
 
125
    /* 1st index is hand id, 2nd index is suit id */
 
126
};
 
127
 
 
128
 
 
129
struct moveType {
 
130
  int suit;
 
131
  int rank;
 
132
  unsigned short int sequence;          /* Whether or not this move is
 
133
                                        the first in a sequence */
 
134
  short int weight;                     /* Weight used at sorting */
 
135
};
 
136
 
 
137
struct movePlyType {
 
138
  struct moveType move[14];             
 
139
  int current;
 
140
  int last;
 
141
};
 
142
 
 
143
struct highCardType {
 
144
  int rank;
 
145
  int hand;
 
146
};
 
147
 
 
148
struct futureTricks {
 
149
  int nodes;
 
150
  #ifdef BENCH
 
151
  int totalNodes;
 
152
  #endif
 
153
  int cards;
 
154
  int suit[13];
 
155
  int rank[13];
 
156
  int equals[13];
 
157
  int score[13];
 
158
};
 
159
 
 
160
struct deal {
 
161
  int trump;
 
162
  int first;
 
163
  int currentTrickSuit[3];
 
164
  int currentTrickRank[3];
 
165
  unsigned int remainCards[4][4];
 
166
};
 
167
 
 
168
 
 
169
struct dealPBN {
 
170
  int trump;
 
171
  int first;
 
172
  int currentTrickSuit[3];
 
173
  int currentTrickRank[3];
 
174
  char remainCards[80];
 
175
};
 
176
 
 
177
 
 
178
struct pos {
 
179
  unsigned short int rankInSuit[4][4];   /* 1st index is hand, 2nd index is
 
180
                                        suit id */
 
181
  int orderSet[4];
 
182
  int winOrderSet[4];
 
183
  int winMask[4];
 
184
  int leastWin[4];
 
185
  unsigned short int removedRanks[4];    /* Ranks removed from board,
 
186
                                        index is suit */
 
187
  unsigned short int winRanks[50][4];  /* Cards that win by rank,
 
188
                                       indices are depth and suit */
 
189
  unsigned char length[4][4];
 
190
  int ubound;
 
191
  int lbound;
 
192
  int bestMoveSuit;
 
193
  int bestMoveRank;
 
194
  int first[50];                 /* Hand that leads the trick for each ply*/
 
195
  int high[50];                  /* Hand that is presently winning the trick */
 
196
  struct moveType move[50];      /* Presently winning move */              
 
197
  int handRelFirst;              /* The current hand, relative first hand */
 
198
  int tricksMAX;                 /* Aggregated tricks won by MAX */
 
199
  struct highCardType winner[4]; /* Winning rank of the trick,
 
200
                                    index is suit id. */
 
201
  struct highCardType secondBest[4]; /* Second best rank, index is suit id. */
 
202
};
 
203
 
 
204
struct posSearchType {
 
205
  struct winCardType * posSearchPoint; 
 
206
  long long suitLengths;
 
207
  struct posSearchType * left;
 
208
  struct posSearchType * right;
 
209
};
 
210
 
 
211
 
 
212
struct nodeCardsType {
 
213
  char ubound;  /* ubound and
 
214
                        lbound for the N-S side */
 
215
  char lbound;
 
216
  char bestMoveSuit;
 
217
  char bestMoveRank;
 
218
  char leastWin[4];
 
219
};
 
220
 
 
221
struct winCardType {
 
222
  int orderSet;
 
223
  int winMask;
 
224
  struct nodeCardsType * first;
 
225
  struct winCardType * prevWin;
 
226
  struct winCardType * nextWin;
 
227
  struct winCardType * next;
 
228
}; 
 
229
 
 
230
 
 
231
struct evalType {
 
232
  int tricks;
 
233
  unsigned short int winRanks[4];
 
234
};
 
235
 
 
236
struct absRankType {
 
237
  char rank;
 
238
  char hand;
 
239
};
 
240
 
 
241
struct relRanksType {
 
242
  int aggrRanks[4];
 
243
  int winMask[4];
 
244
  char relRank[15][4];
 
245
  struct absRankType absRank[15][4];
 
246
};
 
247
 
 
248
struct adaptWinRanksType {
 
249
  unsigned short int winRanks[14];
 
250
};
 
251
 
 
252
 
 
253
struct ttStoreType {
 
254
  struct nodeCardsType * cardsP;
 
255
  char tricksLeft;
 
256
  char target;
 
257
  char ubound;
 
258
  char lbound;
 
259
  unsigned char first;
 
260
  unsigned short int suit[4][4];
 
261
};
 
262
 
 
263
struct card {
 
264
  int suit;
 
265
  int rank;
 
266
};
 
267
 
 
268
struct boards {
 
269
  int noOfBoards;
 
270
  struct deal deals[MAXNOOFBOARDS];
 
271
  int target[MAXNOOFBOARDS];
 
272
  int solutions[MAXNOOFBOARDS];
 
273
  int mode[MAXNOOFBOARDS];
 
274
};
 
275
 
 
276
struct boardsPBN {
 
277
  int noOfBoards;
 
278
  struct dealPBN deals[MAXNOOFBOARDS];
 
279
  int target[MAXNOOFBOARDS];
 
280
  int solutions[MAXNOOFBOARDS];
 
281
  int mode[MAXNOOFBOARDS];
 
282
};
 
283
 
 
284
struct solvedBoards {
 
285
  int noOfBoards;
 
286
  struct futureTricks solvedBoard[MAXNOOFBOARDS];
 
287
};
 
288
 
 
289
struct paramType {
 
290
  int noOfBoards;
 
291
  struct boards *bop;
 
292
  struct solvedBoards *solvedp;
 
293
  int error;
 
294
};
 
295
 
 
296
struct ddTableDeal {
 
297
  unsigned int cards[4][4];
 
298
};
 
299
 
 
300
struct ddTableDeals {
 
301
  int noOfTables;
 
302
  struct ddTableDeal deals[MAXNOOFBOARDS>>2];
 
303
};
 
304
 
 
305
struct ddTableDealPBN {
 
306
  char cards[80];
 
307
};
 
308
 
 
309
struct ddTableDealsPBN {
 
310
  int noOfTables;
 
311
  struct ddTableDealPBN deals[MAXNOOFBOARDS>>2];
 
312
};
 
313
 
 
314
struct ddTableResults {
 
315
  int resTable[5][4];
 
316
};
 
317
 
 
318
struct ddTablesRes {
 
319
  int noOfBoards;
 
320
  struct ddTableResults results[MAXNOOFBOARDS>>2];
 
321
};
 
322
 
 
323
 
 
324
struct parResults {
 
325
  char parScore[2][16]; /* index = 0 is NS view and index = 1 is EW view. */
 
326
  char parContractsString[2][128]; /* index = 0 is NS view and index = 1 
 
327
                                      is EW view. By �view� is here meant 
 
328
                                      which side that starts the bidding. */
 
329
};
 
330
 
 
331
 
 
332
struct allParResults {
 
333
  struct parResults presults[MAXNOOFBOARDS / 20];
 
334
};
 
335
 
 
336
struct par_suits_type {
 
337
  int suit;
 
338
  int tricks;
 
339
  int score;
 
340
}; 
 
341
 
 
342
 
 
343
struct localVarType {
 
344
  int nodeTypeStore[4];
 
345
  int trump;
 
346
  unsigned short int lowestWin[50][4];
 
347
  #ifdef STAT
 
348
  int nodes;
 
349
  #endif
 
350
  int trickNodes;
 
351
  int no[50];
 
352
  int iniDepth;
 
353
  int handToPlay;
 
354
  int payOff;
 
355
  int val;
 
356
  struct pos iniPosition;
 
357
  struct pos lookAheadPos; /* Is initialized for starting
 
358
                              alpha-beta search */
 
359
  struct moveType forbiddenMoves[14];
 
360
  struct moveType initialMoves[4];
 
361
  struct moveType cd;
 
362
  struct movePlyType movePly[50];
 
363
  int tricksTarget;
 
364
  struct gameInfo game;
 
365
  int newDeal;
 
366
  int newTrump;
 
367
  int similarDeal;
 
368
  unsigned short int diffDeal;
 
369
  unsigned short int aggDeal;
 
370
  int estTricks[4];
 
371
  FILE *fp2;
 
372
  FILE *fp7;
 
373
  FILE *fp11;
 
374
  /*FILE *fdp */
 
375
  struct moveType bestMove[50];
 
376
  struct moveType bestMoveTT[50];
 
377
  struct winCardType temp_win[5];
 
378
  int nodeSetSizeLimit;
 
379
  int winSetSizeLimit;
 
380
  int lenSetSizeLimit;
 
381
  unsigned long long maxmem;            /* bytes */
 
382
  unsigned long long allocmem;
 
383
  unsigned long long summem;
 
384
  int wmem;
 
385
  int nmem; 
 
386
  int lmem;
 
387
  int maxIndex;
 
388
  int wcount;
 
389
  int ncount;
 
390
  int lcount;
 
391
  int clearTTflag;
 
392
  int windex;
 
393
  /*int ttCollect;
 
394
  int suppressTTlog;*/
 
395
  struct relRanksType * rel;
 
396
  struct adaptWinRanksType * adaptWins;
 
397
  struct posSearchType *rootnp[14][4];
 
398
  struct winCardType **pw;
 
399
  struct nodeCardsType **pn;
 
400
  struct posSearchType **pl;
 
401
  struct nodeCardsType * nodeCards;
 
402
  struct winCardType * winCards;
 
403
  struct posSearchType * posSearch;
 
404
  /*struct ttStoreType * ttStore;
 
405
  int lastTTstore;*/
 
406
  unsigned short int iniRemovedRanks[4];
 
407
 
 
408
  int nodeSetSize; /* Index with range 0 to nodeSetSizeLimit */
 
409
  int winSetSize;  /* Index with range 0 to winSetSizeLimit */
 
410
  int lenSetSize;  /* Index with range 0 to lenSetSizeLimit */
 
411
};
 
412
 
 
413
extern int noOfThreads;
 
414
extern int noOfCores;
 
415
extern struct localVarType localVar[MAXNOOFTHREADS];
 
416
extern int * highestRank;
 
417
extern int * counttable;
 
418
extern struct adaptWinRanksType * adaptWins;
 
419
extern unsigned short int bitMapRank[16];
 
420
extern unsigned short int relRankInSuit[4][4];
 
421
extern int sum;
 
422
extern int score1Counts[50], score0Counts[50];
 
423
extern int c1[50], c2[50], c3[50], c4[50], c5[50], c6[50], c7[50],
 
424
  c8[50], c9[50];
 
425
extern int nodeTypeStore[4];            /* Look-up table for determining if
 
426
                                        node is MAXNODE or MINNODE */
 
427
extern int lho[4], rho[4], partner[4];                                        
 
428
extern int nodes;                       /* Number of nodes searched */
 
429
extern int no[50];                      /* Number of nodes searched on each
 
430
                                        depth level */
 
431
extern int payOff;
 
432
extern int iniDepth;
 
433
extern int treeDepth;
 
434
extern int tricksTarget;                /* No of tricks for MAX in order to
 
435
                                        meet the game goal, e.g. to make the
 
436
                                        contract */
 
437
extern int tricksTargetOpp;             /* Target no of tricks for MAX
 
438
                                        opponent */
 
439
extern int targetNS;
 
440
extern int targetEW;
 
441
extern int handToPlay;
 
442
extern int searchTraceFlag;
 
443
extern int countMax;
 
444
extern int depthCount;
 
445
extern int highHand;
 
446
extern int nodeSetSizeLimit;
 
447
extern int winSetSizeLimit;
 
448
extern int lenSetSizeLimit;
 
449
extern int estTricks[4];
 
450
extern int recInd; 
 
451
extern int suppressTTlog;
 
452
extern unsigned char suitChar[4];
 
453
extern unsigned char rankChar[15];
 
454
extern unsigned char handChar[4];
 
455
extern unsigned char cardRank[15], cardSuit[5], cardHand[4];
 
456
extern int totalNodes;
 
457
extern struct futureTricks fut, ft;
 
458
extern struct futureTricks *futp;
 
459
extern char stri[128];
 
460
 
 
461
extern FILE *fp, /**fp2, *fp7, *fp11,*/ *fpx;
 
462
  /* Pointers to logs */
 
463
 
 
464
extern struct ttStoreType * ttStore;
 
465
extern int lastTTstore;
 
466
extern int ttCollect;
 
467
extern int suppressTTlog;
 
468
 
 
469
EXTERN_C DLLEXPORT int STDCALL SolveBoard(struct deal dl, 
 
470
  int target, int solutions, int mode, struct futureTricks *futp, int threadIndex);
 
471
EXTERN_C DLLEXPORT int STDCALL CalcDDtable(struct ddTableDeal tableDeal, 
 
472
  struct ddTableResults * tablep);
 
473
 
 
474
#ifdef PBN
 
475
EXTERN_C DLLEXPORT int STDCALL SolveBoardPBN(struct dealPBN dlpbn, int target, 
 
476
    int solutions, int mode, struct futureTricks *futp, int thrId);
 
477
EXTERN_C DLLEXPORT int STDCALL CalcDDtablePBN(struct ddTableDealPBN tableDealPBN, 
 
478
  struct ddTableResults * tablep);
 
479
#endif
 
480
 
 
481
#ifdef PBN_PLUS
 
482
EXTERN_C DLLEXPORT int STDCALL SolveAllBoards(struct boardsPBN *bop, struct solvedBoards *solvedp);
 
483
EXTERN_C DLLEXPORT int STDCALL SolveAllChunks(struct boardsPBN *bop, struct solvedBoards *solvedp, int chunkSize);
 
484
EXTERN_C DLLEXPORT int STDCALL CalcAllTables(struct ddTableDeals *dealsp, int mode, 
 
485
        int trumpFilter[5], struct ddTablesRes *resp, struct allParResults *presp);
 
486
EXTERN_C DLLEXPORT int STDCALL CalcAllTablesPBN(struct ddTableDealsPBN *dealsp, int mode, 
 
487
        int trumpFilter[5], struct ddTablesRes *resp, struct allParResults *presp);
 
488
EXTERN_C DLLEXPORT int STDCALL CalcPar(struct ddTableDeal tableDeal, int vulnerable, 
 
489
    struct ddTableResults * tablep, struct parResults *presp);
 
490
EXTERN_C DLLEXPORT int STDCALL CalcParPBN(struct ddTableDealPBN tableDealPBN, 
 
491
  struct ddTableResults * tablep, int vulnerable, struct parResults *presp);
 
492
#endif
 
493
 
 
494
#ifdef __linux
 
495
EXTERN_C void InitStart(int gb_ram, int ncores);   /* For usage with ctypes in Linux. */ 
 
496
#endif
 
497
 
 
498
EXTERN_C void InitStart(int gb_ram, int ncores);
 
499
void InitGame(int gameNo, int moveTreeFlag, int first, int handRelFirst, int thrId);
 
500
void InitSearch(struct pos * posPoint, int depth,
 
501
  struct moveType startMoves[], int first, int mtd, int thrId);
 
502
int ABsearch(struct pos * posPoint, int target, int depth, int thrId);
 
503
void Make(struct pos * posPoint, unsigned short int trickCards[4], 
 
504
  int depth, int trump, struct movePlyType *mply, int thrId);
 
505
int MoveGen(struct pos * posPoint, int depth, int trump, struct movePlyType *mply, int thrId);
 
506
void MergeSort(int n, struct moveType *a);
 
507
inline int WinningMove(struct moveType * mvp1, struct moveType * mvp2, int trump);
 
508
inline int WinningMoveNT(struct moveType * mvp1, struct moveType * mvp2);
 
509
int AdjustMoveList(int thrId);
 
510
int QuickTricks(struct pos * posPoint, int hand, 
 
511
        int depth, int target, int trump, int *result, int thrId);
 
512
int LaterTricksMIN(struct pos *posPoint, int hand, int depth, int target, int trump, int thrId); 
 
513
int LaterTricksMAX(struct pos *posPoint, int hand, int depth, int target, int trump, int thrId);
 
514
struct nodeCardsType * CheckSOP(struct pos * posPoint, struct nodeCardsType
 
515
  * nodep, int target, int tricks, int * result, int *value, int thrId);
 
516
struct nodeCardsType * UpdateSOP(struct pos * posPoint, struct nodeCardsType
 
517
  * nodep);  
 
518
struct nodeCardsType * FindSOP(struct pos * posPoint,
 
519
  struct winCardType * nodeP, int firstHand, 
 
520
        int target, int tricks, int * valp, int thrId);  
 
521
struct nodeCardsType * BuildPath(struct pos * posPoint, 
 
522
  struct posSearchType *nodep, int * result, int thrId);
 
523
void BuildSOP(struct pos * posPoint, long long suitLengths, int tricks, int firstHand, int target,
 
524
  int depth, int scoreFlag, int score, int thrId);
 
525
struct posSearchType * SearchLenAndInsert(struct posSearchType
 
526
        * rootp, long long key, int insertNode, int *result, int thrId);  
 
527
void Undo(struct pos * posPoint, int depth, struct movePlyType *mply, int thrId);
 
528
int CheckDeal(struct moveType * cardp, int thrId);
 
529
int InvBitMapRank(unsigned short bitMap);
 
530
int InvWinMask(int mask);
 
531
void ReceiveTTstore(struct pos *posPoint, struct nodeCardsType * cardsP, int target, 
 
532
  int depth, int thrId);
 
533
int NextMove(struct pos *posPoint, int depth, struct movePlyType *mply, int thrId); 
 
534
int DumpInput(int errCode, struct deal dl, int target, int solutions, int mode); 
 
535
void Wipe(int thrId);
 
536
void AddNodeSet(int thrId);
 
537
void AddLenSet(int thrId);
 
538
void AddWinSet(int thrId);
 
539
 
 
540
void PrintDeal(FILE *fp, unsigned short ranks[4][4]);
 
541
 
 
542
int SolveAllBoards4(struct boards *bop, struct solvedBoards *solvedp);
 
543
int SolveAllBoardsN(struct boards *bop, struct solvedBoards *solvedp, int chunkSize);
 
544
int SolveAllBoards1(struct boards *bop, struct solvedBoards *solvedp);
 
545
 
 
546
int IsCard(char cardChar);
 
547
void UpdateDealInfo(int card);
 
548
int IsVal(char cardChar);
 
549
 
 
550