UCI Programming

Code, algorithms, languages, construction...
lucasart
Posts: 201
Joined: Mon Dec 17, 2012 1:09 pm
Contact:

Re: UCI Programming

Post by lucasart » Wed Jan 16, 2013 4:03 am

CDaley11 wrote:Also, could someone please recommend a good UCI interface for use with my engine? Jose chess, just like sigma chess, was written for a power pc mac which does not work on OSX Lion.
cutechess, although you'll have to compile it yourself. you'll find it on github.
"Talk is cheap. Show me the code." -- Linus Torvalds.

lucasart
Posts: 201
Joined: Mon Dec 17, 2012 1:09 pm
Contact:

Re: UCI Programming

Post by lucasart » Wed Jan 16, 2013 4:13 am

User923005 wrote: This is the UCI interface for Sungorus chess program, written in C. As you can see, it's not rocket science:

Code: Select all

...
    setbuf(stdin, NULL);
    setbuf(stdout, NULL);
...
I didn't know the setbuf(..., NULL) trick. But I think this is slightly better:

Code: Select all

setvbuf(stdin, NULL, _IOLBF);
setvbuf(stdout, NULL, _IOLBF);
The point is that instead of disabling buffering completely, it just ensures that the stream is flushed everytime a '\n' is processed. So it solves the problem without having to compromise performance.

PS: When you test improvements in your engine, you typically need to play thousands of super fast games. At such time controls, things like unbuffered pipe I/O, or even task switching, become important in terms of performance. For example, when I play games in 10,000 nodes per move, and my engine does 2 million nodes per seconds, that's 5 msec per move. In that time, you have a ton of I/O going on, especially if you write all the UCI garbage (like info currmove seldepth pv etc.), and you don't wan't it unbuffered.
Although at any *normal* time control, the difference is not measurable, of course.
"Talk is cheap. Show me the code." -- Linus Torvalds.

lucasart
Posts: 201
Joined: Mon Dec 17, 2012 1:09 pm
Contact:

Re: UCI Programming

Post by lucasart » Wed Jan 16, 2013 4:42 am

CDaley11 wrote:Also, could someone please recommend a good UCI interface for use with my engine? Jose chess, just like sigma chess, was written for a power pc mac which does not work on OSX Lion.
Regarding the UCI protocol, you'll see that it's very simple to implement. You don't need to read the protocol description, which goes into a lot of details, most of which are useless to a beginner engine. Here is an example of a minimalist implementation (">" means GUI->Engine, and "<" means Engine->GUI, every line is ended with a "\n" obviously):

Code: Select all

> uci
< id name Name of your engine
< id author Your Name
< uciok

> ucinewgame
> isready
< readyok

> postition startpos moves e2e4 c7c5
> isready
< readyok

> go movetime 1000
< info depth 1 score 15 pv b1c3
< info depth 2 score 10 pv g1f3
< info depth 3 score 12 pv g1f3
< bestmove g1f3
You should look in particular at all the possible options of the "go" command. I justy gave one example (movetime = play a fixed time/move). Most common are
- Fischer clock (time+inc): go wtime 60000 winc 1000 btime 60000 binc 1000, meaning 60" + 1" increment (for white and black)
- Tournament time control: go wtime 60000 btime 60000 movestogo 40, meaning 60" on the clock, and you have 40 moves left (before the clock gets incremented again, although the protocol doesn't tell you by how much it will be incremented). Although it is not the intent of the UCI protocol, and perhaps not many GUI support it, you can combine Tournament+Fischet in this way: go wtime 60000 winc 1000 btime 60000 binc 1000 movestogo 40, meaning 60" left on the clock, 1" increment per move, and in 40 moves the clock will be incremented by some (unknown) amount. Cutechess supports that hybrid time control.
- fixed time: go movetime 1000, plays this move in 1"
- fixed depth: go depth 5
- fixed number of nodes: go nodes 10000
- find a mate in X moves: go mate 10. You stop when you have found a mate in <= 10 moves, otherwise you continue the search forever. Probably better to combine this option with a nother limit, like "go mate 10 movetime 10000" (stop after a mate in <= 10 moves is found, or 10" have elapsed).
And you can mix all these conditions as you like in any order.

Also there's "position fen". For example:
"position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 moves e2e4 c7c5"

Oh, and moves are always in algebraic notation (e2e4 c7c5 g1f3 b8c6 etc.) Promotions are in lower case like so: e7f8q (which in SAN count get "decorated" like e7xf8=Q, or e7xf8=Q+, e7xf8=Q#, e7xf8=Q! etc.)

Last, is the quit command. When the GUI sends this, you simply terminate your program.

That is all you need to know for now.
"Talk is cheap. Show me the code." -- Linus Torvalds.

Post Reply