-
Notifications
You must be signed in to change notification settings - Fork 5
/
engine-intf.html
2465 lines (2215 loc) · 109 KB
/
engine-intf.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Chess Engine Communication Protocol</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
.header {
border-top:2px solid black;
border-bottom:2px solid black;
}
.version1 { color: red;}
.version43 { color: green;}
.version44 { color: blue; }
.version47 { color: purple; }
.version48 { color: brown; }
table tr { text-align: left}
tr > td:first-child { font-weight:bold;}
dt { font-weight:bold;}
</style>
</head>
<body>
<div class="header">
<h1>Chess Engine Communication Protocol</h1>
<h2><a href="http://www.tim-mann.org/">Tim Mann</a> & <a href="http://home.hccnet.nl/h.g.muller/winboardF.html">H.G.Muller</a></h2>
<p>
Version 2; implemented in xboard/WinBoard 4.2.1 and later. (Sept 3, 2009)<br />
Changes since version 1 are indicated in <span class="version1">red</span>.<br />
Changes for WinBoard 4.3.xx are indicated in <span class="version43">green</span>.<br />
Changes for WinBoard 4.4.xx are indicated in <span class="version44">blue</span>.
</p>
</div>
<ul>
<li><a href="#1">1. Introduction</a></li>
<li><a href="#2">2. Connection</a></li>
<li><a href="#3">3. Debugging</a></li>
<li><a href="#4">4. How it got this way</a></li>
<li><a href="#5">5. WinBoard requires Win32 engines</a></li>
<li><a href="#6">6. Hints on input/output</a></li>
<li><a href="#7">7. Signals</a></li>
<li><a href="#8">8. Commands from xboard to the engine</a></li>
<li><a href="#9">9. Commands from the engine to xboard</a></li>
<li><a href="#10">10. Thinking Output</a></li>
<li><a href="#11">11. Time control</a></li>
<li><a href="#12">12. Analyze Mode</a></li>
<li><a href="#13">13. Idioms and backward compatibility features</a></li>
</ul>
<hr />
<h2><a name="1">1. Introduction</a></h2>
<p>
This document is a set of rough notes on the protocol that xboard and
WinBoard use to communicate with gnuchessx and other chess engines.
These notes may be useful if you want to connect a different chess
engine to xboard. Throughout the notes, "xboard" means both xboard
and WinBoard except where they are specifically contrasted.
</p>
<p>
There are two reasons I can imagine someone wanting to do this:
</p>
<ol>
<li>You have, or are developing, a chess engine but you don't want to
write your own graphical interface. </li>
<li>You have, or are developing,a chess engine, and you want to
interface it to the Internet Chess Server.</li>
</ol>
<p>
In case (2), if you are using xboard, you will need to configure the
"Zippy" code into it, but WinBoard includes this code already. See
the file <a
href="http://www.tim-mann.org/xboard/zippy.README">zippy.README</a>
in the xboard or WinBoard distribution for more information.
</p>
<p>
These notes are unpolished, but I've attempted to make them complete
in this release. If you notice any errors, omissions, or misleading
statements, let me know.
</p>
<p>
I'd like to hear from everyone who is trying to interface their own
chess engine to xboard/WinBoard. Please join the mailing list for
authors of xboard/WinBoard compatible chess engines and post a message
about what you're doing. The list is now hosted by Yahoo Groups; you
can join at <a href="http://groups.yahoo.com/group/chess-engines"
>http://groups.yahoo.com/group/chess-engines</a>, or you can read the
list there without joining. The list is filtered to prevent spam.
</p>
<p class="version43">
Note that the WinBoard 4.3.xx line was developed independently of the
original GNU project, by H.G.Muller.
If you have questions about WinBoard 4.3.xx, or want to report bugs in it,
report them in the appropriate section of the
<a href="http://www.open-aurec.com/wbforum/">WinBoard forum</a>.
</p>
<h2><a name="2">2. Connection</a></h2>
<p>
An xboard chess engine runs as a separate process from xboard itself,
connected to xboard through a pair of anonymous pipes. The engine
does not have to do anything special to set up these pipes. xboard
sets up the pipes itself and starts the engine with one pipe as its
standard input and the other as its standard output. The engine then
reads commands from its standard input and writes responses to its
standard output. This is, unfortunately, a little more complicated to
do right than it sounds; see <a href="#6">section 6</a> below.
</p>
<p>
And yes, contrary to some people's expectations, exactly the same
thing is true for WinBoard. Pipes and standard input/output are
implemented in Win32 and work fine. You don't have to use DDE, COM,
DLLs, BSOD, or any of the other infinite complexity that
Microsoft has created just to talk between two programs. A WinBoard
chess engine is a Win32 console program that simply reads from its
standard input and writes to its standard output. See sections
<a href="#5">5</a> and <a href="#6">6</a> below for additional details.
</p>
<h2><a name="3">3. Debugging</a></h2>
<p>
To diagnose problems in your engine's interaction with xboard, use the
-debug flag on xboard's command line to see the messages that are
being exchanged. In WinBoard, these messages are written to the file
WinBoard.debug instead of going to the screen.
</p>
<p>
You can turn debug mode on or off while WinBoard is running by
pressing Ctrl+Alt+F12. You can turn debug mode on or off while xboard
is running by binding DebugProc to a shortcut key (and pressing the
key!); see the instructions on shortcut keys in the xboard man page.
</p>
<p>
While your engine is running under xboard/WinBoard, you can send a
command directly to the engine by pressing Shift+1 (xboard) or Alt+1
(WinBoard 4.0.3 and later). This brings up a dialog that you can type
your command into. Press Shift+2 (Alt+2) instead to send to the
second chess engine in Two Machines mode. On WinBoard 4.0.2 and earlier,
Ctrl+Alt is used in place of Alt; this had to be changed due to a conflict
with typing the @-sign on some European keyboards.
</p>
<h2><a name="4">4. How it got this way</a></h2>
<p>
Originally, xboard was just trying to talk to the existing
command-line interface of GNU Chess 3.1+ and 4, which was designed
for people to type commands to. So the communication protocol is very
ad-hoc. It might have been good to redesign it early on, but because
xboard and GNU Chess are separate programs, I didn't want to force
people to upgrade them together to versions that matched. I
particularly wanted to keep new versions of xboard working with old
versions of GNU Chess, to make it easier to compare the play of old
and new gnuchess versions. I didn't foresee the need for a clean
protocol to be used with other chess engines in the future.
</p>
<p>
Circumstances have changed over the years, and now there are many more
engines that work with xboard. I've had to make the protocol
description more precise, I've added some features that GNU Chess
does not support, and I've specified the standard semantics of a few
features to be slightly different from what GNU Chess 4 does.
</p>
<p class="version1">
This release of the protocol specification is the first to carry a
version number of its own -- version 2. Previous releases simply
carried a last-modified date and were loosely tied to specific
releases of xboard and WinBoard. The version number "1" applies
generally to all those older versions of the protocol.
</p>
<p class="version1">
Protocol version 2 remains compatible with older engines but has
several new capabilities. In particular, it adds the
"feature" command, a new mechanism for making backward-compatible
changes and extensions to the protocol. Engines that do not support a
particular new feature do not have to use it; new features are not
enabled unless the engine specifically requests them using the feature
command. If an engine does not send the feature command at all, the
protocol behavior is nearly identical to version 1. Several new
features can be selected by the feature command in version 2,
including the "ping" command (recommended for all engines), the
"setboard" command, and many optional parameters. Additional features
will probably be added in future versions.
</p>
<p class="version43">
If it is necessary to have a separate name,
it would be best to refer to the protocol including the green additions as version 2f.
I really don't think it is a different protocol from version 2, though.
I just tried to clarify some ambiguities in the original definition,
now that the WinBoard 4.3.xx line has implemented them in a specific way.
The hand-shaking protocol for features as defined in protocol 2 perfectly
allows addition of an occasional new features without any need for stepping up the protocol version number,
and I think refraining from the latter would enormously lower the barrier for actual
implementation of these features in engines.
<br />
The two really new things are the engine debug comments, and the "nps" command.
The former merely tries to regulate an extremely common existing pactice
of having engines dump debug messages on WinBoard in an unprotected way,
as usually you get away with that.
</p>
<h2><a name="5">5. WinBoard requires Win32 engines</a></h2>
<p>
Due to some Microsoft brain damage that I don't understand, WinBoard
does not work with chess engines that were compiled to use a DOS
extender for 32-bit addressing. (Probably not with 16-bit DOS or
Windows programs either.) WinBoard works only with engines that are
compiled for the Win32 API. You can get a free compiler that targets
the Win32 API from <a href="http://sources.redhat.com/cygwin/"
>http://sources.redhat.com/cygwin/</a>. I think DJGPP 2.x should also
work if you use the RSXNTDJ extension, but I haven't tried it. Of
course, Microsoft Visual C++ will work. Most likely the other
commercial products that support Win32 will work too (Borland, etc.),
but I have not tried them. Delphi has been successfully used to write
engines for WinBoard; if you want to do this, Tony Werten has donated
some <a href="http://www.tim-mann.org/winboard/delphi.txt" >sample
code</a> that should help you get started.
</p>
<h2><a name="6">6. Hints on input/output</a></h2>
<p>
Beware of using buffered I/O in your chess engine. The C stdio
library, C++ streams, and the I/O packages in most other languages use
buffering both on input and output. That means two things. First,
when your engine tries to write some characters to xboard, the library
stashes them in an internal buffer and does not actually write them to
the pipe connected to xboard until either the buffer fills up or you
call a special library routine asking for it to be flushed. (In C
stdio, this routine is named <tt>fflush</tt>.) Second, when your engine tries
to read some characters from xboard, the library does not read just
the characters you asked for -- it reads all the characters that are
currently available (up to some limit) and stashes any characters you
are not yet ready for in an internal buffer. The next time you ask to
read, you get the characters from the buffer (if any) before the
library tries to read more data from the actual pipe.
</p>
<p>
Why does this cause problems? First, on the output side, remember
that your engine produces output in small quantities (say, a few
characters for a move, or a line or two giving the current analysis),
and that data always needs to be delivered to xboard/WinBoard for
display immediately. If you use buffered output, the data you print
will sit in a buffer in your own address space instead of being
delivered.
</p>
<p>
You can usually fix the output buffering problem by asking for the
buffering to be turned off. In C stdio, you do this by calling
<tt>setbuf(stdout, NULL)</tt>. A more laborious and error-prone
method is to carefully call <tt>fflush(stdout)</tt> after every line
you output; I don't recommend this. In C++, you can try
<tt>cout.setf(ios::unitbuf)</tt>, which is documented in current
editions of "The C++ Programming Language," but not older ones.
Another C++ method that might work is
<tt>cout.rdbuf()->setbuf(NULL, 0)</tt>. Alternatively, you can
carefully call <tt>cout.flush()</tt> after every line you output;
again, I don't recommend this.
</p>
<p>
Another way to fix the problem is to use unbuffered operating system
calls to write directly to the file descriptor for standard output.
On Unix, this means <tt>write(1, ...)</tt> -- see the man page for write(2).
On Win32, you can use either the Unix-like <tt>_write(1, ...)</tt> or Win32
native routines like <tt>WriteFile</tt>.
</p>
<p>
Second, on the input side, you are likely to want to poll during your
search and stop it if new input has come in. If you implement
pondering, you'll need this so that pondering stops when the user
makes a move. You should also poll during normal thinking on your
move, so that you can implement the "?" (move now) command, and so
that you can respond promptly to a "result", "force", or "quit"
command if xboard wants to end the game or terminate your engine.
Buffered input makes polling more complicated -- when you poll, you
must stop your search if there are <em>either</em> characters in the buffer
<em>or</em> characters available from the underlying file descriptor.
</p>
<p>
The most direct way to fix this problem is to use unbuffered operating
system calls to read (and poll) the underlying file descriptor
directly. On Unix, use <tt>read(0, ...)</tt> to read from standard input, and
use <tt>select()</tt> to poll it. See the man pages read(2) and select(2).
(Don't follow the example of GNU Chess 4 and use the FIONREAD ioctl to
poll for input. It is not very portable; that is, it does not exist
on all versions of Unix, and is broken on some that do have it.) On
Win32, you can use either the Unix-like <tt>_read(0, ...)</tt> or the native
Win32 <tt>ReadFile()</tt> to read. Unfortunately, under Win32, the function to
use for polling is different depending on whether the input device is
a pipe, a console, or something else. (More Microsoft brain damage
here -- did they never hear of device independence?) For pipes, you
can use <tt>PeekNamedPipe</tt> to poll (even when the pipe is unnamed).
For consoles,
you can use <tt>GetNumberOfConsoleInputEvents</tt>. For sockets only, you can
use <tt>select()</tt>. It might be possible to use
<tt>WaitForSingleObject</tt> more
generally, but I have not tried it. Some code to do these things can
be found in Crafty's utility.c, but I don't guarantee that it's all
correct or optimal.
</p>
<p>
A second way to fix the problem might be to ask your I/O library not
to buffer on input. It should then be safe to poll the underlying
file descriptor as described above. With C, you can try calling
<tt>setbuf(stdin, NULL)</tt>. However, I have never tried this. Also, there
could be problems if you use <tt>scanf()</tt>, at least with certain patterns,
because <tt>scanf()</tt> sometimes needs to read one extra character and "push
it back" into the buffer; hence, there is a one-character pushback
buffer even if you asked for stdio to be unbuffered. With C++, you
can try <tt>cin.rdbuf()->setbuf(NULL, 0)</tt>, but again, I have never tried
this.
</p>
<p>
A third way to fix the problem is to check whether there are
characters in the buffer whenever you poll. C I/O libraries generally
do not provide any portable way to do this. Under C++, you can use
<tt>cin.rdbuf()->in_avail()</tt>. This method has been reported to
work with
EXchess. Remember that if there are no characters in the buffer, you
still have to poll the underlying file descriptor too, using the
method described above.
</p>
<p>
A fourth way to fix the problem is to use a separate thread to read
from stdin. This way works well if you are familiar with thread
programming. This thread can be blocked waiting for input to come in
at all times, while the main thread of your engine does its thinking.
When input arrives, you have the thread put the input into a buffer
and set a flag in a global variable. Your search routine then
periodically tests the global variable to see if there is input to
process, and stops if there is. WinBoard and my Win32 ports of ICC
timestamp and FICS timeseal use threads to handle multiple input
sources.
</p>
<h2><a name="7">7. Signals</a></h2>
<p>Engines that run on Unix need to be concerned with two Unix
signals: <tt>SIGTERM</tt> and <tt>SIGINT</tt>. This applies both to
engines that run under xboard and (the unusual case of) engines that
WinBoard remotely runs on a Unix host using the -firstHost or
-secondHost feature. It does not apply to engines that run on
Windows, because Windows does not have Unix-style signals.
<span class="version1">
Beginning with version 2, you can now turn off the use of
either or both
signals. See the "feature" command in <a href="#6">section 9</a> below.
</span>
</p>
<p>First, when an engine is sent the "quit" command, it is also given
a <tt>SIGTERM</tt> signal shortly afterward to make sure it goes away.
If your engine reliably responds to "quit", and the signal causes
problems for you, you should either ignore it by calling
<tt>signal(SIGTERM, SIG_IGN)</tt> at the start of your program,
or disable it with the "feature" command.</p>
<p>Second, xboard will send an interrupt signal (<tt>SIGINT</tt>) at
certain times when it believes the engine may not be listening to user
input (thinking or pondering). WinBoard currently does this only when
the engine is running remotely using the -firstHost or -secondHost
feature, not when it is running locally. You probably need to know
only enough about this grungy feature to keep it from getting in your
way.
</p>
<p>
The <tt>SIGINT</tt>s are basically tailored to the needs of GNU Chess 4
on systems where its input polling code is broken or disabled.
Because they work in a rather peculiar way, it is recommended that you
either ignore <tt>SIGINT</tt> by having your engine call
<tt>signal(SIGINT, SIG_IGN)</tt>, or disable it with the "feature"
command.</p>
<p>
Here are details for the curious. If xboard needs to send a command
when it is the chess engine's move (such as before the "?" command),
it sends a <tt>SIGINT</tt> first. If xboard needs to send commands when it is
not the chess engine's move, but the chess engine may be pondering
(thinking on its opponent's time) or analyzing (analysis or analyze
file mode), xboard sends a <tt>SIGINT</tt> before the first such command only.
Another <tt>SIGINT</tt> is not sent until another move is made, even if xboard
issues more commands. This behavior is necessary for GNU Chess 4. The
first <tt>SIGINT</tt> stops it from pondering until the next move, but on some
systems, GNU Chess 4 will die if it receives a <tt>SIGINT</tt> when not
actually thinking or pondering.
</p>
<p>
There are two reasons why WinBoard does not send the Win32 equivalent
of <tt>SIGINT</tt> (which is called <tt>CTRL_C_EVENT</tt>) to local
engines. First, the Win32 GNU Chess 4 port does not need it. Second, I
could not find a way to get it to work. Win32 seems to be designed
under the assumption that only console applications, not windowed
applications, would ever want to send a <tt>CTRL_C_EVENT</tt>.
</p>
<h2><a name="8">8. Commands from xboard to the engine</a></h2>
<p>
All commands from xboard to the engine end with a newline (\n), even
where that is not explicitly stated. All your output to xboard must
be in complete lines; any form of prompt or partial line will cause
problems.
</p>
<p>
At the beginning of each game, xboard sends an initialization string.
This is currently "new\nrandom\n" unless the user changes it with the
initString or secondInitString option.
</p>
<p>
xboard normally reuses the same chess engine process for multiple
games. At the end of a game, xboard will send the "force" command
(see below) to make sure your engine stops thinking about the current
position. It will later send the initString again to start a new
game. If your engine can't play multiple games, you can disable reuse
<span class="version1">
either with the "feature" command (beginning in protocol version
2; see below) or
</span>
with xboard's -xreuse (or -xreuse2) command line
option. xboard will then ask the process to quit after each game and
start a new process for the next game.
</p>
<dl>
<dt>xboard</dt>
<dd>This command will be sent once immediately after your engine
process is started. You can use it to put your engine into "xboard
mode" if that is needed. If your engine prints a prompt to ask for
user input, you must turn off the prompt and output a newline when the
"xboard" command comes in.
</dd>
<dt class="version1">protover N</dt>
<dd class="version1">
<p>Beginning in protocol version 2 (in which N=2), this command will
be sent immediately after the "xboard" command. If you receive some
other command immediately after "xboard" (such as "new"), you can
assume that protocol version 1 is in use. The "protover" command is
the only new command that xboard always sends in version 2. All other
new commands to the engine are sent only if the engine first enables
them with the "feature" command. Protocol versions will always be
simple integers so that they can easily be compared.
</p>
<p>Your engine should reply to the protover command by sending the
"feature" command (see below) with the list of non-default feature
settings that you require, if any.</p>
<p>Your engine should never refuse to run due to receiving a higher
protocol version number than it is expecting! New protocol versions
will always be compatible with older ones by default; the larger
version number is simply a hint that additional "feature" command
options added in later protocol versions may be accepted.
</p>
</dd>
<dt class="version1">accepted</dt>
<dt class="version1">rejected</dt>
<dd class="version1">
These commands may be sent to your engine in reply to the "feature"
command; see its documentation below.
</dd>
<dt>new</dt>
<dd>Reset the board to the standard chess starting position. Set
White on move. Leave force mode and set the engine to play Black.
Associate the engine's clock with Black and the opponent's clock with
White. Reset clocks and time controls to the start of a new game.
Use wall clock for time measurement.
Stop clocks. Do not ponder on this move, even if pondering is on.
Remove any search depth limit previously set by the sd command.
</dd>
<dt>variant VARNAME</dt>
<dd>If the game is not standard chess, but a variant, this command is
sent after "new" and before the first move or "edit" command. Currently
defined variant names are:
<table>
<tr><td>wildcastle</td><td>Shuffle chess where king can castle from d file</td></tr>
<tr><td>nocastle</td><td>Shuffle chess with no castling at all</td></tr>
<tr><td>fischerandom</td><td>Fischer Random</td></tr>
<tr><td>bughouse</td><td>Bughouse, ICC/FICS rules</td></tr>
<tr><td>crazyhouse</td><td>Crazyhouse, ICC/FICS rules</td></tr>
<tr><td>losers</td><td>Win by losing all pieces or getting mated (ICC)</td></tr>
<tr><td>suicide</td><td>Win by losing all pieces including king,
or by having fewer pieces when one player has no legal moves (FICS)</td></tr>
<tr class="version1"><td>giveaway</td><td>Win by losing all pieces including king,
or by having no legal moves (ICC)</td></tr>
<tr><td>twokings</td><td>Weird ICC wild 9</td></tr>
<tr><td>kriegspiel</td><td>Kriegspiel (engines not supported)</td></tr>
<tr><td>atomic</td><td>Atomic</td></tr>
<tr><td>3check</td><td>Win by giving check 3 times</td></tr>
<tr class="version43"><td>xiangqi </td><td>Chinese Chess (9x10 board)</td></tr>
<tr class="version43"><td>shogi </td><td>Japanese Chess (9x9 bord)</td></tr>
<tr class="version43"><td>capablanca</td><td>Capablanca Chess (10x8 board, with Archbishop and Chancellor)</td></tr>
<tr class="version43"><td>gothic </td><td>Gothic Chess (10x8 board, same with better opening setup)</td></tr>
<tr class="version43"><td>falcon </td><td>Falcon Chess (10x8 board, with two Falcon pieces)</td></tr>
<tr class="version43"><td>shatranj </td><td>ancient Arabic Chess, with Elephants and General in stead of B and Q</td></tr>
<tr class="version43"><td>courier </td><td>Courier Chess (12x8 board, a medieval precursor of modern Chess</td></tr>
<tr class="version43"><td>knightmate </td><td>King moves as Knight and vice versa</td></tr>
<tr class="version43"><td>berolina</td><td> Pawns capture straight ahead, and move diagonally</td></tr>
<tr class="version43"><td>janus</td><td> Janus Chess (10x8, with two Archbishops)</td></tr>
<tr class="version43"><td>caparandom </td><td>shuffle variant like FRC (10x8 board)</td></tr>
<tr class="version43"><td>cylinder </td><td>Pieces wrap around between side edges, like board is a cylinder</td></tr>
<tr class="version44"><td>super </td><td>Superchess: a shuffle variant with 4 fairy pieces on 8x8 board</td></tr>
<tr class="version44"><td>great </td><td>Great Shatranj: sliders are replaced by corresponding short-range pieces on a 10x8 board</td></tr>
<tr class="version48"><td>lion </td><td>Mighty-Lion Chess, with a super-knight, more powerful than a Queen</td></tr>
<tr class="version48"><td>elven </td><td>Elven Chess: hybrid between Chess and Chu Shogi on 10x10 board</td></tr>
<tr class="version48"><td>chu </td><td>Chu Shogi: Edo-period Japanese Chess on a 12x12 board</td></tr>
<tr><td>unknown</td><td>Unknown variant (not supported)</td></tr>
</table>
<span class="version48">As of XBoard 4.8, engines can define arbitrary variant names; see the "feature" and "setup" commands in <a href="#9">section 9</a>.</span>
</dd>
<dt>quit</dt>
<dd>The chess engine should immediately exit. This command is used
when xboard is itself exiting, and also between games if the -xreuse
command line option is given (or -xreuse2 for the second engine).
See also <a href="#7">Signals</a> above.
</dd>
<dt>random</dt>
<dd>This command is specific to GNU Chess 4. You can either ignore it
completely (that is, treat it as a no-op) or implement it as GNU Chess
does. The command toggles "random" mode (that is, it sets random =
!random). In random mode, the engine adds a small random value to its
evaluation function to vary its play. The "new" command sets random
mode off.
</dd>
<dt>force</dt>
<dd>Set the engine to play neither color ("force mode"). Stop clocks.
The engine should check that moves received in force mode are legal
and made in the proper turn, but should not think, ponder, or make
moves of its own.
</dd>
<dt>go</dt>
<dd>Leave force mode and set the engine to play the color that is on
move. Associate the engine's clock with the color that is on move,
the opponent's clock with the color that is not on move. Start the engine's
clock. Start thinking and eventually make a move.
</dd>
<dt class="version1">playother</dt>
<dd class="version1">
(This command is new in protocol version 2. It is not
sent unless you enable it with the feature command.)
Leave force mode and set the engine to play the color that is <i>not</i> on
move. Associate the opponent's clock with the color that is on move,
the engine's clock with the color that is not on move. Start the opponent's
clock. If pondering is enabled, the engine should begin pondering.
If the engine later receives a move, it should start thinking and eventually
reply.
</dd>
<dt>white</dt>
<dd>
<p><span class="version1">
(This command is obsolete as of protocol version 2, but is still
sent in some situations to accommodate older engines unless you disable it
with the feature command.)
</span>
Set White on move. Set the engine to play Black. Stop clocks.
</p>
</dd>
<dt>black </dt>
<dd>
<span class="version1">
(This command is obsolete as of protocol version 2, but is still
sent in some situations to accommodate older engines unless you disable it
with the feature command.)
</span>
Set Black on move. Set the engine to play White. Stop clocks.
</dd>
<dt>level MPS BASE INC</dt>
<dd>Set time controls. See the <a href="#11">Time Control</a> section below.
</dd>
<dt>st TIME</dt>
<dd>Set time controls. See the <a href="#11">Time Control</a> section
below.
</dd>
<dt>sd DEPTH</dt>
<dd>
<p>The engine should limit its thinking to DEPTH ply.
<span class="version43">The commands "level" or "st" and "sd" can be used together in an orthogonal way.
If both are issued, the engine should observe both limitations:</span>
In the protocol, the "sd" command isn't a time control. It doesn't
say that your engine has unlimited time but must search to exactly the
given depth. It says that you should pay attention to the time
control as normal, but cut off the search at the specified depth even
if you have time to search deeper. If you don't have time to search
to the specified depth, given your normal time management algorithm,
then you will want to stop sooner than the given depth.
</p><p>
The "new" command should set the search depth back to unlimited. This
is already stated in the spec. The "level" command should not affect
the search depth. As it happens, xboard/WinBoard currently always
sends sd (if needed) right after level, but that isn't part of the
spec.</p>
</dd>
<dt><span class="version43">nps NODE_RATE</span></dt>
<dd><span class="version43">The engine should not use wall-clock time to make its timing decisions,
but an own internal time measure based on the number of nodes it has searched
(and will report as "thinking output", see <a href="#10">section 10</a>),
converted to seconds through dividing by the given NODE_RATE.
Example: after receiving the commands "st 8" and "nps 10000",
the engine should never use more that 80,000 nodes in the search for any move.
In this mode, the engine should report user CPU time used (in its thinking output),
rather than wall-clock time.
This even holds if NODE_RATE is given as 0,
but in that case it should also use the user CPU time for its timing decisions.
The effect of an "nps" command should persist until the next "new" command.
</span>
</dd>
<dt>time N</dt>
<dd>Set a clock that always belongs to the engine. N is a number in
centiseconds (units of 1/100 second). Even if the engine changes to
playing the opposite color, this clock remains with the engine.
</dd>
<dt>otim N</dt>
<dd><p>Set a clock that always belongs to the opponent. N is a number in
centiseconds (units of 1/100 second). Even if the opponent changes to
playing the opposite color, this clock remains with the opponent.
</p><p>
If needed for purposes of board display in force mode (where the
engine is not participating in the game) the time clock should be
associated with the last color that the engine was set to play, the
otim clock with the opposite color.
</p>
<p>
<span class="version43">This business of "clocks remaining with the engine" is apparently so ambiguous
that many engines implement it wrong.
The clocks in fact always remain with the color.
Which clock reading is relayed with "time", and which by "otim", is determined by which side the engine plays.
Note that the way the clocks operate and receive extra time (in accordance with the selected time control)
is not affected in any way by which moves are made by the engine, which by the opponent, and which were forced.
</span>
</p>
<p>
<span class="version1">
Beginning in protocol version 2, if you can't handle the time and
otim commands, you can use the "feature" command to disable them; see
below.
</span>
The following techniques from older protocol versions also
work: You can ignore the time and otim commands (that is, treat them
as no-ops), or send back "Error (unknown command): time" the first
time you see "time".
</p></dd>
<dt>MOVE</dt>
<dd>
<p>See below for the syntax of moves. If the move is illegal, print
an error message; see the section "<a href="#9">Commands from the engine to
xboard</a>". If the move is legal and in turn, make it. If not in force
mode, stop the opponent's clock, start the engine's clock, start
thinking, and eventually make a move.
</p><p>
When xboard sends your engine a move, it normally sends coordinate
algebraic notation. Examples:
</p>
<table>
<tr><td>Normal moves:</td><td>e2e4</td></tr>
<tr><td>Pawn promotion:</td><td>e7e8q</td></tr>
<tr><td>Castling:</td><td>e1g1, e1c1, e8g8, e8c8</td></tr>
<tr><td>Bughouse/crazyhouse drop:</td><td>P@h3</td></tr>
<tr><td>ICS Wild 0/1 castling:</td><td>d1f1, d1b1, d8f8, d8b8</td></tr>
<tr><td>FischerRandom castling:</td><td>O-O, O-O-O (oh, not zero)</td></tr>
<tr class="version48"><td>Multi-leg move:</td><td>c4d5,d5e4 (legs separated by comma)</td></tr>
<tr class="version48"><td>Null move:</td><td>@@@@</td></tr>
</table>
<p class="version43">
Note that on boards with <span class="version48">exactly 10</span> ranks, counting of the ranks starts at 0.
</p>
<p class="version1">
Beginning in protocol version 2, you can use the feature command
to select SAN (standard algebraic notation) instead; for example, e4,
Nf3, exd5, Bxf7+, Qxf7#, e8=Q, O-O, or P@h3. Note that the last form,
P@h3, is a extension to the PGN standard's definition of SAN, which does
not support bughouse or crazyhouse.
</p>
<p>
xboard doesn't reliably detect illegal moves, because it does not keep
track of castling unavailability due to king or rook moves, or en
passant availability. If xboard sends an illegal move, send back an
error message so that xboard can retract it and inform the user; see
the section "<a href="#9">Commands from the engine to xboard</a>".
</p>
</dd>
<dt class="version1">usermove MOVE</dt>
<dd class="version1">
By default, moves are sent to the engine without a command name;
the notation is just sent as a line by itself.
Beginning in protocol version 2, you can use the feature command
to cause the command name "usermove" to be sent before the move.
Example: "usermove e2e4".
</dd>
<dt>?</dt>
<dd><p>Move now. If your engine is thinking, it should move immediately;
otherwise, the command should be ignored (treated as a no-op). It
is permissible for your engine to always ignore the ? command. The
only bad consequence is that xboard's Move Now menu command will do
nothing.
</p><p>
It is also permissible for your engine to move immediately if it gets
any command while thinking, as long as it processes the command right
after moving, but it's preferable if you don't do this. For example,
xboard may send post, nopost, easy, hard, force, quit,
<span class="version1">
or other commands
</span>
while the engine is on move.
</p>
</dd>
<dt class="version1">ping N</dt>
<dd class="version1">
<p>In this command, N is a decimal number. When you receive the command,
reply by sending the string <strong>pong N</strong>, where N is the
same number you received. Important: You must not reply to a "ping"
command until you have finished executing all commands that you
received before it. Pondering does not count; if you receive a ping
while pondering, you should reply immediately and continue pondering.
Because of the way xboard uses the ping command, if you implement the
other commands in this protocol, you should never see a "ping" command
when it is your move; however, if you do, you must not send the "pong"
reply to xboard until after you send your move. For example, xboard
may send "?" immediately followed by "ping". If you implement the "?"
command, you will have moved by the time you see the subsequent ping
command. Similarly, xboard may send a sequence like "force", "new",
"ping". You must not send the pong response until after you have
finished executing the "new" command and are ready for the new game to
start.
</p>
<p>
The ping command is new in protocol version 2 and will not be sent
unless you enable it with the "feature" command. Its purpose is to
allow several race conditions that could occur in previous versions of
the protocol to be fixed, so it is highly recommended that you
implement it. It is especially important in simple engines that do
not ponder and do not poll for input while thinking, but it is needed in all
engines.
</p>
</dd>
<dt>draw</dt>
<dd>The engine's opponent offers the engine a draw. To accept the
draw, send "offer draw". To decline, ignore the offer (that is, send
nothing). If you're playing on ICS, it's possible for the draw offer
to have been withdrawn by the time you accept it, so don't assume the
game is over because you accept a draw offer. Continue playing until
xboard tells you the game is over. See also "offer draw" below.
</dd>
<dt>result RESULT {COMMENT}</dt>
<dd>After the end of each game, xboard will send you a result command.
You can use this command to trigger learning. RESULT is either 1-0,
0-1, 1/2-1/2, or *, indicating whether white won, black won, the game
was a draw, or the game was unfinished. The COMMENT string is purely
a human-readable comment; its content is unspecified and subject to
change. In ICS mode, it is passed through from ICS uninterpreted.
Example: <pre>result 1-0 {White mates}</pre>
<p>
Here are some notes on interpreting the "result" command. Some apply
only to playing on ICS ("Zippy" mode).
</p>
<p>
If you won but did not just play a mate, your opponent must have
resigned or forfeited. If you lost but were not just mated, you
probably forfeited on time, or perhaps the operator resigned manually.
If there was a draw for some nonobvious reason, perhaps your opponent
called your flag when he had insufficient mating material (or vice
versa), or perhaps the operator agreed to a draw manually.
</p>
<p>
You will get a result command even if you already know the game ended
-- for example, after you just checkmated your opponent. In fact, if
you send the "RESULT {COMMENT}" command (discussed below), you will
simply get the same thing fed back to you with "result" tacked in
front. You might not always get a "result *" command, however. In
particular, you won't get one in local chess engine mode when the user
stops playing by selecting Reset, Edit Game, Exit or the like.
</p>
</dd>
<dt><span class="version1">setboard FEN</span></dt>
<dd>
<p><span class="version1">
The setboard command is the new way to set up positions, beginning
in protocol version 2. It is not used unless it has been selected
with the feature command. Here FEN is a position in Forsythe-Edwards
Notation, as defined in the PGN standard.</span>
<span class="version43">Note that this PGN standard referred to here
only applies to normal Chess;
Obviously in variants that cannot be described by a FEN for normal Chess,
e.g. because the board is not 8x8, other pieces then PNBRQK participate,
there are holdings that need to be specified, etc.,
xboard will use a FEN format that is standard or suitable for that variant.
In particular, in FRC or CRC, WinBoard will use Shredder-FEN or X-FEN standard,
i.e. it can use the rook-file indicator letter to represent a castling right
(like HAha) whenever it wants, but if it uses KQkq, this will always refer
to the outermost rook on the given side.</span>
</p>
<p class="version1">
<em>Illegal positions:</em> Note that either setboard or edit can
be used to send an illegal position to the engine. The user can
create any position with xboard's Edit Position command (even, say,
an empty board, or a board with 64 white kings and no black ones).
If your engine receives a position that it considers illegal,
I suggest that you send the response "tellusererror Illegal position",
and then respond to any attempted move with "Illegal move" until
the next new, edit, or setboard command.
</p>
</dd>
<dt>edit</dt>
<dd>
<p><span class="version1">
The edit command is the old way to set up positions. For compatibility
with old engines, it is still used by default, but new engines may prefer
to use the feature command (see below) to cause xboard to use setboard instead.
</span>
The edit command puts the chess engine into a special mode, where
it accepts the following subcommands:</p>
<table>
<tr><td>c</td><td>change current piece color, initially white</td></tr>
<tr><td>Pa4 (for example)</td><td>place pawn of current color on a4</td></tr>
<tr><td>xa4 (for example)</td><td>empty the square a4 (not used by xboard)</td></tr>
<tr><td>#</td><td>clear board</td></tr>
<tr><td>.</td><td>leave edit mode</td></tr>
</table>
<p class="version1">
See the Idioms section below for additional subcommands used in
ChessBase's implementation of the protocol.
</p>
<p>The edit command does not change the side to move. To set up a
black-on-move position, xboard uses the following command sequence:
</p>
<pre>
new
force
a2a3
edit
<edit commands>
.
</pre>
<p>
This sequence is used to avoid the "black" command, which is now
considered obsolete and which many engines never did implement as
specified in this document.
</p>
<p>
After an edit command is complete, if a king and a rook are on their
home squares, castling is assumed to be available to them. En passant
capture is assumed to be illegal on the current move regardless of the
positions of the pawns. The clock for the 50 move rule starts at
zero, and for purposes of the draw by repetition rule, no prior
positions are deemed to have occurred.
<span class="version43">
In FRC or CRC, any rook and king put on the back rank should be considered to
have castling rights, even if it later becomes apparent that they cannot be both in the
initial position, because the position just set up is asymmetric.
It is upto WinBoard to find work-around in cases where this is not desired,
similar to the "black kludge" shown above, by setting up an earlier position,
and then do a move to destroy castling rights or create e.p. rights.
(Don't bet your life on it...)
</span>
</p>
</dd>
<dt>hint</dt>
<dd>If the user asks for a hint, xboard sends your engine the command
"hint". Your engine should respond with "Hint: xxx", where xxx is a
suggested move. If there is no move to suggest, you can ignore the
hint command (that is, treat it as a no-op).
</dd>
<dt>bk</dt>
<dd>If the user selects "Book" from the xboard menu, xboard will send
your engine the command "bk". You can send any text you like as the
response, as long as each line begins with a blank space or tab (\t)
character, and you send an empty line at the end. The text pops up in
a modal information dialog.
</dd>
<dt>undo</dt>
<dd>If the user asks to back up one move, xboard will send you the
"undo" command. xboard will not send this command without putting you
in "force" mode first, so you don't have to worry about what should
happen if the user asks to undo a move your engine made. (GNU Chess 4
actually switches to playing the opposite color in this case.)
</dd>
<dt>remove</dt>
<dd>If the user asks to retract a move, xboard will send you the
"remove" command. It sends this command only when the user is on
move. Your engine should undo the last two moves (one for each
player) and continue playing the same color.
</dd>
<dt>hard</dt>
<dd>Turn on pondering (thinking on the opponent's time, also known as
"permanent brain"). xboard will not make any assumption about what
your default is for pondering or whether "new" affects this setting.
</dd>
<dt>easy</dt>
<dd>Turn off pondering.</dd>
<dt>post</dt>
<dd>Turn on thinking/pondering output.
See <a href="#10">Thinking Output</a> section.</dd>
<dt>nopost</dt>
<dd>Turn off thinking/pondering output.</dd>
<dt>analyze</dt>
<dd>Enter analyze mode. See <a href="#12">Analyze Mode</a> section.</dd>
<dt>name X </dt>
<dd>This command informs the engine of its
opponent's name. When the engine is playing on a chess server, xboard
obtains the opponent's name from the server.
<span class="version1">
When the engine is
playing locally against a human user, xboard obtains the user's login
name from the local operating system. When the engine is playing
locally against another engine, xboard uses either the other engine's
filename or the name that the other engine supplied in the myname
option to the feature command. By default, xboard uses the name
command only when the engine is playing on a chess server. Beginning
in protocol version 2, you can change this with the name option to the
feature command; see below.
</span>
</dd>
<dt>rating</dt>
<dd>In ICS mode, xboard obtains the ICS opponent's rating from the
"Creating:" message that appears before each game. (This message may
not appear on servers using outdated versions of the FICS code.) In
Zippy mode, it sends these ratings on to the chess engine using the
"rating" command. The chess engine's own rating comes first, and if
either opponent is not rated, his rating is given as 0.
<span class="version1">
In the future this command may also be used in other modes, if ratings
are known.
</span>