UCI code

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

UCI code

Post by lucasart »

Hello

I wrote the UCI interfacing code, and trying to test with XBoard, but it simply hands and nothing happens. Is there a way to see what is being sent/recieved by XBoard, so help debugging. Here's my UCI code, it's pretty minimal, and works when I manually talk to my enging in UCI language

Code: Select all

void main_loop()
{
	setvbuf(stdin,NULL,_IONBF,0);
	setvbuf(stdout,NULL,_IONBF,0);

	char line[0x1000];
	while (fgets(line, 0x1000, stdin) && parse_line(line));
}

static bool parse_line(char *line)
{
	const char *token = strtok(line, " \n");
	
	if (!strcmp(token, "go"))
		parse_go();
	else if (!strcmp(token, "uci")) {	
		puts("id name BibiChess");
		puts("id author Lucas Braesch");
		puts("uciok");
	}
	else if (!strcmp(token, "ucinewgame"))
		tt_clear();
	else if (!strcmp(token, "position"))
		parse_position(line + strlen("position")+1);
	else if (!strcmp(token, "isready"))
		puts("readyok");
	else if (!strcmp(token, "stop"))
		abort_search = true;
	else if (!strcmp(token, "quit"))
		return false;
	
	return true;
}

static void parse_position(const char *line)
{
	const char *fen = strstr(line, "fen ");
	const char *moves = strstr(line, "moves ");	
	
	if (fen)
		load_fen(&B, fen + strlen("fen "));
	else	// assume startpos
		load_fen(&B, StartFen);
	
	if (moves) {
		const char *ptr = moves + strlen("moves ");
		char s[8];
		
		while (*ptr != '\n') {
			s[0] = *ptr++;
			s[1] = *ptr++;
			s[2] = *ptr++;
			s[3] = *ptr++;
			
			if (*ptr == '\n' || *ptr == ' ')
				s[4] = '\0';
			else {	// promotion
				s[4] = *ptr++;
				s[5] = '\0';
			}
			
			move_t m = string_to_move(&B, s);
			play(&B, m);
			
			while (*ptr == ' ') ptr++;
		}
	}
}

static void parse_go()
{
	const char *token;
	int wtime, btime, winc, binc;

	for (token = strtok(NULL," \n"); token; token = strtok(NULL," \n"))
	{
		if (!strcmp(token, "wtime")) {
			if (!(token = strtok(NULL, " \n")))
				fatal("parse_go(): missing argument\n");
			wtime = atoi(token);
		}
		else if (!strcmp(token, "btime")) {
			if (!(token = strtok(NULL, " \n")))
				fatal("parse_go(): missing argument\n");
			btime = atoi(token);
		}
		else if (!strcmp(token, "winc")) {
			if (!(token = strtok(NULL, " \n")))
				fatal("parse_go(): missing argument\n");
			winc = atoi(token);
		}
		else if (!strcmp(token, "binc")) {
			if (!(token = strtok(NULL, " \n")))
				fatal("parse_go(): missing argument\n");
			binc = atoi(token);
		}
	}
	
	int time = B.turn ? btime : wtime,
		inc = B.turn ? binc : winc;
	
	move_t best = id_loop(&B, time/40 + inc);
	
	char s[8];
	move_to_string(&B, best, s, false);
	printf("info bestmove %s\n", s);	
	play(&B, best);
}
And here's a sample session

Code: Select all

