-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMANUAL.html
More file actions
1118 lines (815 loc) · 61.9 KB
/
MANUAL.html
File metadata and controls
1118 lines (815 loc) · 61.9 KB
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
<head>
<link rel=stylesheet type=text/css href="/usr/local/lib/ruby/gems/2.0.0/gems/github-markdown-preview-1.5.0/lib/github-markdown-preview/../../data/css/github.css">
<link rel=stylesheet type=text/css href="/usr/local/lib/ruby/gems/2.0.0/gems/github-markdown-preview-1.5.0/lib/github-markdown-preview/../../data/css/github2.css">
<style type="text/css">
html, .markdown-body {
overflow: inherit;
}
.markdown-body h1 {
margin-top: 0;
}
</style>
</head>
<body class="markdown-body" style="padding:20px;">
<div id="slider">
<div class="frames">
<div class="frame frame-center">
<h1>Chess Toolkit Manual</h1>
<p>Chess Toolkit is a software library for creating chess programs. It is written in C with an object-oriented API, so it is straightforward to use on its own or to extend other languages such as Ruby.</p>
<p>The Chess Toolkit is a work in progress, but it is quite useful in its current state. It understands the rules of chess, Forsyth-Edwards Notation (FEN), Short Algebraic Notation (SAN), and the basics of Portable Game Notation (PGN). With the current features, you can do a lot. For example, the author has used it to write software that parses millions of games, collecting statistics on openings and scoring each position based on the game results and player ratings. Hopefully, you will find it useful too.</p>
<p>This document is the Chess Toolkit's primary documentation, though many questions can also be answered by exploring the comprehensive unit test suite in the tests directory, or by reviewing the library's code in the lib directory. This document is organized into the following sections:</p>
<ul>
<li>License</li>
<li>Installation</li>
<li>Example</li>
<li>Data Types</li>
<li>Memory Management</li>
<li>Functions</li>
<li>User Stories</li>
<li>Future Work</li>
</ul><h2>License</h2>
<p>Chess Toolkit: a software library for creating chess programs<br>
Copyright (C) 2013 Steve Ortiz</p>
<p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a></p>
<p>Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.</p>
<h2>Installation</h2>
<p>The Chess Toolkit uses the standard GNU autoconf tools, so you can build from source by running</p>
<pre><code>./configure
make
sudo make install
</code></pre>
<p>Optionally, if you wish to run the unit tests and have GNU check installed, you can run</p>
<pre><code>make check
</code></pre>
<p>To run the long version of the unit tests, which executes more <a href="http://chessprogramming.wikispaces.com/Perft">perfts</a>, you can first run </p>
<pre><code>export CT_CHECK_LONG=1
make check
</code></pre>
<p>See INSTALL for all the usual autconf installation details.</p>
<h2>Example</h2>
<p>The following example is included in examples/filter_position/filter_position.c. It's a simple program you might use to filter a PGN file, printing out only those games that include the position reached by the movetext you pass in. For instance, you can command <code>./filter_position "e4 c5" < ../../tests/candidates2013.pgn</code> to see those games where the Sicilian Defense was played. This program is only meant to demonstrate the basics of the Chess Toolkit API. If you were trying to filter millions of games, this program would eventually get the job done, but hashing every position from every game is not a computationally efficient approach.</p>
<div class="highlight"><pre><span class="cm">/* This is the only header you need to include for access to the Chess Toolkit API. */</span>
<span class="cp">#include "chess_toolkit.h"</span>
<span class="cp">#include <stdio.h></span>
<span class="cp">#include <stdlib.h></span>
<span class="cm">/* Notice, there are a few variable types pulled in from chess_toolkit.h here... */</span>
<span class="k">typedef</span> <span class="k">struct</span> <span class="n">MyDataStruct</span>
<span class="p">{</span>
<span class="kt">int64_t</span> <span class="n">hash</span><span class="p">;</span> <span class="cm">/* chess_toolkit.h includes stdint.h, which defines int64_t among others */</span>
<span class="n">CtGraph</span> <span class="n">graph</span><span class="p">;</span> <span class="cm">/* CtGraph is probably the most useful data type provided by the Chess Toolkit */</span>
<span class="n">CtGameTags</span> <span class="n">game_tags</span><span class="p">;</span> <span class="cm">/* CtGameTags stores PGN tag pairs */</span>
<span class="kt">bool</span> <span class="n">is_a_match</span><span class="p">;</span> <span class="cm">/* chess_toolkit.h also includes stdbool.h which defines bool */</span>
<span class="p">}</span> <span class="n">MyDataStruct</span><span class="p">;</span>
<span class="kt">void</span> <span class="nf">filter_game</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">delegate</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">filter_position</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">delegate</span><span class="p">,</span> <span class="n">CtMove</span> <span class="n">move</span><span class="p">);</span>
<span class="kt">int</span>
<span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">char</span> <span class="o">*</span><span class="n">move_text</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
<span class="kt">char</span> <span class="o">*</span><span class="n">filename</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span> <span class="cm">/* optional argument */</span>
<span class="n">CtCommand</span> <span class="n">pgn_command</span><span class="p">;</span> <span class="cm">/* CtCommand, CtMoveCommand, CtPieceCommand all follow the Command design pattern. */</span>
<span class="n">MyDataStruct</span> <span class="n">my_data_struct</span><span class="p">;</span>
<span class="n">CtGraph</span> <span class="n">temp_graph</span><span class="p">;</span>
<span class="kt">FILE</span> <span class="o">*</span><span class="n">file</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="kt">char</span> <span class="n">error_message</span><span class="p">[</span><span class="n">CT_GRAPH_FROM_PGN_ERROR_MESSAGE_MAX_LENGTH</span><span class="p">];</span> <span class="cm">/* space to store an error message */</span>
<span class="kt">int</span> <span class="n">exit_status</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">argc</span> <span class="o"><</span> <span class="mi">2</span> <span class="o">||</span> <span class="n">argc</span> <span class="o">></span> <span class="mi">3</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Usage: filter_position </span><span class="se">\"</span><span class="s"><move text></span><span class="se">\"</span><span class="s"> [filename]</span><span class="se">\n</span><span class="s">"</span>
<span class="s">" for example: ./filter_position </span><span class="se">\"</span><span class="s">e4 c5</span><span class="se">\"</span><span class="s"> < ../../tests/candidates2013.pgn</span><span class="se">\n</span><span class="s">"</span>
<span class="s">" or ./filter_position </span><span class="se">\"</span><span class="s">1. e4 c5</span><span class="se">\"</span><span class="s"> ../../tests/candidates2013.pgn</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="cm">/* You must initialize the chess_toolkit before doing anything with it. */</span>
<span class="n">chess_toolkit_init</span><span class="p">();</span>
<span class="cm">/* A graph lets you navigate from one position to the next, storing the chess moves as you go. */</span>
<span class="cm">/* ct_graph_from_pgn will fill in a graph you provide, or give give you a shared graph (as shown here). Be careful if</span>
<span class="cm"> you use the shared graph; this graph is shared by any code calling the same function. So it's not a good idea for</span>
<span class="cm"> multi-threaded applications; but it is convenient for simple programs because it does not have to be free'd. In</span>
<span class="cm"> fact, free'ing it causes problems, so please don't. Only free things created with "new" as you'll see later. */</span>
<span class="n">temp_graph</span> <span class="o">=</span> <span class="n">ct_graph_from_pgn</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">move_text</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">temp_graph</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="cm">/* ct_graph_from_pgn will return 0 if there was an error with the PGN such as an invalid move. */</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"Error: could not parse move_text.</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">argc</span> <span class="o">==</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">file</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">"r"</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">file</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"Error: could not open %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">filename</span><span class="p">);</span>
<span class="n">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="n">file</span> <span class="o">=</span> <span class="n">stdin</span><span class="p">;</span>
<span class="cm">/* You could get a copy of the CtPosition object from graph, and the call ct_position_hash, but for convenience, the</span>
<span class="cm"> there is a ct_graph_position_hash method which returns the hash of the graph's current position. */</span>
<span class="n">my_data_struct</span><span class="p">.</span><span class="n">hash</span> <span class="o">=</span> <span class="n">ct_graph_position_hash</span><span class="p">(</span><span class="n">temp_graph</span><span class="p">);</span>
<span class="cm">/* Earlier, you saw ct_graph_from_pgn can return a shared graph. Next time it's called, it will be with a dedicated</span>
<span class="cm"> graph object, created like so: */</span>
<span class="n">my_data_struct</span><span class="p">.</span><span class="n">graph</span> <span class="o">=</span> <span class="n">ct_graph_new</span><span class="p">();</span>
<span class="cm">/* To store the PGN game tags requires a dedicated object. */</span>
<span class="n">my_data_struct</span><span class="p">.</span><span class="n">game_tags</span> <span class="o">=</span> <span class="n">ct_game_tags_new</span><span class="p">();</span>
<span class="cm">/* One last thing before the program is ready to start parsing the PGN file. For each game, it will callback to a</span>
<span class="cm"> command object, which is simply a pointer to some data and a function name that takes a void pointer. There are</span>
<span class="cm"> two ways to build a command object, here it is done with ct_command_new: */</span>
<span class="n">pgn_command</span> <span class="o">=</span> <span class="n">ct_command_new</span><span class="p">(</span><span class="o">&</span><span class="n">my_data_struct</span><span class="p">,</span> <span class="n">filter_game</span><span class="p">);</span>
<span class="cm">/* This call is similar to the ct_graph_from_pgn, but rather than taking a string, it takes a FILE pointer and a</span>
<span class="cm"> command to be run after each game has been read. Note, the last parameter is an optional buffer to store an error</span>
<span class="cm"> message. */</span>
<span class="n">ct_graph_from_pgn_file</span><span class="p">(</span><span class="n">my_data_struct</span><span class="p">.</span><span class="n">graph</span><span class="p">,</span> <span class="n">my_data_struct</span><span class="p">.</span><span class="n">game_tags</span><span class="p">,</span> <span class="n">file</span><span class="p">,</span> <span class="n">pgn_command</span><span class="p">,</span> <span class="n">error_message</span><span class="p">);</span>
<span class="cm">/* If there was an error, the error message describes it. If there was no error, the error message is set to an</span>
<span class="cm"> empty string. */</span>
<span class="k">if</span> <span class="p">(</span><span class="n">error_message</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"Error parsing pgn: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">error_message</span><span class="p">);</span>
<span class="n">exit_status</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>
<span class="cm">/* Since the program is exiting next, free'ing memory isn't necessary, but here is how it would work. */</span>
<span class="n">ct_command_free</span><span class="p">(</span><span class="n">pgn_command</span><span class="p">);</span>
<span class="n">ct_game_tags_free</span><span class="p">(</span><span class="n">my_data_struct</span><span class="p">.</span><span class="n">game_tags</span><span class="p">);</span>
<span class="n">ct_graph_free</span><span class="p">(</span><span class="n">my_data_struct</span><span class="p">.</span><span class="n">graph</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">fclose</span><span class="p">(</span><span class="n">file</span><span class="p">);</span>
<span class="n">exit</span><span class="p">(</span><span class="n">exit_status</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span>
<span class="nf">filter_game</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">delegate</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">MyDataStruct</span> <span class="o">*</span><span class="n">my_data</span> <span class="o">=</span> <span class="p">(</span><span class="n">MyDataStruct</span> <span class="o">*</span><span class="p">)</span> <span class="n">delegate</span><span class="p">;</span>
<span class="kt">char</span> <span class="o">*</span><span class="n">pgn</span><span class="p">;</span>
<span class="cm">/* Earlier ct_command_new was demonstrated. Here is the other way to build a command, directly in a local variable.</span>
<span class="cm"> Another difference is that this command takes CtMove as an argument. You'll see this is used to navigate all the</span>
<span class="cm"> moves in a game. Notice, this is a "Command Struct." A "Command" is just a pointer to a Command Struct. */</span>
<span class="n">CtMoveCommandStruct</span> <span class="n">move_command_struct</span> <span class="o">=</span> <span class="n">ct_move_command_make</span><span class="p">(</span><span class="n">my_data</span><span class="p">,</span> <span class="n">filter_position</span><span class="p">);</span>
<span class="n">my_data</span><span class="o">-></span><span class="n">is_a_match</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="cm">/* This function will rewind a graph and play through each move, updating the position as it goes. After each move,</span>
<span class="cm"> it calls back to the CtMoveCommand we pass it. */</span>
<span class="n">ct_graph_for_each_move_made</span><span class="p">(</span><span class="n">my_data</span><span class="o">-></span><span class="n">graph</span><span class="p">,</span> <span class="o">&</span><span class="n">move_command_struct</span><span class="p">);</span>
<span class="cm">/* We need to check the final position too, since the above function provides the position BEFORE each move. */</span>
<span class="n">filter_position</span><span class="p">(</span><span class="n">my_data</span><span class="p">,</span> <span class="n">NULL_MOVE</span><span class="p">);</span>
<span class="cm">/* If you peak ahead to filter_position, you'll see that the hash for each position was calculated and</span>
<span class="cm"> my_data->is_a_match was updated if the position's hash matched one we were looking for. */</span>
<span class="k">if</span> <span class="p">(</span><span class="n">my_data</span><span class="o">-></span><span class="n">is_a_match</span><span class="p">)</span>
<span class="p">{</span>
<span class="cm">/* For those games that had a matching position, we calculate their PGN and write it to stdout. Notice that</span>
<span class="cm"> ct_graph_to_new_pgn returns a newly malloc'd character string. It's up to the caller to free it. Anything "new"</span>
<span class="cm"> needs to eventually be freed or your program will leak memory. */</span>
<span class="n">pgn</span> <span class="o">=</span> <span class="n">ct_graph_to_new_pgn</span><span class="p">(</span><span class="n">my_data</span><span class="o">-></span><span class="n">graph</span><span class="p">,</span> <span class="n">my_data</span><span class="o">-></span><span class="n">game_tags</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"%s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">pgn</span><span class="p">);</span>
<span class="n">free</span><span class="p">(</span><span class="n">pgn</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kt">void</span>
<span class="nf">filter_position</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">delegate</span><span class="p">,</span> <span class="n">CtMove</span> <span class="n">move</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">MyDataStruct</span> <span class="o">*</span><span class="n">my_data</span> <span class="o">=</span> <span class="p">(</span><span class="n">MyDataStruct</span> <span class="o">*</span><span class="p">)</span> <span class="n">delegate</span><span class="p">;</span>
<span class="cm">/* This is the same method used above to hash the current position. */</span>
<span class="kt">int64_t</span> <span class="n">current_hash</span> <span class="o">=</span> <span class="n">ct_graph_position_hash</span><span class="p">(</span><span class="n">my_data</span><span class="o">-></span><span class="n">graph</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">current_hash</span> <span class="o">==</span> <span class="n">my_data</span><span class="o">-></span><span class="n">hash</span><span class="p">)</span>
<span class="n">my_data</span><span class="o">-></span><span class="n">is_a_match</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<h2>Data Types</h2>
<p>chess_toolkit.h includes stdint.h and stdbool.h since it uses some of these standard data types in function declarations as you'll see in the next section. See lib/chess_toolkit/ct_types.h for more information.</p>
<p><strong>CtSquare</strong> is an enum, and defines A1, A2, ... H8. It also defines SQUARE_NOT_FOUND and NUMBER_OF_SQUARES.</p>
<p><strong>CtFile</strong> is an enum, and defines FILE_A, FILE_B, ... FILE_H. It also defines FILE_NOT_FOUND and NUMBER_OF_FILES. Additionally an anonymous enum defines, LAST_FILE = FILE_H.</p>
<p><strong>CtRank</strong> is an enum, and defines RANK_1, RANK_2, ... RANK_8. It also defines RANK_NOT_FOUND and NUMBER_OF_RANKS. Additionally an anonymous enum defines, LAST_RANK = RANK_8.</p>
<p><strong>CtDirection</strong> is an enum, and defines the different offsets from any square as compass directions (from white's perspective with North meaning one rank forward and East meaning one file right). For instance A1 + D_N == A2, and B2 + D_NE == C3. The directions are: D_NONE, D_N, D_E, D_S, D_W, D_NE, D_SE, D_SW, D_NW, D_NNE, D_ENE, D_ESE, D_SSE, D_SSW, D_WSW, D_WNW, and D_NNW.</p>
<p><strong>CtPiece</strong> is an enum, and defines values for each piece (WHITE_KING, WHITE_QUEEN, etc.), including EMPTY. The value for each piece was carefully chosen to help categorize the piece by color or type, useful in move generation. See ct_types.h for more details.</p>
<p><strong>CtPieceColor</strong> is an enum, and defines two values WHITE_PIECE and BLACK_PIECE.</p>
<p><strong>CtBitBoard</strong> is an uint64_t with one bit for each square on the board. The low order bit (bit 0) represents square A1, and it proceeds across each rank before moving up to the next file. A1, A2, ... A8, B1, B2, ... H7, H8. Several macros are defined to create bit boards: BITB_EMPTY, BITB_FULL, and BITB_ONE.</p>
<p><strong>CtBitBoardArray</strong> is a pointer to an array of CtBitBoard values, indexed by the piece value. For instance, bit_board_array[WHITE_KING] should return a bit board with one bit set corresponding to the white king's position on the board. bit_board_array[EMPTY] returns a bit board marking all the empty squares.</p>
<p><strong>CtPosition</strong> is the first of several abstract data types (ADT) to be mentioned here. Basically, it is a pointer to a representation of a chess position. Like most of the ADTs, it reveals little about its internal representation, and you treat it as you would an object in object oriented programming.</p>
<p><strong>CtCastleRights</strong> is an enum, and defines 16 possible states corresponding to whether or not each side still has the right to castle kingside or queenside. If no one can castle any more, the castling rights are CASTLE_NONE. If both players can castle either direction, it is CASTLE_KQkq. The other 14 possible combinations are represented by removing the corresponding letter from KQkq; for instance, if white can only castle kingside and black can only castle queenside, castle rights are CASTLE_Kq.</p>
<p><strong>CtMove</strong> is an int16_t, however which bits mean what is not specified in its type definition. Other than its size being specified, it is treated as an ADT. Two special moves are defined by an anonymous enum: NULL_MOVE and AMBIGUOUS_MOVE.</p>
<p><strong>CtMoveType</strong> is an enum, and defines several move types, useful in notating moves and making moves on the board: NORMAL_MOVE, CASTLE_KINGSIDE, CASTLE_QUEENSIDE, EN_PASSANT_POSSIBLE, EN_PASSANT_CAPTURE, and PROMOTION.</p>
<p><strong>CtMoveStack</strong> is an ADT used to collect moves.</p>
<p><strong>CtGraph</strong> is an ADT used to navigate from one position to the next given a move. Each graph starts from a given chess position and saves the moves it is provided as it transitions from one position to the next. This is the most useful object in the Chess Toolkit.</p>
<p><strong>CtGameTags</strong> is an ADT used to store tag pairs describing a chess game. Currently, it is limited to the following tags: Event, Site, Date, Round, White, Black, Result, WhiteElo, BlackElo and ECO. A future release is likely to add the FEN tag so that graphs from positions other than the standard initial position are easier to work with.</p>
<p><strong>CtCommand</strong> is a pointer to a <strong>CtCommandStruct</strong>. The reason CtCommandStruct is available, is there could be cases where you'd rather not manually manage memory for each command; then it's handy to reference the struct version. The command object follows the command design pattern. It is provided a delegate and a method which accepts that delegate. Passing a command object allows the receiver to easily callback to the delegate of your command. There are several variants of this command object.</p>
<p><strong>CtMoveCommand</strong> is a pointer to a <strong>CtMoveCommandStruct</strong>. It follows the command design pattern, and takes an argument of CtMove.</p>
<p><strong>CtPieceCommand</strong> is a pointer to a <strong>CtPieceCommandStruct</strong>. It follows the command design pattern, and takes two arguments: CtPiece and CtSquare.</p>
<h2>Memory Management</h2>
<p>Any of the Chess Toolkit types that are enums (CtSquare, CtFile, etc.) or integers (CtBitBoard and CtMove) are passed by value and do not require any special memory management. To distinguish their creation from other types that require explicit memory management, their constructors use the word "make" instead of "new". Here are several examples:</p>
<pre><code>CtSquare square = ct_square_make(FILE_E, RANK_2);
CtMove move = ct_move_make(E2, E4);
CtBitBoard bit_board = ct_bit_board_make();
</code></pre>
<p>Those values that are ADTs, do require some care. They are usually created by a "new" function and must later be released by the corresponding "free" function or your program will leak memory. For instance, a new CtPosition is created with:</p>
<pre><code>position = ct_position_new();
</code></pre>
<p>This position must later by released with:</p>
<pre><code>ct_position_free(position);
</code></pre>
<p>There are similar functions to free other ADTs, including CtMoveStack, CtGraph, CtGameTags, and the commands (CtCommand, CtMoveCommand and CtPieceCommand).</p>
<h3>Special Cases: CtBitBoardArrays</h3>
<p>CtBitBoardArrays do not have new or free functions. They should be defined like this:</p>
<pre><code>CtBitBoard bit_board_array[CT_BIT_BOARD_ARRAY_LENGTH];
</code></pre>
<p>or with your choice of malloc, calloc, etc.</p>
<pre><code>CtBitBoard *bit_board_array = malloc(CT_BIT_BOARD_ARRAY_LENGTH * sizeof(CtBitBoard));
/* use the bit board array and then later... */
free(bit_board_array);
</code></pre>
<h3>Special Cases: Commands</h3>
<p>If you would rather not use the new and free functions for a command, you can use the make functions instead.</p>
<pre><code>CtCommandStruct command_struct = ct_command_make(delegate, function);
CtCommand command = &command_struct; /* or you just remember to use &command_struct where you would use command */
</code></pre>
<p>Since a CtCommand is just a pointer to CtCommand struct, you could also write it this way:</p>
<pre><code>CtCommandStruct command[] = { ct_command_make(delegate, function) };
</code></pre>
<p>Both of the above are equivalent to:</p>
<pre><code>CtCommand command = ct_command_new(delegate, function);
/* use the command and then later... */
ct_command_free(command);
</code></pre>
<p>but in the former cases, you never need to free anything, which comes in handy.</p>
<h3>Special Cases: to destination</h3>
<p>Several functions write to an object (string, position or graph). Assuming the caller provides an object to write to, it writes there and returns the result. However, as a convenience, the caller can pass in zero. This signifies the caller wants to use a shared object that they don't have to worry about freeing later. This is handy, but in some ways it's also a double-edged sword. Freeing the shared object will cause mysterious problems the next time this function is called. Using the shared object is also not appropriate for multi-threaded applications; but it does make simple programs even simpler to write. Here is an example using a shared destination:</p>
<pre><code>printf("Your last move was %s.\n", ct_move_to_s(move, 0));
</code></pre>
<p>without the shared destination, it could be written like this:</p>
<pre><code>char move_string[CT_MOVE_TO_S_MAX_LENGTH];
ct_move_to_s(move, move_string);
printf("Your last move was %s.\n", move_string);
</code></pre>
<p>or</p>
<pre><code>char move_string[CT_MOVE_TO_S_MAX_LENGTH];
printf("Your last move was %s.\n", ct_move_to_s(move, move_string));
</code></pre>
<p>All three of these will produce the same result, but the first method, using the shared destination, would not work in a multi-threaded application where other threads could also call ct_move_to_s around the same time and unexpectedly change the shared destination to something else.</p>
<h3>Special Cases: to_new</h3>
<p>One function in particular is worth noting in regards to memory management, because it is unique. ct_graph_to_new_pgn returns a newly allocated memory buffer. It is up to the caller to later free this with free(). Most of the to_xxx functions write directly to a buffer (or a shared buffer). However, in those cases, the maximum size of the object being written is known at compile time. In this case, the length of the PGN written depends on how many moves the graph has, so it cannot be known until runtime. Here is how it should be used:</p>
<pre><code>char * pgn = ct_graph_to_new_pgn(graph, game_tags);
/* use the pgn and then later... */
free(pgn);
</code></pre>
<h2>Functions</h2>
<pre><code>void chess_toolkit_init(void);
</code></pre>
<blockquote>
<p>must be called before using anything else in the toolkit.</p>
</blockquote>
<h3>Error handling functions</h3>
<pre><code>void ct_error(const char *str);
</code></pre>
<blockquote>
<p>is called if something unexpected and catastrophic occurs (for instance running out of memory), but there is no reason your program can't call it as well. The default behavior is to print a message to stdout and exit. You can override this behavior with ct_error_set_custom_handler.</p>
</blockquote>
<pre><code>typedef void (*CtErrorHandler) (const char *);
void ct_error_set_custom_handler(CtErrorHandler error_handler);
</code></pre>
<blockquote>
<p>is used to set up a custom error handler. You pass it a function that takes const char * as an argument, and this function will be called instead of the default error handling function, should an unrecoverable error occur.</p>
</blockquote>
<h3>Square functions</h3>
<p>Please note that none of these square functions are designed to handle unexpected inputs. For instance, ct_square_make assumes that it is being passed a valid file and rank. If you pass it something unexpected, you will not get a meaningful result. These are low-level functions written to run as fast as possible; they are not intended to be overly robust.</p>
<pre><code>CtSquare ct_square_make(CtFile file, CtRank rank);
</code></pre>
<blockquote>
<p>returns a CtSquare by value, given a file and rank. It expects file is between FILE_A and FILE_H.</p>
</blockquote>
<pre><code>CtFile ct_square_file(CtSquare square);
CtRank ct_square_rank(CtSquare square);
</code></pre>
<blockquote>
<p>returns the file or rank of the square. It expects square is between A1 and H8.</p>
</blockquote>
<pre><code>CtFile ct_file_from_char(char c);
</code></pre>
<blockquote>
<p>returns the file corresponding to the character passed in. It expects c is between 'a' and 'h'. For instance, ct_file_from_char('a') will return FILE_A.</p>
</blockquote>
<pre><code>CtRank ct_rank_from_char(char c);
</code></pre>
<blockquote>
<p>returns the rank corresponding to the character passed in. It expects c is between '1' and '8'. For instance, ct_rank_from_char('1') will return RANK_1.</p>
</blockquote>
<pre><code>char ct_file_to_char(CtFile file);
char ct_rank_to_char(CtRank rank);
</code></pre>
<blockquote>
<p>returns the character corresponding to the file or rank. It expects file to be between FILE_A and FILE_H; rank between RANK_1 and RANK_8.</p>
</blockquote>
<h3>Piece functions</h3>
<pre><code>CtPieceColor ct_piece_color(CtPiece piece);
</code></pre>
<blockquote>
<p>returns the color of a piece. Expect a valid piece (excluding EMPTY).</p>
</blockquote>
<pre><code>CtPiece ct_piece_from_char(char piece_char);
</code></pre>
<blockquote>
<p>returns a piece corresponding to the character. If piece_char is either a B, K, N, P, Q, or R, it will return a Bishop, King, Knight, Pawn, Queen or Rook; White for uppercase and Black for lowercase. For instance, given 'n' returns a BLACK_KNIGHT. Any other character not corresponding to a chess piece returns EMPTY.</p>
</blockquote>
<pre><code>char ct_piece_to_char(CtPiece piece);
</code></pre>
<blockquote>
<p>returns a character corresponding to the piece. An invalid piece returns 0. Otherwise, it returns an uppercase letter for white pieces and a lowercase letter for black pieces... B, K, N, P, Q, or R, for a Bishop, King, Knight, Pawn, Queen or Rook, respectively.</p>
</blockquote>
<pre><code>CtPiece ct_piece_to_white(CtPiece piece);
CtPiece ct_piece_to_black(CtPiece piece);
</code></pre>
<blockquote>
<p>returns a white or black version of the piece respectively. EMPTY remains EMPTY.</p>
</blockquote>
<pre><code>bool ct_piece_is_pawn(CtPiece piece);
</code></pre>
<blockquote>
<p>returns true if the piece is a pawn; false otherwise. Similar functions could test for other piece types, but it seems this was the only one needed thus far.</p>
</blockquote>
<h3>Bit Board functions</h3>
<pre><code>CtBitBoard ct_bit_board_make(CtSquare square);
</code></pre>
<blockquote>
<p>returns a CtBitBoard with a single bit set corresponding to the square. It expects square is between A1 and H8.</p>
</blockquote>
<pre><code>CtSquare ct_bit_board_find_first_square(CtBitBoard bit_board);
</code></pre>
<blockquote>
<p>finds the first bit set in bit_board and returns the index of that bit which corresponds to a square. If no bits are set, it returns SQUARE_NOT_FOUND.</p>
</blockquote>
<pre><code>char *ct_bit_board_to_s(CtBitBoard bit_board, char *destination);
</code></pre>
<blockquote>
<p>stores a string representation of the bit board in destination and returns the start of that string. The string will be at most CT_BIT_BOARD_TO_S_MAX_LENGTH characters long, including the null terminator. If no destination is provided, a shared destination will be used.</p>
</blockquote>
<pre><code>void ct_bit_board_array_reset(CtBitBoardArray bit_board_array);
</code></pre>
<blockquote>
<p>zeros all the bit boards except for EMPTY which it fills to represent a completely empty board.</p>
</blockquote>
<pre><code>bool ct_bit_board_array_is_white_attacking(CtBitBoardArray bit_board_array, CtSquare square);
bool ct_bit_board_array_is_black_attacking(CtBitBoardArray bit_board_array, CtSquare square);
</code></pre>
<blockquote>
<p>examines the bit_board_array to determine if white (or black respectively) is attacking the square. Using the bit board array in combination with some predetermined masks is an extremely fast way to see if the king is in check, and therefore if a position is legal.</p>
</blockquote>
<h3>Position functions</h3>
<pre><code>CtPosition ct_position_new();
</code></pre>
<blockquote>
<p>returns a new position starting from the initial board position.</p>
</blockquote>
<pre><code>void ct_position_free(CtPosition position);
</code></pre>
<blockquote>
<p>frees the position.</p>
</blockquote>
<pre><code>void ct_position_clear(CtPosition position);
</code></pre>
<blockquote>
<p>removes all the pieces from the position, sets white to move, no castling rights and no en-passant possible.</p>
</blockquote>
<pre><code>void ct_position_reset(CtPosition position);
</code></pre>
<blockquote>
<p>restores the standard starting position with white to move, everyone can castle either direction and no en-passant is possible.</p>
</blockquote>
<pre><code>void ct_position_copy(CtPosition destination, CtPosition source);
</code></pre>
<blockquote>
<p>copies the source position to destination.</p>
</blockquote>
<pre><code>CtPiece ct_position_get_piece(CtPosition position, CtSquare square);
</code></pre>
<blockquote>
<p>returns the piece on the square. If no piece is on the square, it returns EMPTY.</p>
</blockquote>
<pre><code>void ct_position_set_piece(CtPosition position, CtSquare square, CtPiece piece);
</code></pre>
<blockquote>
<p>sets the piece on the square. If the piece is EMPTY, it removes the piece.</p>
</blockquote>
<pre><code>void ct_position_for_each_piece(CtPosition position, CtPieceCommand piece_command);
</code></pre>
<blockquote>
<p>iterates through each piece in the position that's not EMPTY and calls back to the piece command.</p>
</blockquote>
<pre><code>void ct_position_for_each_active_piece(CtPosition position, CtPieceCommand piece_command);
</code></pre>
<blockquote>
<p>iterates similar to ct_position_for_each_piece, but it only calls back for the color pieces whose turn it is to move.</p>
</blockquote>
<pre><code>CtBitBoard ct_position_get_bit_board(CtPosition position, CtPiece piece);
</code></pre>
<blockquote>
<p>returns a copy of the bit board. It expects piece to be any valid piece, including EMPTY.</p>
</blockquote>
<pre><code>bool ct_position_is_white_to_move(CtPosition position);
</code></pre>
<blockquote>
<p>returns true if it is white's turn to move in the position; false otherwise.</p>
</blockquote>
<pre><code>void ct_position_set_white_to_move(CtPosition position);
void ct_position_set_black_to_move(CtPosition position);
void ct_position_change_turns(CtPosition position);
</code></pre>
<blockquote>
<p>sets whose turn it is to move in the position.</p>
</blockquote>
<pre><code>CtFile ct_position_get_en_passant(CtPosition position);
</code></pre>
<blockquote>
<p>returns NO_EN_PASSANT if en-passant is not possible or the file of the pawn which can be captured by en-passant</p>
</blockquote>
<pre><code>void ct_position_set_en_passant(CtPosition position, CtFile file);
</code></pre>
<blockquote>
<p>sets en-passant of the position to the file. It expects file to be between FILE_A and FILE_H, but passing a square between A1 and H8 works also, and is equivalent to sending that square's file. Passing NO_EN_PASSANT for the file will not work; to clear the en-passant state, use ct_position_clear_en_passant.</p>
</blockquote>
<pre><code>void ct_position_clear_en_passant(CtPosition position);
</code></pre>
<blockquote>
<p>clears en-passant of the position, so en-passant is not possible.</p>
</blockquote>
<pre><code>CtCastleRights ct_position_get_castle(CtPosition position);
</code></pre>
<blockquote>
<p>returns the castling rights of the position.</p>
</blockquote>
<pre><code>void ct_position_set_castle(CtPosition position, CtCastleRights castle_rights);
</code></pre>
<blockquote>
<p>sets the castling rights of the position.</p>
</blockquote>
<pre><code>bool ct_position_is_legal(CtPosition position);
</code></pre>
<blockquote>
<p>returns true if the the player who does not have the move is not in check since you must not leave yourself in check.</p>
</blockquote>
<pre><code>bool ct_position_is_check(CtPosition position);
CtCastleRights ct_position_can_castle(CtPosition position);
</code></pre>
<blockquote>
<p>returns true if the player who does have the move is in check.</p>
</blockquote>
<pre><code>char *ct_position_to_s(CtPosition position, char *destination);
</code></pre>
<blockquote>
<p>stores a string representation of the position in destination and returns the start of that string. The string will be at most CT_POSITION_TO_S_MAX_LENGTH characters long, including the null terminator. If no destination is provided, a shared destination will be used.</p>
</blockquote>
<pre><code>int64_t ct_position_hash(CtPosition position);
</code></pre>
<blockquote>
<p>calculates and returns a Zobrist hash of the position.</p>
</blockquote>
<pre><code>char *ct_position_to_fen(CtPosition position, char *destination);
</code></pre>
<blockquote>
<p>stores a four part FEN representation of the position in destination and returns the start of that string. The string will be at most CT_FEN_MAX_LENGTH characters long, including the null terminator. If no destination is provided, a shared destination will be used.</p>
</blockquote>
<pre><code>CtPosition ct_position_from_fen(CtPosition position, char *fen);
</code></pre>
<blockquote>
<p>reads the first four parts of the FEN notation and updates the position to match. If there is a problem with the FEN, the position is cleared and 0 is returned.</p>
</blockquote>
<h3>Move functions</h3>
<pre><code>CtMove ct_move_make(CtSquare from, CtSquare to);
</code></pre>
<blockquote>
<p>returns a move of type NORMAL_MOVE by value. There are other move_make functions to return the special move types (CASTLE_KINGSIDE, CASTLE_QUEENSIDE, EN_PASSANT_POSSIBLE, EN_PASSANT_CAPTURE, and PROMOTION).</p>
</blockquote>
<pre><code>CtMove ct_move_make_castle_kingside(CtSquare from);
CtMove ct_move_make_castle_queenside(CtSquare from);
</code></pre>
<blockquote>
<p>returns a move of type CASTLE_KINGSIDE (or CASTLE_QUEENSIDE respectively) by value. Only the position of the king needs to be passed in.</p>
</blockquote>
<pre><code>CtMove ct_move_make_en_passant_possible(CtSquare from, CtSquare to);
</code></pre>
<blockquote>
<p>returns a move of type EN_PASSANT_POSSIBLE by value.</p>
</blockquote>
<pre><code>CtMove ct_move_make_en_passant_capture(CtSquare from, CtSquare to);
</code></pre>
<blockquote>
<p>returns a move of type EN_PASSANT_CAPTURE by value.</p>
</blockquote>
<pre><code>CtMove ct_move_make_promotion_Q(CtSquare from, CtSquare to);
CtMove ct_move_make_promotion_R(CtSquare from, CtSquare to);
CtMove ct_move_make_promotion_B(CtSquare from, CtSquare to);
CtMove ct_move_make_promotion_N(CtSquare from, CtSquare to);
CtMove ct_move_make_promotion_q(CtSquare from, CtSquare to);
CtMove ct_move_make_promotion_r(CtSquare from, CtSquare to);
CtMove ct_move_make_promotion_b(CtSquare from, CtSquare to);
CtMove ct_move_make_promotion_n(CtSquare from, CtSquare to);
</code></pre>
<blockquote>
<p>returns a move of type PROMOTION by value. The piece it promotes to corresponds to the method used.</p>
</blockquote>
<pre><code>CtMoveType ct_move_type(CtMove move);
</code></pre>
<blockquote>
<p>returns type of move (NORMAL_MOVE, CASTLE_KINGSIDE, CASTLE_QUEENSIDE, EN_PASSANT_POSSIBLE, EN_PASSANT_CAPTURE, and PROMOTION). It expects a legitimate move, not NULL_MOVE or AMBIGUOUS_MOVE.</p>
</blockquote>
<pre><code>CtSquare ct_move_from(CtMove move);
CtSquare ct_move_to(CtMove move);
</code></pre>
<blockquote>
<p>returns square the move is from (or to respectively). It expects a legitimate move, not NULL_MOVE or AMBIGUOUS_MOVE.</p>
</blockquote>
<pre><code>CtPiece ct_move_promotes_to(CtMove move);
</code></pre>
<blockquote>
<p>returns the piece that a move of type PROMOTION should promote into. Expects a move of type PROMOTION, but will return EMPTY for other legitimate moves.</p>
</blockquote>
<pre><code>char *ct_move_to_s(CtMove move, char *destination);
</code></pre>
<blockquote>
<p>stores a string representation of the move in destination and returns the start of that string. The string will be at most CT_MOVE_TO_S_MAX_LENGTH characters long, including the null terminator. If no destination is provided, a shared destination will be used.</p>
</blockquote>
<h3>Move Stack functions</h3>
<pre><code>CtMoveStack ct_move_stack_new();
</code></pre>
<blockquote>
<p>returns a new move stack.</p>
</blockquote>
<pre><code>void ct_move_stack_free(CtMoveStack move_stack);
</code></pre>
<blockquote>
<p>frees the move stack.</p>
</blockquote>
<pre><code>void ct_move_stack_push(CtMoveStack move_stack, CtMove move);
</code></pre>
<blockquote>
<p>adds move to the top of the move stack.</p>
</blockquote>
<pre><code>CtMove ct_move_stack_pop(CtMoveStack move_stack);
</code></pre>
<blockquote>
<p>takes and returns one move from the top of the stack</p>
</blockquote>
<pre><code>void ct_move_stack_reset(CtMoveStack move_stack);
</code></pre>
<blockquote>
<p>empties the stack</p>
</blockquote>
<pre><code>bool ct_move_stack_is_empty(CtMoveStack move_stack);
</code></pre>
<blockquote>
<p>returns true if the stack is empty; otherwise returns false.</p>
</blockquote>
<pre><code>unsigned int ct_move_stack_length(CtMoveStack move_stack);
</code></pre>
<blockquote>
<p>returns the number of moves on the stack.</p>
</blockquote>
<pre><code>CtMoveCommand ct_move_stack_push_command(CtMoveStack move_stack);
</code></pre>
<blockquote>
<p>returns a move command which can be used to push moves on the stack.</p>
</blockquote>
<pre><code>void ct_move_stack_for_each(CtMoveStack move_stack, CtMoveCommand move_command);
</code></pre>
<blockquote>
<p>executes the move_command on each move in the stack, starting with the first one added (bottom of the stack) and working up to the top.</p>
</blockquote>
<h3>Graph functions</h3>
<pre><code>CtGraph ct_graph_new(void);
</code></pre>
<blockquote>
<p>returns a new graph starting from the standard chess starting position.</p>
</blockquote>
<pre><code>void ct_graph_free(CtGraph graph);
</code></pre>
<blockquote>
<p>frees the graph.</p>
</blockquote>
<pre><code>void ct_graph_reset(CtGraph graph);
</code></pre>
<blockquote>
<p>removes all moves made and returns to the standard chess starting position.</p>
</blockquote>
<pre><code>int ct_graph_ply(CtGraph graph);
</code></pre>
<blockquote>
<p>returns the number of moves between the starting position of this graph and its current position.</p>
</blockquote>
<pre><code>void ct_graph_for_each_legal_move(CtGraph graph, CtMoveCommand move_command);
</code></pre>
<blockquote>
<p>generates every possible move from the current position and for each legal move it executes the move_command</p>
</blockquote>
<pre><code>void ct_graph_for_each_move_made(CtGraph graph, CtMoveCommand move_command);
</code></pre>
<blockquote>
<p>rewinds the graph to its starting position and executes move_command on each each move that was made, making the move before executing the next move_command so the graph appears to be in the state it was when the move was originally made. The graph should not be modified during execution of the move_command.</p>
</blockquote>
<pre><code>void ct_graph_make_move(CtGraph graph, CtMove move);
</code></pre>
<blockquote>
<p>causes graph to save the move and update its position.</p>
</blockquote>
<pre><code>CtMove ct_graph_unmake_move(CtGraph graph);
</code></pre>
<blockquote>
<p>causes graph to undo the last move made, reverting to the previous state. It returns the last move, which was just undone.</p>
</blockquote>
<pre><code>char *ct_graph_move_to_san(CtGraph graph, CtMove move, char *destination);
</code></pre>
<blockquote>
<p>stores a string representation of the move (using short algebraic notation) in destination and returns the start of that string. The string will be at most CT_SAN_MAX_LENGTH characters long, including the null terminator. If no destination is provided, a shared destination will be used.</p>
</blockquote>
<pre><code>CtMove ct_graph_move_from_san(CtGraph graph, char *notation);
</code></pre>
<blockquote>
<p>compares legal moves to the notation to determine if the notation uniquely identifies a legal move, and returns the resulting move. If no move matches, NULL_MOVE is returned. If more than one legal move matches, AMBIGUOUS_MOVE is returned.</p>
</blockquote>
<pre><code>void ct_graph_dfs(CtGraph graph, CtCommand command, int depth);
</code></pre>
<blockquote>
<p>perform a depth first search from the graph's current position, executing the given command at every position it reaches that is depth moves away. It expects depth is at least 1, and the command's execution does not modify graph.</p>
</blockquote>
<pre><code>CtPosition ct_graph_to_position(CtGraph graph, CtPosition position);
</code></pre>
<blockquote>
<p>copies the graph's current position to the specified position and returns that position. If no position is provided, a shared position will be used and returned.</p>
</blockquote>
<pre><code>CtGraph ct_graph_from_position(CtGraph graph, CtPosition position);
</code></pre>
<blockquote>
<p>resets the graph, copies the specified position to the graph's starting position, and returns the graph. If no graph is provided, a shared graph wil be used and returned.</p>
</blockquote>
<pre><code>char *ct_graph_to_fen(CtGraph graph, char *destination);
</code></pre>
<blockquote>
<p>stores a four part FEN representation of the graph's current position in destination and returns the start of that string. The string will be at most CT_FEN_MAX_LENGTH characters long, including the null terminator. If no destination is provided, a shared destination will be used.</p>
</blockquote>
<pre><code>CtGraph ct_graph_from_fen(CtGraph graph, char *fen);
</code></pre>
<blockquote>
<p>reads the first four parts of the FEN notation, resets the graph and sets the graph's starting position to match the fen. If there is a problem with the FEN, the graph is reset, the position is cleared and 0 is returned. If no destination is provided, a shared destination will be used.</p>
</blockquote>
<pre><code>int64_t ct_graph_position_hash(CtGraph graph);
</code></pre>
<blockquote>
<p>calculates and returns a Zobrist hash of the graph's current position.</p>
</blockquote>
<pre><code>char *ct_graph_to_s(CtGraph graph, char *destination);
</code></pre>
<blockquote>
<p>stores a string representation of the graph in destination and returns the start of that string. The string will be at most CT_GRAPH_TO_S_MAX_LENGTH characters long, including the null terminator. If no destination is provided, a shared destination will be used.</p>
</blockquote>
<pre><code>char *ct_graph_to_new_pgn(CtGraph graph, CtGameTags game_tags);
</code></pre>
<blockquote>
<p>returns a newly allocated buffer of memory with the PGN representation of the graph and game_tags. If no game_tags object is provided, it returns only the movetext portion of the PGN with a "*" game termination. If no graph is provided, it returns only the game tags portion of the PGN.</p>
</blockquote>
<pre><code>CtGraph ct_graph_from_pgn(CtGraph graph, CtGameTags game_tags, char *pgn_string, char *error_message);
</code></pre>
<blockquote>
<p>reads a PGN string, updating graph and game_tags to match. If successful, it sets the error_message to an empty string and returns the resulting graph. If unsuccessful, it stores an error message indicating where the error occurred to the character buffer provided, and returns 0. The error_message buffer should be at least CT_GRAPH_FROM_PGN_ERROR_MESSAGE_MAX_LENGTH characters long. If no error_message buffer is provided, it still returns 0, but the caller will not have any more information on the error. If no graph is provided, a shared graph will be used. If no game_tags are provided, game tag information will not be available. It expects a valid PGN string for a single game.</p>
</blockquote>
<pre><code>void ct_graph_from_pgn_file(CtGraph graph, CtGameTags game_tags, FILE * file, CtCommand command, char *error_message);
</code></pre>
<blockquote>
<p>reads a file of PGN games, updating graph and game_tags as each game is read, and executing command when the game is successfully loaded. If there is an error reading the PGN, it does not execute command for that game. When the function returns, error_message will be empty to indicate no errors, or it will fill error_message with a description of where the error occurred. The error_message buffer should be at least CT_GRAPH_FROM_PGN_ERROR_MESSAGE_MAX_LENGTH characters long. Providing an error_message buffer is optional, but there is no indication of errors without one. If no graph is provided, a shared graph will be used. If no game_tags are provided, game tag information will not be available. It expects a FILE stream to be provided containing PGN for one or more games.</p>
</blockquote>
<h3>Game Tag functions</h3>
<pre><code>CtGameTags ct_game_tags_new(void);
</code></pre>
<blockquote>
<p>returns a new game tags object. The Result tag is "*" and everything else is unknown.</p>
</blockquote>
<pre><code>void ct_game_tags_free(CtGameTags game_tags);
</code></pre>
<blockquote>
<p>frees the game tags object.</p>
</blockquote>
<pre><code>void ct_game_tags_reset(CtGameTags game_tags);
</code></pre>
<blockquote>
<p>restores the game tags object like new, with Result = "*" and everything else unknown.</p>
</blockquote>
<pre><code>char *ct_game_tags_get(CtGameTags game_tags, char *key);
</code></pre>
<blockquote>
<p>looks up the key for the specified value. If the key is found, it returns its associated value; otherwise it returns a pointer to "?" which indicates the value is unknown and can be used in accordance with the PGN specification for any tag value (except for Result). Result will always have a value assigned to it; "*" indicates in progress, abandoned or unknown.</p>
</blockquote>
<pre><code>void ct_game_tags_set(CtGameTags game_tags, char *key, char *value);
</code></pre>
<blockquote>
<p>looks up the key, and if it is one of the valid game tag keys, it sets its value. The maximum length for a game tag value is GAME_TAGS_VALUE_MAX_LENGTH (including the null terminator), and any string longer than that is truncated to that length. Non-printable characters are also converted to spaces, to be in compliance with the PGN specification.</p>
</blockquote>
<h3>Command, Move Command, and Piece Command functions</h3>
<pre><code>CtCommand ct_command_new(void *delegate, CtCommandMethod method);
CtMoveCommand ct_move_command_new(void *delegate, CtMoveCommandMethod method);
CtPieceCommand ct_piece_command_new(void *delegate, CtPieceCommandMethod method);
</code></pre>
<blockquote>
<p>returns a new command object.</p>
</blockquote>
<pre><code>void ct_command_free(CtCommand command);
void ct_move_command_free(CtMoveCommand command);
void ct_piece_command_free(CtPieceCommand command);
</code></pre>
<blockquote>
<p>frees the command object.</p>
</blockquote>
<pre><code>CtCommandStruct ct_command_make(void *delegate, CtCommandMethod method);
CtMoveCommandStruct ct_move_command_make(void *delegate, CtMoveCommandMethod method);
CtPieceCommandStruct ct_piece_command_make(void *delegate, CtPieceCommandMethod method);
</code></pre>
<blockquote>
<p>returns a command struct by value.</p>
</blockquote>