Here you have the .h file and an example of how to probe the tablebases.
Before I release the code, I want to make sure that the interface has been chosen properly. Is there any potential problem I am missing? This is in C, is there anything that will inconvenience C++ programmers?
Miguel
This is the gtb.h file that should be included in the engine source. Each of the functions will be properly commented, but I think they have a name that are self-explanatory for now. Are they?
Code: Select all
#if !defined(H_GTB)
#define H_GTB
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
/* this includes a boolean type bool_t */
#include "bool_t.h"
/* this includes SQUARE and SQ_CONTENT types */
#include "tbtypes.h"
enum TB_return_values { tb_DRAW = 0, tb_WMATE = 1, tb_BMATE = 2, tb_FORBID = 3, tb_UNKNOWN = 7};
extern void tb_init (void);
extern void tb_done (void);
extern bool_t tb_probe
(unsigned int stm,
SQUARE epsq,
const SQUARE *inp_wSQ,
const SQUARE *inp_bSQ,
const SQ_CONTENT *inp_wPC,
const SQ_CONTENT *inp_bPC,
/*@out@*/ unsigned int *tbinfo,
/*@out@*/ unsigned int *plies);
/* tb cache routines */
extern bool_t tbcache_init (size_t cache_mem, size_t block_mem);
extern void tbcache_done (void);
extern void tbcache_on();
extern void tbcache_off();
/* for path */
extern bool_t tb_setpath (const char *path);
extern bool_t tb_addpath (const char *path);
/* building routines */
extern void tb_set_build (int men);
extern bool_t tb_set_validate (int men);
extern void tb_individual_validate (const char *s);
extern void tb_set_cpu_usage (int cores);
/* testing routines */
extern bool_t tb_set_testlongest (int men);
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
#endif
Code: Select all
#include <stdlib.h>
#include "gtb.h"
#include "tbtypes.h"
#include "bool_t.h"
/* The following are numbers the programmer should be aware of */
/*-------------------
SQ_CONTENT Values
-------------------*/
/* pieces definition */
#define NOPIECE 0u
#define PAWN 1u
#define KNIGHT 2u
#define BISHOP 3u
#define ROOK 4u
#define QUEEN 5u
#define KING 6u
/*-------------------
SIDE Values
-------------------*/
#define WHITE_TO_MOVE 0
#define BLACK_TO_MOVE 1
/*-------------------
SQUARES
-------------------*/
/* from A1=1 to H8=63, different squares posibles, NOSQUARE=64 */
enum SQUARES {
A1,B1,C1,D1,E1,F1,G1,H1,
A2,B2,C2,D2,E2,F2,G2,H2,
A3,B3,C3,D3,E3,F3,G3,H3,
A4,B4,C4,D4,E4,F4,G4,H4,
A5,B5,C5,D5,E5,F5,G5,H5,
A6,B6,C6,D6,E6,F6,G6,H6,
A7,B7,C7,D7,E7,F7,G7,H7,
A8,B8,C8,D8,E8,F8,G8,H8,
NOSQUARE
};
/*-------------------
CASTLING
-------------------*/
#define wOO (1<<3)
#define wOOO (1<<2)
#define bOO (1<<1)
#define bOOO (1<<0)
int main (void)
{
unsigned int info = tb_UNKNOWN; /* default, no tbvalue */
unsigned int pliestomate;
bool_t tb_available;
/*----------------------------------*\
| Elements that define a position
\*----------------------------------*/
unsigned int stm; /* side to move */
SQUARE ep_square; /* target square for an en passant capture */
unsigned int castl; /* castling availability, 0 => no castles avail */
SQUARE ws[17], bs[17]; /* list of squares for white and black */
SQ_CONTENT wp[17], bp[17]; /* what pieces are on those squares */
/*----------------------------------*\
| Initialization
|
| Include somthing like this at
| the beginning of the program.
\*----------------------------------*/
tb_init();
tb_setcompression (1);
tb_setpath ("/home/user/tb/");
tb_addpath ("/media/bigdisk/tb/");
tbcache_init (64*1024*1024 /* cache size 64M */, 32*1024 /* block size 32k */);
tbcache_on();
/*--------------------------------------*\
|
| ASSIGNING POSITIONAL VALUES for
| one probing example
|
| Position we try to probe:
|
| 1r6/6k1/8/8/8/8/1P6/1KR5 w - - 0 1
|
\*--------------------------------------*/
stm = WHITE_TO_MOVE; /* 0 = white to move, 1 = black to move */
ep_square = NOSQUARE; /* no ep available */
castl = 0; /* No castling available */
ws[0] = B1;
ws[1] = C1;
ws[2] = B2;
ws[3] = NOSQUARE;
wp[0] = KING;
wp[1] = ROOK;
wp[2] = PAWN;
wp[3] = NOPIECE;
bs[0] = G7;
bs[1] = B8;
bs[2] = NOSQUARE;
bp[0] = KING;
bp[1] = ROOK;
bp[2] = NOPIECE;
/*----------------------------------*\
|
| PROBING TBs
|
\*----------------------------------*/
tb_available = egtb_probe (stm, ep_square, ws, bs, wp, bp, &info, &pliestomate);
if (tb_available) {
if (info == tb_DRAW)
printf ("Draw\n");
else if (info == tb_WMATE && stm == WHITE_TO_MOVE)
printf ("White mates, plies=%d\n", pliestomate);
else if (info == tb_BMATE && stm == BLACK_TO_MOVE)
printf ("Black mates, plies=%d\n", pliestomate);
else if (info == tb_WMATE && stm == BLACK_TO_MOVE)
printf ("Black is mated, plies=%d\n", pliestomate);
else if (info == tb_BMATE && stm == WHITE_TO_MOVE)
printf ("White is mated, plies=%d\n", pliestomate);
else {
printf ("ERROR\n");
}
} else {
printf ("Tablebase not available\n");
}
/*----------------------------------*\
| Clean up
\*----------------------------------*/
tbcache_done();
tb_done();
/*----------------------------------*\
| Return
\*----------------------------------*/
if (tb_available)
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}