lucas@lucas-laptop:~/.codelite/Chess$ ./chess 
uci
id name BibiChess
id author Lucas Braesch
uciok
isready
readyok
ucinewgame
position startpos moves e2e4
go btime 100 binc 50
info depth 1
info currmove Na6 currmovenumber 1
info currmove Nc6 currmovenumber 2
info currmove Nf6 currmovenumber 3
info currmove Nh6 currmovenumber 4
info currmove a5 currmovenumber 5
info currmove b5 currmovenumber 6
info currmove c5 currmovenumber 7
info currmove d5 currmovenumber 8
info currmove e5 currmovenumber 9
info currmove f5 currmovenumber 10
info currmove g5 currmovenumber 11
info currmove h5 currmovenumber 12
info currmove a6 currmovenumber 13
info currmove b6 currmovenumber 14
info currmove c6 currmovenumber 15
info currmove d6 currmovenumber 16
info currmove e6 currmovenumber 17
info currmove f6 currmovenumber 18
info currmove g6 currmovenumber 19
info currmove h6 currmovenumber 20
info cp 35 nodes 25 time 0
info score 35 pv Nf6
info depth 2
info currmove Nf6 currmovenumber 1
info currmove Nh6 currmovenumber 2
info currmove a5 currmovenumber 3
info currmove b5 currmovenumber 4
info currmove c5 currmovenumber 5
info currmove d5 currmovenumber 6
info currmove e5 currmovenumber 7
info currmove f5 currmovenumber 8
info currmove g5 currmovenumber 9
info currmove h5 currmovenumber 10
info currmove a6 currmovenumber 11
info currmove b6 currmovenumber 12
info currmove c6 currmovenumber 13
info currmove d6 currmovenumber 14
info currmove e6 currmovenumber 15
info currmove f6 currmovenumber 16
info currmove g6 currmovenumber 17
info currmove h6 currmovenumber 18
info currmove Na6 currmovenumber 19
info currmove Nc6 currmovenumber 20
info cp -24 nodes 102 time 0
info score -24 pv Nf6 Nc3 Nxe4 Nxe4
info depth 3
info currmove Nf6 currmovenumber 1
info currmove Nh6 currmovenumber 2
info currmove a5 currmovenumber 3
info currmove b5 currmovenumber 4
info currmove c5 currmovenumber 5
info currmove d5 currmovenumber 6
info currmove e5 currmovenumber 7
info currmove f5 currmovenumber 8
info currmove g5 currmovenumber 9
info currmove h5 currmovenumber 10
info currmove a6 currmovenumber 11
info currmove b6 currmovenumber 12
info currmove c6 currmovenumber 13
info currmove d6 currmovenumber 14
info currmove e6 currmovenumber 15
info currmove f6 currmovenumber 16
info currmove g6 currmovenumber 17
info currmove h6 currmovenumber 18
info currmove Na6 currmovenumber 19
info currmove Nc6 currmovenumber 20
info cp 30 nodes 870 time 0
info score 30 pv Nf6 e5 Nd5 Nxe4 Bd7
info depth 4
info currmove Nf6 currmovenumber 1
info currmove Nc6 currmovenumber 2
info currmove Nh6 currmovenumber 3
info currmove f5 currmovenumber 4
info currmove f6 currmovenumber 5
info currmove b5 currmovenumber 6
info currmove c5 currmovenumber 7
info currmove d5 currmovenumber 8
info currmove e5 currmovenumber 9
info currmove Na6 currmovenumber 10
info currmove g5 currmovenumber 11
info currmove h5 currmovenumber 12
info currmove a6 currmovenumber 13
info currmove b6 currmovenumber 14
info currmove c6 currmovenumber 15
info currmove d6 currmovenumber 16
info currmove e6 currmovenumber 17
info currmove a5 currmovenumber 18
info currmove g6 currmovenumber 19
info currmove h6 currmovenumber 20
info cp -31 nodes 4606 time 0
info score -31 pv Nc6 Nf3 Nf6 Nc3 Bd7 Nxe4 Qxd7 Nxd5 Nxd5 Nxd5 Qxd5
info depth 5
info currmove Nc6 currmovenumber 1
info currmove Nf6 currmovenumber 2
info currmove d5 currmovenumber 3
info currmove e5 currmovenumber 4
info currmove g6 currmovenumber 5
info currmove f5 currmovenumber 6
info currmove f6 currmovenumber 7
info currmove Nh6 currmovenumber 8
info currmove a5 currmovenumber 9
info currmove b5 currmovenumber 10
info currmove g5 currmovenumber 11
info currmove h5 currmovenumber 12
info currmove a6 currmovenumber 13
info currmove b6 currmovenumber 14
info currmove c6 currmovenumber 15
info currmove d6 currmovenumber 16
info currmove e6 currmovenumber 17
info currmove c5 currmovenumber 18
info currmove Na6 currmovenumber 19
info currmove h6 currmovenumber 20
info cp 1 nodes 21783 time 30
info score 1 pv Nf6 e5 Ne4 Nc3 d5 Nxe4 Qxd5 Nxe4 Qxd5 Bxc6+ Qxc6
info depth 6
info currmove Nf6 currmovenumber 1
info bestmove g8f6
Any idea what I can do ?
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: UCI code

