Sample code that shows a FEN string lookup for Miguel's egtb

Discussion of chess software programming and technical issues.

Moderator: Ras

Dann Corbit
Posts: 12790
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Sample code that shows a FEN string lookup for Miguel's egtb

Post by Dann Corbit »

It's just a simple worked out example that I cobbled together from Miguel's public gtb code and Aaron's public gtb probe code:
http://cap.connx.com/chess-engines/new-approach/gtb.7z

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern int      load_gtb(char *gtb_pathlist, int cache_size_bytes, int compression_scheme);
extern void     unload_gtb(void);
extern int      probe_gtb_soft_dtm(char *fen, int *score);
extern int      probe_gtb_soft(char *fen, int *score);
extern int      probe_gtb_hard_dtm(char *fen, int *score);
extern int      probe_gtb_hard(char *fen, int *score);
extern int      probe_gtb_firm_dtm(char *fen, int *score);
extern int      probe_gtb_firm(char *fen, int *score);

char gtb_path[FILENAME_MAX*100];

char *getsafe(char *buffer, int count)
{
    char *result = buffer, *np;
    if ((buffer == NULL) || (count < 1))
        result = NULL;
    else if (count == 1)
        *result = '\0';
    else if ((result = fgets(buffer, count, stdin)) != NULL)
        if (np = strchr(buffer, '\n'))
            *np = '\0';
    return result;
}

char string[256];

int main(void)
{
    char *gtb_pathlist = getenv("GTB_LOCATION");
    int result;
    int method;
    if (gtb_pathlist == NULL)
    {
        puts("Enter the path to the gaviota tablebase files:");
        gtb_pathlist = getsafe(gtb_path, sizeof gtb_path);
    }
    puts("What is the method of compression? (0,1,2,3,4,5,6,7,8 or 9):");
    puts("If unsure, look at the extension on your gtb files...");
    getsafe(string, sizeof string);
    method = atoi(string);
    result = load_gtb(gtb_pathlist, 1 << 22, method);
    if (result)
    {
        int score;
        int result;
        puts("Enter a FEN or EPD chess position:");
        getsafe(string, sizeof string);
		puts("First probe results...");
        result =     probe_gtb_soft_dtm(string, &score);
        printf("result = %d, score = %d\n", result, score);
        result =     probe_gtb_soft(string, &score);
        printf("result = %d, score = %d\n", result, score);
        result =     probe_gtb_hard_dtm(string, &score);
        printf("result = %d, score = %d\n", result, score);
        result =     probe_gtb_hard(string, &score);
        printf("result = %d, score = %d\n", result, score);
        result =     probe_gtb_firm_dtm(string, &score);
        printf("result = %d, score = %d\n", result, score);
        result =     probe_gtb_firm(string, &score);
        printf("result = %d, score = %d\n", result, score);

		puts("Second probe results...");
        result =     probe_gtb_soft_dtm(string, &score);
        printf("result = %d, score = %d\n", result, score);
        result =     probe_gtb_soft(string, &score);
        printf("result = %d, score = %d\n", result, score);
        result =     probe_gtb_hard_dtm(string, &score);
        printf("result = %d, score = %d\n", result, score);
        result =     probe_gtb_hard(string, &score);
        printf("result = %d, score = %d\n", result, score);
        result =     probe_gtb_firm_dtm(string, &score);
        printf("result = %d, score = %d\n", result, score);
        result =     probe_gtb_firm(string, &score);
        printf("result = %d, score = %d\n", result, score);

    }
    else
    {
        puts("gtb base load failed.");
        exit(EXIT_FAILURE);
    }
    return 0;
}
/*
Possible output:
Enter the path to the gaviota tablebase files:
c:\chess\winboard\gtb
What is the method of compression? (0,1,2,3,4,5,6,7,8 or 9):
If unsure, look at the extension on your gtb files...
4
Enter a FEN or EPD chess position:
8/8/8/8/4P1k1/8/5K2/8 w - - bm Ke3; id "J&S42.05";
First probe results...
result = 0, score = -858993460
result = 0, score = -858993460
result = 1, score = 32732
result = 1, score = 32000
result = 1, score = 32732
result = 1, score = 32000
Second probe results...
result = 1, score = 32732
result = 1, score = 31743
result = 1, score = 32732
result = 1, score = 32000
result = 1, score = 32732
result = 1, score = 32000
*/
User avatar
jshriver
Posts: 1358
Joined: Wed Mar 08, 2006 9:41 pm
Location: Morgantown, WV, USA

Re: Sample code that shows a FEN string lookup for Miguel's

Post by jshriver »

nice, ty for posting.
Dann Corbit
Posts: 12790
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Sample code that shows a FEN string lookup for Miguel's

Post by Dann Corbit »

jshriver wrote:nice, ty for posting.
Now, this is not a good model for actual probing. The whole idea of it was a simple stand-alone demo program that does nothing but probe the gtb files. So you can study what is going on with a very simple interface and see how it works.

In the actual case, you will eventually want to implement it directly like Aaron did. {I just took Aaron's code and snipped out the stuff that did not apply and spliced a few things for a quick demo}.
User avatar
michiguel
Posts: 6401
Joined: Thu Mar 09, 2006 8:30 pm
Location: Chicago, Illinois, USA

Re: Sample code that shows a FEN string lookup for Miguel's

Post by michiguel »

Dann Corbit wrote:
jshriver wrote:nice, ty for posting.
Now, this is not a good model for actual probing. The whole idea of it was a simple stand-alone demo program that does nothing but probe the gtb files. So you can study what is going on with a very simple interface and see how it works.

In the actual case, you will eventually want to implement it directly like Aaron did. {I just took Aaron's code and snipped out the stuff that did not apply and spliced a few things for a quick demo}.
Cool!

I will place a link to it.

Miguel
User avatar
jshriver
Posts: 1358
Joined: Wed Mar 08, 2006 9:41 pm
Location: Morgantown, WV, USA

Re: Sample code that shows a FEN string lookup for Miguel's

Post by jshriver »

I'm a little confused by the code. Using all 3 source trees I was able to get this working using a modified compile.sh script.

For example
First probe results...
result = 0, score = -858993460
result = 0, score = -858993460
result = 1, score = 32732

What are result and score? Isn't score of a move engine dependent and result seems to be a 0/1 results so guessing win/loss?

The function names suggest distance to mate, so wondering why it doesn't give x depth like mate in 18 moves for y move.

Looking at some of the online egtb prob sites they give the various legal moves for a given fen position and list the DTM for each.

Is this possible here?

Thanks in advance.
Dann Corbit
Posts: 12790
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Sample code that shows a FEN string lookup for Miguel's

Post by Dann Corbit »

jshriver wrote:I'm a little confused by the code. Using all 3 source trees I was able to get this working using a modified compile.sh script.

For example
First probe results...
result = 0, score = -858993460
result = 0, score = -858993460
result = 1, score = 32732

What are result and score? Isn't score of a move engine dependent and result seems to be a 0/1 results so guessing win/loss?

The function names suggest distance to mate, so wondering why it doesn't give x depth like mate in 18 moves for y move.

Looking at some of the online egtb prob sites they give the various legal moves for a given fen position and list the DTM for each.

Is this possible here?

Thanks in advance.
Result =0 indicates that the result has not cached and so there was a cache miss. Hence, the mate distance is undefined.

If you look at the names of the functions, I think it will become clear.

There are really three versions of each.
1. Always go to disk and read it
2. Try the cache, and if missing, get it from disk
3. Only read it from the cache if present, otherwise return a cache miss

There is also a bitmap and DTM version.