UCI Protocol – Capturing User Input

I found the Monarch’s source code in a corner of my hard disk.  One of the strong points of Monarch was its stability.  Users reported running thousands and thousand of games without it crashing.  I put this down to defensive programming (hat tip Fabian!) and a solid implementation of the UCI protocol. So since Monarch was so stable I’ll reuse as much of the UCI code as possible.  This is not intended as a lesson on the UCI protocol, so if you want ot find out more I suggest you read Shredder’s Page or UCI Chess Engine:

Listening for the Input:

There are basically two ways to listen for input in console mode.  One way is to check for the data in the standard pipes (Fruit uses this approach).  The other is to use a separate Thread for the engine and use the main thread to get the user input.  Monarch used the second approach.  It seemed to work well.

So here’s the main loop.  This sets the buffer to zero (highly recommended to avoid any timing delays) and initializes the engine:

int main(int argc, char *argv[])
setbuf(stdout, NULL);
setbuf(stdin, NULL);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stdin, NULL, _IONBF, 0);

//uci.engine_initialized = FALSE;
//uci.mode = UCI_STARTING;



return TRUE;

Creating the engine thread is relatively straightforward:

void create_uci_engine_thread()
thread_handle = (HANDLE)_beginthreadex( NULL, 0, &engine_loop, NULL, 0, &threadID );
SetThreadPriority(thread_handle, THREAD_PRIORITY_NORMAL); // needed for Fritz GUI! :-))

Now the engine just needs to respond to changes in the UCI mode.

unsigned __stdcall engine_loop(void* pArguments)
uci.mode = UCI_WAITING;
if (uci.mode == UCI_START_THINKING){
if (uci.mode != UCI_QUIT)
uci.mode = UCI_WAITING;
if (uci.mode == UCI_QUIT){
return 0;

Now I have a working console application I can start to write the basic building blocks of the engine and test them with the “test” command.

Tomorrow, I’ll work on the chess board data structure.

  • So looks like you are doing in in plain C. I found wrapping the functionality in classes very helpful for the design. But I was always more the object oriented programmer anyway.


    • Steve Maughan

      Hi Thomas, I love object orientated programming. All of my Delphi programs have an oo structure. However, my knowledge of c is really only quite basic, and I haven’t ventured into c++. I think I’ll stick with vanilla c at this stage but thanks for the input! Steve

      • Yes, Delphi is cool. My first chess engine was written in Free Pascal (Lazarus) which looks very much like Delphi. My 2nd engine (ice) is now in C++ and I also learnt most of the stuff by doing. So first it was just a “C with classes” implementation. Much later I then started to use some function templates mostly in eval to get rid of the code duplication (eval for white, eval for black).

        So good luck and the iceMan is looking forward to meet Maverick.