Post by Evert »

You're aware the XBoard uses the XBoard protocol to talk to a chess engine, not the UCI protocol, right?
You can still use an UCI engine with XBoard through polyglot, which translates xboard to UCI, but that's a layer of inderection.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: UCI code

Post by lucasart »

Evert wrote:You're aware the XBoard uses the XBoard protocol to talk to a chess engine, not the UCI protocol, right?
You can still use an UCI engine with XBoard through polyglot, which translates xboard to UCI, but that's a layer of inderection.
yes I am aware of that. I have polyglot installed and use the following command

Code: Select all

lucas@lucas-laptop:~$ xboard -fUCI -fcp .codelite/Chess/chess
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: UCI code

Post by hgm »

So what you are interested in is not what goes between XBoard and Polyglot, but between Polyglot and your engine. For that you should look in the Polyglot log. You can switch Polyglot logging on in the XBoard Engine Settings menu, and also specify a file name for the log (default is polyglot.log).

Btw, what XBoard sends and receives can be seen in the file xboard.debug, when you run XBoard with the additional option -debug. But it will probably be less useful to you.

An alternative (for if the Polyglot log is not clear) would be to run the engine under UCI2WB rather than Polyglot, and look at the xboard.debug file, because UCI2WB sends what it received from the engine also to XBoard as a comment, so it will appear in the xboard.debug file.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: UCI code

Post by lucasart »

hgm wrote:So what you are interested in is not what goes between XBoard and Polyglot, but between Polyglot and your engine. For that you should look in the Polyglot log. You can switch Polyglot logging on in the XBoard Engine Settings menu, and also specify a file name for the log (default is polyglot.log).

Btw, what XBoard sends and receives can be seen in the file xboard.debug, when you run XBoard with the additional option -debug. But it will probably be less useful to you.

An alternative (for if the Polyglot log is not clear) would be to run the engine under UCI2WB rather than Polyglot, and look at the xboard.debug file, because UCI2WB sends what it received from the engine also to XBoard as a comment, so it will appear in the xboard.debug file.
it hangs at the engine loading stage. and the engine settings button is unresponsive to mouse clicks.
i think this xboard + polyglot is a nightmare, and would rather not introduce more complications than i already have.

does anyone know how to use cutechess-cli ? I can't understasnd the command line syntax, and find the help page rather obscure. he could at least have put an example there :( but i noticed a debug option that just does exactly what i want
User avatar
Desperado
Posts: 879
Joined: Mon Dec 15, 2008 11:45 am

Re: UCI code

Post by Desperado »

lucasart wrote:
hgm wrote:So what you are interested in is not what goes between XBoard and Polyglot, but between Polyglot and your engine. For that you should look in the Polyglot log. You can switch Polyglot logging on in the XBoard Engine Settings menu, and also specify a file name for the log (default is polyglot.log).

Btw, what XBoard sends and receives can be seen in the file xboard.debug, when you run XBoard with the additional option -debug. But it will probably be less useful to you.

An alternative (for if the Polyglot log is not clear) would be to run the engine under UCI2WB rather than Polyglot, and look at the xboard.debug file, because UCI2WB sends what it received from the engine also to XBoard as a comment, so it will appear in the xboard.debug file.
it hangs at the engine loading stage. and the engine settings button is unresponsive to mouse clicks.
i think this xboard + polyglot is a nightmare, and would rather not introduce more complications than i already have.

does anyone know how to use cutechess-cli ? I can't understasnd the command line syntax, and find the help page rather obscure. he could at least have put an example there :( but i noticed a debug option that just does exactly what i want
===

example:

cutechess-cli -fcp cmd=ENGINE.exe -scp cmd=ENGINE.exe -both proto=uci tc=4+0.1

-fcp: after that command you can configure the "fcp" first chess program
-scp: after that command you can configure the "scp" second chess program

-both: after that command the settings are effectual for both programs

proto,tc(timecontrol) in the example

b:
===

i dont remember all the possibilities now, because it is a while back i used it.
now with the help you should figure out the other options which are available.

example:

cutechess-cli -fcp cmd=Engine.exe name=Lucas...

here, when games will be saved or simple listed, the name of option will be
used instead of the execution file name....

c:
===

tip. of course you can make a (some) batchfile, so you dont need to type
in everytime the same.

d:
===

if you like to play fast games you should also have a look at "LittleBlitzer"
which is more handy to use imho.

Michael
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: UCI code

Post by hgm »

lucasart wrote:it hangs at the engine loading stage. and the engine settings button is unresponsive to mouse clicks.
i think this xboard + polyglot is a nightmare, and would rather not introduce more complications than i already have.
Well, that is what you get for using UCI. :P

But it already tells enough: If the Engine Settings menu is unresponsive, it means that XBoad has not received ant engine options, so that the settings dialog would be completely empty. And that again means that Polyglot has not received the UCI options from the engine, and in particular not the 'uciok' command. Either that or you are using a defective Polyglot.
Michel
Posts: 2272
Joined: Mon Sep 29, 2008 1:50 am

Re: UCI code

Post by Michel »

i think this xboard + polyglot is a nightmare
Simple starting polyglot on the command line will likely show you
where the problem is

polyglot.exe -noini -ec engine.exe -log true

A typical xboard sequence of commands would be something like

xboard
protover 2
time 100
otim 100
go

Does this give the desired result?

If not you can look in the log file (polyglot.log). Or else post it here.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: UCI code

Post by lucasart »

hgm wrote:
lucasart wrote:it hangs at the engine loading stage. and the engine settings button is unresponsive to mouse clicks.
i think this xboard + polyglot is a nightmare, and would rather not introduce more complications than i already have.
Well, that is what you get for using UCI. :P

But it already tells enough: If the Engine Settings menu is unresponsive, it means that XBoad has not received ant engine options, so that the settings dialog would be completely empty. And that again means that Polyglot has not received the UCI options from the engine, and in particular not the 'uciok' command. Either that or you are using a defective Polyglot.
you are right, my engine doesn't send any options. it just says it's name (id name and id author) and then it says uciok. i am certain of that as testing in command line, that's what happens.
anyway i'll see what happens with cutechess-cli, which seems like a much simpler setup than polyglot+xboard. or else, i rewrite to use the xboard protocol, and perhaps even put some #ifdef so my engine supports both, through separate compiles :)
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: UCI code

Post by lucasart »

Desperado wrote:
lucasart wrote:
hgm wrote:So what you are interested in is not what goes between XBoard and Polyglot, but between Polyglot and your engine. For that you should look in the Polyglot log. You can switch Polyglot logging on in the XBoard Engine Settings menu, and also specify a file name for the log (default is polyglot.log).

Btw, what XBoard sends and receives can be seen in the file xboard.debug, when you run XBoard with the additional option -debug. But it will probably be less useful to you.

An alternative (for if the Polyglot log is not clear) would be to run the engine under UCI2WB rather than Polyglot, and look at the xboard.debug file, because UCI2WB sends what it received from the engine also to XBoard as a comment, so it will appear in the xboard.debug file.
it hangs at the engine loading stage. and the engine settings button is unresponsive to mouse clicks.
i think this xboard + polyglot is a nightmare, and would rather not introduce more complications than i already have.

does anyone know how to use cutechess-cli ? I can't understasnd the command line syntax, and find the help page rather obscure. he could at least have put an example there :( but i noticed a debug option that just does exactly what i want
===

example:

cutechess-cli -fcp cmd=ENGINE.exe -scp cmd=ENGINE.exe -both proto=uci tc=4+0.1

-fcp: after that command you can configure the "fcp" first chess program
-scp: after that command you can configure the "scp" second chess program

-both: after that command the settings are effectual for both programs

proto,tc(timecontrol) in the example

b:
===

i dont remember all the possibilities now, because it is a while back i used it.
now with the help you should figure out the other options which are available.

example:

cutechess-cli -fcp cmd=Engine.exe name=Lucas...

here, when games will be saved or simple listed, the name of option will be
used instead of the execution file name....

c:
===

tip. of course you can make a (some) batchfile, so you dont need to type
in everytime the same.

d:
===

if you like to play fast games you should also have a look at "LittleBlitzer"
which is more handy to use imho.

Michael
many thanks! i'll go ahead and try that