├── .gitignore ├── AUTHORS ├── COPYING ├── ChangeLog ├── INSTALL ├── Makefile.am ├── Makefile.in ├── NEWS ├── README ├── README1.3 ├── README1.4 ├── README1.4w ├── README1.4w10UCI ├── TODO ├── aclocal.m4 ├── adapter.cpp ├── adapter.h ├── attack.c ├── attack.cpp ├── attack.h ├── board.c ├── board.cpp ├── board.h ├── book.c ├── book.cpp ├── book.h ├── book_format.html ├── book_make.c ├── book_make.cpp ├── book_make.h ├── book_merge.c ├── book_merge.cpp ├── book_merge.h ├── colour.c ├── colour.cpp ├── colour.h ├── config.guess ├── config.h ├── config.h.in ├── config.sub ├── configure ├── configure.ac ├── debian ├── README ├── changelog ├── compat ├── control ├── copyright ├── docs ├── files ├── polyglot.substvars └── rules ├── depcomp ├── engine.c ├── engine.cpp ├── engine.h ├── epd.c ├── epd.cpp ├── epd.h ├── fen.c ├── fen.cpp ├── fen.h ├── game.c ├── game.cpp ├── game.h ├── gui.c ├── gui.cpp ├── gui.h ├── hash.c ├── hash.cpp ├── hash.h ├── ini.c ├── ini.h ├── install-sh ├── io.c ├── io.cpp ├── io.h ├── line.c ├── line.cpp ├── line.h ├── list.c ├── list.cpp ├── list.h ├── main.c ├── main.cpp ├── main.h ├── mainloop.c ├── mainloop.cpp ├── mainloop.h ├── makefile.gcc ├── makefile.ms ├── missing ├── move.c ├── move.cpp ├── move.h ├── move_do.c ├── move_do.cpp ├── move_do.h ├── move_gen.c ├── move_gen.cpp ├── move_gen.h ├── move_legal.c ├── move_legal.cpp ├── move_legal.h ├── option.c ├── option.cpp ├── option.h ├── parse.c ├── parse.cpp ├── parse.h ├── pgheader.c ├── pgheader.h ├── pgn.c ├── pgn.cpp ├── pgn.h ├── piece.c ├── piece.cpp ├── piece.h ├── pipe.cpp ├── pipe.h ├── pipex.h ├── pipex_posix.c ├── pipex_win32.c ├── polyglot.man ├── polyglot.pod ├── polyglot.spec ├── posix.cpp ├── posix.h ├── random.c ├── random.cpp ├── random.h ├── san.c ├── san.cpp ├── san.h ├── search.c ├── search.cpp ├── search.h ├── square.c ├── square.cpp ├── square.h ├── uci.c ├── uci.cpp ├── uci.h ├── uci2uci.c ├── uci2uci.cpp ├── uci2uci.h ├── util.c ├── util.cpp ├── util.h ├── xboard2uci.c ├── xboard2uci.cpp └── xboard2uci.h /.gitignore: -------------------------------------------------------------------------------- 1 | /aclocal.m4 2 | /autom4te.cache 3 | /config.log 4 | /config.status 5 | /configure 6 | /install-sh 7 | /missing 8 | /polyglot 9 | /stamp-h1 10 | 11 | Makefile 12 | Makefile.in 13 | .deps 14 | *.o 15 | .*.swp 16 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Main author: Fabien Letouzey 2 | Native Windows port: Huang Chen ("Morning Yellow") 3 | Various enhancements: Fonzy Bleumers 4 | Harm Geert Muller 5 | Michel Van den Bergh 6 | Daniel Dugovic 7 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | #AM_CPPFLAGS=-DDEBUG 2 | #AM_CXXFLAGS=-g 3 | 4 | bin_PROGRAMS = polyglot 5 | polyglot_SOURCES = mainloop.c attack.c board.c book.c book_make.c book_merge.c colour.c engine.c epd.c fen.c gui.c game.c hash.c ini.c io.c line.c list.c main.c move.c move_do.c move_gen.c move_legal.c option.c parse.c pgheader.c pgn.c piece.c pipex_posix.c pipex_win32.c random.c san.c search.c square.c uci.c uci2uci.c util.c xboard2uci.c mainloop.h colour.h hash.h ini.h move_gen.h piece.h uci2uci.h attack.h config.h gui.h io.h move.h pipex.h uci.h board.h engine.h line.h move_legal.h random.h util.h book.h epd.h list.h option.h san.h book_make.h fen.h main.h parse.h search.h book_merge.h game.h move_do.h pgheader.h pgn.h square.h xboard2uci.h 6 | 7 | dist_doc_DATA = README README1.3 README1.4 README1.4w README1.4w10UCI book_format.html 8 | 9 | man6_MANS = polyglot.man 10 | 11 | polyglot.man: polyglot.pod 12 | pod2man -c "" -r "" -s 6 polyglot.pod > polyglot.man 13 | 14 | README: polyglot.man 15 | groff -t -e -mandoc -Tascii polyglot.man| col -bx > README 16 | 17 | deb: 18 | dpkg-buildpackage 19 | 20 | rpm: 21 | make dist 22 | rpmbuild -ta polyglot-@VERSION@.tar.gz 23 | 24 | EXTRA_DIST=makefile.gcc makefile.ms polyglot.man polyglot.pod polyglot.spec debian/changelog debian/control debian/docs debian/README debian/compat debian/copyright debian/files debian/polyglot.substvars debian/rules 25 | 26 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ddugovic/polyglot/a7aa136d3f87279b0e87ad5f9330d179f40fafc1/NEWS -------------------------------------------------------------------------------- /README1.3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ddugovic/polyglot/a7aa136d3f87279b0e87ad5f9330d179f40fafc1/README1.3 -------------------------------------------------------------------------------- /README1.4w: -------------------------------------------------------------------------------- 1 | === PolyGlot 1.4W ==== 2 | Polyglot 1.4W is a modified polyglot 1.4 from Fabien Letouzy. 3 | Just like polyglot 1.3w(see below) it can be compiled and run under windows without 4 | the need for a cygwin1.dll 5 | 6 | Best 7 | Fonzy 8 | www.geenvis.net 9 | =========1.4w10================ 10 | fixed analysis output for toga 11 | =========1.4w9================= 12 | fixed bug in 1.4w8 13 | added RepeatPV workaround 14 | fixed disappearing output in engine-output window while in multipv mode 15 | when an engine sends its move to polyglot, 16 | polyglot normally repeats the last pv string(which also contains score,depth and time usage) it got from the engine. 17 | Some engines however do not send a new pv string just before sending the move and the now old pv string 18 | find might confuse debugtools that parse the winboard debug files. 19 | Adding "RepeatPV = false" to the [POLYGLOT] section of the ini file stops this repetition. 20 | 21 | =========1.4w8================= 22 | fixed multipv output 23 | note that the pv with the worst score will be on top of the engine-output window. 24 | added timestamp in logfile (Jaap Weidemann) 25 | 26 | =========1.4w7================= 27 | compiles under linux/unix again 28 | =========1.4w6================= 29 | access to winboard draw/drawoffer and resign 30 | 31 | 1:to activate draw offers the engine has 32 | to define the "UCI_DrawOffers" parameter with the 'option" command at startup. 33 | 2:to offer a draw or accept a draw offer:just send "info string DrawOffer" to polyglot. 34 | 3:if winboard sends "draw" polyglot sends "setoption DrawOffer draw" to the engine. 35 | 4.to resign: send "info string Resign" to polyglot. 36 | please check the winboard documentation for the draw/drawoffer and resign commands. 37 | 38 | =========1.4w5:================ 39 | Fixed errors in SyncStop handling. 40 | book building: the error message now also contains the game number 41 | added Affinity option: 42 | In the [PolyGlot] section(ini file): 43 | - "Affinity" mask 44 | mask is a bit vector in which each bit represents the processors that a process is allowed to run on. 45 | 46 | some minor bugs fixed 47 | 48 | checks if child did really quit. 49 | 50 | ********************************************************* 51 | === PolyGlot 1.3W ReadMe === 52 | 53 | PolyGlot - a UCI-to-WinBoard Adapter for UCI Chess Engines 54 | Designed by Fabien Letouzey and Morning Yellow 55 | Version: 1.3W, Last Modified: Jan. 2006 56 | 57 | PolyGlot 1.3W is the modified version of 1.3 by Fabien Letouzey, 58 | which can be compiled and run under Windows without CygWin. 59 | 60 | The version of 1.3W has the following modifications: 61 | 1. Added a module, "pipe.h/pipe.cpp". 62 | 2. Removed two modules, "io.h/io.cpp" and "engine.h/engine.cpp". 63 | 3. Modified all statements related on these modules. 64 | 4. fixed "Move Now" (the bug fixed in PolyGlot 1.4). 65 | 66 | PolyGlot 1.3W can be downloaded from: 67 | http://www.elephantbase.net/download/polyglot.rar 68 | For information of "PolyGlot 1.3", see "readme.txt" 69 | For legal information, see "copying.txt" 70 | -------------------------------------------------------------------------------- /README1.4w10UCI: -------------------------------------------------------------------------------- 1 | Description 2 | =========== 3 | 4 | This is an enhanced version of Polyglot 1.4w10 which can be used as a book 5 | engine for Polyglot books on UCI GUIs. It also has some additional features 6 | (see below). 7 | 8 | Some GUIs like ChessBase do not support Winboard engines at all. Other 9 | GUIs do support the Winboard protocol (like Arena) but you still lose 10 | the convenience of setting UCI options through the GUI. 11 | 12 | This new version of Polyglot is fully backward compatible and will in 13 | fact under normal circumstances autodetect the protocol. 14 | 15 | The following is a minimal polyglot.ini file to make an engine use 16 | Polyglot books. 17 | 18 | [Polyglot] 19 | EngineCommand = 20 | [Engine] 21 | 22 | By default this does not use any book but you can set the Polyglot book 23 | in the GUI through the exported UCI option "Polyglot BookFile". 24 | 25 | Any normal polyglot.ini file can be used with this version. The UCI 26 | options you set in the [Engine] section will be honored and the 27 | results will be shown in the GUI as "defaults". 28 | 29 | If you want to override the protocol autodetection (which should 30 | normally not be necessary) you can put 31 | 32 | UCI = true 33 | 34 | in the [Polyglot] section. 35 | 36 | Just like 1.4w10, this version of Polyglot compiles both under Linux 37 | and Windows. 38 | 39 | Some notes on Arena 40 | =================== 41 | 42 | Up to version 1.99b5 Arena contained a bug with drive letters in UCI 43 | options. This bug has been fixed in 2.00. 44 | 45 | If you let Arena autodetect the engine type then it will use Polyglot 46 | in Winboard mode. So either configure the Polyglot adapter explicitly 47 | as a UCI engine in the Arena GUI, or else put UCI = true in the 48 | [Polyglot] section of polyglot.ini. 49 | 50 | 51 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | Move static queue for engine writing to engine struct 2 | Continue refactoring of option stuff (search for option_nb) 3 | Make _ex versions the default (more object oriented). 4 | Remove most uci option commands. Refer directly to Uci->option. It will make the code more 5 | readable. 6 | Replace "append_option" by "insert_option". 7 | Inifile handling. 8 | Refactor removinging of trailing EOL and white space from string (see util.c and ini file parsing). 9 | ML's utility 10 | -------------------------------------------------------------------------------- /adapter.h: -------------------------------------------------------------------------------- 1 | 2 | // adapter.h 3 | 4 | #ifndef ADAPTER_H 5 | #define ADAPTER_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | #include "uci2uci.h" 11 | 12 | // types 13 | 14 | 15 | 16 | 17 | 18 | // functions 19 | 20 | extern void adapter_loop (); 21 | 22 | 23 | #endif // !defined ADAPTER_H 24 | 25 | // end of adapter.h 26 | 27 | -------------------------------------------------------------------------------- /attack.c: -------------------------------------------------------------------------------- 1 | 2 | // attack.c 3 | 4 | // includes 5 | 6 | #include "board.h" 7 | #include "colour.h" 8 | #include "move.h" 9 | #include "attack.h" 10 | #include "piece.h" 11 | #include "util.h" 12 | 13 | // macros 14 | 15 | #define DELTA_INC(delta) (DeltaInc[128+(delta)]) 16 | #define DELTA_MASK(delta) (DeltaMask[128+(delta)]) 17 | 18 | // "constants" 19 | 20 | const sint8 KnightInc[8+1] = { 21 | -33, -31, -18, -14, +14, +18, +31, +33, 0 22 | }; 23 | 24 | const sint8 BishopInc[4+1] = { 25 | -17, -15, +15, +17, 0 26 | }; 27 | 28 | const sint8 RookInc[4+1] = { 29 | -16, -1, +1, +16, 0 30 | }; 31 | 32 | const sint8 QueenInc[8+1] = { 33 | -17, -16, -15, -1, +1, +15, +16, +17, 0 34 | }; 35 | 36 | const sint8 KingInc[8+1] = { 37 | -17, -16, -15, -1, +1, +15, +16, +17, 0 38 | }; 39 | 40 | // variables 41 | 42 | static sint8 DeltaInc[256]; 43 | static uint8 DeltaMask[256]; 44 | 45 | // prototypes 46 | 47 | static bool delta_is_ok (int delta); 48 | static bool inc_is_ok (int inc); 49 | 50 | // functions 51 | 52 | // attack_init() 53 | 54 | void attack_init() { 55 | 56 | int delta; 57 | int dir, inc, dist; 58 | 59 | for (delta = -128; delta < +128; delta++) { 60 | DeltaInc[128+delta] = IncNone; 61 | DeltaMask[128+delta] = 0; 62 | } 63 | 64 | DeltaMask[128-17] |= BlackPawnFlag; 65 | DeltaMask[128-15] |= BlackPawnFlag; 66 | 67 | DeltaMask[128+15] |= WhitePawnFlag; 68 | DeltaMask[128+17] |= WhitePawnFlag; 69 | 70 | for (dir = 0; dir < 8; dir++) { 71 | delta = KnightInc[dir]; 72 | ASSERT(delta_is_ok(delta)); 73 | DeltaMask[128+delta] |= KnightFlag; 74 | } 75 | 76 | for (dir = 0; dir < 4; dir++) { 77 | inc = BishopInc[dir]; 78 | ASSERT(inc!=IncNone); 79 | for (dist = 1; dist < 8; dist++) { 80 | delta = inc*dist; 81 | ASSERT(delta_is_ok(delta)); 82 | ASSERT(DeltaInc[128+delta]==IncNone); 83 | DeltaInc[128+delta] = inc; 84 | DeltaMask[128+delta] |= BishopFlag; 85 | } 86 | } 87 | 88 | for (dir = 0; dir < 4; dir++) { 89 | inc = RookInc[dir]; 90 | ASSERT(inc!=IncNone); 91 | for (dist = 1; dist < 8; dist++) { 92 | delta = inc*dist; 93 | ASSERT(delta_is_ok(delta)); 94 | ASSERT(DeltaInc[128+delta]==IncNone); 95 | DeltaInc[128+delta] = inc; 96 | DeltaMask[128+delta] |= RookFlag; 97 | } 98 | } 99 | 100 | for (dir = 0; dir < 8; dir++) { 101 | delta = KingInc[dir]; 102 | ASSERT(delta_is_ok(delta)); 103 | DeltaMask[128+delta] |= KingFlag; 104 | } 105 | } 106 | 107 | // delta_is_ok() 108 | 109 | static bool delta_is_ok(int delta) { 110 | 111 | if (delta < -119 || delta > +119) return FALSE; 112 | 113 | return TRUE; 114 | } 115 | 116 | // inc_is_ok() 117 | 118 | static bool inc_is_ok(int inc) { 119 | 120 | int dir; 121 | 122 | for (dir = 0; dir < 8; dir++) { 123 | if (KingInc[dir] == inc) return TRUE; 124 | } 125 | 126 | return FALSE; 127 | } 128 | 129 | // is_in_check() 130 | 131 | bool is_in_check(const board_t * board, int colour) { 132 | 133 | ASSERT(board_is_ok(board)); 134 | ASSERT(colour_is_ok(colour)); 135 | 136 | if (board->variant==Horde&&king_pos(board,colour)==SquareNone) return FALSE; 137 | return is_attacked(board,king_pos(board,colour),colour_opp(colour)); 138 | } 139 | 140 | // is_attacked() 141 | 142 | bool is_attacked(const board_t * board, int to, int colour) { 143 | 144 | const uint8 * ptr; 145 | int from, piece; 146 | 147 | ASSERT(board_is_ok(board)); 148 | ASSERT(square_is_ok(to)); 149 | ASSERT(colour_is_ok(colour)); 150 | 151 | for (ptr = board->list[colour]; (from=*ptr) != SquareNone; ptr++) { 152 | 153 | piece = board->square[from]; 154 | ASSERT(colour_equal(piece,colour)); 155 | 156 | if (piece_attack(board,piece,from,to)) return TRUE; 157 | } 158 | 159 | return FALSE; 160 | } 161 | 162 | // piece_attack() 163 | 164 | bool piece_attack(const board_t * board, int piece, int from, int to) { 165 | 166 | int delta; 167 | int inc, sq; 168 | 169 | ASSERT(board_is_ok(board)); 170 | ASSERT(piece_is_ok(piece)); 171 | ASSERT(square_is_ok(from)); 172 | ASSERT(square_is_ok(to)); 173 | 174 | delta = to - from; 175 | ASSERT(delta_is_ok(delta)); 176 | 177 | if ((piece & DELTA_MASK(delta)) == 0) return FALSE; // no pseudo-attack 178 | 179 | if (!piece_is_slider(piece)) return TRUE; 180 | 181 | inc = DELTA_INC(delta); 182 | ASSERT(inc_is_ok(inc)); 183 | 184 | for (sq = from+inc; sq != to; sq += inc) { 185 | ASSERT(square_is_ok(sq)); 186 | if (board->square[sq] != Empty) return FALSE; // blocker 187 | } 188 | 189 | return TRUE; 190 | } 191 | 192 | // is_pinned() 193 | 194 | bool is_pinned(const board_t * board, int from, int to, int colour) { 195 | 196 | int king; 197 | int inc; 198 | int sq, piece; 199 | 200 | ASSERT(board!=NULL); 201 | ASSERT(square_is_ok(from)); 202 | ASSERT(square_is_ok(to)); 203 | ASSERT(colour_is_ok(colour)); 204 | 205 | king = king_pos(board,colour); 206 | 207 | inc = DELTA_INC(king-from); 208 | if (inc == IncNone) return FALSE; // not a line 209 | 210 | sq = from; 211 | do sq += inc; while (board->square[sq] == Empty); 212 | 213 | if (sq != king) return FALSE; // blocker 214 | 215 | sq = from; 216 | do sq -= inc; while ((piece=board->square[sq]) == Empty); 217 | 218 | return square_is_ok(sq) 219 | && (piece & DELTA_MASK(king-sq)) != 0 220 | && piece_colour(piece) == colour_opp(colour) 221 | && DELTA_INC(king-to) != inc; 222 | } 223 | 224 | // end of attack.cpp 225 | 226 | -------------------------------------------------------------------------------- /attack.cpp: -------------------------------------------------------------------------------- 1 | 2 | // attack.cpp 3 | 4 | // includes 5 | 6 | #include "board.h" 7 | #include "colour.h" 8 | #include "move.h" 9 | #include "attack.h" 10 | #include "piece.h" 11 | #include "util.h" 12 | 13 | // macros 14 | 15 | #define DELTA_INC(delta) (DeltaInc[128+(delta)]) 16 | #define DELTA_MASK(delta) (DeltaMask[128+(delta)]) 17 | 18 | // "constants" 19 | 20 | const sint8 KnightInc[8+1] = { 21 | -33, -31, -18, -14, +14, +18, +31, +33, 0 22 | }; 23 | 24 | const sint8 BishopInc[4+1] = { 25 | -17, -15, +15, +17, 0 26 | }; 27 | 28 | const sint8 RookInc[4+1] = { 29 | -16, -1, +1, +16, 0 30 | }; 31 | 32 | const sint8 QueenInc[8+1] = { 33 | -17, -16, -15, -1, +1, +15, +16, +17, 0 34 | }; 35 | 36 | const sint8 KingInc[8+1] = { 37 | -17, -16, -15, -1, +1, +15, +16, +17, 0 38 | }; 39 | 40 | // variables 41 | 42 | static sint8 DeltaInc[256]; 43 | static uint8 DeltaMask[256]; 44 | 45 | // prototypes 46 | 47 | static bool delta_is_ok (int delta); 48 | static bool inc_is_ok (int inc); 49 | 50 | // functions 51 | 52 | // attack_init() 53 | 54 | void attack_init() { 55 | 56 | int delta; 57 | int dir, inc, dist; 58 | 59 | for (delta = -128; delta < +128; delta++) { 60 | DeltaInc[128+delta] = IncNone; 61 | DeltaMask[128+delta] = 0; 62 | } 63 | 64 | DeltaMask[128-17] |= BlackPawnFlag; 65 | DeltaMask[128-15] |= BlackPawnFlag; 66 | 67 | DeltaMask[128+15] |= WhitePawnFlag; 68 | DeltaMask[128+17] |= WhitePawnFlag; 69 | 70 | for (dir = 0; dir < 8; dir++) { 71 | delta = KnightInc[dir]; 72 | ASSERT(delta_is_ok(delta)); 73 | DeltaMask[128+delta] |= KnightFlag; 74 | } 75 | 76 | for (dir = 0; dir < 4; dir++) { 77 | inc = BishopInc[dir]; 78 | ASSERT(inc!=IncNone); 79 | for (dist = 1; dist < 8; dist++) { 80 | delta = inc*dist; 81 | ASSERT(delta_is_ok(delta)); 82 | ASSERT(DeltaInc[128+delta]==IncNone); 83 | DeltaInc[128+delta] = inc; 84 | DeltaMask[128+delta] |= BishopFlag; 85 | } 86 | } 87 | 88 | for (dir = 0; dir < 4; dir++) { 89 | inc = RookInc[dir]; 90 | ASSERT(inc!=IncNone); 91 | for (dist = 1; dist < 8; dist++) { 92 | delta = inc*dist; 93 | ASSERT(delta_is_ok(delta)); 94 | ASSERT(DeltaInc[128+delta]==IncNone); 95 | DeltaInc[128+delta] = inc; 96 | DeltaMask[128+delta] |= RookFlag; 97 | } 98 | } 99 | 100 | for (dir = 0; dir < 8; dir++) { 101 | delta = KingInc[dir]; 102 | ASSERT(delta_is_ok(delta)); 103 | DeltaMask[128+delta] |= KingFlag; 104 | } 105 | } 106 | 107 | // delta_is_ok() 108 | 109 | static bool delta_is_ok(int delta) { 110 | 111 | if (delta < -119 || delta > +119) return false; 112 | 113 | return true; 114 | } 115 | 116 | // inc_is_ok() 117 | 118 | static bool inc_is_ok(int inc) { 119 | 120 | int dir; 121 | 122 | for (dir = 0; dir < 8; dir++) { 123 | if (KingInc[dir] == inc) return true; 124 | } 125 | 126 | return false; 127 | } 128 | 129 | // is_in_check() 130 | 131 | bool is_in_check(const board_t * board, int colour) { 132 | 133 | ASSERT(board_is_ok(board)); 134 | ASSERT(colour_is_ok(colour)); 135 | 136 | return is_attacked(board,king_pos(board,colour),colour_opp(colour)); 137 | } 138 | 139 | // is_attacked() 140 | 141 | bool is_attacked(const board_t * board, int to, int colour) { 142 | 143 | const uint8 * ptr; 144 | int from, piece; 145 | 146 | ASSERT(board_is_ok(board)); 147 | ASSERT(square_is_ok(to)); 148 | ASSERT(colour_is_ok(colour)); 149 | 150 | for (ptr = board->list[colour]; (from=*ptr) != SquareNone; ptr++) { 151 | 152 | piece = board->square[from]; 153 | ASSERT(colour_equal(piece,colour)); 154 | 155 | if (piece_attack(board,piece,from,to)) return true; 156 | } 157 | 158 | return false; 159 | } 160 | 161 | // piece_attack() 162 | 163 | bool piece_attack(const board_t * board, int piece, int from, int to) { 164 | 165 | int delta; 166 | int inc, sq; 167 | 168 | ASSERT(board_is_ok(board)); 169 | ASSERT(piece_is_ok(piece)); 170 | ASSERT(square_is_ok(from)); 171 | ASSERT(square_is_ok(to)); 172 | 173 | delta = to - from; 174 | ASSERT(delta_is_ok(delta)); 175 | 176 | if ((piece & DELTA_MASK(delta)) == 0) return false; // no pseudo-attack 177 | 178 | if (!piece_is_slider(piece)) return true; 179 | 180 | inc = DELTA_INC(delta); 181 | ASSERT(inc_is_ok(inc)); 182 | 183 | for (sq = from+inc; sq != to; sq += inc) { 184 | ASSERT(square_is_ok(sq)); 185 | if (board->square[sq] != Empty) return false; // blocker 186 | } 187 | 188 | return true; 189 | } 190 | 191 | // is_pinned() 192 | 193 | bool is_pinned(const board_t * board, int from, int to, int colour) { 194 | 195 | int king; 196 | int inc; 197 | int sq, piece; 198 | 199 | ASSERT(board!=NULL); 200 | ASSERT(square_is_ok(from)); 201 | ASSERT(square_is_ok(to)); 202 | ASSERT(colour_is_ok(colour)); 203 | 204 | king = king_pos(board,colour); 205 | 206 | inc = DELTA_INC(king-from); 207 | if (inc == IncNone) return false; // not a line 208 | 209 | sq = from; 210 | do sq += inc; while (board->square[sq] == Empty); 211 | 212 | if (sq != king) return false; // blocker 213 | 214 | sq = from; 215 | do sq -= inc; while ((piece=board->square[sq]) == Empty); 216 | 217 | return square_is_ok(sq) 218 | && (piece & DELTA_MASK(king-sq)) != 0 219 | && piece_colour(piece) == colour_opp(colour) 220 | && DELTA_INC(king-to) != inc; 221 | } 222 | 223 | // end of attack.cpp 224 | 225 | -------------------------------------------------------------------------------- /attack.h: -------------------------------------------------------------------------------- 1 | 2 | // attack.h 3 | 4 | #ifndef ATTACK_H 5 | #define ATTACK_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "util.h" 11 | 12 | // defines 13 | 14 | #define IncNone 0 15 | 16 | // "constants" 17 | 18 | extern const sint8 KnightInc[8+1]; 19 | extern const sint8 BishopInc[4+1]; 20 | extern const sint8 RookInc[4+1]; 21 | extern const sint8 QueenInc[8+1]; 22 | extern const sint8 KingInc[8+1]; 23 | 24 | // functions 25 | 26 | extern void attack_init (); 27 | 28 | extern bool is_in_check (const board_t * board, int colour); 29 | extern bool is_attacked (const board_t * board, int to, int colour); 30 | extern bool piece_attack (const board_t * board, int piece, int from, int to); 31 | 32 | extern bool is_pinned (const board_t * board, int from, int to, int colour); 33 | 34 | #endif // !defined ATTACK_H 35 | 36 | // end of attack.h 37 | 38 | -------------------------------------------------------------------------------- /board.h: -------------------------------------------------------------------------------- 1 | 2 | // board.h 3 | 4 | #ifndef BOARD_H 5 | #define BOARD_H 6 | 7 | // includes 8 | 9 | #include "colour.h" 10 | #include "square.h" 11 | #include "util.h" 12 | 13 | // defines 14 | 15 | #define Normal 0 16 | #define Checks 1 17 | #define Atomic 2 18 | #define Horde 3 19 | #define Empty 0 20 | #define SideH 0 21 | #define SideA 1 22 | #define SideNb 2 23 | 24 | // types 25 | 26 | typedef struct { 27 | uint8 variant; 28 | uint8 square[SquareNb]; 29 | sint8 pos[SquareNb]; 30 | 31 | uint8 list[ColourNb][SquareNb]; 32 | sint8 list_size[ColourNb]; 33 | 34 | sint8 number[12]; 35 | 36 | sint8 turn; 37 | uint8 castle[ColourNb][SideNb]; 38 | uint8 ep_square; 39 | 40 | sint16 ply_nb; 41 | sint16 move_nb; 42 | 43 | uint64 key; 44 | } board_t; 45 | 46 | // functions 47 | 48 | extern bool board_is_ok (const board_t * board); 49 | 50 | extern void board_clear (board_t * board); 51 | extern void board_start (board_t * board); 52 | 53 | extern void board_copy (board_t * dst, const board_t * src); 54 | extern bool board_equal (const board_t * board_1, const board_t * board_2); 55 | 56 | extern void board_init_list (board_t * board); 57 | 58 | extern int board_flags (const board_t * board); 59 | 60 | extern bool board_can_play (const board_t * board); 61 | extern int board_mobility (const board_t * board); 62 | 63 | extern bool board_is_check (const board_t * board); 64 | extern bool board_is_mate (const board_t * board); 65 | extern bool board_is_stalemate (const board_t * board); 66 | 67 | extern int king_pos (const board_t * board, int colour); 68 | 69 | extern void board_disp (const board_t * board); 70 | 71 | #endif // !defined BOARD_H 72 | 73 | // end of board.h 74 | 75 | -------------------------------------------------------------------------------- /book.h: -------------------------------------------------------------------------------- 1 | 2 | // book.h 3 | 4 | #ifndef BOOK_H 5 | #define BOOK_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "util.h" 11 | #include "list.h" 12 | 13 | // functions 14 | 15 | extern void book_clear (); 16 | 17 | extern void book_open (const char file_name[]); 18 | extern bool book_is_open (); 19 | extern void book_close (); 20 | 21 | extern bool is_in_book (const board_t * board); 22 | extern int book_move (const board_t * board, bool random); 23 | extern void book_moves (list_t * list, const board_t * board); 24 | extern void book_disp (const board_t * board); 25 | 26 | extern void book_learn_move (const board_t * board, int move, int result); 27 | extern void book_flush (); 28 | 29 | #endif // !defined BOOK_H 30 | 31 | // end of book.h 32 | 33 | -------------------------------------------------------------------------------- /book_make.h: -------------------------------------------------------------------------------- 1 | 2 | // book_make.h 3 | 4 | #ifndef BOOK_MAKE_H 5 | #define BOOK_MAKE_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // functions 12 | 13 | extern void book_make (int argc, char * argv[]); 14 | extern void book_dump (int argc, char * argv[]); 15 | extern void book_info (int argc, char * argv[]); 16 | 17 | #endif // !defined BOOK_MAKE_H 18 | 19 | // end of book_make.h 20 | 21 | -------------------------------------------------------------------------------- /book_merge.cpp: -------------------------------------------------------------------------------- 1 | 2 | // book_merge.cpp 3 | 4 | // includes 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "book_merge.h" 12 | #include "util.h" 13 | 14 | // types 15 | 16 | struct book_t { 17 | FILE * file; 18 | int size; 19 | }; 20 | 21 | struct entry_t { 22 | uint64 key; 23 | uint16 move; 24 | uint16 count; 25 | uint16 n; 26 | uint16 sum; 27 | }; 28 | 29 | // variables 30 | 31 | static book_t In1[1]; 32 | static book_t In2[1]; 33 | static book_t Out[1]; 34 | 35 | // prototypes 36 | 37 | static void book_clear (book_t * book); 38 | 39 | static void book_open (book_t * book, const char file_name[], const char mode[]); 40 | static void book_close (book_t * book); 41 | 42 | static bool read_entry (book_t * book, entry_t * entry, int n); 43 | static void write_entry (book_t * book, const entry_t * entry); 44 | 45 | static uint64 read_integer (FILE * file, int size); 46 | static void write_integer (FILE * file, int size, uint64 n); 47 | 48 | // functions 49 | 50 | // book_merge() 51 | 52 | void book_merge(int argc, char * argv[]) { 53 | 54 | int i; 55 | const char * in_file_1; 56 | const char * in_file_2; 57 | const char * out_file; 58 | int i1, i2; 59 | bool b1, b2; 60 | entry_t e1[1], e2[1]; 61 | int skip; 62 | 63 | in_file_1 = NULL; 64 | my_string_clear(&in_file_1); 65 | 66 | in_file_2 = NULL; 67 | my_string_clear(&in_file_2); 68 | 69 | out_file = NULL; 70 | my_string_set(&out_file,"out.bin"); 71 | 72 | for (i = 1; i < argc; i++) { 73 | 74 | if (false) { 75 | 76 | } else if (my_string_equal(argv[i],"merge-book")) { 77 | 78 | // skip 79 | 80 | } else if (my_string_equal(argv[i],"-in1")) { 81 | 82 | i++; 83 | if (argv[i] == NULL) my_fatal("book_merge(): missing argument\n"); 84 | 85 | my_string_set(&in_file_1,argv[i]); 86 | 87 | } else if (my_string_equal(argv[i],"-in2")) { 88 | 89 | i++; 90 | if (argv[i] == NULL) my_fatal("book_merge(): missing argument\n"); 91 | 92 | my_string_set(&in_file_2,argv[i]); 93 | 94 | } else if (my_string_equal(argv[i],"-out")) { 95 | 96 | i++; 97 | if (argv[i] == NULL) my_fatal("book_merge(): missing argument\n"); 98 | 99 | my_string_set(&out_file,argv[i]); 100 | 101 | } else { 102 | 103 | my_fatal("book_merge(): unknown option \"%s\"\n",argv[i]); 104 | } 105 | } 106 | 107 | book_clear(In1); 108 | book_clear(In2); 109 | book_clear(Out); 110 | 111 | book_open(In1,in_file_1,"rb"); 112 | book_open(In2,in_file_2,"rb"); 113 | book_open(Out,out_file,"wb"); 114 | 115 | skip = 0; 116 | 117 | i1 = 0; 118 | i2 = 0; 119 | 120 | while (true) { 121 | 122 | b1 = read_entry(In1,e1,i1); 123 | b2 = read_entry(In2,e2,i2); 124 | 125 | if (false) { 126 | 127 | } else if (!b1 && !b2) { 128 | 129 | break; 130 | 131 | } else if (b1 && !b2) { 132 | 133 | write_entry(Out,e1); 134 | i1++; 135 | 136 | } else if (b2 && !b1) { 137 | 138 | write_entry(Out,e2); 139 | i2++; 140 | 141 | } else { 142 | 143 | ASSERT(b1); 144 | ASSERT(b2); 145 | 146 | if (false) { 147 | } else if (e1->key < e2->key) { 148 | write_entry(Out,e1); 149 | i1++; 150 | } else if (e1->key > e2->key) { 151 | write_entry(Out,e2); 152 | i2++; 153 | } else { 154 | ASSERT(e1->key==e2->key); 155 | skip++; 156 | i2++; 157 | } 158 | } 159 | } 160 | 161 | book_close(In1); 162 | book_close(In2); 163 | book_close(Out); 164 | 165 | if (skip != 0) { 166 | printf("skipped %d entr%s.\n",skip,(skip>1)?"ies":"y"); 167 | } 168 | 169 | printf("done!\n"); 170 | } 171 | 172 | // book_clear() 173 | 174 | static void book_clear(book_t * book) { 175 | 176 | ASSERT(book!=NULL); 177 | 178 | book->file = NULL; 179 | book->size = 0; 180 | } 181 | 182 | // book_open() 183 | 184 | static void book_open(book_t * book, const char file_name[], const char mode[]) { 185 | 186 | ASSERT(book!=NULL); 187 | ASSERT(file_name!=NULL); 188 | ASSERT(mode!=NULL); 189 | 190 | book->file = fopen(file_name,mode); 191 | if (book->file == NULL) my_fatal("book_open(): can't open file \"%s\": %s\n",file_name,strerror(errno)); 192 | 193 | if (fseek(book->file,0,SEEK_END) == -1) { 194 | my_fatal("book_open(): fseek(): %s\n",strerror(errno)); 195 | } 196 | 197 | book->size = ftell(book->file) / 16; 198 | } 199 | 200 | // book_close() 201 | 202 | static void book_close(book_t * book) { 203 | 204 | ASSERT(book!=NULL); 205 | 206 | if (fclose(book->file) == EOF) { 207 | my_fatal("book_close(): fclose(): %s\n",strerror(errno)); 208 | } 209 | } 210 | 211 | // read_entry() 212 | 213 | static bool read_entry(book_t * book, entry_t * entry, int n) { 214 | 215 | ASSERT(book!=NULL); 216 | ASSERT(entry!=NULL); 217 | 218 | if (n < 0 || n >= book->size) return false; 219 | 220 | ASSERT(n>=0&&nsize); 221 | 222 | if (fseek(book->file,n*16,SEEK_SET) == -1) { 223 | my_fatal("read_entry(): fseek(): %s\n",strerror(errno)); 224 | } 225 | 226 | entry->key = read_integer(book->file,8); 227 | entry->move = read_integer(book->file,2); 228 | entry->count = read_integer(book->file,2); 229 | entry->n = read_integer(book->file,2); 230 | entry->sum = read_integer(book->file,2); 231 | 232 | return true; 233 | } 234 | 235 | // write_entry() 236 | 237 | static void write_entry(book_t * book, const entry_t * entry) { 238 | 239 | ASSERT(book!=NULL); 240 | ASSERT(entry!=NULL); 241 | 242 | write_integer(book->file,8,entry->key); 243 | write_integer(book->file,2,entry->move); 244 | write_integer(book->file,2,entry->count); 245 | write_integer(book->file,2,entry->n); 246 | write_integer(book->file,2,entry->sum); 247 | } 248 | 249 | // read_integer() 250 | 251 | static uint64 read_integer(FILE * file, int size) { 252 | 253 | uint64 n; 254 | int i; 255 | int b; 256 | 257 | ASSERT(file!=NULL); 258 | ASSERT(size>0&&size<=8); 259 | 260 | n = 0; 261 | 262 | for (i = 0; i < size; i++) { 263 | 264 | b = fgetc(file); 265 | 266 | if (b == EOF) { 267 | if (feof(file)) { 268 | my_fatal("read_integer(): fgetc(): EOF reached\n"); 269 | } else { // error 270 | my_fatal("read_integer(): fgetc(): %s\n",strerror(errno)); 271 | } 272 | } 273 | 274 | ASSERT(b>=0&&b<256); 275 | n = (n << 8) | b; 276 | } 277 | 278 | return n; 279 | } 280 | 281 | // write_integer() 282 | 283 | static void write_integer(FILE * file, int size, uint64 n) { 284 | 285 | int i; 286 | int b; 287 | 288 | ASSERT(file!=NULL); 289 | ASSERT(size>0&&size<=8); 290 | ASSERT(size==8||n>>(size*8)==0); 291 | 292 | for (i = size-1; i >= 0; i--) { 293 | 294 | b = (n >> (i*8)) & 0xFF; 295 | ASSERT(b>=0&&b<256); 296 | 297 | if (fputc(b,file) == EOF) { 298 | my_fatal("write_integer(): fputc(): %s\n",strerror(errno)); 299 | } 300 | } 301 | } 302 | 303 | // end of book_merge.cpp 304 | 305 | -------------------------------------------------------------------------------- /book_merge.h: -------------------------------------------------------------------------------- 1 | 2 | // book_merge.h 3 | 4 | #ifndef BOOK_MERGE_H 5 | #define BOOK_MERGE_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // functions 12 | 13 | extern void book_merge (int argc, char * argv[]); 14 | 15 | #endif // !defined BOOK_MERGE_H 16 | 17 | // end of book_merge.h 18 | 19 | -------------------------------------------------------------------------------- /colour.c: -------------------------------------------------------------------------------- 1 | 2 | // colour.c 3 | 4 | // includes 5 | 6 | #include "colour.h" 7 | #include "util.h" 8 | 9 | // functions 10 | 11 | // colour_is_ok() 12 | 13 | bool colour_is_ok(int colour) { 14 | 15 | return colour == Black || colour == White; 16 | } 17 | 18 | // colour_is_white() 19 | 20 | bool colour_is_white(int colour) { 21 | 22 | ASSERT(colour_is_ok(colour)); 23 | 24 | return colour == White; 25 | } 26 | 27 | // colour_is_black() 28 | 29 | bool colour_is_black(int colour) { 30 | 31 | ASSERT(colour_is_ok(colour)); 32 | 33 | return colour == Black; 34 | } 35 | 36 | // colour_equal() 37 | 38 | bool colour_equal(int colour_1, int colour_2) { 39 | 40 | ASSERT(colour_is_ok(colour_2)); 41 | 42 | return (colour_1 & colour_2) != 0; 43 | } 44 | 45 | // colour_opp() 46 | 47 | int colour_opp(int colour) { 48 | 49 | ASSERT(colour_is_ok(colour)); 50 | 51 | return colour ^ (BlackFlag^WhiteFlag); 52 | } 53 | 54 | // end of colour.cpp 55 | 56 | -------------------------------------------------------------------------------- /colour.cpp: -------------------------------------------------------------------------------- 1 | 2 | // colour.cpp 3 | 4 | // includes 5 | 6 | #include "colour.h" 7 | #include "util.h" 8 | 9 | // functions 10 | 11 | // colour_is_ok() 12 | 13 | bool colour_is_ok(int colour) { 14 | 15 | return colour == Black || colour == White; 16 | } 17 | 18 | // colour_is_white() 19 | 20 | bool colour_is_white(int colour) { 21 | 22 | ASSERT(colour_is_ok(colour)); 23 | 24 | return colour == White; 25 | } 26 | 27 | // colour_is_black() 28 | 29 | bool colour_is_black(int colour) { 30 | 31 | ASSERT(colour_is_ok(colour)); 32 | 33 | return colour == Black; 34 | } 35 | 36 | // colour_equal() 37 | 38 | bool colour_equal(int colour_1, int colour_2) { 39 | 40 | ASSERT(colour_is_ok(colour_2)); 41 | 42 | return (colour_1 & colour_2) != 0; 43 | } 44 | 45 | // colour_opp() 46 | 47 | int colour_opp(int colour) { 48 | 49 | ASSERT(colour_is_ok(colour)); 50 | 51 | return colour ^ (BlackFlag^WhiteFlag); 52 | } 53 | 54 | // end of colour.cpp 55 | 56 | -------------------------------------------------------------------------------- /colour.h: -------------------------------------------------------------------------------- 1 | 2 | // colour.h 3 | 4 | #ifndef COLOUR_H 5 | #define COLOUR_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // defines 12 | 13 | #define BlackFlag (1 << 0) 14 | #define WhiteFlag (1 << 1) 15 | #define ColourNone 0 16 | #define Black BlackFlag 17 | #define White WhiteFlag 18 | #define ColourNb 3 19 | 20 | // functions 21 | 22 | extern bool colour_is_ok (int colour); 23 | 24 | extern bool colour_is_white (int colour); 25 | extern bool colour_is_black (int colour); 26 | extern bool colour_equal (int colour_1, int colour_2); 27 | 28 | extern int colour_opp (int colour); 29 | 30 | #endif // !defined COLOUR_H 31 | 32 | // end of colour.h 33 | 34 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | /* config.h. Generated from config.h.in by configure. */ 2 | /* config.h.in. Generated from configure.ac by autoheader. */ 3 | 4 | /* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */ 5 | /* #undef HAVE_DOPRNT */ 6 | 7 | /* Define to 1 if you have the `dup2' function. */ 8 | #define HAVE_DUP2 1 9 | 10 | /* Define to 1 if you have the `floor' function. */ 11 | #define HAVE_FLOOR 1 12 | 13 | /* Define to 1 if you have the `fork' function. */ 14 | #define HAVE_FORK 1 15 | 16 | /* Define to 1 if you have the `gettimeofday' function. */ 17 | #define HAVE_GETTIMEOFDAY 1 18 | 19 | /* Define to 1 if you have the header file. */ 20 | #define HAVE_INTTYPES_H 1 21 | 22 | /* Define to 1 if you have the `m' library (-lm). */ 23 | #define HAVE_LIBM 1 24 | 25 | /* Define to 1 if your system has a GNU libc compatible `malloc' function, and 26 | to 0 otherwise. */ 27 | #define HAVE_MALLOC 1 28 | 29 | /* Define to 1 if you have the `memchr' function. */ 30 | #define HAVE_MEMCHR 1 31 | 32 | /* Define to 1 if you have the `memmove' function. */ 33 | #define HAVE_MEMMOVE 1 34 | 35 | /* Define to 1 if you have the header file. */ 36 | #define HAVE_MEMORY_H 1 37 | 38 | /* Define to 1 if your system has a GNU libc compatible `realloc' function, 39 | and to 0 otherwise. */ 40 | #define HAVE_REALLOC 1 41 | 42 | /* Define to 1 if you have the `select' function. */ 43 | #define HAVE_SELECT 1 44 | 45 | /* Define to 1 if stdbool.h conforms to C99. */ 46 | #define HAVE_STDBOOL_H 1 47 | 48 | /* Define to 1 if you have the header file. */ 49 | #define HAVE_STDINT_H 1 50 | 51 | /* Define to 1 if you have the header file. */ 52 | #define HAVE_STDLIB_H 1 53 | 54 | /* Define to 1 if you have the `strchr' function. */ 55 | #define HAVE_STRCHR 1 56 | 57 | /* Define to 1 if you have the `strdup' function. */ 58 | #define HAVE_STRDUP 1 59 | 60 | /* Define to 1 if you have the `strerror' function. */ 61 | #define HAVE_STRERROR 1 62 | 63 | /* Define to 1 if you have the header file. */ 64 | #define HAVE_STRINGS_H 1 65 | 66 | /* Define to 1 if you have the header file. */ 67 | #define HAVE_STRING_H 1 68 | 69 | /* Define to 1 if you have the `strstr' function. */ 70 | #define HAVE_STRSTR 1 71 | 72 | /* Define to 1 if you have the header file. */ 73 | #define HAVE_SYS_SELECT_H 1 74 | 75 | /* Define to 1 if you have the header file. */ 76 | #define HAVE_SYS_SOCKET_H 1 77 | 78 | /* Define to 1 if you have the header file. */ 79 | #define HAVE_SYS_STAT_H 1 80 | 81 | /* Define to 1 if you have the header file. */ 82 | #define HAVE_SYS_TIME_H 1 83 | 84 | /* Define to 1 if you have the header file. */ 85 | #define HAVE_SYS_TYPES_H 1 86 | 87 | /* Define to 1 if you have the header file. */ 88 | #define HAVE_UNISTD_H 1 89 | 90 | /* Define to 1 if you have the `vfork' function. */ 91 | #define HAVE_VFORK 1 92 | 93 | /* Define to 1 if you have the header file. */ 94 | /* #undef HAVE_VFORK_H */ 95 | 96 | /* Define to 1 if you have the `vprintf' function. */ 97 | #define HAVE_VPRINTF 1 98 | 99 | /* Define to 1 if `fork' works. */ 100 | #define HAVE_WORKING_FORK 1 101 | 102 | /* Define to 1 if `vfork' works. */ 103 | #define HAVE_WORKING_VFORK 1 104 | 105 | /* Define to 1 if the system has the type `_Bool'. */ 106 | #define HAVE__BOOL 1 107 | 108 | /* Define to 1 if assertions should be disabled. */ 109 | #define NDEBUG 1 110 | 111 | /* Name of package */ 112 | #define PACKAGE "polyglot" 113 | 114 | /* Define to the address where bug reports for this package should be sent. */ 115 | #define PACKAGE_BUGREPORT "dandydand@gmail.com" 116 | 117 | /* Define to the full name of this package. */ 118 | #define PACKAGE_NAME "polyglot" 119 | 120 | /* Define to the full name and version of this package. */ 121 | #define PACKAGE_STRING "polyglot 1.4.70b" 122 | 123 | /* Define to the one symbol short name of this package. */ 124 | #define PACKAGE_TARNAME "polyglot" 125 | 126 | /* Define to the home page for this package. */ 127 | #define PACKAGE_URL "" 128 | 129 | /* Define to the version of this package. */ 130 | #define PACKAGE_VERSION "1.4.70b" 131 | 132 | /* Define as the return type of signal handlers (`int' or `void'). */ 133 | #define RETSIGTYPE void 134 | 135 | /* Define to the type of arg 1 for `select'. */ 136 | #define SELECT_TYPE_ARG1 int 137 | 138 | /* Define to the type of args 2, 3 and 4 for `select'. */ 139 | #define SELECT_TYPE_ARG234 (fd_set *) 140 | 141 | /* Define to the type of arg 5 for `select'. */ 142 | #define SELECT_TYPE_ARG5 (struct timeval *) 143 | 144 | /* Define to 1 if you have the ANSI C header files. */ 145 | #define STDC_HEADERS 1 146 | 147 | /* Define to 1 if you can safely include both and . */ 148 | #define TIME_WITH_SYS_TIME 1 149 | 150 | /* Version number of package */ 151 | #define VERSION "1.4.70b" 152 | 153 | /* Define to empty if `const' does not conform to ANSI C. */ 154 | /* #undef const */ 155 | 156 | /* Define to `__inline__' or `__inline' if that's what the C compiler 157 | calls it, or to nothing if 'inline' is not supported under any name. */ 158 | #ifndef __cplusplus 159 | /* #undef inline */ 160 | #endif 161 | 162 | /* Define to rpl_malloc if the replacement function should be used. */ 163 | /* #undef malloc */ 164 | 165 | /* Define to `int' if does not define. */ 166 | /* #undef pid_t */ 167 | 168 | /* Define to rpl_realloc if the replacement function should be used. */ 169 | /* #undef realloc */ 170 | 171 | /* Define to `unsigned int' if does not define. */ 172 | /* #undef size_t */ 173 | 174 | /* Define as `fork' if `vfork' does not work. */ 175 | /* #undef vfork */ 176 | -------------------------------------------------------------------------------- /config.h.in: -------------------------------------------------------------------------------- 1 | /* config.h.in. Generated from configure.ac by autoheader. */ 2 | 3 | /* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */ 4 | #undef HAVE_DOPRNT 5 | 6 | /* Define to 1 if you have the `dup2' function. */ 7 | #undef HAVE_DUP2 8 | 9 | /* Define to 1 if you have the `floor' function. */ 10 | #undef HAVE_FLOOR 11 | 12 | /* Define to 1 if you have the `fork' function. */ 13 | #undef HAVE_FORK 14 | 15 | /* Define to 1 if you have the `gettimeofday' function. */ 16 | #undef HAVE_GETTIMEOFDAY 17 | 18 | /* Define to 1 if you have the header file. */ 19 | #undef HAVE_INTTYPES_H 20 | 21 | /* Define to 1 if you have the `m' library (-lm). */ 22 | #undef HAVE_LIBM 23 | 24 | /* Define to 1 if your system has a GNU libc compatible `malloc' function, and 25 | to 0 otherwise. */ 26 | #undef HAVE_MALLOC 27 | 28 | /* Define to 1 if you have the `memchr' function. */ 29 | #undef HAVE_MEMCHR 30 | 31 | /* Define to 1 if you have the `memmove' function. */ 32 | #undef HAVE_MEMMOVE 33 | 34 | /* Define to 1 if you have the header file. */ 35 | #undef HAVE_MEMORY_H 36 | 37 | /* Define to 1 if your system has a GNU libc compatible `realloc' function, 38 | and to 0 otherwise. */ 39 | #undef HAVE_REALLOC 40 | 41 | /* Define to 1 if you have the `select' function. */ 42 | #undef HAVE_SELECT 43 | 44 | /* Define to 1 if stdbool.h conforms to C99. */ 45 | #undef HAVE_STDBOOL_H 46 | 47 | /* Define to 1 if you have the header file. */ 48 | #undef HAVE_STDINT_H 49 | 50 | /* Define to 1 if you have the header file. */ 51 | #undef HAVE_STDLIB_H 52 | 53 | /* Define to 1 if you have the `strchr' function. */ 54 | #undef HAVE_STRCHR 55 | 56 | /* Define to 1 if you have the `strdup' function. */ 57 | #undef HAVE_STRDUP 58 | 59 | /* Define to 1 if you have the `strerror' function. */ 60 | #undef HAVE_STRERROR 61 | 62 | /* Define to 1 if you have the header file. */ 63 | #undef HAVE_STRINGS_H 64 | 65 | /* Define to 1 if you have the header file. */ 66 | #undef HAVE_STRING_H 67 | 68 | /* Define to 1 if you have the `strstr' function. */ 69 | #undef HAVE_STRSTR 70 | 71 | /* Define to 1 if you have the header file. */ 72 | #undef HAVE_SYS_SELECT_H 73 | 74 | /* Define to 1 if you have the header file. */ 75 | #undef HAVE_SYS_SOCKET_H 76 | 77 | /* Define to 1 if you have the header file. */ 78 | #undef HAVE_SYS_STAT_H 79 | 80 | /* Define to 1 if you have the header file. */ 81 | #undef HAVE_SYS_TIME_H 82 | 83 | /* Define to 1 if you have the header file. */ 84 | #undef HAVE_SYS_TYPES_H 85 | 86 | /* Define to 1 if you have the header file. */ 87 | #undef HAVE_UNISTD_H 88 | 89 | /* Define to 1 if you have the `vfork' function. */ 90 | #undef HAVE_VFORK 91 | 92 | /* Define to 1 if you have the header file. */ 93 | #undef HAVE_VFORK_H 94 | 95 | /* Define to 1 if you have the `vprintf' function. */ 96 | #undef HAVE_VPRINTF 97 | 98 | /* Define to 1 if `fork' works. */ 99 | #undef HAVE_WORKING_FORK 100 | 101 | /* Define to 1 if `vfork' works. */ 102 | #undef HAVE_WORKING_VFORK 103 | 104 | /* Define to 1 if the system has the type `_Bool'. */ 105 | #undef HAVE__BOOL 106 | 107 | /* Define to 1 if assertions should be disabled. */ 108 | #undef NDEBUG 109 | 110 | /* Name of package */ 111 | #undef PACKAGE 112 | 113 | /* Define to the address where bug reports for this package should be sent. */ 114 | #undef PACKAGE_BUGREPORT 115 | 116 | /* Define to the full name of this package. */ 117 | #undef PACKAGE_NAME 118 | 119 | /* Define to the full name and version of this package. */ 120 | #undef PACKAGE_STRING 121 | 122 | /* Define to the one symbol short name of this package. */ 123 | #undef PACKAGE_TARNAME 124 | 125 | /* Define to the home page for this package. */ 126 | #undef PACKAGE_URL 127 | 128 | /* Define to the version of this package. */ 129 | #undef PACKAGE_VERSION 130 | 131 | /* Define as the return type of signal handlers (`int' or `void'). */ 132 | #undef RETSIGTYPE 133 | 134 | /* Define to the type of arg 1 for `select'. */ 135 | #undef SELECT_TYPE_ARG1 136 | 137 | /* Define to the type of args 2, 3 and 4 for `select'. */ 138 | #undef SELECT_TYPE_ARG234 139 | 140 | /* Define to the type of arg 5 for `select'. */ 141 | #undef SELECT_TYPE_ARG5 142 | 143 | /* Define to 1 if you have the ANSI C header files. */ 144 | #undef STDC_HEADERS 145 | 146 | /* Define to 1 if you can safely include both and . */ 147 | #undef TIME_WITH_SYS_TIME 148 | 149 | /* Version number of package */ 150 | #undef VERSION 151 | 152 | /* Define to empty if `const' does not conform to ANSI C. */ 153 | #undef const 154 | 155 | /* Define to `__inline__' or `__inline' if that's what the C compiler 156 | calls it, or to nothing if 'inline' is not supported under any name. */ 157 | #ifndef __cplusplus 158 | #undef inline 159 | #endif 160 | 161 | /* Define to rpl_malloc if the replacement function should be used. */ 162 | #undef malloc 163 | 164 | /* Define to `int' if does not define. */ 165 | #undef pid_t 166 | 167 | /* Define to rpl_realloc if the replacement function should be used. */ 168 | #undef realloc 169 | 170 | /* Define to `unsigned int' if does not define. */ 171 | #undef size_t 172 | 173 | /* Define as `fork' if `vfork' does not work. */ 174 | #undef vfork 175 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ(2.61) 5 | AC_INIT([polyglot], [1.4.70b], [dandydand@gmail.com]) 6 | AM_INIT_AUTOMAKE 7 | AC_CONFIG_SRCDIR([mainloop.c]) 8 | AC_CONFIG_HEADER([config.h]) 9 | 10 | # Checks for programs. 11 | AC_PROG_CC 12 | 13 | # Checks for libraries. 14 | AC_CHECK_LIB([m], [main]) 15 | 16 | # Checks for header files. 17 | AC_HEADER_ASSERT 18 | AC_HEADER_STDC 19 | AC_CHECK_HEADERS([stdlib.h string.h sys/time.h unistd.h]) 20 | 21 | # Checks for typedefs, structures, and compiler characteristics. 22 | AC_HEADER_STDBOOL 23 | AC_C_CONST 24 | AC_C_INLINE 25 | AC_TYPE_PID_T 26 | AC_TYPE_SIZE_T 27 | AC_HEADER_TIME 28 | 29 | # Checks for library functions. 30 | AC_FUNC_FORK 31 | AC_FUNC_MALLOC 32 | AC_FUNC_REALLOC 33 | AC_FUNC_SELECT_ARGTYPES 34 | AC_FUNC_SETVBUF_REVERSED 35 | AC_TYPE_SIGNAL 36 | AC_FUNC_VPRINTF 37 | AC_CHECK_FUNCS([dup2 floor gettimeofday memchr memmove select strchr strdup strerror strstr]) 38 | 39 | 40 | AC_CONFIG_FILES([Makefile]) 41 | AC_OUTPUT 42 | -------------------------------------------------------------------------------- /debian/README: -------------------------------------------------------------------------------- 1 | The Debian Package polyglot 2 | ---------------------------- 3 | 4 | Comments regarding the Package 5 | 6 | -- Michel Van den Bergh Thu, 01 Jan 2009 21:32:12 +0100 7 | -------------------------------------------------------------------------------- /debian/compat: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: polyglot 2 | Section: games 3 | Priority: extra 4 | Maintainer: Michel Van den Bergh 5 | Build-Depends: debhelper (>= 5), autotools-dev 6 | Standards-Version: 3.7.2 7 | 8 | Package: polyglot 9 | Architecture: any 10 | Depends: ${shlibs:Depends}, ${misc:Depends} 11 | Description: A Winboard protocol to UCI protocol adapter 12 | PolyGlot is a "UCI adapter". It connects a GUI interface (such as 13 | XBoard, Winboard, Arena or Chessbase) to a UCI chess engine. 14 | -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- 1 | This package was debianized by Michel Van den Bergh on 2 | Thu, 01 Jan 2009 21:32:12 +0100. 3 | 4 | It was downloaded from 5 | 6 | Upstream Authors: 7 | 8 | Fabien Letouzey 9 | Huang Chen ("Morning Yellow") 10 | Fonzy Bleumers 11 | Michel Van den Bergh 12 | 13 | Copyright: 14 | 15 | Copyright (C) 2006 Fabien Letouzey 16 | Copyright (C) 2006 Huang Chen 17 | Copyright (C) 2009 Fonzy Bleumers 18 | Copyright (C) 2009 Michel Van den Bergh 19 | 20 | License: 21 | 22 | This package is free software; you can redistribute it and/or modify 23 | it under the terms of the GNU General Public License as published by 24 | the Free Software Foundation; either version 2 of the License, or 25 | (at your option) any later version. 26 | 27 | This package is distributed in the hope that it will be useful, 28 | but WITHOUT ANY WARRANTY; without even the implied warranty of 29 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 30 | GNU General Public License for more details. 31 | 32 | You should have received a copy of the GNU General Public License 33 | along with this package; if not, write to the Free Software 34 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 35 | 36 | On Debian systems, the complete text of the GNU General 37 | Public License can be found in `/usr/share/common-licenses/GPL'. 38 | 39 | The Debian packaging is (C) 2009, Michel Van den Bergh and 40 | is licensed under the GPL, see above. 41 | 42 | -------------------------------------------------------------------------------- /debian/docs: -------------------------------------------------------------------------------- 1 | NEWS 2 | README 3 | README1.3 4 | README1.4 5 | README1.4w 6 | README1.4w10UCI 7 | -------------------------------------------------------------------------------- /debian/files: -------------------------------------------------------------------------------- 1 | polyglot_1.4.65b_amd64.deb games extra 2 | -------------------------------------------------------------------------------- /debian/polyglot.substvars: -------------------------------------------------------------------------------- 1 | shlibs:Depends=libc6 (>= 2.4) 2 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | # -*- makefile -*- 3 | # Sample debian/rules that uses debhelper. 4 | # This file was originally written by Joey Hess and Craig Small. 5 | # As a special exception, when this file is copied by dh-make into a 6 | # dh-make output file, you may use that output file without restriction. 7 | # This special exception was added by Craig Small in version 0.37 of dh-make. 8 | 9 | # Uncomment this to turn on verbose mode. 10 | #export DH_VERBOSE=1 11 | 12 | 13 | # These are used for cross-compiling and for saving the configure script 14 | # from having to guess our platform (since we know it already) 15 | DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE) 16 | DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE) 17 | 18 | 19 | config.status: configure 20 | dh_testdir 21 | # Add here commands to configure the package. 22 | ifneq "$(wildcard /usr/share/misc/config.sub)" "" 23 | cp -f /usr/share/misc/config.sub config.sub 24 | endif 25 | ifneq "$(wildcard /usr/share/misc/config.guess)" "" 26 | cp -f /usr/share/misc/config.guess config.guess 27 | endif 28 | ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --bindir=\$${prefix}/games CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs" 29 | 30 | 31 | build: build-stamp 32 | 33 | build-stamp: config.status 34 | dh_testdir 35 | 36 | # Add here commands to compile the package. 37 | $(MAKE) 38 | #docbook-to-man debian/polyglot.sgml > polyglot.1 39 | 40 | touch $@ 41 | 42 | clean: 43 | dh_testdir 44 | dh_testroot 45 | rm -f build-stamp 46 | 47 | # Add here commands to clean up after the build process. 48 | [ ! -f Makefile ] || $(MAKE) distclean 49 | rm -f config.sub config.guess 50 | 51 | dh_clean 52 | 53 | install: build 54 | dh_testdir 55 | dh_testroot 56 | dh_clean -k 57 | dh_installdirs 58 | 59 | # Add here commands to install the package into debian/polyglot. 60 | $(MAKE) DESTDIR=$(CURDIR)/debian/polyglot install 61 | 62 | 63 | # Build architecture-independent files here. 64 | binary-indep: build install 65 | # We have nothing to do by default. 66 | 67 | # Build architecture-dependent files here. 68 | binary-arch: build install 69 | dh_testdir 70 | dh_testroot 71 | dh_installchangelogs ChangeLog 72 | dh_installdocs 73 | dh_installexamples 74 | # dh_install 75 | # dh_installmenu 76 | # dh_installdebconf 77 | # dh_installlogrotate 78 | # dh_installemacsen 79 | # dh_installpam 80 | # dh_installmime 81 | # dh_python 82 | # dh_installinit 83 | # dh_installcron 84 | # dh_installinfo 85 | dh_installman 86 | dh_link 87 | dh_strip 88 | dh_compress 89 | dh_fixperms 90 | # dh_perl 91 | # dh_makeshlibs 92 | dh_installdeb 93 | dh_shlibdeps 94 | dh_gencontrol 95 | dh_md5sums 96 | dh_builddeb 97 | 98 | binary: binary-indep binary-arch 99 | .PHONY: build clean binary-indep binary-arch binary install 100 | -------------------------------------------------------------------------------- /engine.c: -------------------------------------------------------------------------------- 1 | // engine.c 2 | 3 | // includes 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "engine.h" 11 | #include "option.h" 12 | #include "pipex.h" 13 | #include "util.h" 14 | 15 | // variables 16 | 17 | engine_t Engine[1]; 18 | 19 | // functions 20 | 21 | // set_affinity() 22 | 23 | void set_affinity(engine_t *engine, int value){ 24 | pipex_set_affinity(engine->pipex,value); 25 | } 26 | 27 | // engine_set_nice_value() 28 | 29 | void engine_set_nice_value(engine_t *engine, int value){ 30 | pipex_set_priority(engine->pipex,value); 31 | } 32 | 33 | // engine_send_queue() 34 | 35 | void engine_send_queue(engine_t * engine, const char *format, ...) { 36 | char buf[FormatBufferSize]; 37 | CONSTRUCT_ARG_STRING(format,buf); 38 | pipex_write(engine->pipex,buf); 39 | } 40 | 41 | // engine_send() 42 | 43 | void engine_send(engine_t * engine, const char *format, ...) { 44 | char buf[FormatBufferSize]; 45 | CONSTRUCT_ARG_STRING(format,buf); 46 | pipex_writeln(engine->pipex,buf); 47 | } 48 | 49 | // engine_close() 50 | 51 | void engine_close(engine_t * engine){ 52 | char string[StringSize]; 53 | int elapsed_time; 54 | int ret; 55 | int close_timeout=500; 56 | my_log("POLYGLOT Closing engine.\n"); 57 | pipex_send_eof(engine->pipex); 58 | // TODO: Timeout 59 | 60 | elapsed_time=0; 61 | while (!engine_eof(engine) && (elapsed_time=close_timeout){ 70 | my_log("POLYGLOT Waited more than %dms. Moving on.\n",close_timeout); 71 | } 72 | pipex_exit(engine->pipex,200); 73 | } 74 | 75 | // engine_open() 76 | 77 | void engine_open(engine_t * engine){ 78 | int affinity; 79 | pipex_open(engine->pipex, 80 | "Engine", 81 | option_get_string(Option,"EngineDir"), 82 | option_get_string(Option,"EngineCommand")); 83 | if(pipex_active(engine->pipex)){ 84 | //play with affinity (bad idea) 85 | affinity=option_get_int(Option,"Affinity"); 86 | if(affinity!=-1) set_affinity(engine,affinity); //AAA 87 | // set a low priority 88 | if (option_get_bool(Option,"UseNice")){ 89 | my_log("POLYGLOT Adjust Engine Piority\n"); 90 | engine_set_nice_value(engine, option_get_int(Option,"NiceValue")); 91 | } 92 | } 93 | 94 | } 95 | 96 | bool engine_active(engine_t *engine){ 97 | return pipex_active(engine->pipex); 98 | } 99 | 100 | bool engine_eof(engine_t *engine){ 101 | return pipex_eof(engine->pipex); 102 | } 103 | 104 | bool engine_get_non_blocking(engine_t * engine, char *string){ 105 | return pipex_readln_nb(engine->pipex,string); 106 | } 107 | 108 | void engine_get(engine_t * engine, char *string){ 109 | pipex_readln(engine->pipex,string); 110 | } 111 | 112 | 113 | -------------------------------------------------------------------------------- /engine.h: -------------------------------------------------------------------------------- 1 | // engine.h 2 | 3 | #ifndef ENGINE_H 4 | #define ENGINE_H 5 | 6 | // includes 7 | 8 | #include "util.h" 9 | #include "pipex.h" 10 | 11 | // types 12 | 13 | typedef struct { 14 | pipex_t pipex[1]; 15 | } engine_t; 16 | 17 | 18 | 19 | // variables 20 | 21 | extern engine_t Engine[1]; 22 | 23 | // functions 24 | 25 | extern bool engine_is_ok (const engine_t * engine); 26 | extern void engine_open (engine_t * engine); 27 | extern void engine_close (engine_t * engine); 28 | extern bool engine_active (engine_t * engine); 29 | extern bool engine_eof (engine_t * engine); 30 | extern void engine_send (engine_t * engine, const char format[], ...); 31 | extern void engine_send_queue (engine_t * engine, const char format[], ...); 32 | extern bool engine_get_non_blocking(engine_t * engine, char string[]); 33 | extern void engine_get (engine_t * engine, char string[]); 34 | extern void engine_set_nice_value(engine_t * engine, int value); 35 | 36 | #endif // !defined ENGINE_H 37 | -------------------------------------------------------------------------------- /epd.h: -------------------------------------------------------------------------------- 1 | 2 | // epd.h 3 | 4 | #ifndef EPD_H 5 | #define EPD_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // functions 12 | 13 | extern void epd_test (int argc, char * argv[]); 14 | 15 | extern bool epd_get_op (const char record[], const char opcode[], char string[], int size); 16 | 17 | #endif // !defined EPD_H 18 | 19 | // end of epd.h 20 | 21 | -------------------------------------------------------------------------------- /fen.h: -------------------------------------------------------------------------------- 1 | 2 | // fen.h 3 | 4 | #ifndef FEN_H 5 | #define FEN_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "util.h" 11 | 12 | // "constants" 13 | 14 | extern const char * StartFen; 15 | extern const char * StartFenHorde; 16 | 17 | // functions 18 | 19 | extern bool board_from_fen (board_t * board, const char string[]); 20 | extern bool board_to_fen (const board_t * board, char string[], int size); 21 | 22 | #endif // !defined FEN_H 23 | 24 | // end of fen.h 25 | 26 | -------------------------------------------------------------------------------- /game.h: -------------------------------------------------------------------------------- 1 | 2 | // game.h 3 | 4 | #ifndef GAME_H 5 | #define GAME_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "move.h" 11 | #include "util.h" 12 | 13 | // defines 14 | 15 | #define GameSize 4096 16 | 17 | // types 18 | 19 | typedef enum { 20 | PLAYING, 21 | WHITE_MATES, 22 | BLACK_MATES, 23 | STALEMATE, 24 | DRAW_MATERIAL, 25 | DRAW_FIFTY, 26 | DRAW_REPETITION 27 | } status_t; 28 | 29 | // types 30 | 31 | typedef struct { 32 | board_t start_board[1]; 33 | board_t board[1]; 34 | sint16 size; 35 | sint16 pos; 36 | sint8 status; 37 | move_t move[GameSize]; 38 | uint64 key[GameSize]; 39 | } game_t; 40 | 41 | // variables 42 | 43 | extern game_t Game[1]; 44 | 45 | // functions 46 | 47 | extern bool game_is_ok (const game_t * game); 48 | 49 | extern void game_clear (game_t * game); 50 | extern bool game_init (game_t * game, const char fen[]); 51 | 52 | extern int game_status (const game_t * game); 53 | 54 | extern int game_size (const game_t * game); 55 | extern int game_pos (const game_t * game); 56 | extern int game_move (const game_t * game, int pos); 57 | 58 | extern void game_get_board (const game_t * game, board_t * board); 59 | extern void game_get_board_ex (const game_t * game, board_t * board, int pos); 60 | extern int game_turn (const game_t * game); 61 | extern int game_move_nb (const game_t * game); 62 | 63 | extern void game_add_move (game_t * game, int move); 64 | extern void game_rem_move (game_t * game); 65 | 66 | extern void game_goto (game_t * game, int pos); 67 | 68 | extern void game_disp (const game_t * game); 69 | 70 | #endif // !defined GAME_H 71 | 72 | // end of game.h 73 | 74 | -------------------------------------------------------------------------------- /gui.c: -------------------------------------------------------------------------------- 1 | // gui.c 2 | 3 | // includes 4 | 5 | #include 6 | #include 7 | 8 | #include "gui.h" 9 | #include "main.h" 10 | 11 | // variables 12 | 13 | gui_t GUI[1]; 14 | 15 | // functions 16 | 17 | // sig_int() 18 | 19 | static void sig_int(int dummy){ 20 | my_log("POLYGLOT *** SIGINT Received ***\n"); 21 | quit(); 22 | } 23 | 24 | // sig_pipe() 25 | 26 | static void sig_pipe(int dummy){ 27 | my_log("POLYGLOT *** SIGPIPE Received ***\n"); 28 | quit(); 29 | } 30 | 31 | // sig_term() 32 | 33 | static void sig_term(int dummy){ 34 | my_log("POLYGLOT *** SIGTERM Received ***\n"); 35 | quit(); 36 | } 37 | 38 | 39 | // gui_init() 40 | 41 | void gui_init(gui_t *gui){ 42 | 43 | // the following is nice if the "GUI" is a console! 44 | signal(SIGINT,sig_int); 45 | #ifdef SIGTERM 46 | signal(SIGTERM,sig_term); 47 | #endif 48 | #ifdef SIGPIPE 49 | signal(SIGPIPE,sig_pipe); 50 | #endif 51 | pipex_open(gui->pipex,"GUI",NULL,NULL); 52 | } 53 | 54 | 55 | // gui_get_non_blocking() 56 | 57 | bool gui_get_non_blocking(gui_t * gui, char *string) { 58 | 59 | ASSERT(gui!=NULL); 60 | ASSERT(string!=NULL); 61 | 62 | if(pipex_eof(gui->pipex)){ 63 | quit(); 64 | return TRUE; // we never get here 65 | } 66 | return pipex_readln_nb(gui->pipex,string); 67 | } 68 | 69 | // gui_get() 70 | 71 | void gui_get(gui_t * gui, char *string) { 72 | if(pipex_eof(gui->pipex)){ 73 | quit(); 74 | } 75 | pipex_readln(gui->pipex,string); 76 | } 77 | 78 | 79 | // gui_send() 80 | 81 | void gui_send(gui_t * gui, const char format[], ...) { 82 | 83 | char string[FormatBufferSize]; 84 | 85 | ASSERT(gui!=NULL); 86 | ASSERT(format!=NULL); 87 | 88 | // format 89 | 90 | CONSTRUCT_ARG_STRING(format,string); 91 | 92 | // send 93 | 94 | pipex_writeln(gui->pipex,string); 95 | 96 | } 97 | 98 | -------------------------------------------------------------------------------- /gui.cpp: -------------------------------------------------------------------------------- 1 | // gui.cpp 2 | 3 | // includes 4 | 5 | #include 6 | #include 7 | 8 | #include "gui.h" 9 | #include "main.h" 10 | 11 | // constants 12 | 13 | static const int StringSize = 4096; 14 | 15 | // variables 16 | 17 | gui_t GUI[1]; 18 | 19 | // functions 20 | 21 | // sig_quit() 22 | 23 | static void sig_quit(int dummy){ 24 | my_log("POLYGLOT *** SIGINT Received ***\n"); 25 | quit(); 26 | } 27 | 28 | 29 | // gui_init() 30 | 31 | void gui_init(gui_t *gui){ 32 | 33 | // the following is nice if the "GUI" is a console! 34 | signal(SIGINT,sig_quit); 35 | #ifdef _WIN32 36 | signal(SIGTERM,SIG_IGN); 37 | #ifdef SIGPIPE 38 | signal(SIGPIPE,SIG_IGN); 39 | #endif 40 | #endif 41 | 42 | #ifdef _WIN32 43 | (gui->io).Open(); 44 | #else 45 | 46 | gui->io->in_fd = STDIN_FILENO; 47 | gui->io->out_fd = STDOUT_FILENO; 48 | gui->io->name = "GUI"; 49 | 50 | io_init(gui->io); 51 | #endif 52 | } 53 | 54 | 55 | // gui_get_non_blocking() 56 | 57 | bool gui_get_non_blocking(gui_t * gui, char string[], int size) { 58 | 59 | ASSERT(gui!=NULL); 60 | ASSERT(string!=NULL); 61 | ASSERT(size>=256); 62 | #ifndef _WIN32 63 | if(io_line_ready(gui->io)){ 64 | gui_get(GUI,string,StringSize); 65 | return true; 66 | }else{ 67 | string[0]='\0'; 68 | return false; 69 | } 70 | #else 71 | if((gui->io).EOF_()){ 72 | my_log("POLYGLOT *** EOF from GUI ***\n"); 73 | quit(); 74 | return true; // we never get here 75 | }else if ((gui->io).GetBuffer(string)) { 76 | my_log("GUI->Adapter: %s\n", string); 77 | return true; 78 | } else { 79 | string[0]='\0'; 80 | return false; 81 | } 82 | #endif 83 | } 84 | 85 | // gui_get() 86 | 87 | void gui_get(gui_t * gui, char string[], int size) { 88 | bool data_available; 89 | #ifdef _WIN32 90 | if((gui->io).EOF_()){ 91 | my_log("POLYGLOT *** EOF from GUI ***\n"); 92 | quit(); 93 | } 94 | (gui->io).LineInput(string); 95 | my_log("GUI->Adapter: %s\n", string); 96 | #else 97 | if (!io_get_line(gui->io,string,size)) { // EOF 98 | my_log("POLYGLOT *** EOF from GUI ***\n"); 99 | quit(); 100 | } 101 | #endif 102 | } 103 | 104 | 105 | // gui_send() 106 | 107 | void gui_send(gui_t * gui, const char format[], ...) { 108 | 109 | va_list arg_list; 110 | char string[StringSize]; 111 | 112 | ASSERT(gui!=NULL); 113 | ASSERT(format!=NULL); 114 | 115 | // format 116 | 117 | va_start(arg_list,format); 118 | vsprintf(string,format,arg_list); 119 | va_end(arg_list); 120 | 121 | // send 122 | 123 | #ifndef _WIN32 124 | io_send(gui->io,"%s",string); 125 | #else 126 | gui->io.LineOutput(string); 127 | my_log("Adapter->GUI: %s\n",string); 128 | #endif 129 | } 130 | 131 | -------------------------------------------------------------------------------- /gui.h: -------------------------------------------------------------------------------- 1 | // gui.h 2 | 3 | #ifndef GUI_H 4 | #define GUI_H 5 | 6 | // includes 7 | 8 | #include "pipex.h" 9 | #include "io.h" 10 | 11 | // types 12 | 13 | typedef struct { 14 | pipex_t pipex[1]; 15 | } gui_t; 16 | 17 | // variables 18 | 19 | extern gui_t GUI[1]; 20 | 21 | // functions 22 | 23 | extern void gui_init(gui_t * gui); 24 | extern void gui_send(gui_t * gui, const char *format, ...); 25 | extern void gui_get (gui_t * gui, char *string); 26 | extern bool gui_get_non_blocking (gui_t * gui, char *string); 27 | 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /hash.c: -------------------------------------------------------------------------------- 1 | 2 | // hash.c 3 | 4 | // includes 5 | 6 | #include "board.h" 7 | #include "hash.h" 8 | #include "piece.h" 9 | #include "random.h" 10 | #include "square.h" 11 | #include "util.h" 12 | 13 | // variables 14 | 15 | static uint64 Castle64[16]; 16 | 17 | // prototypes 18 | 19 | static uint64 hash_castle_key_debug (int flags); 20 | 21 | // functions 22 | 23 | // hash_init() 24 | 25 | void hash_init() { 26 | 27 | int i; 28 | 29 | for (i = 0; i < 16; i++) Castle64[i] = hash_castle_key_debug(i); 30 | } 31 | 32 | // hash_key() 33 | 34 | uint64 hash_key(const board_t * board) { 35 | 36 | uint64 key; 37 | int colour; 38 | const uint8 * ptr; 39 | int sq, piece; 40 | 41 | ASSERT(board_is_ok(board)); 42 | 43 | // init 44 | 45 | key = board->variant; 46 | 47 | // pieces 48 | 49 | for (colour = 1; colour <= 2; colour++) { // HACK 50 | for (ptr = board->list[colour]; (sq=*ptr) != SquareNone; ptr++) { 51 | piece = board->square[sq]; 52 | key ^= hash_piece_key(piece,sq); 53 | } 54 | } 55 | 56 | // castle flags 57 | 58 | key ^= hash_castle_key(board_flags(board)); 59 | 60 | // en-passant square 61 | 62 | sq = board->ep_square; 63 | if (sq != SquareNone) key ^= hash_ep_key(sq); 64 | 65 | // turn 66 | 67 | key ^= hash_turn_key(board->turn); 68 | 69 | return key; 70 | } 71 | 72 | // hash_piece_key() 73 | 74 | uint64 hash_piece_key(int piece, int square) { 75 | 76 | ASSERT(piece_is_ok(piece)); 77 | ASSERT(square_is_ok(square)); 78 | 79 | return random_64(RandomPiece+piece_to_12(piece)*64+square_to_64(square)); 80 | } 81 | 82 | // hash_castle_key() 83 | 84 | uint64 hash_castle_key(int flags) { 85 | 86 | ASSERT((flags&~0xF)==0); 87 | 88 | return Castle64[flags]; 89 | } 90 | 91 | // hash_castle_key_debug() 92 | 93 | static uint64 hash_castle_key_debug(int flags) { 94 | 95 | uint64 key; 96 | int i; 97 | 98 | ASSERT((flags&~0xF)==0); 99 | 100 | key = 0; 101 | 102 | for (i = 0; i < 4; i++) { 103 | if ((flags & (1<list[colour]; (sq=*ptr) != SquareNone; ptr++) { 51 | piece = board->square[sq]; 52 | key ^= hash_piece_key(piece,sq); 53 | } 54 | } 55 | 56 | // castle flags 57 | 58 | key ^= hash_castle_key(board_flags(board)); 59 | 60 | // en-passant square 61 | 62 | sq = board->ep_square; 63 | if (sq != SquareNone) key ^= hash_ep_key(sq); 64 | 65 | // turn 66 | 67 | key ^= hash_turn_key(board->turn); 68 | 69 | return key; 70 | } 71 | 72 | // hash_piece_key() 73 | 74 | uint64 hash_piece_key(int piece, int square) { 75 | 76 | ASSERT(piece_is_ok(piece)); 77 | ASSERT(square_is_ok(square)); 78 | 79 | return random_64(RandomPiece+piece_to_12(piece)*64+square_to_64(square)); 80 | } 81 | 82 | // hash_castle_key() 83 | 84 | uint64 hash_castle_key(int flags) { 85 | 86 | ASSERT((flags&~0xF)==0); 87 | 88 | return Castle64[flags]; 89 | } 90 | 91 | // hash_castle_key_debug() 92 | 93 | static uint64 hash_castle_key_debug(int flags) { 94 | 95 | uint64 key; 96 | int i; 97 | 98 | ASSERT((flags&~0xF)==0); 99 | 100 | key = 0; 101 | 102 | for (i = 0; i < 4; i++) { 103 | if ((flags & (1< 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | #include "io.h" 17 | #include "util.h" 18 | 19 | // constants 20 | 21 | static const bool UseDebug = false; 22 | static const bool UseCR = false; 23 | 24 | static const int StringSize = 4096; 25 | 26 | static const char LF = '\n'; 27 | static const char CR = '\r'; 28 | 29 | // prototypes 30 | 31 | static int my_read (int fd, char string[], int size); 32 | static void my_write (int fd, const char string[], int size); 33 | 34 | // functions 35 | 36 | // io_is_ok() 37 | 38 | bool io_is_ok(const io_t * io) { 39 | 40 | if (io == NULL) return false; 41 | 42 | if (io->name == NULL) return false; 43 | 44 | if (io->in_eof != true && io->in_eof != false) return false; 45 | 46 | if (io->in_size < 0 || io->in_size > BufferSize) return false; 47 | if (io->out_size < 0 || io->out_size > BufferSize) return false; 48 | 49 | return true; 50 | } 51 | 52 | // io_init() 53 | 54 | void io_init(io_t * io) { 55 | 56 | ASSERT(io!=NULL); 57 | 58 | io->in_eof = false; 59 | 60 | io->in_size = 0; 61 | io->out_size = 0; 62 | 63 | ASSERT(io_is_ok(io)); 64 | } 65 | 66 | // io_close() 67 | 68 | void io_close(io_t * io) { 69 | 70 | ASSERT(io_is_ok(io)); 71 | 72 | ASSERT(io->out_fd>=0); 73 | 74 | my_log("Adapter>Engine: EOF\n",io->name); 75 | 76 | if (close(io->out_fd) == -1) { 77 | my_fatal("io_close(): close(): %s\n",strerror(errno)); 78 | } 79 | 80 | io->out_fd = -1; 81 | } 82 | 83 | // io_get_update() 84 | 85 | void io_get_update(io_t * io) { 86 | 87 | int pos, size; 88 | int n; 89 | 90 | ASSERT(io_is_ok(io)); 91 | 92 | ASSERT(io->in_fd>=0); 93 | ASSERT(!io->in_eof); 94 | 95 | // init 96 | 97 | pos = io->in_size; 98 | 99 | size = BufferSize - pos; 100 | if (size <= 0) my_fatal("io_get_update(): buffer overflow\n"); 101 | 102 | // read as many data as possible 103 | n = my_read(io->in_fd,&io->in_buffer[pos],size); 104 | if (UseDebug) my_log("POLYGLOT read %d byte%s from %s\n",n,(n>1)?"s":"",io->name); 105 | 106 | if (n > 0) { // at least one character was read 107 | 108 | // update buffer size 109 | 110 | ASSERT(n>=1&&n<=size); 111 | 112 | io->in_size += n; 113 | ASSERT(io->in_size>=0&&io->in_size<=BufferSize); 114 | 115 | } else { // EOF 116 | 117 | ASSERT(n==0); 118 | 119 | io->in_eof = true; 120 | } 121 | } 122 | 123 | // io_line_ready() 124 | 125 | bool io_line_ready(const io_t * io) { 126 | 127 | ASSERT(io_is_ok(io)); 128 | 129 | if (io->in_eof) return true; 130 | 131 | if (memchr(io->in_buffer,LF,io->in_size) != NULL) return true; // buffer contains LF 132 | 133 | return false; 134 | } 135 | 136 | // io_get_line() 137 | 138 | bool io_get_line(io_t * io, char string[], int size) { 139 | 140 | int src, dst; 141 | int c; 142 | 143 | ASSERT(io_is_ok(io)); 144 | ASSERT(string!=NULL); 145 | ASSERT(size>=256); 146 | 147 | src = 0; 148 | dst = 0; 149 | 150 | while (true) { 151 | 152 | // test for end of buffer 153 | 154 | if (src >= io->in_size) { 155 | if (io->in_eof) { 156 | my_log("%s->Adapter: EOF\n",io->name); 157 | return false; 158 | } else { 159 | my_fatal("io_get_line(): no EOL in buffer\n"); 160 | } 161 | } 162 | 163 | // test for end of string 164 | 165 | if (dst >= size) my_fatal("io_get_line(): buffer overflow\n"); 166 | 167 | // copy the next character 168 | 169 | c = io->in_buffer[src++]; 170 | 171 | if (c == LF) { // LF => line complete 172 | string[dst] = '\0'; 173 | break; 174 | } else if (c != CR) { // skip CRs 175 | string[dst++] = c; 176 | } 177 | } 178 | 179 | // shift the buffer 180 | 181 | ASSERT(src>0); 182 | 183 | io->in_size -= src; 184 | ASSERT(io->in_size>=0); 185 | 186 | if (io->in_size > 0) memmove(&io->in_buffer[0],&io->in_buffer[src],io->in_size); 187 | 188 | // return 189 | 190 | my_log("%s->Adapter: %s\n",io->name,string); 191 | 192 | return true; 193 | } 194 | 195 | // io_send() 196 | 197 | void io_send(io_t * io, const char format[], ...) { 198 | 199 | va_list arg_list; 200 | char string[StringSize]; 201 | int len; 202 | 203 | ASSERT(io_is_ok(io)); 204 | ASSERT(format!=NULL); 205 | 206 | ASSERT(io->out_fd>=0); 207 | 208 | // format 209 | 210 | va_start(arg_list,format); 211 | vsprintf(string,format,arg_list); 212 | va_end(arg_list); 213 | 214 | // append string to buffer 215 | 216 | len = strlen(string); 217 | if (io->out_size + len > BufferSize-2) my_fatal("io_send(): buffer overflow\n"); 218 | 219 | memcpy(&io->out_buffer[io->out_size],string,len); 220 | io->out_size += len; 221 | 222 | ASSERT(io->out_size>=0&&io->out_size<=BufferSize-2); 223 | 224 | // log 225 | 226 | io->out_buffer[io->out_size] = '\0'; 227 | my_log("Adapter->%s: %s\n",io->name,io->out_buffer); 228 | // my_log("> %f %s %s\n",now_real(),io->name,io->out_buffer); 229 | // append EOL to buffer 230 | 231 | if (UseCR) io->out_buffer[io->out_size++] = CR; 232 | io->out_buffer[io->out_size++] = LF; 233 | 234 | ASSERT(io->out_size>=0&&io->out_size<=BufferSize); 235 | 236 | // flush buffer 237 | 238 | if (UseDebug) my_log("POLYGLOT writing %d byte%s to %s\n",io->out_size,(io->out_size>1)?"s":"",io->name); 239 | my_write(io->out_fd,io->out_buffer,io->out_size); 240 | 241 | io->out_size = 0; 242 | } 243 | 244 | // io_send_queue() 245 | 246 | void io_send_queue(io_t * io, const char format[], ...) { 247 | 248 | va_list arg_list; 249 | char string[StringSize]; 250 | int len; 251 | 252 | ASSERT(io_is_ok(io)); 253 | ASSERT(format!=NULL); 254 | 255 | ASSERT(io->out_fd>=0); 256 | 257 | // format 258 | 259 | va_start(arg_list,format); 260 | vsprintf(string,format,arg_list); 261 | va_end(arg_list); 262 | 263 | // append string to buffer 264 | 265 | len = strlen(string); 266 | if (io->out_size + len > BufferSize-2) my_fatal("io_send_queue(): buffer overflow\n"); 267 | 268 | memcpy(&io->out_buffer[io->out_size],string,len); 269 | io->out_size += len; 270 | 271 | ASSERT(io->out_size>=0&&io->out_size<=BufferSize-2); 272 | } 273 | 274 | // my_read() 275 | 276 | static int my_read(int fd, char string[], int size) { 277 | 278 | int n; 279 | 280 | ASSERT(fd>=0); 281 | ASSERT(string!=NULL); 282 | ASSERT(size>0); 283 | 284 | do { 285 | n = read(fd,string,size); 286 | } while (n == -1 && errno == EINTR); 287 | 288 | if (n == -1) my_fatal("my_read(): read(): %s\n",strerror(errno)); 289 | 290 | ASSERT(n>=0); 291 | 292 | return n; 293 | } 294 | 295 | // my_write() 296 | 297 | static void my_write(int fd, const char string[], int size) { 298 | 299 | int n; 300 | 301 | ASSERT(fd>=0); 302 | ASSERT(string!=NULL); 303 | ASSERT(size>0); 304 | 305 | do { 306 | 307 | n = write(fd,string,size); 308 | 309 | // if (n == -1 && errno != EINTR && errno != EPIPE) my_fatal("my_write(): write(): %s\n",strerror(errno)); 310 | 311 | if (n == -1) { 312 | if (false) { 313 | } else if (errno == EINTR) { 314 | n = 0; // nothing has been written 315 | } else if (errno == EPIPE) { 316 | n = size; // pretend everything has been written 317 | } else { 318 | my_fatal("my_write(): write(): %s\n",strerror(errno)); 319 | } 320 | } 321 | 322 | ASSERT(n>=0); 323 | 324 | string += n; 325 | size -= n; 326 | 327 | } while (size > 0); 328 | 329 | ASSERT(size==0); 330 | } 331 | 332 | // end of io.cpp 333 | 334 | #endif 335 | -------------------------------------------------------------------------------- /io.h: -------------------------------------------------------------------------------- 1 | 2 | // io.h 3 | 4 | #ifndef IO_H 5 | #define IO_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // defined 12 | 13 | #define BufferSize 16384 14 | 15 | // types 16 | 17 | typedef struct { 18 | 19 | int in_fd; 20 | int out_fd; 21 | 22 | const char * name; 23 | 24 | bool in_eof; 25 | 26 | sint32 in_size; 27 | sint32 out_size; 28 | 29 | char in_buffer[BufferSize]; 30 | char out_buffer[BufferSize]; 31 | } io_t; 32 | 33 | // functions 34 | 35 | extern bool io_is_ok (const io_t * io); 36 | 37 | extern void io_init (io_t * io); 38 | extern void io_close (io_t * io); 39 | 40 | extern void io_get_update (io_t * io); 41 | 42 | extern bool io_peek (io_t * io); 43 | 44 | extern bool io_line_ready (const io_t * io); 45 | extern bool io_get_line (io_t * io, char string[], int size); 46 | 47 | extern void io_send (io_t * io, const char format[], ...); 48 | extern void io_send_queue (io_t * io, const char format[], ...); 49 | 50 | #endif // !defined IO_H 51 | 52 | // end of io.h 53 | 54 | -------------------------------------------------------------------------------- /line.c: -------------------------------------------------------------------------------- 1 | 2 | // line.c 3 | 4 | // includes 5 | 6 | #include 7 | 8 | #include "board.h" 9 | #include "line.h" 10 | #include "move.h" 11 | #include "move_do.h" 12 | #include "move_legal.h" 13 | #include "san.h" 14 | #include "util.h" 15 | 16 | // constants 17 | 18 | static const bool Strict = FALSE; // FALSE 19 | static const bool UseDebug = FALSE; // FALSE 20 | 21 | // functions 22 | 23 | // line_is_ok() 24 | 25 | bool line_is_ok(const move_t line[]) { 26 | 27 | int move; 28 | 29 | if (line == NULL) return FALSE; 30 | 31 | while ((move = *line++) != MoveNone) { 32 | if (!move_is_ok(move)) return FALSE; 33 | } 34 | 35 | return TRUE; 36 | } 37 | 38 | // line_clear() 39 | 40 | void line_clear(move_t line[]) { 41 | 42 | ASSERT(line!=NULL); 43 | 44 | *line = MoveNone; 45 | } 46 | 47 | // line_copy() 48 | 49 | void line_copy(move_t dst[], const move_t src[]) { 50 | 51 | ASSERT(dst!=NULL); 52 | ASSERT(src!=NULL); 53 | 54 | ASSERT(dst!=src); 55 | 56 | while ((*dst++ = *src++) != MoveNone) 57 | ; 58 | } 59 | 60 | // line_from_can() 61 | 62 | bool line_from_can (move_t line[], const board_t * board, const char string[], int size) { 63 | 64 | int pos; 65 | char new_string[StringSize], *p; 66 | int move; 67 | board_t new_board[1]; 68 | 69 | ASSERT(line!=NULL); 70 | ASSERT(board_is_ok(board)); 71 | ASSERT(string!=NULL); 72 | ASSERT(size>=LineSize); 73 | 74 | // init 75 | 76 | pos = 0; 77 | board_copy(new_board,board); 78 | 79 | // loop 80 | 81 | strcpy(new_string,string); // HACK 82 | 83 | for (p = strtok(new_string," "); p != NULL; p = strtok(NULL," ")) { 84 | 85 | move = move_from_can(p,new_board); 86 | 87 | ASSERT(move!=MoveNone); 88 | ASSERT(move_is_legal(move,new_board)); 89 | 90 | if (move == MoveNone || !move_is_legal(move,new_board)) break; // HACK: ignore illegal moves 91 | 92 | if (pos >= size) return FALSE; 93 | line[pos++] = move; 94 | 95 | move_do(new_board,move); 96 | } 97 | 98 | if (pos >= size) return FALSE; 99 | line[pos] = MoveNone; 100 | 101 | return TRUE; 102 | } 103 | 104 | // line_to_can() 105 | 106 | bool line_to_can(const move_t line[], const board_t * board, char string[], int size) { 107 | 108 | board_t new_board[1]; 109 | int pos; 110 | int move; 111 | 112 | ASSERT(line_is_ok(line)); 113 | ASSERT(board_is_ok(board)); 114 | ASSERT(string!=NULL); 115 | ASSERT(size>=StringSize); 116 | 117 | // init 118 | 119 | if (size < StringSize) return FALSE; 120 | 121 | board_copy(new_board,board); 122 | pos = 0; 123 | 124 | // loop 125 | 126 | while ((move = *line++) != MoveNone) { 127 | 128 | if (pos != 0) { 129 | if (pos >= size) return FALSE; 130 | string[pos++] = ' '; 131 | } 132 | 133 | if (!move_to_can(move,new_board,&string[pos],size-pos)) return FALSE; 134 | pos += strlen(&string[pos]); 135 | 136 | move_do(new_board,move); 137 | } 138 | 139 | if (pos >= size) return FALSE; 140 | string[pos] = '\0'; 141 | 142 | return TRUE; 143 | } 144 | 145 | // line_to_san() 146 | 147 | bool line_to_san(const move_t line[], const board_t * board, char string[], int size) { 148 | 149 | board_t new_board[1]; 150 | int pos; 151 | int move; 152 | char move_string[256]; 153 | 154 | ASSERT(line_is_ok(line)); 155 | ASSERT(board_is_ok(board)); 156 | ASSERT(string!=NULL); 157 | ASSERT(size>=StringSize); 158 | 159 | // init 160 | 161 | if (size < StringSize) return FALSE; 162 | 163 | board_copy(new_board,board); 164 | pos = 0; 165 | 166 | // loop 167 | 168 | while ((move = *line++) != MoveNone) { 169 | 170 | if (pos != 0) { 171 | if (pos >= size) return FALSE; 172 | string[pos++] = ' '; 173 | } 174 | 175 | if (!move_is_legal(move,new_board) 176 | || !move_to_san(move,new_board,&string[pos],size-pos)) { 177 | 178 | if (Strict || UseDebug) { 179 | 180 | move_to_can(move,new_board,move_string,256); 181 | my_log("POLYGLOT ILLEGAL MOVE IN LINE %s\n",move_string); 182 | 183 | board_disp(new_board); 184 | } 185 | 186 | if (Strict) my_fatal("line_to_san(): illegal move\n"); 187 | 188 | break; 189 | } 190 | 191 | pos += strlen(&string[pos]); 192 | 193 | move_do(new_board,move); 194 | } 195 | 196 | if (pos >= size) return FALSE; 197 | string[pos] = '\0'; 198 | 199 | return TRUE; 200 | } 201 | 202 | // end of line.cpp 203 | 204 | -------------------------------------------------------------------------------- /line.cpp: -------------------------------------------------------------------------------- 1 | 2 | // line.cpp 3 | 4 | // includes 5 | 6 | #include 7 | 8 | #include "board.h" 9 | #include "line.h" 10 | #include "move.h" 11 | #include "move_do.h" 12 | #include "move_legal.h" 13 | #include "san.h" 14 | #include "util.h" 15 | 16 | // constants 17 | 18 | static const bool Strict = false; // false 19 | static const bool UseDebug = false; // false 20 | 21 | static const int StringSize = 1024; 22 | 23 | // functions 24 | 25 | // line_is_ok() 26 | 27 | bool line_is_ok(const move_t line[]) { 28 | 29 | int move; 30 | 31 | if (line == NULL) return false; 32 | 33 | while ((move = *line++) != MoveNone) { 34 | if (!move_is_ok(move)) return false; 35 | } 36 | 37 | return true; 38 | } 39 | 40 | // line_clear() 41 | 42 | void line_clear(move_t line[]) { 43 | 44 | ASSERT(line!=NULL); 45 | 46 | *line = MoveNone; 47 | } 48 | 49 | // line_copy() 50 | 51 | void line_copy(move_t dst[], const move_t src[]) { 52 | 53 | ASSERT(dst!=NULL); 54 | ASSERT(src!=NULL); 55 | 56 | ASSERT(dst!=src); 57 | 58 | while ((*dst++ = *src++) != MoveNone) 59 | ; 60 | } 61 | 62 | // line_from_can() 63 | 64 | bool line_from_can (move_t line[], const board_t * board, const char string[], int size) { 65 | 66 | int pos; 67 | char new_string[StringSize], *p; 68 | int move; 69 | board_t new_board[1]; 70 | 71 | ASSERT(line!=NULL); 72 | ASSERT(board_is_ok(board)); 73 | ASSERT(string!=NULL); 74 | ASSERT(size>=LineSize); 75 | 76 | // init 77 | 78 | pos = 0; 79 | board_copy(new_board,board); 80 | 81 | // loop 82 | 83 | strcpy(new_string,string); // HACK 84 | 85 | for (p = strtok(new_string," "); p != NULL; p = strtok(NULL," ")) { 86 | 87 | move = move_from_can(p,new_board); 88 | 89 | ASSERT(move!=MoveNone); 90 | ASSERT(move_is_legal(move,new_board)); 91 | 92 | if (move == MoveNone || !move_is_legal(move,new_board)) break; // HACK: ignore illegal moves 93 | 94 | if (pos >= size) return false; 95 | line[pos++] = move; 96 | 97 | move_do(new_board,move); 98 | } 99 | 100 | if (pos >= size) return false; 101 | line[pos] = MoveNone; 102 | 103 | return true; 104 | } 105 | 106 | // line_to_can() 107 | 108 | bool line_to_can(const move_t line[], const board_t * board, char string[], int size) { 109 | 110 | board_t new_board[1]; 111 | int pos; 112 | int move; 113 | 114 | ASSERT(line_is_ok(line)); 115 | ASSERT(board_is_ok(board)); 116 | ASSERT(string!=NULL); 117 | ASSERT(size>=StringSize); 118 | 119 | // init 120 | 121 | if (size < StringSize) return false; 122 | 123 | board_copy(new_board,board); 124 | pos = 0; 125 | 126 | // loop 127 | 128 | while ((move = *line++) != MoveNone) { 129 | 130 | if (pos != 0) { 131 | if (pos >= size) return false; 132 | string[pos++] = ' '; 133 | } 134 | 135 | if (!move_to_can(move,new_board,&string[pos],size-pos)) return false; 136 | pos += strlen(&string[pos]); 137 | 138 | move_do(new_board,move); 139 | } 140 | 141 | if (pos >= size) return false; 142 | string[pos] = '\0'; 143 | 144 | return true; 145 | } 146 | 147 | // line_to_san() 148 | 149 | bool line_to_san(const move_t line[], const board_t * board, char string[], int size) { 150 | 151 | board_t new_board[1]; 152 | int pos; 153 | int move; 154 | char move_string[256]; 155 | 156 | ASSERT(line_is_ok(line)); 157 | ASSERT(board_is_ok(board)); 158 | ASSERT(string!=NULL); 159 | ASSERT(size>=StringSize); 160 | 161 | // init 162 | 163 | if (size < StringSize) return false; 164 | 165 | board_copy(new_board,board); 166 | pos = 0; 167 | 168 | // loop 169 | 170 | while ((move = *line++) != MoveNone) { 171 | 172 | if (pos != 0) { 173 | if (pos >= size) return false; 174 | string[pos++] = ' '; 175 | } 176 | 177 | if (!move_is_legal(move,new_board) 178 | || !move_to_san(move,new_board,&string[pos],size-pos)) { 179 | 180 | if (Strict || UseDebug) { 181 | 182 | move_to_can(move,new_board,move_string,256); 183 | my_log("POLYGLOT ILLEGAL MOVE IN LINE %s\n",move_string); 184 | 185 | board_disp(new_board); 186 | } 187 | 188 | if (Strict) my_fatal("line_to_san(): illegal move\n"); 189 | 190 | break; 191 | } 192 | 193 | pos += strlen(&string[pos]); 194 | 195 | move_do(new_board,move); 196 | } 197 | 198 | if (pos >= size) return false; 199 | string[pos] = '\0'; 200 | 201 | return true; 202 | } 203 | 204 | // end of line.cpp 205 | 206 | -------------------------------------------------------------------------------- /line.h: -------------------------------------------------------------------------------- 1 | 2 | // line.h 3 | 4 | #ifndef LINE_H 5 | #define LINE_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "move.h" 11 | #include "util.h" 12 | 13 | // constants 14 | 15 | #define LineSize 256 16 | 17 | // functions 18 | 19 | extern bool line_is_ok (const move_t line[]); 20 | 21 | extern void line_clear (move_t line[]); 22 | extern void line_copy (move_t dst[], const move_t src[]); 23 | 24 | extern bool line_from_can (move_t line[], const board_t * board, const char string[], int size); 25 | 26 | extern bool line_to_can (const move_t line[], const board_t * board, char string[], int size); 27 | extern bool line_to_san (const move_t line[], const board_t * board, char string[], int size); 28 | 29 | #endif // !defined LINE_H 30 | 31 | // end of line.h 32 | 33 | -------------------------------------------------------------------------------- /list.c: -------------------------------------------------------------------------------- 1 | 2 | // list.c 3 | 4 | // includes 5 | 6 | #include "board.h" 7 | #include "list.h" 8 | #include "move.h" 9 | #include "util.h" 10 | 11 | // functions 12 | 13 | // list_is_ok() 14 | 15 | bool list_is_ok(const list_t * list) { 16 | 17 | if (list == NULL) return FALSE; 18 | 19 | if (list->size >= ListSize) return FALSE; 20 | 21 | return TRUE; 22 | } 23 | 24 | // list_clear() 25 | 26 | void list_clear(list_t * list) { 27 | 28 | ASSERT(list!=NULL); 29 | 30 | list->size = 0; 31 | } 32 | 33 | // list_add 34 | 35 | void list_add(list_t * list, int move){ 36 | list_add_ex(list, move, 0); 37 | } 38 | 39 | // list_add_ex() 40 | 41 | void list_add_ex(list_t * list, int move, int value) { 42 | 43 | ASSERT(list_is_ok(list)); 44 | ASSERT(move_is_ok(move)); 45 | ASSERT(value>=-32767&&value<=+32767); 46 | 47 | ASSERT(list->sizemove[list->size] = move; 50 | list->value[list->size] = value; 51 | list->size++; 52 | } 53 | 54 | // list_remove() 55 | 56 | void list_remove(list_t * list, int index) { 57 | 58 | int i; 59 | 60 | ASSERT(list_is_ok(list)); 61 | ASSERT(index>=0&&indexsize); 62 | 63 | for (i = index; i < list->size-1; i++) { 64 | list->move[i] = list->move[i+1]; 65 | list->value[i] = list->value[i+1]; 66 | } 67 | 68 | list->size--; 69 | } 70 | 71 | // list_is_empty() 72 | 73 | bool list_is_empty(const list_t * list) { 74 | 75 | ASSERT(list_is_ok(list)); 76 | 77 | return list->size == 0; 78 | } 79 | 80 | // list_size() 81 | 82 | int list_size(const list_t * list) { 83 | 84 | ASSERT(list_is_ok(list)); 85 | 86 | return list->size; 87 | } 88 | 89 | // list_move() 90 | 91 | int list_move(const list_t * list, int index) { 92 | 93 | ASSERT(list_is_ok(list)); 94 | ASSERT(index>=0&&indexsize); 95 | 96 | return list->move[index]; 97 | } 98 | 99 | // list_value() 100 | 101 | int list_value(const list_t * list, int index) { 102 | 103 | ASSERT(list_is_ok(list)); 104 | ASSERT(index>=0&&indexsize); 105 | 106 | return list->value[index]; 107 | } 108 | 109 | // list_copy() 110 | 111 | void list_copy(list_t * dst, const list_t * src) { 112 | 113 | int i; 114 | 115 | ASSERT(dst!=NULL); 116 | ASSERT(list_is_ok(src)); 117 | 118 | dst->size = src->size; 119 | 120 | for (i = 0; i < src->size; i++) { 121 | dst->move[i] = src->move[i]; 122 | dst->value[i] = src->value[i]; 123 | } 124 | } 125 | 126 | // list_move_to_front() 127 | 128 | void list_move_to_front(list_t * list, int index) { 129 | 130 | int i; 131 | int move, value; 132 | 133 | ASSERT(list_is_ok(list)); 134 | ASSERT(index>=0&&indexsize); 135 | 136 | if (index != 0) { 137 | 138 | move = list->move[index]; 139 | value = list->value[index]; 140 | 141 | for (i = index; i > 0; i--) { 142 | list->move[i] = list->move[i-1]; 143 | list->value[i] = list->value[i-1]; 144 | } 145 | 146 | list->move[0] = move; 147 | list->value[0] = value; 148 | } 149 | } 150 | 151 | // list_note() 152 | 153 | void list_note(list_t * list) { 154 | 155 | int i, move; 156 | 157 | ASSERT(list_is_ok(list)); 158 | 159 | for (i = 0; i < list->size; i++) { 160 | move = list->move[i]; 161 | ASSERT(move_is_ok(move)); 162 | list->value[i] = -move_order(move); 163 | } 164 | } 165 | 166 | // list_sort() 167 | 168 | void list_sort(list_t * list) { 169 | 170 | int i, j; 171 | int best_index, best_move, best_value; 172 | 173 | ASSERT(list_is_ok(list)); 174 | 175 | for (i = 0; i < list->size-1; i++) { 176 | 177 | best_index = i; 178 | best_value = list->value[i]; 179 | 180 | for (j = i+1; j < list->size; j++) { 181 | if (list->value[j] > best_value) { 182 | best_index = j; 183 | best_value = list->value[j]; 184 | } 185 | } 186 | 187 | if (best_index != i) { 188 | 189 | best_move = list->move[best_index]; 190 | ASSERT(best_value==list->value[best_index]); 191 | 192 | for (j = best_index; j > i; j--) { 193 | list->move[j] = list->move[j-1]; 194 | list->value[j] = list->value[j-1]; 195 | } 196 | 197 | list->move[i] = best_move; 198 | list->value[i] = best_value; 199 | } 200 | } 201 | 202 | if (DEBUG) { 203 | for (i = 0; i < list->size-1; i++) { 204 | ASSERT(list->value[i]>=list->value[i+1]); 205 | } 206 | } 207 | } 208 | 209 | // list_contain() 210 | 211 | bool list_contain(const list_t * list, int move) { 212 | 213 | int i; 214 | 215 | ASSERT(list_is_ok(list)); 216 | ASSERT(move_is_ok(move)); 217 | 218 | for (i = 0; i < list->size; i++) { 219 | if (list->move[i] == move) return TRUE; 220 | } 221 | 222 | return FALSE; 223 | } 224 | 225 | // list_equal() 226 | 227 | bool list_equal(list_t * list_1, list_t * list_2) { 228 | 229 | list_t copy_1[1], copy_2[1]; 230 | int i; 231 | 232 | ASSERT(list_is_ok(list_1)); 233 | ASSERT(list_is_ok(list_2)); 234 | 235 | if (list_1->size != list_2->size) return FALSE; 236 | 237 | list_copy(copy_1,list_1); 238 | list_note(copy_1); 239 | list_sort(copy_1); 240 | 241 | list_copy(copy_2,list_2); 242 | list_note(copy_2); 243 | list_sort(copy_2); 244 | 245 | for (i = 0; i < copy_1->size; i++) { 246 | if (copy_1->move[i] != copy_2->move[i]) return FALSE; 247 | } 248 | 249 | return TRUE; 250 | } 251 | 252 | // list_disp() 253 | 254 | void list_disp(const list_t * list, const board_t * board) { 255 | 256 | int i, move, value; 257 | char string[256]; 258 | 259 | ASSERT(list_is_ok(list)); 260 | ASSERT(board_is_ok(board)); 261 | 262 | for (i = 0; i < list->size; i++) { 263 | 264 | move = list->move[i]; 265 | value = list->value[i]; 266 | 267 | if (!move_to_can(move,board,string,256)) ASSERT(FALSE); 268 | my_log("POLYGLOT %-5s %04X %+4d\n",string,move,value); 269 | } 270 | 271 | my_log("POLYGLOT\n"); 272 | } 273 | 274 | // end of list.cpp 275 | 276 | -------------------------------------------------------------------------------- /list.cpp: -------------------------------------------------------------------------------- 1 | 2 | // list.cpp 3 | 4 | // includes 5 | 6 | #include "board.h" 7 | #include "list.h" 8 | #include "move.h" 9 | #include "util.h" 10 | 11 | // functions 12 | 13 | // list_is_ok() 14 | 15 | bool list_is_ok(const list_t * list) { 16 | 17 | if (list == NULL) return false; 18 | 19 | if (list->size >= ListSize) return false; 20 | 21 | return true; 22 | } 23 | 24 | // list_clear() 25 | 26 | void list_clear(list_t * list) { 27 | 28 | ASSERT(list!=NULL); 29 | 30 | list->size = 0; 31 | } 32 | 33 | // list_add() 34 | 35 | void list_add(list_t * list, int move, int value) { 36 | 37 | ASSERT(list_is_ok(list)); 38 | ASSERT(move_is_ok(move)); 39 | ASSERT(value>=-32767&&value<=+32767); 40 | 41 | ASSERT(list->sizemove[list->size] = move; 44 | list->value[list->size] = value; 45 | list->size++; 46 | } 47 | 48 | // list_remove() 49 | 50 | void list_remove(list_t * list, int index) { 51 | 52 | int i; 53 | 54 | ASSERT(list_is_ok(list)); 55 | ASSERT(index>=0&&indexsize); 56 | 57 | for (i = index; i < list->size-1; i++) { 58 | list->move[i] = list->move[i+1]; 59 | list->value[i] = list->value[i+1]; 60 | } 61 | 62 | list->size--; 63 | } 64 | 65 | // list_is_empty() 66 | 67 | bool list_is_empty(const list_t * list) { 68 | 69 | ASSERT(list_is_ok(list)); 70 | 71 | return list->size == 0; 72 | } 73 | 74 | // list_size() 75 | 76 | int list_size(const list_t * list) { 77 | 78 | ASSERT(list_is_ok(list)); 79 | 80 | return list->size; 81 | } 82 | 83 | // list_move() 84 | 85 | int list_move(const list_t * list, int index) { 86 | 87 | ASSERT(list_is_ok(list)); 88 | ASSERT(index>=0&&indexsize); 89 | 90 | return list->move[index]; 91 | } 92 | 93 | // list_value() 94 | 95 | int list_value(const list_t * list, int index) { 96 | 97 | ASSERT(list_is_ok(list)); 98 | ASSERT(index>=0&&indexsize); 99 | 100 | return list->value[index]; 101 | } 102 | 103 | // list_copy() 104 | 105 | void list_copy(list_t * dst, const list_t * src) { 106 | 107 | int i; 108 | 109 | ASSERT(dst!=NULL); 110 | ASSERT(list_is_ok(src)); 111 | 112 | dst->size = src->size; 113 | 114 | for (i = 0; i < src->size; i++) { 115 | dst->move[i] = src->move[i]; 116 | dst->value[i] = src->value[i]; 117 | } 118 | } 119 | 120 | // list_move_to_front() 121 | 122 | void list_move_to_front(list_t * list, int index) { 123 | 124 | int i; 125 | int move, value; 126 | 127 | ASSERT(list_is_ok(list)); 128 | ASSERT(index>=0&&indexsize); 129 | 130 | if (index != 0) { 131 | 132 | move = list->move[index]; 133 | value = list->value[index]; 134 | 135 | for (i = index; i > 0; i--) { 136 | list->move[i] = list->move[i-1]; 137 | list->value[i] = list->value[i-1]; 138 | } 139 | 140 | list->move[0] = move; 141 | list->value[0] = value; 142 | } 143 | } 144 | 145 | // list_note() 146 | 147 | void list_note(list_t * list) { 148 | 149 | int i, move; 150 | 151 | ASSERT(list_is_ok(list)); 152 | 153 | for (i = 0; i < list->size; i++) { 154 | move = list->move[i]; 155 | ASSERT(move_is_ok(move)); 156 | list->value[i] = -move_order(move); 157 | } 158 | } 159 | 160 | // list_sort() 161 | 162 | void list_sort(list_t * list) { 163 | 164 | int i, j; 165 | int best_index, best_move, best_value; 166 | 167 | ASSERT(list_is_ok(list)); 168 | 169 | for (i = 0; i < list->size-1; i++) { 170 | 171 | best_index = i; 172 | best_value = list->value[i]; 173 | 174 | for (j = i+1; j < list->size; j++) { 175 | if (list->value[j] > best_value) { 176 | best_index = j; 177 | best_value = list->value[j]; 178 | } 179 | } 180 | 181 | if (best_index != i) { 182 | 183 | best_move = list->move[best_index]; 184 | ASSERT(best_value==list->value[best_index]); 185 | 186 | for (j = best_index; j > i; j--) { 187 | list->move[j] = list->move[j-1]; 188 | list->value[j] = list->value[j-1]; 189 | } 190 | 191 | list->move[i] = best_move; 192 | list->value[i] = best_value; 193 | } 194 | } 195 | 196 | if (DEBUG) { 197 | for (i = 0; i < list->size-1; i++) { 198 | ASSERT(list->value[i]>=list->value[i+1]); 199 | } 200 | } 201 | } 202 | 203 | // list_contain() 204 | 205 | bool list_contain(const list_t * list, int move) { 206 | 207 | int i; 208 | 209 | ASSERT(list_is_ok(list)); 210 | ASSERT(move_is_ok(move)); 211 | 212 | for (i = 0; i < list->size; i++) { 213 | if (list->move[i] == move) return true; 214 | } 215 | 216 | return false; 217 | } 218 | 219 | // list_equal() 220 | 221 | bool list_equal(list_t * list_1, list_t * list_2) { 222 | 223 | list_t copy_1[1], copy_2[1]; 224 | int i; 225 | 226 | ASSERT(list_is_ok(list_1)); 227 | ASSERT(list_is_ok(list_2)); 228 | 229 | if (list_1->size != list_2->size) return false; 230 | 231 | list_copy(copy_1,list_1); 232 | list_note(copy_1); 233 | list_sort(copy_1); 234 | 235 | list_copy(copy_2,list_2); 236 | list_note(copy_2); 237 | list_sort(copy_2); 238 | 239 | for (i = 0; i < copy_1->size; i++) { 240 | if (copy_1->move[i] != copy_2->move[i]) return false; 241 | } 242 | 243 | return true; 244 | } 245 | 246 | // list_disp() 247 | 248 | void list_disp(const list_t * list, const board_t * board) { 249 | 250 | int i, move, value; 251 | char string[256]; 252 | 253 | ASSERT(list_is_ok(list)); 254 | ASSERT(board_is_ok(board)); 255 | 256 | for (i = 0; i < list->size; i++) { 257 | 258 | move = list->move[i]; 259 | value = list->value[i]; 260 | 261 | if (!move_to_can(move,board,string,256)) ASSERT(false); 262 | my_log("POLYGLOT %-5s %04X %+4d\n",string,move,value); 263 | } 264 | 265 | my_log("POLYGLOT\n"); 266 | } 267 | 268 | // end of list.cpp 269 | 270 | -------------------------------------------------------------------------------- /list.h: -------------------------------------------------------------------------------- 1 | 2 | // list.h 3 | 4 | #ifndef LIST_H 5 | #define LIST_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "move.h" 11 | #include "util.h" 12 | 13 | // defines 14 | 15 | #define ListSize 256 16 | 17 | // types 18 | 19 | typedef struct { 20 | sint16 size; 21 | move_t move[ListSize]; 22 | sint16 value[ListSize]; 23 | } list_t; 24 | 25 | // functions 26 | 27 | extern bool list_is_ok (const list_t * list); 28 | 29 | extern void list_clear (list_t * list); 30 | extern void list_add (list_t * list, int move); 31 | extern void list_add_ex (list_t * list, int move, int value); 32 | 33 | extern void list_remove (list_t * list, int index); 34 | 35 | extern bool list_is_empty (const list_t * list); 36 | extern int list_size (const list_t * list); 37 | 38 | extern int list_move (const list_t * list, int index); 39 | extern int list_value (const list_t * list, int index); 40 | 41 | extern void list_copy (list_t * dst, const list_t * src); 42 | 43 | extern void list_note (list_t * list); 44 | extern void list_sort (list_t * list); 45 | 46 | extern bool list_contain (const list_t * list, int move); 47 | extern bool list_equal (list_t * list_1, list_t * list_2); 48 | 49 | extern void list_disp (const list_t * list, const board_t * board); 50 | 51 | #endif // !defined LIST_H 52 | 53 | // end of list.h 54 | 55 | -------------------------------------------------------------------------------- /main.h: -------------------------------------------------------------------------------- 1 | 2 | // main.h 3 | 4 | #ifndef MAIN_H 5 | #define MAIN_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // functions 12 | 13 | extern void quit (); 14 | extern void polyglot_set_option(const char *name, const char *value); 15 | 16 | #endif // !defined MAIN_H 17 | 18 | // end of main.h 19 | 20 | -------------------------------------------------------------------------------- /mainloop.c: -------------------------------------------------------------------------------- 1 | // mainloop.c 2 | 3 | // includes 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "main.h" 11 | #include "engine.h" 12 | #include "gui.h" 13 | #include "option.h" 14 | //#include "ini.h" 15 | #include "xboard2uci.h" 16 | #include "uci2uci.h" 17 | 18 | // prototypes 19 | 20 | static void mainloop_init (); 21 | static void mainloop_wait_for_event (); 22 | static void mainloop_engine_step(char * string); 23 | static void mainloop_gui_step(char * string); 24 | 25 | // functions 26 | 27 | // mainloop_init() 28 | 29 | static void mainloop_init(){ 30 | if(!option_get_bool(Option,"UCI")){ 31 | xboard2uci_init(); // the default 32 | } 33 | } 34 | 35 | // mainloop_engine_step() 36 | 37 | static void mainloop_engine_step(char * string){ 38 | if(option_get_bool(Option,"UCI")){ 39 | uci2uci_engine_step(string); 40 | }else{ 41 | xboard2uci_engine_step(string); 42 | } 43 | } 44 | 45 | // mainloop_gui_step() 46 | 47 | static void mainloop_gui_step(char * string){ 48 | if(option_get_bool(Option,"UCI")){ 49 | uci2uci_gui_step(string); 50 | }else if(my_string_equal(string,"uci")){ // mode auto detection 51 | my_log("POLYGLOT *** Switching to UCI mode ***\n"); 52 | option_set(Option,"UCI","true"); 53 | uci2uci_gui_step(string); 54 | }else{ 55 | xboard2uci_gui_step(string); 56 | } 57 | } 58 | 59 | // mainloop() 60 | 61 | void mainloop() { 62 | char string[StringSize]; 63 | my_log("POLYGLOT *** Mainloop started ***\n"); 64 | mainloop_init(); 65 | while (!engine_eof(Engine)) { 66 | // process buffered lines 67 | while(TRUE){ 68 | if(gui_get_non_blocking(GUI,string)){ 69 | mainloop_gui_step(string); 70 | }else if(!engine_eof(Engine) && 71 | engine_get_non_blocking(Engine,string) ){ 72 | mainloop_engine_step(string); 73 | }else{ 74 | break; 75 | } 76 | } 77 | mainloop_wait_for_event(); 78 | } 79 | my_log("POLYGLOT *** Mainloop has ended ***\n"); 80 | // This should be handled better. 81 | engine_close(Engine); 82 | my_log("POLYGLOT Calling exit\n"); 83 | exit(EXIT_SUCCESS); 84 | 85 | } 86 | 87 | 88 | 89 | 90 | // mainloop_wait_for_event() 91 | 92 | static void mainloop_wait_for_event(){ 93 | pipex_t *pipex[3]; 94 | pipex[0]=GUI->pipex; 95 | pipex[1]=Engine->pipex; 96 | pipex[2]=NULL; 97 | pipex_wait_event(pipex); 98 | } 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /mainloop.cpp: -------------------------------------------------------------------------------- 1 | // mainloop.cpp 2 | 3 | // constants 4 | 5 | static const int StringSize = 4096; 6 | 7 | // includes 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #ifndef _WIN32 15 | #include 16 | #include // Mac OS X needs this one 17 | #include 18 | #endif 19 | 20 | #include "main.h" 21 | #include "engine.h" 22 | #include "gui.h" 23 | #include "option.h" 24 | #include "xboard2uci.h" 25 | #include "uci2uci.h" 26 | 27 | // prototypes 28 | 29 | static void mainloop_init (); 30 | static void mainloop_wait_for_event (); 31 | static void mainloop_engine_step(char * string); 32 | static void mainloop_gui_step(char * string); 33 | 34 | // functions 35 | 36 | // mainloop_init() 37 | 38 | static void mainloop_init(){ 39 | if(!option_get_bool("UCI")){ 40 | xboard2uci_init(); // the default 41 | } 42 | } 43 | 44 | // mainloop_engine_step() 45 | 46 | static void mainloop_engine_step(char * string){ 47 | if(option_get_bool("UCI")){ 48 | uci2uci_engine_step(string); 49 | }else{ 50 | xboard2uci_engine_step(string); 51 | } 52 | } 53 | 54 | // mainloop_gui_step() 55 | 56 | static void mainloop_gui_step(char * string){ 57 | if(option_get_bool("UCI")){ 58 | uci2uci_gui_step(string); 59 | }else if(my_string_equal(string,"uci")){ // mode auto detection 60 | my_log("POLYGLOT *** Switching to UCI mode ***\n"); 61 | option_set("UCI","true"); 62 | uci2uci_gui_step(string); 63 | }else{ 64 | xboard2uci_gui_step(string); 65 | } 66 | } 67 | 68 | // mainloop() 69 | 70 | void mainloop() { 71 | char string[StringSize]; 72 | mainloop_init(); 73 | while (!engine_eof(Engine)) { 74 | // process buffered lines 75 | while(TRUE){ 76 | if(gui_get_non_blocking(GUI,string,StringSize)){ 77 | mainloop_gui_step(string); 78 | }else if(!engine_eof(Engine) && 79 | engine_get_non_blocking(Engine,string,StringSize) ){ 80 | mainloop_engine_step(string); 81 | }else{ 82 | break; 83 | } 84 | } 85 | mainloop_wait_for_event(); 86 | } 87 | my_log("POLYGLOT *** Mainloop has ended ***\n"); 88 | // This should be handled better. 89 | engine_close(Engine); 90 | my_log("POLYGLOT Calling exit\n"); 91 | exit(EXIT_SUCCESS); 92 | 93 | } 94 | 95 | 96 | 97 | 98 | // mainloop_wait_for_event() 99 | 100 | static void mainloop_wait_for_event(){ 101 | #ifdef _WIN32 102 | HANDLE hHandles[2]; 103 | char string[StringSize]; 104 | hHandles[0]=(GUI->io).hEvent; 105 | hHandles[1]=(Engine->io).hEvent; 106 | WaitForMultipleObjects(2, // count 107 | hHandles, // 108 | FALSE, // return if one object is signaled 109 | INFINITE // no timeout 110 | ); 111 | #else 112 | fd_set set[1]; 113 | int fd_max; 114 | int val; 115 | char string[StringSize]; 116 | // init 117 | 118 | FD_ZERO(set); 119 | fd_max = -1; // HACK 120 | 121 | // add gui input 122 | 123 | ASSERT(GUI->io->in_fd>=0); 124 | 125 | FD_SET(GUI->io->in_fd,set); 126 | if (GUI->io->in_fd > fd_max) fd_max = GUI->io->in_fd; 127 | 128 | // add engine input 129 | 130 | ASSERT(Engine->io->in_fd>=0); 131 | 132 | FD_SET(Engine->io->in_fd,set); 133 | if (Engine->io->in_fd > fd_max) fd_max = Engine->io->in_fd; 134 | 135 | // wait for something to read (no timeout) 136 | 137 | ASSERT(fd_max>=0); 138 | val = select(fd_max+1,set,NULL,NULL,NULL); 139 | if (val == -1 && errno != EINTR) my_fatal("adapter_step(): select(): %s\n",strerror(errno)); 140 | 141 | if (val > 0) { 142 | if (FD_ISSET(GUI->io->in_fd,set)) io_get_update(GUI->io); // read some xboard input 143 | if (FD_ISSET(Engine->io->in_fd,set)) io_get_update(Engine->io); // read some engine input 144 | } 145 | #endif 146 | } 147 | 148 | 149 | 150 | -------------------------------------------------------------------------------- /mainloop.h: -------------------------------------------------------------------------------- 1 | 2 | // mainloop.h 3 | 4 | #ifndef MAINLOOP_H 5 | #define MAINLOOP_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | #include "uci2uci.h" 11 | 12 | // types 13 | 14 | 15 | 16 | 17 | 18 | // functions 19 | 20 | extern void mainloop (); 21 | 22 | 23 | #endif // !defined MAINLOOP_H 24 | 25 | // end of mainloop.h 26 | 27 | -------------------------------------------------------------------------------- /makefile.gcc: -------------------------------------------------------------------------------- 1 | EXE = polyglot.exe 2 | 3 | OBJS = attack.o board.o book.o book_make.o book_merge.o colour.o engine.o\ 4 | epd.o fen.o game.o gui.o hash.o io.o ini.o line.o list.o main.o mainloop.o\ 5 | move.o move_do.o move_gen.o move_legal.o option.o parse.o pgheader.o pipex_win32.o\ 6 | pipex_posix.o pgn.o piece.o random.o san.o search.o square.o\ 7 | uci.o uci2uci.o util.o xboard2uci.o 8 | 9 | # set up for NO cygwin 10 | CYGF = -mno-cygwin 11 | CYGL = -lmsvcrt 12 | 13 | CC = gcc 14 | OPTS = -Os -frename-registers -funit-at-a-time -fstrict-aliasing -fstrength-reduce -fomit-frame-pointer 15 | CFLAGS = -Wall -pipe $(DEFS) $(OPTS) $(CYGF) 16 | LFLAGS = -fpic -s $(CYGF) 17 | LIBS = $(CYGL) 18 | 19 | all: $(EXE) 20 | 21 | clean: 22 | rm -rf $(OBJS) $(EXE) 23 | 24 | $(EXE): $(OBJS) 25 | $(CC) $(LFLAGS) $(LIBS) $(OBJS) -o $(EXE) 26 | 27 | %.o: %.c 28 | $(CC) $(CFLAGS) -c $< 29 | -------------------------------------------------------------------------------- /makefile.ms: -------------------------------------------------------------------------------- 1 | PROJ = polyglot 2 | EXE = $(PROJ).exe 3 | 4 | OBJS = attack.obj board.obj book.obj book_make.obj book_merge.obj colour.obj\ 5 | engine.obj epd.obj fen.obj game.obj gui.obj hash.obj io.obj line.obj list.obj\ 6 | main.obj mainloop.obj move.obj move_do.obj move_gen.obj move_legal.obj\ 7 | option.obj parse.obj pgheader.obj pgn.obj piece.obj pipex_win32.obj pipex_posix.obj\ 8 | random.obj san.obj search.obj square.obj uci.obj uci2uci.obj util.obj\ 9 | xboard2uci.obj 10 | 11 | LIBS = oldnames.lib 12 | 13 | 14 | VCVER=6 15 | DEBUG=0 16 | 17 | 18 | # VC++ version 19 | !if $(VCVER) >= 8 20 | CLIB = libcmt 21 | LIBF = -MT 22 | DEPRECATE=-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_USE_32BIT_TIME_T 23 | !else 24 | VSO = -Gf -Og 25 | CLIB = msvcrt 26 | LIBF = -MD 27 | !endif 28 | 29 | 30 | # Enable debugging? 31 | !if $(DEBUG) == 0 32 | CFDEBUG = -DNDEBUG 33 | OPTS = -Os -Oy -Gs -GA -Zl $(VSO) $(LIBF) 34 | LFDEBUG = $(CLIB).lib 35 | !else 36 | CFDEBUG = -D_DEBUG -DDEBUG 37 | OPTS = -Od -Zi -Fd$(PROJ).pdb $(LIBF)d 38 | LFDEBUG = -DEBUG -PDB:$(PROJ).pdb -MAP:$(PROJ).map $(CLIB)d.lib 39 | !endif 40 | 41 | 42 | CC = @cl 43 | LINK = link 44 | #WARN = -W3 45 | DEFS = $(CFDEBUG) $(DEPRECATE) 46 | CFLAGS = -c -nologo $(WARN) $(DEFS) $(OPTS) 47 | LFLAGS = -nologo $(LFDEBUG) 48 | 49 | all: $(EXE) 50 | 51 | clean: 52 | -erase $(OBJS) $(EXE) 53 | 54 | $(EXE): $(OBJS) 55 | $(LINK) $(LFLAGS) $(OBJS) $(LIBS) -out:$(EXE) 56 | 57 | .c.obj: 58 | $(CC) $(CFLAGS) -c $< 59 | -------------------------------------------------------------------------------- /move.h: -------------------------------------------------------------------------------- 1 | 2 | // move.h 3 | 4 | #ifndef MOVE_H 5 | #define MOVE_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "util.h" 11 | 12 | // defined 13 | 14 | // HACK: a1a1 cannot be a legal move 15 | #define MoveNone (0) 16 | 17 | #define MovePromoteKnight (1 << 12) 18 | #define MovePromoteBishop (2 << 12) 19 | #define MovePromoteRook (3 << 12) 20 | #define MovePromoteQueen (4 << 12) 21 | #define MoveFlags (7 << 12) 22 | 23 | // types 24 | 25 | typedef uint16 move_t; 26 | 27 | // functions 28 | 29 | extern bool move_is_ok (int move); 30 | 31 | extern int move_make (int from, int to); 32 | extern int move_make_flags (int from, int to, int flags); 33 | 34 | extern int move_from (int move); 35 | extern int move_to (int move); 36 | extern int move_promote_hack (int move); 37 | 38 | extern bool move_is_capture (int move, const board_t * board); 39 | extern bool move_is_promote (int move); 40 | extern bool move_is_en_passant (int move, const board_t * board); 41 | extern bool move_is_castle (int move, const board_t * board); 42 | 43 | extern int move_piece (int move, const board_t * board); 44 | extern int move_capture (int move, const board_t * board); 45 | extern int move_promote (int move, const board_t * board); 46 | 47 | extern bool move_is_check (int move, const board_t * board); 48 | extern bool move_is_mate (int move, const board_t * board); 49 | 50 | extern int move_order (int move); 51 | 52 | extern bool move_to_can (int move, const board_t * board, char string[], int size); 53 | extern int move_from_can (const char string[], const board_t * board); 54 | 55 | extern void move_disp (int move, const board_t * board); 56 | 57 | #endif // !defined MOVE_H 58 | 59 | // end of move.h 60 | 61 | -------------------------------------------------------------------------------- /move_do.h: -------------------------------------------------------------------------------- 1 | 2 | // move_do.h 3 | 4 | #ifndef MOVE_DO_H 5 | #define MOVE_DO_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "util.h" 11 | 12 | // functions 13 | 14 | extern void move_do (board_t * board, int move); 15 | 16 | #endif // !defined MOVE_DO_H 17 | 18 | // end of move_do.h 19 | 20 | -------------------------------------------------------------------------------- /move_gen.h: -------------------------------------------------------------------------------- 1 | 2 | // move_gen.h 3 | 4 | #ifndef MOVE_GEN_H 5 | #define MOVE_GEN_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "list.h" 11 | #include "util.h" 12 | 13 | // functions 14 | 15 | extern void gen_legal_moves (list_t * list, const board_t * board); 16 | extern void gen_moves (list_t * list, const board_t * board); 17 | 18 | #endif // !defined MOVE_GEN_H 19 | 20 | // end of move_gen.h 21 | 22 | -------------------------------------------------------------------------------- /move_legal.c: -------------------------------------------------------------------------------- 1 | 2 | // move_legal.c 3 | 4 | // includes 5 | 6 | #include "attack.h" 7 | #include "colour.h" 8 | #include "fen.h" 9 | #include "list.h" 10 | #include "move.h" 11 | #include "move_do.h" 12 | #include "move_gen.h" 13 | #include "move_legal.h" 14 | #include "piece.h" 15 | #include "square.h" 16 | #include "util.h" 17 | 18 | // prototypes 19 | 20 | static bool move_is_legal_debug (int move, const board_t * board); 21 | 22 | // functions 23 | 24 | // move_is_pseudo() 25 | 26 | bool move_is_pseudo(int move, const board_t * board) { 27 | 28 | list_t list[1]; 29 | 30 | ASSERT(move_is_ok(move)); 31 | ASSERT(board_is_ok(board)); 32 | 33 | gen_moves(list,board); 34 | 35 | return list_contain(list,move); 36 | } 37 | 38 | // pseudo_is_legal() 39 | 40 | bool pseudo_is_legal(int move, const board_t * board) { 41 | 42 | board_t new_board[1]; 43 | 44 | ASSERT(move_is_ok(move)); 45 | ASSERT(board_is_ok(board)); 46 | 47 | ASSERT(move_is_pseudo(move,board)); 48 | 49 | board_copy(new_board,board); 50 | move_do(new_board,move); 51 | 52 | return !is_in_check(new_board,colour_opp(new_board->turn)); 53 | } 54 | 55 | // move_is_legal() 56 | 57 | bool move_is_legal(int move, const board_t * board) { 58 | 59 | bool legal; 60 | 61 | ASSERT(move_is_ok(move)); 62 | ASSERT(board_is_ok(board)); 63 | 64 | legal = move_is_pseudo(move,board) && pseudo_is_legal(move,board); 65 | ASSERT(legal==move_is_legal_debug(move,board)); 66 | 67 | return legal; 68 | } 69 | 70 | // filter_legal() 71 | 72 | void filter_legal(list_t * list, const board_t * board) { 73 | 74 | int pos; 75 | int i, move, value; 76 | 77 | ASSERT(list_is_ok(list)); 78 | ASSERT(board_is_ok(board)); 79 | 80 | pos = 0; 81 | 82 | for (i = 0; i < list_size(list); i++) { 83 | 84 | ASSERT(pos>=0&&pos<=i); 85 | 86 | move = list_move(list,i); 87 | value = list_value(list,i); 88 | 89 | if (pseudo_is_legal(move,board)) { 90 | list->move[pos] = move; 91 | list->value[pos] = value; 92 | pos++; 93 | } 94 | } 95 | 96 | ASSERT(pos>=0&&pos<=list_size(list)); 97 | list->size = pos; 98 | } 99 | 100 | // move_is_legal_debug() 101 | 102 | static bool move_is_legal_debug(int move, const board_t * board) { 103 | 104 | list_t list[1]; 105 | 106 | ASSERT(move_is_ok(move)); 107 | ASSERT(board_is_ok(board)); 108 | 109 | gen_legal_moves(list,board); 110 | 111 | return list_contain(list,move); 112 | } 113 | 114 | // end of move_legal.cpp 115 | 116 | -------------------------------------------------------------------------------- /move_legal.cpp: -------------------------------------------------------------------------------- 1 | 2 | // move_legal.cpp 3 | 4 | // includes 5 | 6 | #include "attack.h" 7 | #include "colour.h" 8 | #include "fen.h" 9 | #include "list.h" 10 | #include "move.h" 11 | #include "move_do.h" 12 | #include "move_gen.h" 13 | #include "move_legal.h" 14 | #include "piece.h" 15 | #include "square.h" 16 | #include "util.h" 17 | 18 | // prototypes 19 | 20 | static bool move_is_legal_debug (int move, const board_t * board); 21 | 22 | // functions 23 | 24 | // move_is_pseudo() 25 | 26 | bool move_is_pseudo(int move, const board_t * board) { 27 | 28 | list_t list[1]; 29 | 30 | ASSERT(move_is_ok(move)); 31 | ASSERT(board_is_ok(board)); 32 | 33 | gen_moves(list,board); 34 | 35 | return list_contain(list,move); 36 | } 37 | 38 | // pseudo_is_legal() 39 | 40 | bool pseudo_is_legal(int move, const board_t * board) { 41 | 42 | board_t new_board[1]; 43 | 44 | ASSERT(move_is_ok(move)); 45 | ASSERT(board_is_ok(board)); 46 | 47 | ASSERT(move_is_pseudo(move,board)); 48 | 49 | board_copy(new_board,board); 50 | move_do(new_board,move); 51 | 52 | return !is_in_check(new_board,colour_opp(new_board->turn)); 53 | } 54 | 55 | // move_is_legal() 56 | 57 | bool move_is_legal(int move, const board_t * board) { 58 | 59 | bool legal; 60 | 61 | ASSERT(move_is_ok(move)); 62 | ASSERT(board_is_ok(board)); 63 | 64 | legal = move_is_pseudo(move,board) && pseudo_is_legal(move,board); 65 | ASSERT(legal==move_is_legal_debug(move,board)); 66 | 67 | return legal; 68 | } 69 | 70 | // filter_legal() 71 | 72 | void filter_legal(list_t * list, const board_t * board) { 73 | 74 | int pos; 75 | int i, move, value; 76 | 77 | ASSERT(list_is_ok(list)); 78 | ASSERT(board_is_ok(board)); 79 | 80 | pos = 0; 81 | 82 | for (i = 0; i < list_size(list); i++) { 83 | 84 | ASSERT(pos>=0&&pos<=i); 85 | 86 | move = list_move(list,i); 87 | value = list_value(list,i); 88 | 89 | if (pseudo_is_legal(move,board)) { 90 | list->move[pos] = move; 91 | list->value[pos] = value; 92 | pos++; 93 | } 94 | } 95 | 96 | ASSERT(pos>=0&&pos<=list_size(list)); 97 | list->size = pos; 98 | } 99 | 100 | // move_is_legal_debug() 101 | 102 | static bool move_is_legal_debug(int move, const board_t * board) { 103 | 104 | list_t list[1]; 105 | 106 | ASSERT(move_is_ok(move)); 107 | ASSERT(board_is_ok(board)); 108 | 109 | gen_legal_moves(list,board); 110 | 111 | return list_contain(list,move); 112 | } 113 | 114 | // end of move_legal.cpp 115 | 116 | -------------------------------------------------------------------------------- /move_legal.h: -------------------------------------------------------------------------------- 1 | 2 | // move_legal.h 3 | 4 | #ifndef MOVE_LEGAL_H 5 | #define MOVE_LEGAL_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "list.h" 11 | #include "util.h" 12 | 13 | // functions 14 | 15 | extern bool move_is_pseudo (int move, const board_t * board); 16 | extern bool pseudo_is_legal (int move, const board_t * board); 17 | extern bool move_is_legal (int move, const board_t * board); 18 | 19 | extern void filter_legal (list_t * list, const board_t * board); 20 | 21 | #endif // !defined MOVE_LEGAL_H 22 | 23 | // end of move_legal.h 24 | 25 | -------------------------------------------------------------------------------- /option.cpp: -------------------------------------------------------------------------------- 1 | 2 | // option.cpp 3 | 4 | // includes 5 | 6 | #include 7 | #include 8 | 9 | #include "option.h" 10 | #include "util.h" 11 | 12 | // constants 13 | 14 | static const bool UseDebug = false; 15 | 16 | 17 | // variables 18 | 19 | #define NNB { NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL } 20 | option_t Option[] = { 21 | 22 | { "OptionFile", "string","0","0", "polyglot.ini", NULL,0,NNB, PG}, 23 | 24 | // options 25 | 26 | { "EngineName", "string","0","0", "" , NULL,0,NNB, PG}, 27 | { "EngineDir", "string","0","0", "." , NULL,0,NNB, PG}, 28 | { "EngineCommand", "string","0","0", "" , NULL,0,NNB, PG}, 29 | 30 | { "Log", "check","0","0", "false" , NULL,0,NNB, PG|XBOARD|UCI}, 31 | { "LogFile", "string","0","0", "polyglot.log", NULL,0,NNB, PG|XBOARD|UCI}, 32 | 33 | { "UCI", "string","0","0", "false" , NULL,0,NNB, PG}, 34 | 35 | { "UseNice", "check","0","0", "false" , NULL,0,NNB, PG|XBOARD|UCI}, 36 | { "NiceValue", "spin", "0","20", "5" , NULL,0,NNB, PG|XBOARD|UCI}, 37 | 38 | { "Chess960", "check","0","0", "false" , NULL,0,NNB, PG|XBOARD}, 39 | 40 | { "Resign", "check","0","0", "false" , NULL,0,NNB, PG|XBOARD}, 41 | { "ResignMoves", "spin","0","10000", "3" , NULL,0,NNB, PG|XBOARD}, 42 | { "ResignScore", "spin","0","10000", "600" , NULL,0,NNB, PG|XBOARD}, 43 | 44 | { "MateScore", "spin","0","1000000", "10000" , NULL,0,NNB, PG|XBOARD}, 45 | 46 | { "Book", "check","0","0", "false" , NULL,0,NNB, PG|XBOARD|UCI}, 47 | { "BookFile", "string","0","0", "book.bin" , NULL,0,NNB, PG|XBOARD|UCI}, 48 | 49 | { "BookRandom", "check","0","0", "true" , NULL,0,NNB, PG|XBOARD|UCI}, 50 | { "BookLearn", "check","0","0", "false" , NULL,0,NNB, PG|XBOARD}, 51 | 52 | { "KibitzMove", "check","0","0", "false" , NULL,0,NNB, PG|XBOARD}, 53 | { "KibitzPV", "check","0","0", "false" , NULL,0,NNB, PG|XBOARD}, 54 | 55 | { "KibitzCommand", "string","0","0", "tellall" , NULL,0,NNB, PG|XBOARD}, 56 | { "KibitzDelay", "spin","0","10000", "5" , NULL,0,NNB, PG|XBOARD}, 57 | { "KibitzInterval", "spin","0","10000", "0" , NULL,0,NNB, PG|XBOARD}, 58 | 59 | { "ShowPonder", "check","0","0", "false" , NULL,0,NNB, PG|XBOARD}, 60 | { "ScoreWhite", "check","0","0", "false" , NULL,0,NNB, PG|XBOARD}, 61 | 62 | // work-arounds 63 | 64 | { "UCIVersion", "spin","1","2", "2" , NULL,0,NNB, PG|XBOARD}, 65 | { "CanPonder", "check","1","2", "false" , NULL,0,NNB, PG|XBOARD}, 66 | { "SyncStop", "check","1","2", "false" , NULL,0,NNB, PG|XBOARD}, 67 | { "Affinity", "spin","-1","32", "-1" , NULL,0,NNB, PG}, 68 | { "RepeatPV", "check","0","0", "true" , NULL,0,NNB, PG|XBOARD}, 69 | { "PromoteWorkAround","check","0","0", "false" , NULL,0,NNB, PG|XBOARD}, 70 | 71 | { NULL, NULL,"0","0", NULL , NULL,0,NNB, 0}, 72 | }; 73 | 74 | // prototypes 75 | 76 | static option_t * option_find (const char var[]); 77 | 78 | // functions 79 | 80 | // option_init() 81 | 82 | void option_init() { 83 | 84 | option_t *p=Option; 85 | const char * name; 86 | 87 | while((name=(p++)->name)){ 88 | option_set(name,option_get_default(name)); 89 | } 90 | } 91 | 92 | 93 | // option_set() 94 | 95 | bool option_set(const char name[], const char value[]) { 96 | 97 | option_t * opt; 98 | ASSERT(name!=NULL); 99 | ASSERT(value!=NULL); 100 | 101 | opt = option_find(name); 102 | if (opt == NULL) return false; 103 | 104 | my_string_set(&opt->value,value); 105 | 106 | if (UseDebug) my_log("POLYGLOT OPTION SET \"%s\" -> \"%s\"\n",opt->name,opt->value); 107 | 108 | return true; 109 | } 110 | // option_set() 111 | 112 | bool option_set_default(const char name[], const char value[]) { 113 | 114 | option_t * opt; 115 | ASSERT(name!=NULL); 116 | ASSERT(value!=NULL); 117 | 118 | opt = option_find(name); 119 | if (opt == NULL) return false; 120 | 121 | opt->default_=my_strdup(value); 122 | 123 | if (UseDebug) my_log("POLYGLOT OPTION DEFAULT SET \"%s\" -> \"%s\"\n",opt->name,opt->default_); 124 | 125 | return true; 126 | } 127 | 128 | // option_get() 129 | 130 | const char * option_get(const char name[]) { 131 | 132 | option_t * opt; 133 | 134 | ASSERT(name!=NULL); 135 | 136 | opt = option_find(name); 137 | if (opt == NULL) my_fatal("option_get(): unknown option \"%s\"\n",name); 138 | 139 | if (UseDebug) my_log("POLYGLOT OPTION GET \"%s\" -> \"%s\"\n",opt->name,opt->value); 140 | 141 | return opt->value; 142 | } 143 | 144 | // option_get_default() 145 | 146 | const char * option_get_default(const char name[]) { 147 | 148 | option_t * opt; 149 | 150 | ASSERT(name!=NULL); 151 | 152 | opt = option_find(name); 153 | if (opt == NULL) my_fatal("option_get(): unknown option \"%s\"\n",name); 154 | 155 | if (UseDebug) my_log("POLYGLOT OPTION GET \"%s\" -> \"%s\"\n",opt->name,opt->value); 156 | 157 | return opt->default_; 158 | } 159 | 160 | // option_get_bool() 161 | 162 | bool option_get_bool(const char name[]) { 163 | 164 | const char * value; 165 | 166 | value = option_get(name); 167 | 168 | if (false) { 169 | } else if (my_string_case_equal(value,"true") || my_string_case_equal(value,"yes") || my_string_equal(value,"1")) { 170 | return true; 171 | } else if (my_string_case_equal(value,"false") || my_string_case_equal(value,"no") || my_string_equal(value,"0")) { 172 | return false; 173 | } 174 | 175 | ASSERT(false); 176 | 177 | return false; 178 | } 179 | 180 | // option_get_double() 181 | 182 | double option_get_double(const char name[]) { 183 | 184 | const char * value; 185 | 186 | value = option_get(name); 187 | 188 | return atof(value); 189 | } 190 | 191 | // option_get_int() 192 | 193 | int option_get_int(const char name[]) { 194 | 195 | const char * value; 196 | 197 | value = option_get(name); 198 | 199 | return atoi(value); 200 | } 201 | 202 | // option_get_string() 203 | 204 | const char * option_get_string(const char name[]) { 205 | 206 | const char * value; 207 | 208 | value = option_get(name); 209 | 210 | return value; 211 | } 212 | 213 | // option_find() 214 | 215 | static option_t * option_find(const char name[]) { 216 | 217 | option_t * opt; 218 | 219 | ASSERT(name!=NULL); 220 | 221 | 222 | for (opt = &Option[0]; opt->name != NULL; opt++) { 223 | if (my_string_case_equal(opt->name,name)) return opt; 224 | } 225 | 226 | return NULL; 227 | } 228 | 229 | // end of option.cpp 230 | 231 | -------------------------------------------------------------------------------- /option.h: -------------------------------------------------------------------------------- 1 | 2 | // option.h 3 | 4 | #ifndef OPTION_H 5 | #define OPTION_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | #include "ini.h" 11 | 12 | // defines 13 | 14 | #define VarNb 16 15 | #define XBOARD (1<<0) 16 | #define UCI (1<<1) 17 | #define PG (1<<2) 18 | #define XBSEL (1<<3) 19 | #define OptionNb 256 20 | 21 | #define IS_BUTTON(str) (my_string_case_equal(str,"button") || \ 22 | my_string_case_equal(str,"save") || \ 23 | my_string_case_equal(str,"reset")) \ 24 | 25 | #define IS_SPIN(str) (my_string_case_equal(str,"spin") || \ 26 | my_string_case_equal(str,"slider")) \ 27 | 28 | #define IS_STRING(str) (my_string_case_equal(str,"string") || \ 29 | my_string_case_equal(str,"path") || \ 30 | my_string_case_equal(str,"file")) \ 31 | 32 | // types 33 | 34 | typedef struct { // TODO: put back in more logical order 35 | const char * name; 36 | const char * type; 37 | const char * min; 38 | const char * max; 39 | const char * default_; 40 | const char * value; 41 | int var_nb; 42 | const char * var[VarNb]; 43 | int mode; 44 | } option_t; 45 | 46 | // all non NULL data in an option_list_t should be malloc'ed 47 | // use "my_string_set" to fill it. 48 | 49 | typedef struct { 50 | option_t options[OptionNb]; 51 | int option_nb; 52 | int iter; 53 | } option_list_t; 54 | 55 | // variables 56 | 57 | extern option_list_t Option[1]; 58 | 59 | // functions 60 | 61 | 62 | extern void option_init (option_list_t *option); 63 | 64 | extern void option_init_pg (); 65 | 66 | extern bool option_set (option_list_t *option, 67 | const char var[], 68 | const char val[]); 69 | extern bool option_set_default (option_list_t *option, 70 | const char var[], 71 | const char val[]); 72 | 73 | extern const char * option_get (option_list_t *option, const char var[]); 74 | extern const char * option_get_default (option_list_t *option, const char var[]); 75 | 76 | extern bool option_get_bool (option_list_t *option, const char var[]); 77 | extern double option_get_double (option_list_t *option, const char var[]); 78 | extern int option_get_int (option_list_t *option, const char var[]); 79 | extern const char * option_get_string (option_list_t *option, const char var[]); 80 | 81 | extern void option_from_ini (option_list_t *option, 82 | ini_t *ini, 83 | const char *section); 84 | 85 | extern bool option_is_ok (const option_list_t *option); 86 | extern option_t * option_find (option_list_t *option, const char var[]); 87 | extern void option_clear (option_list_t *option); 88 | extern void option_insert (option_list_t *option, option_t *new_option); 89 | 90 | extern void option_start_iter (option_list_t *option); 91 | extern option_t * option_next (option_list_t *option); 92 | 93 | extern void option_free (option_t *option); 94 | 95 | #endif // !defined OPTION_H 96 | 97 | // end of option.h 98 | 99 | -------------------------------------------------------------------------------- /parse.c: -------------------------------------------------------------------------------- 1 | 2 | // parse.c 3 | 4 | // includes 5 | 6 | #include 7 | 8 | #include "parse.h" 9 | #include "util.h" 10 | 11 | // variables 12 | 13 | char * Star[STAR_NUMBER]; 14 | 15 | // prototypes 16 | 17 | static bool match_rec (char string[], const char pattern[], char * star[]); 18 | 19 | // functions 20 | 21 | // match() 22 | 23 | bool match(char string[], const char pattern[]) { 24 | 25 | ASSERT(string!=NULL); 26 | ASSERT(pattern!=NULL); 27 | 28 | ASSERT(strstr(pattern,"**")==NULL); 29 | 30 | return match_rec(string,pattern,Star); 31 | } 32 | 33 | // match_rec() 34 | 35 | static bool match_rec(char string[], const char pattern[], char * star[]) { 36 | 37 | int c; 38 | 39 | ASSERT(string!=NULL); 40 | ASSERT(pattern!=NULL); 41 | ASSERT(star!=NULL); 42 | 43 | // iterative matches 44 | 45 | while ((c=*pattern++) != '*') { 46 | if (FALSE) { 47 | } else if (c == '\0') { // end of pattern 48 | while (*string == ' ') string++; // skip trailing spaces 49 | return *string == '\0'; 50 | } else if (c == ' ') { // spaces 51 | if (*string++ != ' ') return FALSE; // mismatch 52 | while (*string == ' ') string++; // skip trailing spaces 53 | } else { // normal character 54 | if (*string++ != c) return FALSE; // mismatch 55 | } 56 | } 57 | 58 | // recursive wildcard match 59 | 60 | ASSERT(c=='*'); 61 | 62 | while (*string == ' ') string++; // skip leading spaces 63 | *star++ = string; // remember beginning of star 64 | 65 | while ((c=*string++) != '\0') { // reject empty-string match 66 | if (c != ' ' && match_rec(string,pattern,star)) { // shortest match 67 | ASSERT(string>star[-1]); 68 | *string = '\0'; // truncate star 69 | return TRUE; 70 | } 71 | } 72 | 73 | return FALSE; 74 | } 75 | 76 | // parse_is_ok() 77 | 78 | bool parse_is_ok(const parse_t * parse) { 79 | 80 | if (parse == NULL) return FALSE; 81 | if (parse->string == NULL) return FALSE; 82 | if (parse->pos < 0 || parse->pos > (int) strlen(parse->string)) return FALSE; 83 | if (parse->keyword_nb < 0 || parse->keyword_nb >= KEYWORD_NUMBER) return FALSE; 84 | 85 | return TRUE; 86 | } 87 | 88 | // parse_open() 89 | 90 | void parse_open(parse_t * parse, const char string[]) { 91 | 92 | ASSERT(parse!=NULL); 93 | ASSERT(string!=NULL); 94 | 95 | parse->string = string; 96 | parse->pos = 0; 97 | parse->keyword_nb = 0; 98 | } 99 | 100 | // parse_close() 101 | 102 | void parse_close(parse_t * parse) { 103 | 104 | int i; 105 | 106 | ASSERT(parse_is_ok(parse)); 107 | 108 | parse->string = NULL; 109 | parse->pos = 0; 110 | 111 | for (i = 0; i < parse->keyword_nb; i++) { 112 | my_string_clear(&parse->keyword[i]); 113 | } 114 | 115 | parse->keyword_nb = 0; 116 | } 117 | 118 | // parse_add_keyword() 119 | 120 | void parse_add_keyword(parse_t * parse, const char keyword[]) { 121 | 122 | const char * * string; 123 | 124 | ASSERT(parse_is_ok(parse)); 125 | ASSERT(keyword!=NULL); 126 | 127 | if (parse->keyword_nb < KEYWORD_NUMBER) { 128 | 129 | string = &parse->keyword[parse->keyword_nb]; 130 | parse->keyword_nb++; 131 | 132 | *string = NULL; 133 | my_string_set(string,keyword); 134 | } 135 | } 136 | 137 | // parse_get_word() 138 | 139 | bool parse_get_word(parse_t * parse, char string[], int size) { 140 | 141 | int pos; 142 | int c; 143 | 144 | ASSERT(parse!=NULL); 145 | ASSERT(string!=NULL); 146 | ASSERT(size>=256); 147 | 148 | // skip blanks 149 | 150 | for (; parse->string[parse->pos] == ' '; parse->pos++) 151 | ; 152 | 153 | ASSERT(parse->string[parse->pos]!=' '); 154 | 155 | // copy word 156 | 157 | pos = 0; 158 | 159 | while (TRUE) { 160 | 161 | c = parse->string[parse->pos]; 162 | if (c == ' ' || pos >= size-1) c = '\0'; 163 | 164 | string[pos] = c; 165 | if (c == '\0') break; 166 | 167 | parse->pos++; 168 | pos++; 169 | } 170 | 171 | ASSERT(strchr(string,' ')==NULL); 172 | 173 | return pos > 0; // non-empty word? 174 | } 175 | 176 | // parse_get_string() 177 | 178 | bool parse_get_string(parse_t * parse, char string[], int size) { 179 | 180 | int pos; 181 | parse_t parse_2[1]; 182 | char word[StringSize]; 183 | int i; 184 | int c; 185 | 186 | ASSERT(parse!=NULL); 187 | ASSERT(string!=NULL); 188 | ASSERT(size>=256); 189 | 190 | // skip blanks 191 | 192 | for (; parse->string[parse->pos] == ' '; parse->pos++) 193 | ; 194 | 195 | ASSERT(parse->string[parse->pos]!=' '); 196 | 197 | // copy string 198 | 199 | pos = 0; 200 | 201 | while (TRUE) { 202 | 203 | parse_open(parse_2,&parse->string[parse->pos]); 204 | 205 | if (!parse_get_word(parse_2,word,StringSize)) { 206 | string[pos] = '\0'; 207 | parse_close(parse_2); 208 | goto finished; 209 | } 210 | 211 | for (i = 0; i < parse->keyword_nb; i++) { 212 | if (my_string_equal(parse->keyword[i],word)) { 213 | string[pos] = '\0'; 214 | parse_close(parse_2); 215 | goto finished; 216 | } 217 | } 218 | 219 | parse_close(parse_2); 220 | 221 | // copy spaces 222 | 223 | while (TRUE) { 224 | 225 | c = parse->string[parse->pos]; 226 | if (c != ' ') break; 227 | 228 | if (pos >= size-1) c = '\0'; 229 | 230 | string[pos] = c; 231 | if (c == '\0') break; 232 | 233 | parse->pos++; 234 | pos++; 235 | } 236 | 237 | // copy non spaces 238 | 239 | while (TRUE) { 240 | 241 | c = parse->string[parse->pos]; 242 | if (c == ' ' || pos >= size-1) c = '\0'; 243 | 244 | string[pos] = c; 245 | if (c == '\0') break; 246 | 247 | parse->pos++; 248 | pos++; 249 | } 250 | 251 | string[pos] = '\0'; 252 | } 253 | 254 | finished: ; 255 | 256 | return pos > 0; // non-empty string? 257 | } 258 | 259 | // end of parse.cpp 260 | 261 | -------------------------------------------------------------------------------- /parse.cpp: -------------------------------------------------------------------------------- 1 | 2 | // parse.cpp 3 | 4 | // includes 5 | 6 | #include 7 | 8 | #include "parse.h" 9 | #include "util.h" 10 | 11 | // constants 12 | 13 | static const int StringSize = 256; 14 | 15 | // variables 16 | 17 | char * Star[STAR_NUMBER]; 18 | 19 | // prototypes 20 | 21 | static bool match_rec (char string[], const char pattern[], char * star[]); 22 | 23 | // functions 24 | 25 | // match() 26 | 27 | bool match(char string[], const char pattern[]) { 28 | 29 | ASSERT(string!=NULL); 30 | ASSERT(pattern!=NULL); 31 | 32 | ASSERT(strstr(pattern,"**")==NULL); 33 | 34 | return match_rec(string,pattern,Star); 35 | } 36 | 37 | // match_rec() 38 | 39 | static bool match_rec(char string[], const char pattern[], char * star[]) { 40 | 41 | int c; 42 | 43 | ASSERT(string!=NULL); 44 | ASSERT(pattern!=NULL); 45 | ASSERT(star!=NULL); 46 | 47 | // iterative matches 48 | 49 | while ((c=*pattern++) != '*') { 50 | if (false) { 51 | } else if (c == '\0') { // end of pattern 52 | while (*string == ' ') string++; // skip trailing spaces 53 | return *string == '\0'; 54 | } else if (c == ' ') { // spaces 55 | if (*string++ != ' ') return false; // mismatch 56 | while (*string == ' ') string++; // skip trailing spaces 57 | } else { // normal character 58 | if (*string++ != c) return false; // mismatch 59 | } 60 | } 61 | 62 | // recursive wildcard match 63 | 64 | ASSERT(c=='*'); 65 | 66 | while (*string == ' ') string++; // skip leading spaces 67 | *star++ = string; // remember beginning of star 68 | 69 | while ((c=*string++) != '\0') { // reject empty-string match 70 | if (c != ' ' && match_rec(string,pattern,star)) { // shortest match 71 | ASSERT(string>star[-1]); 72 | *string = '\0'; // truncate star 73 | return true; 74 | } 75 | } 76 | 77 | return false; 78 | } 79 | 80 | // parse_is_ok() 81 | 82 | bool parse_is_ok(const parse_t * parse) { 83 | 84 | if (parse == NULL) return false; 85 | if (parse->string == NULL) return false; 86 | if (parse->pos < 0 || parse->pos > (int) strlen(parse->string)) return false; 87 | if (parse->keyword_nb < 0 || parse->keyword_nb >= KEYWORD_NUMBER) return false; 88 | 89 | return true; 90 | } 91 | 92 | // parse_open() 93 | 94 | void parse_open(parse_t * parse, const char string[]) { 95 | 96 | ASSERT(parse!=NULL); 97 | ASSERT(string!=NULL); 98 | 99 | parse->string = string; 100 | parse->pos = 0; 101 | parse->keyword_nb = 0; 102 | } 103 | 104 | // parse_close() 105 | 106 | void parse_close(parse_t * parse) { 107 | 108 | int i; 109 | 110 | ASSERT(parse_is_ok(parse)); 111 | 112 | parse->string = NULL; 113 | parse->pos = 0; 114 | 115 | for (i = 0; i < parse->keyword_nb; i++) { 116 | my_string_clear(&parse->keyword[i]); 117 | } 118 | 119 | parse->keyword_nb = 0; 120 | } 121 | 122 | // parse_add_keyword() 123 | 124 | void parse_add_keyword(parse_t * parse, const char keyword[]) { 125 | 126 | const char * * string; 127 | 128 | ASSERT(parse_is_ok(parse)); 129 | ASSERT(keyword!=NULL); 130 | 131 | if (parse->keyword_nb < KEYWORD_NUMBER) { 132 | 133 | string = &parse->keyword[parse->keyword_nb]; 134 | parse->keyword_nb++; 135 | 136 | *string = NULL; 137 | my_string_set(string,keyword); 138 | } 139 | } 140 | 141 | // parse_get_word() 142 | 143 | bool parse_get_word(parse_t * parse, char string[], int size) { 144 | 145 | int pos; 146 | int c; 147 | 148 | ASSERT(parse!=NULL); 149 | ASSERT(string!=NULL); 150 | ASSERT(size>=256); 151 | 152 | // skip blanks 153 | 154 | for (; parse->string[parse->pos] == ' '; parse->pos++) 155 | ; 156 | 157 | ASSERT(parse->string[parse->pos]!=' '); 158 | 159 | // copy word 160 | 161 | pos = 0; 162 | 163 | while (true) { 164 | 165 | c = parse->string[parse->pos]; 166 | if (c == ' ' || pos >= size-1) c = '\0'; 167 | 168 | string[pos] = c; 169 | if (c == '\0') break; 170 | 171 | parse->pos++; 172 | pos++; 173 | } 174 | 175 | ASSERT(strchr(string,' ')==NULL); 176 | 177 | return pos > 0; // non-empty word? 178 | } 179 | 180 | // parse_get_string() 181 | 182 | bool parse_get_string(parse_t * parse, char string[], int size) { 183 | 184 | int pos; 185 | parse_t parse_2[1]; 186 | char word[StringSize]; 187 | int i; 188 | int c; 189 | 190 | ASSERT(parse!=NULL); 191 | ASSERT(string!=NULL); 192 | ASSERT(size>=256); 193 | 194 | // skip blanks 195 | 196 | for (; parse->string[parse->pos] == ' '; parse->pos++) 197 | ; 198 | 199 | ASSERT(parse->string[parse->pos]!=' '); 200 | 201 | // copy string 202 | 203 | pos = 0; 204 | 205 | while (true) { 206 | 207 | parse_open(parse_2,&parse->string[parse->pos]); 208 | 209 | if (!parse_get_word(parse_2,word,StringSize)) { 210 | string[pos] = '\0'; 211 | parse_close(parse_2); 212 | goto finished; 213 | } 214 | 215 | for (i = 0; i < parse->keyword_nb; i++) { 216 | if (my_string_equal(parse->keyword[i],word)) { 217 | string[pos] = '\0'; 218 | parse_close(parse_2); 219 | goto finished; 220 | } 221 | } 222 | 223 | parse_close(parse_2); 224 | 225 | // copy spaces 226 | 227 | while (true) { 228 | 229 | c = parse->string[parse->pos]; 230 | if (c != ' ') break; 231 | 232 | if (pos >= size-1) c = '\0'; 233 | 234 | string[pos] = c; 235 | if (c == '\0') break; 236 | 237 | parse->pos++; 238 | pos++; 239 | } 240 | 241 | // copy non spaces 242 | 243 | while (true) { 244 | 245 | c = parse->string[parse->pos]; 246 | if (c == ' ' || pos >= size-1) c = '\0'; 247 | 248 | string[pos] = c; 249 | if (c == '\0') break; 250 | 251 | parse->pos++; 252 | pos++; 253 | } 254 | 255 | string[pos] = '\0'; 256 | } 257 | 258 | finished: ; 259 | 260 | return pos > 0; // non-empty string? 261 | } 262 | 263 | // end of parse.cpp 264 | 265 | -------------------------------------------------------------------------------- /parse.h: -------------------------------------------------------------------------------- 1 | 2 | // parse.h 3 | 4 | #ifndef PARSE_H 5 | #define PARSE_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // defined 12 | 13 | #define STAR_NUMBER 16 14 | #define KEYWORD_NUMBER 256 15 | 16 | // types 17 | 18 | typedef struct { 19 | const char * string; 20 | int pos; 21 | int keyword_nb; 22 | const char * keyword[KEYWORD_NUMBER]; 23 | } parse_t; 24 | 25 | // variables 26 | 27 | extern char * Star[STAR_NUMBER]; 28 | 29 | // functions 30 | 31 | extern bool match (char string[], const char pattern[]); 32 | 33 | extern void parse_open (parse_t * parse, const char string[]); 34 | extern void parse_close (parse_t * parse); 35 | 36 | extern void parse_add_keyword (parse_t * parse, const char keyword[]); 37 | 38 | extern bool parse_get_word (parse_t * parse, char string[], int size); 39 | extern bool parse_get_string (parse_t * parse, char string[], int size); 40 | 41 | #endif // !defined PARSE_H 42 | 43 | // end of parse.h 44 | 45 | -------------------------------------------------------------------------------- /pgheader.h: -------------------------------------------------------------------------------- 1 | #ifndef PGHEADER_H 2 | #define PGHEADER_H 3 | 4 | #define PGHEADER_NO_ERROR 0 5 | #define PGHEADER_OS_ERROR 1 6 | #define PGHEADER_BAD_FORMAT 2 7 | #define PGHEADER_NO_HEADER 3 8 | #define PGHEADER_NAME_COLLISION 4 9 | #define PGHEADER_BAD_PARAMETER 5 10 | #define PGHEADER_BAD_HEADER 6 11 | 12 | 13 | const char * pgheader_version; 14 | const char * pgheader_magic; 15 | 16 | /* This does some heuristic tests to make it reasonably sure we are dealing 17 | with a Polyglot book. 18 | */ 19 | int pgheader_detect(const char *infile); 20 | 21 | /* "variants" is a linefeed separated list of supported variants. 22 | "comments" is a free format string which may contain linefeeds. 23 | "header" must be freed if the function returns without error. 24 | */ 25 | int pgheader_create(char **header, const char *variants, const char *comment); 26 | 27 | /* "variants" and "comment" must be freed if the function returns 28 | without error. 29 | */ 30 | int pgheader_parse(const char *header, char **variants, char **comment); 31 | 32 | /* "raw_header" is a byte array ready for prepending to a book file. 33 | It must be freed after use. 34 | */ 35 | int pgheader_create_raw(char **raw_header, const char *header, unsigned int *size); 36 | 37 | /* "header" must be freed if the function returns without error. 38 | */ 39 | int pgheader_read(char **header, const char *infile); 40 | 41 | int pgheader_write(const char *header, const char *infile, const char *outfile); 42 | int pgheader_delete(const char *infile, const char *outfile); 43 | 44 | void pgheader_error(const char *prompt, int ret); 45 | 46 | #endif 47 | 48 | -------------------------------------------------------------------------------- /pgn.h: -------------------------------------------------------------------------------- 1 | 2 | // pgn.h 3 | 4 | #ifndef PGN_H 5 | #define PGN_H 6 | 7 | // includes 8 | 9 | #include 10 | 11 | #include "util.h" 12 | 13 | // defines 14 | 15 | #define PGN_STRING_SIZE 256 16 | 17 | // types 18 | 19 | typedef struct { 20 | 21 | FILE * file; 22 | 23 | int char_hack; 24 | int char_line; 25 | int char_column; 26 | bool char_unread; 27 | bool char_first; 28 | 29 | int token_type; 30 | char token_string[PGN_STRING_SIZE]; 31 | int token_length; 32 | int token_line; 33 | int token_column; 34 | bool token_unread; 35 | bool token_first; 36 | 37 | char result[PGN_STRING_SIZE]; 38 | char variant[PGN_STRING_SIZE]; 39 | char fen[PGN_STRING_SIZE]; 40 | 41 | int move_line; 42 | int move_column; 43 | int game_nb; 44 | } pgn_t; 45 | 46 | // functions 47 | 48 | extern void pgn_open (pgn_t * pgn, const char file_name[]); 49 | extern void pgn_close (pgn_t * pgn); 50 | 51 | extern bool pgn_next_game (pgn_t * pgn); 52 | extern bool pgn_next_move (pgn_t * pgn, char string[], int size); 53 | 54 | #endif // !defined PGN_H 55 | 56 | // end of pgn.h 57 | 58 | -------------------------------------------------------------------------------- /piece.c: -------------------------------------------------------------------------------- 1 | 2 | // piece.c 3 | 4 | // includes 5 | 6 | #include 7 | 8 | #include "colour.h" 9 | #include "piece.h" 10 | #include "util.h" 11 | 12 | // "constants" 13 | 14 | static const uint8 MakePawn[ColourNb] = { PieceNone256, BlackPawn256, WhitePawn256 }; // -BW 15 | 16 | static const uint8 PieceFrom12[12] = { 17 | BlackPawn256, WhitePawn256, 18 | BlackKnight256, WhiteKnight256, 19 | BlackBishop256, WhiteBishop256, 20 | BlackRook256, WhiteRook256, 21 | BlackQueen256, WhiteQueen256, 22 | BlackKing256, WhiteKing256, 23 | }; 24 | 25 | static const char PieceString[12+1] = "pPnNbBrRqQkK"; 26 | 27 | // variables 28 | 29 | static sint8 PieceTo12[256]; 30 | 31 | // functions 32 | 33 | // piece_init() 34 | 35 | void piece_init() { 36 | 37 | int piece; 38 | 39 | for (piece = 0; piece < 256; piece++) PieceTo12[piece] = -1; 40 | 41 | for (piece = 0; piece < 12; piece++) { 42 | PieceTo12[PieceFrom12[piece]] = piece; 43 | } 44 | } 45 | 46 | // piece_is_ok() 47 | 48 | bool piece_is_ok(int piece) { 49 | 50 | if (piece < 0 || piece >= 256) return FALSE; 51 | 52 | if (PieceTo12[piece] < 0) return FALSE; 53 | 54 | return TRUE; 55 | } 56 | 57 | // piece_make_pawn() 58 | 59 | int piece_make_pawn(int colour) { 60 | 61 | ASSERT(colour_is_ok(colour)); 62 | 63 | return MakePawn[colour]; 64 | } 65 | 66 | // piece_pawn_opp() 67 | 68 | int piece_pawn_opp(int piece) { 69 | 70 | ASSERT(piece==BlackPawn256||piece==WhitePawn256); 71 | 72 | return piece ^ 15; 73 | } 74 | 75 | // piece_colour() 76 | 77 | int piece_colour(int piece) { 78 | 79 | ASSERT(piece_is_ok(piece)); 80 | 81 | return piece & 3; 82 | } 83 | 84 | // piece_type() 85 | 86 | int piece_type(int piece) { 87 | 88 | ASSERT(piece_is_ok(piece)); 89 | 90 | return piece & ~3; 91 | } 92 | 93 | // piece_is_pawn() 94 | 95 | bool piece_is_pawn(int piece) { 96 | 97 | ASSERT(piece_is_ok(piece)); 98 | 99 | return (piece & PawnFlags) != 0; 100 | } 101 | 102 | // piece_is_knight() 103 | 104 | bool piece_is_knight(int piece) { 105 | 106 | ASSERT(piece_is_ok(piece)); 107 | 108 | return (piece & KnightFlag) != 0; 109 | } 110 | 111 | // piece_is_bishop() 112 | 113 | bool piece_is_bishop(int piece) { 114 | 115 | ASSERT(piece_is_ok(piece)); 116 | 117 | return (piece & QueenFlags) == BishopFlag; 118 | } 119 | 120 | // piece_is_rook() 121 | 122 | bool piece_is_rook(int piece) { 123 | 124 | ASSERT(piece_is_ok(piece)); 125 | 126 | return (piece & QueenFlags) == RookFlag; 127 | } 128 | 129 | // piece_is_queen() 130 | 131 | bool piece_is_queen(int piece) { 132 | 133 | ASSERT(piece_is_ok(piece)); 134 | 135 | return (piece & QueenFlags) == QueenFlags; 136 | } 137 | 138 | // piece_is_king() 139 | 140 | bool piece_is_king(int piece) { 141 | 142 | ASSERT(piece_is_ok(piece)); 143 | 144 | return (piece & KingFlag) != 0; 145 | } 146 | 147 | // piece_is_slider() 148 | 149 | bool piece_is_slider(int piece) { 150 | 151 | ASSERT(piece_is_ok(piece)); 152 | 153 | return (piece & QueenFlags) != 0; 154 | } 155 | 156 | // piece_to_12() 157 | 158 | int piece_to_12(int piece) { 159 | 160 | ASSERT(piece_is_ok(piece)); 161 | 162 | return PieceTo12[piece]; 163 | } 164 | 165 | // piece_from_12() 166 | 167 | int piece_from_12(int piece) { 168 | 169 | ASSERT(piece>=0&&piece<12); 170 | 171 | return PieceFrom12[piece]; 172 | } 173 | 174 | // piece_to_char() 175 | 176 | int piece_to_char(int piece) { 177 | 178 | ASSERT(piece_is_ok(piece)); 179 | 180 | return PieceString[piece_to_12(piece)]; 181 | } 182 | 183 | // piece_from_char() 184 | 185 | int piece_from_char(int c) { 186 | 187 | const char * ptr; 188 | 189 | ptr = strchr(PieceString,c); 190 | if (ptr == NULL) return PieceNone256; 191 | 192 | return piece_from_12(ptr-PieceString); 193 | } 194 | 195 | // char_is_piece() 196 | 197 | bool char_is_piece(int c) { 198 | 199 | return strchr("PNBRQK",c) != NULL; 200 | } 201 | 202 | // end of piece.cpp 203 | 204 | -------------------------------------------------------------------------------- /piece.cpp: -------------------------------------------------------------------------------- 1 | 2 | // piece.cpp 3 | 4 | // includes 5 | 6 | #include 7 | 8 | #include "colour.h" 9 | #include "piece.h" 10 | #include "util.h" 11 | 12 | // "constants" 13 | 14 | static const uint8 MakePawn[ColourNb] = { PieceNone256, BlackPawn256, WhitePawn256 }; // -BW 15 | 16 | static const uint8 PieceFrom12[12] = { 17 | BlackPawn256, WhitePawn256, 18 | BlackKnight256, WhiteKnight256, 19 | BlackBishop256, WhiteBishop256, 20 | BlackRook256, WhiteRook256, 21 | BlackQueen256, WhiteQueen256, 22 | BlackKing256, WhiteKing256, 23 | }; 24 | 25 | static const char PieceString[12+1] = "pPnNbBrRqQkK"; 26 | 27 | // variables 28 | 29 | static sint8 PieceTo12[256]; 30 | 31 | // functions 32 | 33 | // piece_init() 34 | 35 | void piece_init() { 36 | 37 | int piece; 38 | 39 | for (piece = 0; piece < 256; piece++) PieceTo12[piece] = -1; 40 | 41 | for (piece = 0; piece < 12; piece++) { 42 | PieceTo12[PieceFrom12[piece]] = piece; 43 | } 44 | } 45 | 46 | // piece_is_ok() 47 | 48 | bool piece_is_ok(int piece) { 49 | 50 | if (piece < 0 || piece >= 256) return false; 51 | 52 | if (PieceTo12[piece] < 0) return false; 53 | 54 | return true; 55 | } 56 | 57 | // piece_make_pawn() 58 | 59 | int piece_make_pawn(int colour) { 60 | 61 | ASSERT(colour_is_ok(colour)); 62 | 63 | return MakePawn[colour]; 64 | } 65 | 66 | // piece_pawn_opp() 67 | 68 | int piece_pawn_opp(int piece) { 69 | 70 | ASSERT(piece==BlackPawn256||piece==WhitePawn256); 71 | 72 | return piece ^ 15; 73 | } 74 | 75 | // piece_colour() 76 | 77 | int piece_colour(int piece) { 78 | 79 | ASSERT(piece_is_ok(piece)); 80 | 81 | return piece & 3; 82 | } 83 | 84 | // piece_type() 85 | 86 | int piece_type(int piece) { 87 | 88 | ASSERT(piece_is_ok(piece)); 89 | 90 | return piece & ~3; 91 | } 92 | 93 | // piece_is_pawn() 94 | 95 | bool piece_is_pawn(int piece) { 96 | 97 | ASSERT(piece_is_ok(piece)); 98 | 99 | return (piece & PawnFlags) != 0; 100 | } 101 | 102 | // piece_is_knight() 103 | 104 | bool piece_is_knight(int piece) { 105 | 106 | ASSERT(piece_is_ok(piece)); 107 | 108 | return (piece & KnightFlag) != 0; 109 | } 110 | 111 | // piece_is_bishop() 112 | 113 | bool piece_is_bishop(int piece) { 114 | 115 | ASSERT(piece_is_ok(piece)); 116 | 117 | return (piece & QueenFlags) == BishopFlag; 118 | } 119 | 120 | // piece_is_rook() 121 | 122 | bool piece_is_rook(int piece) { 123 | 124 | ASSERT(piece_is_ok(piece)); 125 | 126 | return (piece & QueenFlags) == RookFlag; 127 | } 128 | 129 | // piece_is_queen() 130 | 131 | bool piece_is_queen(int piece) { 132 | 133 | ASSERT(piece_is_ok(piece)); 134 | 135 | return (piece & QueenFlags) == QueenFlags; 136 | } 137 | 138 | // piece_is_king() 139 | 140 | bool piece_is_king(int piece) { 141 | 142 | ASSERT(piece_is_ok(piece)); 143 | 144 | return (piece & KingFlag) != 0; 145 | } 146 | 147 | // piece_is_slider() 148 | 149 | bool piece_is_slider(int piece) { 150 | 151 | ASSERT(piece_is_ok(piece)); 152 | 153 | return (piece & QueenFlags) != 0; 154 | } 155 | 156 | // piece_to_12() 157 | 158 | int piece_to_12(int piece) { 159 | 160 | ASSERT(piece_is_ok(piece)); 161 | 162 | return PieceTo12[piece]; 163 | } 164 | 165 | // piece_from_12() 166 | 167 | int piece_from_12(int piece) { 168 | 169 | ASSERT(piece>=0&&piece<12); 170 | 171 | return PieceFrom12[piece]; 172 | } 173 | 174 | // piece_to_char() 175 | 176 | int piece_to_char(int piece) { 177 | 178 | ASSERT(piece_is_ok(piece)); 179 | 180 | return PieceString[piece_to_12(piece)]; 181 | } 182 | 183 | // piece_from_char() 184 | 185 | int piece_from_char(int c) { 186 | 187 | const char * ptr; 188 | 189 | ptr = strchr(PieceString,c); 190 | if (ptr == NULL) return PieceNone256; 191 | 192 | return piece_from_12(ptr-PieceString); 193 | } 194 | 195 | // char_is_piece() 196 | 197 | bool char_is_piece(int c) { 198 | 199 | return strchr("PNBRQK",c) != NULL; 200 | } 201 | 202 | // end of piece.cpp 203 | 204 | -------------------------------------------------------------------------------- /piece.h: -------------------------------------------------------------------------------- 1 | 2 | // piece.h 3 | 4 | #ifndef PIECE_H 5 | #define PIECE_H 6 | 7 | // includes 8 | 9 | #include "colour.h" 10 | #include "util.h" 11 | 12 | // defines 13 | 14 | #define BlackPawnFlag (1 << 2) 15 | #define WhitePawnFlag (1 << 3) 16 | #define KnightFlag (1 << 4) 17 | #define BishopFlag (1 << 5) 18 | #define RookFlag (1 << 6) 19 | #define KingFlag (1 << 7) 20 | 21 | #define PawnFlags (BlackPawnFlag | WhitePawnFlag) 22 | #define QueenFlags (BishopFlag | RookFlag) 23 | 24 | #define PieceNone64 (0) 25 | #define BlackPawn64 (BlackPawnFlag) 26 | #define WhitePawn64 (WhitePawnFlag) 27 | #define Knight64 (KnightFlag) 28 | #define Bishop64 (BishopFlag) 29 | #define Rook64 (RookFlag) 30 | #define Queen64 (QueenFlags) 31 | #define King64 (KingFlag) 32 | 33 | #define PieceNone256 (0) 34 | #define BlackPawn256 (BlackPawn64 | Black) 35 | #define WhitePawn256 (WhitePawn64 | White) 36 | #define BlackKnight256 (Knight64 | Black) 37 | #define WhiteKnight256 (Knight64 | White) 38 | #define BlackBishop256 (Bishop64 | Black) 39 | #define WhiteBishop256 (Bishop64 | White) 40 | #define BlackRook256 (Rook64 | Black) 41 | #define WhiteRook256 (Rook64 | White) 42 | #define BlackQueen256 (Queen64 | Black) 43 | #define WhiteQueen256 (Queen64 | White) 44 | #define BlackKing256 (King64 | Black) 45 | #define WhiteKing256 (King64 | White) 46 | 47 | #define BlackPawn12 (0) 48 | #define WhitePawn12 (1) 49 | #define BlackKnight12 (2) 50 | #define WhiteKnight12 (3) 51 | #define BlackBishop12 (4) 52 | #define WhiteBishop12 (5) 53 | #define BlackRook12 (6) 54 | #define WhiteRook12 (7) 55 | #define BlackQueen12 (8) 56 | #define WhiteQueen12 (9) 57 | #define BlackKing12 (10) 58 | #define WhiteKing12 (11) 59 | 60 | // functions 61 | 62 | extern void piece_init (); 63 | 64 | extern bool piece_is_ok (int piece); 65 | 66 | extern int piece_make_pawn (int colour); 67 | extern int piece_pawn_opp (int piece); 68 | 69 | extern int piece_colour (int piece); 70 | extern int piece_type (int piece); 71 | 72 | extern bool piece_is_pawn (int piece); 73 | extern bool piece_is_knight (int piece); 74 | extern bool piece_is_bishop (int piece); 75 | extern bool piece_is_rook (int piece); 76 | extern bool piece_is_queen (int piece); 77 | extern bool piece_is_king (int piece); 78 | 79 | extern bool piece_is_slider (int piece); 80 | 81 | extern int piece_to_12 (int piece); 82 | extern int piece_from_12 (int piece); 83 | 84 | extern int piece_to_char (int piece); 85 | extern int piece_from_char (int c); 86 | 87 | extern bool char_is_piece (int c); 88 | 89 | #endif // !defined PIECE_H 90 | 91 | // end of piece.h 92 | 93 | -------------------------------------------------------------------------------- /pipe.h: -------------------------------------------------------------------------------- 1 | #ifndef PIPE_H 2 | #define PIPE_H 3 | #ifdef _WIN32 4 | // includes 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // constants 12 | 13 | // This should be bigger than the maximum length of an engine output or GUI 14 | // input line. 15 | const int LINE_INPUT_MAX_CHAR = 40960; 16 | 17 | // defines 18 | 19 | #define PIPE_EOF 1 20 | #define PIPE_ACTIVE 2 21 | 22 | // types 23 | 24 | class PipeStruct { 25 | friend DWORD WINAPI ThreadProc(LPVOID lpParam); 26 | public: 27 | HANDLE hProcess; 28 | HANDLE hEvent; 29 | bool GetBuffer(char *szLineStr); 30 | void LineInput(char *szLineStr); 31 | void LineOutput(const char *szLineStr) const; 32 | void Open(const char *szExecFile = NULL); 33 | void Close(void) const; 34 | void Kill(void) const; 35 | bool Active(void); 36 | bool EOF_(void); 37 | private: 38 | HANDLE hInput, hOutput; 39 | FILE *fpInput; 40 | HANDLE hThread; 41 | BOOL bConsole; 42 | BOOL bPipe; 43 | 44 | CRITICAL_SECTION CriticalSection; 45 | 46 | volatile DWORD state; 47 | volatile char * lpFeedEnd; 48 | volatile int nReadEnd; 49 | char lpBuffer[LINE_INPUT_MAX_CHAR]; 50 | char lpReadBuffer[LINE_INPUT_MAX_CHAR]; 51 | 52 | bool EOF_input(void); 53 | void set_EOF_input(void); 54 | void set_Active(void); 55 | void ReadInput(void); 56 | int ReadData(void); 57 | 58 | }; 59 | 60 | // pipe 61 | 62 | #endif 63 | #endif 64 | -------------------------------------------------------------------------------- /pipex.h: -------------------------------------------------------------------------------- 1 | #ifndef PIPEX_H 2 | #define PIPEX_H 3 | #ifdef _WIN32 4 | 5 | // WIN32 part 6 | 7 | // includes 8 | 9 | #include 10 | #include 11 | #include "util.h" 12 | 13 | // defines 14 | 15 | #define PIPEX_EOF (1<<0) 16 | #define PIPEX_ACTIVE (1<<1) 17 | 18 | // This should be bigger than the maximum length of an engine output or GUI 19 | // input line. 20 | 21 | #define LINE_INPUT_MAX_CHAR 4096 22 | 23 | // types 24 | 25 | typedef struct { 26 | HANDLE hProcess; 27 | HANDLE hEvent; 28 | HANDLE hInput, hOutput; 29 | FILE *fpInput; 30 | HANDLE hThread; 31 | BOOL bConsole; 32 | BOOL bPipe; 33 | CRITICAL_SECTION CriticalSection; 34 | volatile DWORD state; 35 | volatile char * lpFeedEnd; 36 | volatile int nReadEnd; 37 | char lpBuffer[LINE_INPUT_MAX_CHAR]; 38 | char lpReadBuffer[LINE_INPUT_MAX_CHAR]; 39 | char szWriteBuffer[LINE_INPUT_MAX_CHAR]; 40 | DWORD dwWriteIndex; 41 | const char *name; 42 | const char *command; 43 | BOOL quit_pending; 44 | 45 | } pipex_t; 46 | 47 | #else 48 | 49 | // POSIX part 50 | 51 | // includes 52 | 53 | #include 54 | #include 55 | #include 56 | #include 57 | 58 | 59 | #include "io.h" 60 | #include "util.h" 61 | 62 | // defines 63 | 64 | #define PIPEX_EOF (1<<0) 65 | #define PIPEX_ACTIVE (1<<1) 66 | 67 | // types 68 | 69 | typedef struct { 70 | io_t io[1]; 71 | pid_t pid; 72 | int state; 73 | bool quit_pending; 74 | const char *command; 75 | } pipex_t; 76 | 77 | #endif 78 | 79 | // part common to WIN32 and POSIX 80 | 81 | // macros 82 | 83 | #define PIPEX_MAGIC "!@#$%" 84 | #define WAIT_GRANULARITY 100 85 | 86 | // functions 87 | 88 | extern void pipex_open (pipex_t *pipex, 89 | const char *name, 90 | const char *working_dir, 91 | const char *command); 92 | extern bool pipex_active (pipex_t *pipex); 93 | extern bool pipex_readln (pipex_t *pipex, char *string); 94 | extern bool pipex_readln_nb (pipex_t *pipex, char *string); 95 | extern void pipex_writeln (pipex_t *pipex, const char *string); 96 | extern void pipex_write (pipex_t *pipex, const char *string); 97 | extern char* pipex_get_buffer (pipex_t *pipex); 98 | extern bool pipex_eof (pipex_t *pipex); 99 | extern void pipex_send_eof (pipex_t *pipex); 100 | extern void pipex_exit (pipex_t *pipex, int kill_timeout); 101 | extern void pipex_set_priority (pipex_t *pipex, int value); 102 | extern void pipex_set_affinity (pipex_t *pipex, int value); 103 | extern void pipex_wait_event (pipex_t *pipex[]); 104 | 105 | 106 | // pipex 107 | 108 | #endif 109 | -------------------------------------------------------------------------------- /polyglot.spec: -------------------------------------------------------------------------------- 1 | Summary: A Winboard protocol to UCI protocol adapter 2 | Name: polyglot 3 | Version: 1.4.70b 4 | Release: 1 5 | License: GPL 6 | Group: Amusement/Games 7 | URL: http://alpha.uhasselt.be/Research/Algebra/Toga 8 | 9 | Source: http://alpha.uhasselt.be/Research/Algebra/Toga/polyglot-%{version}.tar.gz 10 | BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root 11 | 12 | %description 13 | PolyGlot is a "UCI adapter". It connects a GUI interface (such as 14 | XBoard, Winboard, Arena or Chessbase) to a UCI chess engine. 15 | 16 | %prep 17 | %setup 18 | 19 | %build 20 | %configure 21 | %{__make} 22 | 23 | %install 24 | %{__rm} -rf %{buildroot} 25 | %makeinstall 26 | 27 | %clean 28 | %{__rm} -rf %{buildroot} 29 | 30 | %files 31 | %defattr(-, root, root, 0755) 32 | %doc %{_mandir}/man6/polyglot.6* 33 | %doc %{_docdir}/polyglot/README* 34 | %doc %{_docdir}/polyglot/book_format.html 35 | %{_bindir}/polyglot 36 | 37 | %changelog 38 | * Sat Jan 3 2009 Michel Van den Bergh - 1.4w10UCIb10-1 39 | - Initial spec file 40 | 41 | -------------------------------------------------------------------------------- /posix.cpp: -------------------------------------------------------------------------------- 1 | 2 | // posix.cpp 3 | 4 | // includes 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #ifdef _WIN32 12 | 13 | #include 14 | 15 | #else 16 | 17 | #include // Mac OS X needs this one first 18 | #include 19 | #include 20 | #include 21 | 22 | #endif 23 | 24 | #include "posix.h" 25 | #include "util.h" 26 | 27 | #ifndef _WIN32 28 | // prototypes 29 | 30 | static double duration (const struct timeval *t); 31 | 32 | // functions 33 | 34 | 35 | // input_available() 36 | 37 | bool input_available() { 38 | 39 | int val; 40 | fd_set set[1]; 41 | struct timeval time_val[1]; 42 | 43 | FD_ZERO(set); 44 | FD_SET(STDIN_FILENO,set); 45 | 46 | time_val->tv_sec = 0; 47 | time_val->tv_usec = 0; 48 | 49 | val = select(STDIN_FILENO+1,set,NULL,NULL,time_val); 50 | if (val == -1) my_fatal("input_available(): select(): %s\n",strerror(errno)); 51 | 52 | return val != 0; 53 | } 54 | 55 | 56 | // now_real() 57 | 58 | double now_real() { 59 | 60 | struct timeval tv[1]; 61 | struct timezone tz[1]; 62 | 63 | tz->tz_minuteswest = 0; 64 | tz->tz_dsttime = 0; // DST_NONE not declared in linux 65 | 66 | if (gettimeofday(tv,tz) == -1) { 67 | my_fatal("now_real(): gettimeofday(): %s\n",strerror(errno)); 68 | } 69 | 70 | return duration(tv); 71 | } 72 | 73 | // now_cpu() 74 | 75 | double now_cpu() { 76 | 77 | struct rusage ru[1]; 78 | 79 | if (getrusage(RUSAGE_SELF,ru) == -1) { 80 | my_fatal("now_cpu(): getrusage(): %s\n",strerror(errno)); 81 | } 82 | 83 | return duration(&ru->ru_utime); 84 | } 85 | 86 | // duration() 87 | 88 | static double duration(const struct timeval *tv) { 89 | 90 | return tv->tv_sec + tv->tv_usec * 1E-6; 91 | } 92 | 93 | #else 94 | 95 | double now_real(void) { 96 | return (double) GetTickCount() / 1000.0; 97 | } 98 | 99 | #endif 100 | 101 | // end of posix.cpp 102 | -------------------------------------------------------------------------------- /posix.h: -------------------------------------------------------------------------------- 1 | 2 | // posix.h 3 | 4 | #ifndef POSIX_H 5 | #define POSIX_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // functions 12 | 13 | extern bool input_available (); 14 | 15 | extern double now_real (); 16 | 17 | #endif // !defined POSIX_H 18 | 19 | // end of posix.h 20 | 21 | -------------------------------------------------------------------------------- /random.h: -------------------------------------------------------------------------------- 1 | 2 | // random.h 3 | 4 | #ifndef RANDOM_H 5 | #define RANDOM_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // defines 12 | 13 | #define RandomNb 781 14 | 15 | // macros 16 | 17 | #define RANDOM_64(n) (Random64[(n)]) 18 | 19 | // "constants" 20 | 21 | extern const uint64 Random64[RandomNb]; 22 | 23 | // functions 24 | 25 | extern void random_init (); 26 | extern uint64 random_64 (int n); 27 | 28 | #endif // !defined RANDOM_H 29 | 30 | // end of random.h 31 | 32 | -------------------------------------------------------------------------------- /san.h: -------------------------------------------------------------------------------- 1 | 2 | // san.h 3 | 4 | #ifndef SAN_H 5 | #define SAN_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "util.h" 11 | 12 | // functions 13 | 14 | extern bool move_to_san (int move, const board_t * board, char string[], int size); 15 | extern int move_from_san (const char string[], const board_t * board); 16 | 17 | extern int move_from_san_debug (const char string[], const board_t * board); 18 | 19 | #endif // !defined SAN_H 20 | 21 | // end of san.h 22 | 23 | -------------------------------------------------------------------------------- /search.c: -------------------------------------------------------------------------------- 1 | // search.c 2 | 3 | // includes 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "attack.h" 10 | #include "board.h" 11 | #include "colour.h" 12 | #include "engine.h" 13 | #include "fen.h" 14 | #include "line.h" 15 | #include "list.h" 16 | #include "move.h" 17 | #include "move_do.h" 18 | #include "move_gen.h" 19 | #include "move_legal.h" 20 | #include "option.h" 21 | #include "parse.h" 22 | #include "san.h" 23 | #include "search.h" 24 | #include "uci.h" 25 | #include "util.h" 26 | 27 | // variables 28 | 29 | static int Depth; 30 | 31 | static int BestMove; 32 | static int BestValue; 33 | static move_t BestPV[LineSize]; 34 | 35 | static sint64 NodeNb; 36 | static sint64 LeafNb; 37 | static double Time; 38 | 39 | static int Move; 40 | static int MovePos; 41 | static int MoveNb; 42 | 43 | // prototypes 44 | 45 | static bool depth_is_ok (int depth); 46 | static void perft (const board_t * board, int depth); 47 | 48 | // functions 49 | 50 | // depth_is_ok() 51 | 52 | static bool depth_is_ok(int depth) { 53 | 54 | return depth >= 0 && depth < DepthMax; 55 | } 56 | 57 | // search() 58 | 59 | void search(const board_t * board, int depth_max, double time_max) { 60 | 61 | char string[256]; 62 | 63 | ASSERT(board_is_ok(board)); 64 | ASSERT(depth_max>=1&&depth_max=0.0); 66 | 67 | // engine 68 | 69 | Depth = 0; 70 | 71 | BestMove = MoveNone; 72 | BestValue = 0; 73 | line_clear(BestPV); 74 | 75 | NodeNb = 0; 76 | LeafNb = 0; 77 | Time = 0.0; 78 | 79 | Move = MoveNone; 80 | MovePos = 0; 81 | MoveNb = 0; 82 | 83 | // init 84 | 85 | uci_send_ucinewgame(Uci); 86 | uci_send_isready_sync(Uci); 87 | 88 | // position 89 | 90 | if (!board_to_fen(board,string,256)) ASSERT(FALSE); 91 | engine_send(Engine,"position fen %s",string); 92 | 93 | // search 94 | 95 | engine_send_queue(Engine,"go"); 96 | 97 | engine_send_queue(Engine," movetime %.0f",time_max*1000.0); 98 | engine_send_queue(Engine," depth %d",depth_max); 99 | 100 | engine_send(Engine,""); // newline 101 | 102 | // wait for feed-back 103 | 104 | while (!engine_eof(Engine)) { 105 | 106 | engine_get(Engine,string); 107 | 108 | if (FALSE) { 109 | 110 | } else if (match(string,"bestmove * ponder *")) { 111 | 112 | BestMove = move_from_can(Star[0],board); 113 | ASSERT(BestMove!=MoveNone&&move_is_legal(BestMove,board)); 114 | 115 | break; 116 | 117 | } else if (match(string,"bestmove *")) { 118 | 119 | BestMove = move_from_can(Star[0],board); 120 | ASSERT(BestMove!=MoveNone&&move_is_legal(BestMove,board)); 121 | 122 | break; 123 | } 124 | } 125 | 126 | printf("\n"); 127 | } 128 | 129 | // do_perft() 130 | 131 | void do_perft(int argc,char * argv[]){ 132 | const char * fen=NULL; 133 | int depth=1; 134 | board_t board[1]; 135 | int i; 136 | for (i = 1; i < argc; i++) { 137 | if (FALSE) { 138 | } else if (my_string_equal(argv[i],"perft")) { 139 | // skip 140 | } else if (my_string_equal(argv[i],"-fen")) { 141 | i++; 142 | if (argv[i] == NULL) my_fatal("do_perft(): missing argument\n"); 143 | my_string_set(&fen,argv[i]); 144 | } else if (my_string_equal(argv[i],"-max-depth")){ 145 | i++; 146 | if (argv[i] == NULL) my_fatal("do_perft(): missing argument\n"); 147 | depth=atoi(argv[i]); 148 | if(depth<1) my_fatal("do_perft(): illegal depth %d\n",depth); 149 | } else { 150 | my_fatal("do_perft(): unknown option \"%s\"\n",argv[i]); 151 | } 152 | } 153 | if(fen==NULL){ 154 | my_string_set(&fen,StartFen); 155 | } 156 | board_from_fen(board,fen); 157 | search_perft(board,depth); 158 | } 159 | 160 | // search_perft() 161 | 162 | void search_perft(const board_t * board, int depth_max) { 163 | 164 | int depth; 165 | my_timer_t timer[1]; 166 | double time, speed; 167 | char node_string[StringSize]; 168 | char leafnode_string[StringSize]; 169 | 170 | ASSERT(board_is_ok(board)); 171 | ASSERT(depth_max>=1&&depth_maxturn))); 216 | 217 | // init 218 | 219 | NodeNb++; 220 | 221 | // leaf 222 | 223 | if (depth == 0) { 224 | LeafNb++; 225 | return; 226 | } 227 | 228 | // more init 229 | 230 | me = board->turn; 231 | 232 | // move loop 233 | 234 | gen_moves(list,board); 235 | 236 | for (i = 0; i < list_size(list); i++) { 237 | 238 | move = list_move(list,i); 239 | 240 | board_copy(new_board,board); 241 | move_do(new_board,move); 242 | 243 | if (!is_in_check(new_board,me)) perft(new_board,depth-1); 244 | } 245 | } 246 | 247 | // end of search.cpp 248 | 249 | -------------------------------------------------------------------------------- /search.cpp: -------------------------------------------------------------------------------- 1 | // search.cpp 2 | 3 | // includes 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "attack.h" 10 | #include "board.h" 11 | #include "colour.h" 12 | #include "engine.h" 13 | #include "fen.h" 14 | #include "line.h" 15 | #include "list.h" 16 | #include "move.h" 17 | #include "move_do.h" 18 | #include "move_gen.h" 19 | #include "move_legal.h" 20 | #include "option.h" 21 | #include "parse.h" 22 | #include "san.h" 23 | #include "search.h" 24 | #include "uci.h" 25 | #include "util.h" 26 | 27 | // constants 28 | 29 | static const int StringSize = 4096; 30 | 31 | // variables 32 | 33 | static int Depth; 34 | 35 | static int BestMove; 36 | static int BestValue; 37 | static move_t BestPV[LineSize]; 38 | 39 | static sint64 NodeNb; 40 | static sint64 LeafNb; 41 | static double Time; 42 | 43 | static int Move; 44 | static int MovePos; 45 | static int MoveNb; 46 | 47 | // prototypes 48 | 49 | static bool depth_is_ok (int depth); 50 | static void perft (const board_t * board, int depth); 51 | 52 | // functions 53 | 54 | // depth_is_ok() 55 | 56 | static bool depth_is_ok(int depth) { 57 | 58 | return depth >= 0 && depth < DepthMax; 59 | } 60 | 61 | // search() 62 | 63 | void search(const board_t * board, int depth_max, double time_max) { 64 | 65 | char string[256]; 66 | 67 | ASSERT(board_is_ok(board)); 68 | ASSERT(depth_max>=1&&depth_max=0.0); 70 | 71 | // engine 72 | 73 | Depth = 0; 74 | 75 | BestMove = MoveNone; 76 | BestValue = 0; 77 | line_clear(BestPV); 78 | 79 | NodeNb = 0; 80 | LeafNb = 0; 81 | Time = 0.0; 82 | 83 | Move = MoveNone; 84 | MovePos = 0; 85 | MoveNb = 0; 86 | 87 | // init 88 | 89 | uci_send_ucinewgame(Uci); 90 | uci_send_isready_sync(Uci); 91 | 92 | // position 93 | 94 | if (!board_to_fen(board,string,256)) ASSERT(false); 95 | engine_send(Engine,"position fen %s",string); 96 | 97 | // search 98 | 99 | engine_send_queue(Engine,"go"); 100 | 101 | engine_send_queue(Engine," movetime %.0f",time_max*1000.0); 102 | engine_send_queue(Engine," depth %d",depth_max); 103 | 104 | engine_send(Engine,""); // newline 105 | 106 | // wait for feed-back 107 | 108 | while (!engine_eof(Engine)) { 109 | 110 | engine_get(Engine,string,256); 111 | 112 | if (false) { 113 | 114 | } else if (match(string,"bestmove * ponder *")) { 115 | 116 | BestMove = move_from_can(Star[0],board); 117 | ASSERT(BestMove!=MoveNone&&move_is_legal(BestMove,board)); 118 | 119 | break; 120 | 121 | } else if (match(string,"bestmove *")) { 122 | 123 | BestMove = move_from_can(Star[0],board); 124 | ASSERT(BestMove!=MoveNone&&move_is_legal(BestMove,board)); 125 | 126 | break; 127 | } 128 | } 129 | 130 | printf("\n"); 131 | } 132 | 133 | // do_perft() 134 | 135 | void do_perft(int argc,char * argv[]){ 136 | const char * fen=NULL; 137 | int depth=1; 138 | board_t board[1]; 139 | int i; 140 | for (i = 1; i < argc; i++) { 141 | if (false) { 142 | } else if (my_string_equal(argv[i],"perft")) { 143 | // skip 144 | } else if (my_string_equal(argv[i],"-fen")) { 145 | i++; 146 | if (argv[i] == NULL) my_fatal("do_perft(): missing argument\n"); 147 | my_string_set(&fen,argv[i]); 148 | } else if (my_string_equal(argv[i],"-max-depth")){ 149 | i++; 150 | if (argv[i] == NULL) my_fatal("do_perft(): missing argument\n"); 151 | depth=atoi(argv[i]); 152 | if(depth<1) my_fatal("do_perft(): illegal depth %d\n",depth); 153 | } else { 154 | my_fatal("do_perft(): unknown option \"%s\"\n",argv[i]); 155 | } 156 | } 157 | if(fen==NULL){ 158 | my_string_set(&fen,StartFen); 159 | } 160 | board_from_fen(board,fen); 161 | search_perft(board,depth); 162 | } 163 | 164 | // search_perft() 165 | 166 | void search_perft(const board_t * board, int depth_max) { 167 | 168 | int depth; 169 | my_timer_t timer[1]; 170 | double time, speed; 171 | char node_string[StringSize]; 172 | char leafnode_string[StringSize]; 173 | 174 | ASSERT(board_is_ok(board)); 175 | ASSERT(depth_max>=1&&depth_maxturn))); 220 | 221 | // init 222 | 223 | NodeNb++; 224 | 225 | // leaf 226 | 227 | if (depth == 0) { 228 | LeafNb++; 229 | return; 230 | } 231 | 232 | // more init 233 | 234 | me = board->turn; 235 | 236 | // move loop 237 | 238 | gen_moves(list,board); 239 | 240 | for (i = 0; i < list_size(list); i++) { 241 | 242 | move = list_move(list,i); 243 | 244 | board_copy(new_board,board); 245 | move_do(new_board,move); 246 | 247 | if (!is_in_check(new_board,me)) perft(new_board,depth-1); 248 | } 249 | } 250 | 251 | // end of search.cpp 252 | 253 | -------------------------------------------------------------------------------- /search.h: -------------------------------------------------------------------------------- 1 | // search.h 2 | 3 | #ifndef SEARCH_H 4 | #define SEARCH_H 5 | 6 | // includes 7 | 8 | #include "board.h" 9 | #include "util.h" 10 | 11 | // defines 12 | 13 | #define DepthMax 63 14 | 15 | // functions 16 | 17 | extern void search (const board_t * board, int depth_max, double time_max); 18 | extern void search_perft (const board_t * board, int depth_max); 19 | 20 | extern void do_perft (int argc, char * argv[]); 21 | 22 | #endif // !defined SEARCH_H 23 | 24 | // end of search.h 25 | 26 | -------------------------------------------------------------------------------- /square.c: -------------------------------------------------------------------------------- 1 | 2 | // square.c 3 | 4 | // includes 5 | 6 | #include "colour.h" 7 | #include "square.h" 8 | #include "util.h" 9 | 10 | // "constants" 11 | 12 | static const uint8 SquareFrom64[64] = { 13 | A1, B1, C1, D1, E1, F1, G1, H1, 14 | A2, B2, C2, D2, E2, F2, G2, H2, 15 | A3, B3, C3, D3, E3, F3, G3, H3, 16 | A4, B4, C4, D4, E4, F4, G4, H4, 17 | A5, B5, C5, D5, E5, F5, G5, H5, 18 | A6, B6, C6, D6, E6, F6, G6, H6, 19 | A7, B7, C7, D7, E7, F7, G7, H7, 20 | A8, B8, C8, D8, E8, F8, G8, H8, 21 | }; 22 | 23 | // variables 24 | 25 | static sint8 SquareTo64[SquareNb]; 26 | 27 | // functions 28 | 29 | // square_init() 30 | 31 | void square_init() { 32 | 33 | int sq; 34 | 35 | for (sq = 0; sq < SquareNb; sq++) SquareTo64[sq] = -1; 36 | 37 | for (sq = 0; sq < 64; sq++) { 38 | SquareTo64[SquareFrom64[sq]] = sq; 39 | } 40 | } 41 | 42 | // square_is_ok() 43 | 44 | bool square_is_ok(int square) { 45 | 46 | if (square < 0 || square >= SquareNb) return FALSE; 47 | 48 | if (SquareTo64[square] < 0) return FALSE; 49 | 50 | return TRUE; 51 | } 52 | 53 | // square_make() 54 | 55 | int square_make(int file, int rank) { 56 | 57 | int sq_64; 58 | 59 | ASSERT(file>=0&&file<8); 60 | ASSERT(rank>=0&&rank<8); 61 | 62 | sq_64 = (rank << 3) | file; 63 | 64 | return square_from_64(sq_64); 65 | } 66 | 67 | // square_file() 68 | 69 | int square_file(int square) { 70 | 71 | int file; 72 | 73 | ASSERT(square_is_ok(square)); 74 | 75 | file = (square - 4) & 7; 76 | ASSERT(file==(square_to_64(square)&7)); 77 | 78 | return file; 79 | } 80 | 81 | // square_rank() 82 | 83 | int square_rank(int square) { 84 | 85 | int rank; 86 | 87 | ASSERT(square_is_ok(square)); 88 | 89 | rank = (square >> 4) - 2; 90 | ASSERT(rank==square_to_64(square)>>3); 91 | 92 | return rank; 93 | } 94 | 95 | // square_side_rank() 96 | 97 | int square_side_rank(int square, int colour) { 98 | 99 | int rank; 100 | 101 | ASSERT(square_is_ok(square)); 102 | ASSERT(colour_is_ok(colour)); 103 | 104 | rank = square_rank(square); 105 | if (colour_is_black(colour)) rank = 7-rank; 106 | 107 | return rank; 108 | } 109 | 110 | // square_from_64() 111 | 112 | int square_from_64(int square) { 113 | 114 | ASSERT(square>=0&&square<64); 115 | 116 | return SquareFrom64[square]; 117 | } 118 | 119 | // square_to_64() 120 | 121 | int square_to_64(int square) { 122 | 123 | ASSERT(square_is_ok(square)); 124 | 125 | return SquareTo64[square]; 126 | } 127 | 128 | // square_is_promote() 129 | 130 | bool square_is_promote(int square) { 131 | 132 | int rank; 133 | 134 | ASSERT(square_is_ok(square)); 135 | 136 | rank = square_rank(square); 137 | 138 | return rank == Rank1 || rank == Rank8; 139 | } 140 | 141 | // square_ep_dual() 142 | 143 | int square_ep_dual(int square) { 144 | 145 | ASSERT(square_is_ok(square)); 146 | ASSERT(square_rank(square)>=2&&square_rank(square)<=5); 147 | 148 | return square ^ 16; 149 | } 150 | 151 | // square_colour() 152 | 153 | int square_colour(int square) { 154 | 155 | ASSERT(square_is_ok(square)); 156 | 157 | return (square ^ (square >> 4)) & 1; 158 | } 159 | 160 | // file_from_char() 161 | 162 | int file_from_char(int c) { 163 | 164 | ASSERT(c>='a'&&c<='h'); 165 | 166 | return c - 'a'; 167 | } 168 | 169 | // rank_from_char() 170 | 171 | int rank_from_char(int c) { 172 | 173 | ASSERT(c>='1'&&c<='8'); 174 | 175 | return c - '1'; 176 | } 177 | 178 | // file_to_char() 179 | 180 | int file_to_char(int file) { 181 | 182 | ASSERT(file>=0&&file<8); 183 | 184 | return 'a' + file; 185 | } 186 | 187 | // rank_to_char() 188 | 189 | int rank_to_char(int rank) { 190 | 191 | ASSERT(rank>=0&&rank<8); 192 | 193 | return '1' + rank; 194 | } 195 | 196 | // char_is_file() 197 | 198 | bool char_is_file(int c) { 199 | 200 | return c >= 'a' && c <= 'h'; 201 | } 202 | 203 | // char_is_rank() 204 | 205 | bool char_is_rank(int c) { 206 | 207 | return c >= '1' && c <= '8'; 208 | } 209 | 210 | // square_to_string() 211 | 212 | bool square_to_string(int square, char string[], int size) { 213 | 214 | ASSERT(square_is_ok(square)); 215 | ASSERT(string!=NULL); 216 | ASSERT(size>=3); 217 | 218 | if (size < 3) return FALSE; 219 | 220 | string[0] = 'a' + square_file(square); 221 | string[1] = '1' + square_rank(square); 222 | string[2] = '\0'; 223 | 224 | return TRUE; 225 | } 226 | 227 | // square_from_string() 228 | 229 | int square_from_string(const char string[]) { 230 | 231 | int file, rank; 232 | 233 | ASSERT(string!=NULL); 234 | 235 | if (string[0] < 'a' || string[0] > 'h') return SquareNone; 236 | if (string[1] < '1' || string[1] > '8') return SquareNone; 237 | if (string[2] != '\0') return SquareNone; 238 | 239 | file = file_from_char(string[0]); 240 | rank = rank_from_char(string[1]); 241 | 242 | return square_make(file,rank); 243 | } 244 | 245 | // end of square.cpp 246 | 247 | -------------------------------------------------------------------------------- /square.cpp: -------------------------------------------------------------------------------- 1 | 2 | // square.cpp 3 | 4 | // includes 5 | 6 | #include "colour.h" 7 | #include "square.h" 8 | #include "util.h" 9 | 10 | // "constants" 11 | 12 | static const uint8 SquareFrom64[64] = { 13 | A1, B1, C1, D1, E1, F1, G1, H1, 14 | A2, B2, C2, D2, E2, F2, G2, H2, 15 | A3, B3, C3, D3, E3, F3, G3, H3, 16 | A4, B4, C4, D4, E4, F4, G4, H4, 17 | A5, B5, C5, D5, E5, F5, G5, H5, 18 | A6, B6, C6, D6, E6, F6, G6, H6, 19 | A7, B7, C7, D7, E7, F7, G7, H7, 20 | A8, B8, C8, D8, E8, F8, G8, H8, 21 | }; 22 | 23 | // variables 24 | 25 | static sint8 SquareTo64[SquareNb]; 26 | 27 | // functions 28 | 29 | // square_init() 30 | 31 | void square_init() { 32 | 33 | int sq; 34 | 35 | for (sq = 0; sq < SquareNb; sq++) SquareTo64[sq] = -1; 36 | 37 | for (sq = 0; sq < 64; sq++) { 38 | SquareTo64[SquareFrom64[sq]] = sq; 39 | } 40 | } 41 | 42 | // square_is_ok() 43 | 44 | bool square_is_ok(int square) { 45 | 46 | if (square < 0 || square >= SquareNb) return false; 47 | 48 | if (SquareTo64[square] < 0) return false; 49 | 50 | return true; 51 | } 52 | 53 | // square_make() 54 | 55 | int square_make(int file, int rank) { 56 | 57 | int sq_64; 58 | 59 | ASSERT(file>=0&&file<8); 60 | ASSERT(rank>=0&&rank<8); 61 | 62 | sq_64 = (rank << 3) | file; 63 | 64 | return square_from_64(sq_64); 65 | } 66 | 67 | // square_file() 68 | 69 | int square_file(int square) { 70 | 71 | int file; 72 | 73 | ASSERT(square_is_ok(square)); 74 | 75 | file = (square - 4) & 7; 76 | ASSERT(file==(square_to_64(square)&7)); 77 | 78 | return file; 79 | } 80 | 81 | // square_rank() 82 | 83 | int square_rank(int square) { 84 | 85 | int rank; 86 | 87 | ASSERT(square_is_ok(square)); 88 | 89 | rank = (square >> 4) - 2; 90 | ASSERT(rank==square_to_64(square)>>3); 91 | 92 | return rank; 93 | } 94 | 95 | // square_side_rank() 96 | 97 | int square_side_rank(int square, int colour) { 98 | 99 | int rank; 100 | 101 | ASSERT(square_is_ok(square)); 102 | ASSERT(colour_is_ok(colour)); 103 | 104 | rank = square_rank(square); 105 | if (colour_is_black(colour)) rank = 7-rank; 106 | 107 | return rank; 108 | } 109 | 110 | // square_from_64() 111 | 112 | int square_from_64(int square) { 113 | 114 | ASSERT(square>=0&&square<64); 115 | 116 | return SquareFrom64[square]; 117 | } 118 | 119 | // square_to_64() 120 | 121 | int square_to_64(int square) { 122 | 123 | ASSERT(square_is_ok(square)); 124 | 125 | return SquareTo64[square]; 126 | } 127 | 128 | // square_is_promote() 129 | 130 | bool square_is_promote(int square) { 131 | 132 | int rank; 133 | 134 | ASSERT(square_is_ok(square)); 135 | 136 | rank = square_rank(square); 137 | 138 | return rank == Rank1 || rank == Rank8; 139 | } 140 | 141 | // square_ep_dual() 142 | 143 | int square_ep_dual(int square) { 144 | 145 | ASSERT(square_is_ok(square)); 146 | ASSERT(square_rank(square)>=2&&square_rank(square)<=5); 147 | 148 | return square ^ 16; 149 | } 150 | 151 | // square_colour() 152 | 153 | int square_colour(int square) { 154 | 155 | ASSERT(square_is_ok(square)); 156 | 157 | return (square ^ (square >> 4)) & 1; 158 | } 159 | 160 | // file_from_char() 161 | 162 | int file_from_char(int c) { 163 | 164 | ASSERT(c>='a'&&c<='h'); 165 | 166 | return c - 'a'; 167 | } 168 | 169 | // rank_from_char() 170 | 171 | int rank_from_char(int c) { 172 | 173 | ASSERT(c>='1'&&c<='8'); 174 | 175 | return c - '1'; 176 | } 177 | 178 | // file_to_char() 179 | 180 | int file_to_char(int file) { 181 | 182 | ASSERT(file>=0&&file<8); 183 | 184 | return 'a' + file; 185 | } 186 | 187 | // rank_to_char() 188 | 189 | int rank_to_char(int rank) { 190 | 191 | ASSERT(rank>=0&&rank<8); 192 | 193 | return '1' + rank; 194 | } 195 | 196 | // char_is_file() 197 | 198 | bool char_is_file(int c) { 199 | 200 | return c >= 'a' && c <= 'h'; 201 | } 202 | 203 | // char_is_rank() 204 | 205 | bool char_is_rank(int c) { 206 | 207 | return c >= '1' && c <= '8'; 208 | } 209 | 210 | // square_to_string() 211 | 212 | bool square_to_string(int square, char string[], int size) { 213 | 214 | ASSERT(square_is_ok(square)); 215 | ASSERT(string!=NULL); 216 | ASSERT(size>=3); 217 | 218 | if (size < 3) return false; 219 | 220 | string[0] = 'a' + square_file(square); 221 | string[1] = '1' + square_rank(square); 222 | string[2] = '\0'; 223 | 224 | return true; 225 | } 226 | 227 | // square_from_string() 228 | 229 | int square_from_string(const char string[]) { 230 | 231 | int file, rank; 232 | 233 | ASSERT(string!=NULL); 234 | 235 | if (string[0] < 'a' || string[0] > 'h') return SquareNone; 236 | if (string[1] < '1' || string[1] > '8') return SquareNone; 237 | if (string[2] != '\0') return SquareNone; 238 | 239 | file = file_from_char(string[0]); 240 | rank = rank_from_char(string[1]); 241 | 242 | return square_make(file,rank); 243 | } 244 | 245 | // end of square.cpp 246 | 247 | -------------------------------------------------------------------------------- /square.h: -------------------------------------------------------------------------------- 1 | 2 | // square.h 3 | 4 | #ifndef SQUARE_H 5 | #define SQUARE_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // defines 12 | 13 | #define SquareNb (16 * 12) 14 | 15 | #define FileA 0 16 | #define FileB 1 17 | #define FileC 2 18 | #define FileD 3 19 | #define FileE 4 20 | #define FileF 5 21 | #define FileG 6 22 | #define FileH 7 23 | 24 | #define Rank1 0 25 | #define Rank2 1 26 | #define Rank3 2 27 | #define Rank4 3 28 | #define Rank5 4 29 | #define Rank6 5 30 | #define Rank7 6 31 | #define Rank8 7 32 | 33 | #define SquareNone 0 34 | 35 | #define A1 0x24 36 | #define B1 0x25 37 | #define C1 0x26 38 | #define D1 0x27 39 | #define E1 0x28 40 | #define F1 0x29 41 | #define G1 0x2A 42 | #define H1 0x2B 43 | #define A2 0x34 44 | #define B2 0x35 45 | #define C2 0x36 46 | #define D2 0x37 47 | #define E2 0x38 48 | #define F2 0x39 49 | #define G2 0x3A 50 | #define H2 0x3B 51 | #define A3 0x44 52 | #define B3 0x45 53 | #define C3 0x46 54 | #define D3 0x47 55 | #define E3 0x48 56 | #define F3 0x49 57 | #define G3 0x4A 58 | #define H3 0x4B 59 | #define A4 0x54 60 | #define B4 0x55 61 | #define C4 0x56 62 | #define D4 0x57 63 | #define E4 0x58 64 | #define F4 0x59 65 | #define G4 0x5A 66 | #define H4 0x5B 67 | #define A5 0x64 68 | #define B5 0x65 69 | #define C5 0x66 70 | #define D5 0x67 71 | #define E5 0x68 72 | #define F5 0x69 73 | #define G5 0x6A 74 | #define H5 0x6B 75 | #define A6 0x74 76 | #define B6 0x75 77 | #define C6 0x76 78 | #define D6 0x77 79 | #define E6 0x78 80 | #define F6 0x79 81 | #define G6 0x7A 82 | #define H6 0x7B 83 | #define A7 0x84 84 | #define B7 0x85 85 | #define C7 0x86 86 | #define D7 0x87 87 | #define E7 0x88 88 | #define F7 0x89 89 | #define G7 0x8A 90 | #define H7 0x8B 91 | #define A8 0x94 92 | #define B8 0x95 93 | #define C8 0x96 94 | #define D8 0x97 95 | #define E8 0x98 96 | #define F8 0x99 97 | #define G8 0x9A 98 | #define H8 0x9B 99 | 100 | #define Dark 0 101 | #define Light 1 102 | 103 | // functions 104 | 105 | extern void square_init (); 106 | 107 | extern bool square_is_ok (int square); 108 | 109 | extern int square_make (int file, int rank); 110 | 111 | extern int square_file (int square); 112 | extern int square_rank (int square); 113 | extern int square_side_rank (int square, int colour); 114 | 115 | extern int square_from_64 (int square); 116 | extern int square_to_64 (int square); 117 | 118 | extern bool square_is_promote (int square); 119 | extern int square_ep_dual (int square); 120 | 121 | extern int square_colour (int square); 122 | 123 | extern bool char_is_file (int c); 124 | extern bool char_is_rank (int c); 125 | 126 | extern int file_from_char (int c); 127 | extern int rank_from_char (int c); 128 | 129 | extern int file_to_char (int file); 130 | extern int rank_to_char (int rank); 131 | 132 | extern bool square_to_string (int square, char string[], int size); 133 | extern int square_from_string (const char string[]); 134 | 135 | #endif // !defined SQUARE_H 136 | 137 | // end of square.h 138 | 139 | -------------------------------------------------------------------------------- /uci.h: -------------------------------------------------------------------------------- 1 | 2 | // uci.h 3 | 4 | #ifndef UCI_H 5 | #define UCI_H 6 | 7 | // includes 8 | 9 | #include "board.h" 10 | #include "engine.h" 11 | #include "line.h" 12 | #include "move.h" 13 | #include "option.h" 14 | #include "util.h" 15 | 16 | // macros 17 | 18 | // I need to make a uniform string type. 19 | 20 | #define UciStringSize 4096 21 | #define MultiPVStackSize 256 22 | 23 | // types 24 | 25 | typedef struct { 26 | 27 | engine_t * engine; 28 | 29 | const char * name; 30 | const char * author; 31 | 32 | option_list_t option[1]; 33 | 34 | bool ready; 35 | int ready_nb; 36 | 37 | bool searching; 38 | int pending_nb; 39 | 40 | board_t board[1]; 41 | 42 | int best_move; 43 | int ponder_move; 44 | 45 | int score; 46 | int depth; 47 | int sel_depth; 48 | move_t pv[LineSize]; 49 | 50 | int best_score; 51 | int best_depth; 52 | int best_sel_depth; 53 | move_t best_pv[LineSize]; 54 | char bestmove[UciStringSize]; 55 | 56 | sint64 node_nb; 57 | double time; 58 | double speed; 59 | double cpu; 60 | double hash; 61 | move_t current_line[LineSize]; 62 | 63 | int root_move; 64 | int root_move_pos; 65 | int root_move_nb; 66 | bool multipv_mode; 67 | int multipvSP; 68 | int multipvScore[MultiPVStackSize]; 69 | move_t multipvMove[MultiPVStackSize]; 70 | char info[UciStringSize]; 71 | } uci_t; 72 | 73 | typedef enum { 74 | EVENT_NONE = 0, 75 | EVENT_UCI = 1 << 0, 76 | EVENT_READY = 1 << 1, 77 | EVENT_STOP = 1 << 2, 78 | EVENT_MOVE = 1 << 3, 79 | EVENT_PV = 1 << 4, 80 | EVENT_DEPTH = 1 << 5, 81 | EVENT_DRAW = 1 << 6, 82 | EVENT_RESIGN = 1 << 7, 83 | EVENT_ILLEGAL_MOVE = 1 << 8, 84 | EVENT_INFO = 1 << 9 85 | } dummy_event_t; 86 | 87 | // variables 88 | 89 | extern uci_t Uci[1]; 90 | 91 | // functions 92 | 93 | extern void uci_open (uci_t * uci, engine_t * engine); 94 | extern void uci_send_isready (uci_t * uci); 95 | extern void uci_send_isready_sync (uci_t * uci); 96 | extern void uci_send_stop (uci_t * uci); 97 | extern void uci_send_stop_sync (uci_t * uci); 98 | extern void uci_send_ucinewgame (uci_t * uci); 99 | extern void uci_set_threads (uci_t * uci, int n); 100 | extern const char * uci_thread_option(uci_t * uci); 101 | extern bool uci_send_option (uci_t * uci, const char option[], const char format[], ...); 102 | extern void uci_close (uci_t * uci); 103 | extern void uci_clear (uci_t * uci); 104 | extern int uci_parse (uci_t * uci, const char string[]); 105 | 106 | #endif // !defined UCI_H 107 | 108 | // end of uci.h 109 | 110 | -------------------------------------------------------------------------------- /uci2uci.c: -------------------------------------------------------------------------------- 1 | // uci2uci.c 2 | 3 | // includes 4 | 5 | #include 6 | #include 7 | 8 | #include "util.h" 9 | #include "board.h" 10 | #include "engine.h" 11 | #include "fen.h" 12 | #include "gui.h" 13 | #include "move.h" 14 | #include "move_do.h" 15 | #include "move_legal.h" 16 | #include "parse.h" 17 | #include "option.h" 18 | #include "book.h" 19 | #include "main.h" 20 | #include "uci.h" 21 | 22 | // defines 23 | 24 | #define StringSize 4096 25 | 26 | // variables 27 | 28 | static board_t UCIboard[1]; 29 | static bool Init=TRUE; 30 | static int SavedMove=MoveNone; 31 | 32 | // prototypes 33 | 34 | static void send_uci_options(); 35 | 36 | 37 | // normalize_type() 38 | 39 | static void normalize_type(char *dst, const char* src){ 40 | if(option_get_int(Option,"UCIVersion") <=2){ 41 | if(IS_STRING(src)){ 42 | strcpy(dst,"string"); 43 | return; 44 | }else if(IS_SPIN(src)){ 45 | strcpy(dst,"spin"); 46 | }else if(IS_BUTTON(src)){ 47 | strcpy(dst,"button"); 48 | }else{ 49 | strcpy(dst,src); 50 | } 51 | }else{ 52 | strcpy(dst,src); 53 | } 54 | } 55 | 56 | // parse_position() 57 | 58 | static void parse_position(const char string[]) { 59 | 60 | /* This is borrowed from Toga II. This code is quite hacky and will be 61 | rewritten using the routines in parse.cpp. 62 | */ 63 | 64 | const char * fen; 65 | char * moves; 66 | const char * ptr; 67 | char move_string[256]; 68 | int move; 69 | char * string_copy; 70 | 71 | // init 72 | 73 | string_copy=my_strdup(string); 74 | 75 | fen = strstr(string_copy,"fen "); 76 | moves = strstr(string_copy,"moves "); 77 | 78 | // start position 79 | 80 | if (fen != NULL) { // "fen" present 81 | 82 | if (moves != NULL) { // "moves" present 83 | ASSERT(moves>fen); 84 | moves[-1] = '\0'; // dirty, but so is UCI 85 | } 86 | 87 | board_from_fen(UCIboard,fen+4); // CHANGE ME 88 | 89 | } else { 90 | 91 | // HACK: assumes startpos 92 | 93 | board_from_fen(UCIboard,StartFen); 94 | } 95 | 96 | // moves 97 | 98 | if (moves != NULL) { // "moves" present 99 | 100 | ptr = moves + 6; 101 | 102 | while (*ptr != '\0') { 103 | 104 | while (*ptr == ' ') ptr++; 105 | 106 | move_string[0] = *ptr++; 107 | move_string[1] = *ptr++; 108 | move_string[2] = *ptr++; 109 | move_string[3] = *ptr++; 110 | 111 | if (*ptr == '\0' || *ptr == ' ') { 112 | move_string[4] = '\0'; 113 | } else { // promote 114 | move_string[4] = *ptr++; 115 | move_string[5] = '\0'; 116 | } 117 | move = move_from_can(move_string,UCIboard); 118 | 119 | move_do(UCIboard,move); 120 | 121 | } 122 | } 123 | free(string_copy); 124 | } 125 | 126 | 127 | // send_book_move() 128 | 129 | static void send_book_move(int move){ 130 | char move_string[256]; 131 | my_log("POLYGLOT *BOOK MOVE*\n"); 132 | move_to_can(move,UCIboard,move_string,256); 133 | // bogus info lines 134 | gui_send(GUI,"info depth 1 time 0 nodes 0 nps 0 cpuload 0"); 135 | gui_send(GUI,"bestmove %s",move_string); 136 | } 137 | 138 | // format_uci_option_line() 139 | 140 | static void format_uci_option_line(char * option_line,option_t *opt){ 141 | char option_string[StringSize]; 142 | char type[StringSize]; 143 | int j; 144 | strcpy(option_line,""); 145 | // buffer overflow alert 146 | strcat(option_line,"option name"); 147 | if(opt->mode&PG){ 148 | strcat(option_line," Polyglot"); 149 | } 150 | sprintf(option_string," %s",opt->name); 151 | strcat(option_line,option_string); 152 | normalize_type(type,opt->type); 153 | sprintf(option_string," type %s",type); 154 | strcat(option_line,option_string); 155 | if(!IS_BUTTON(opt->type)){ 156 | sprintf(option_string," default %s",opt->value); 157 | strcat(option_line,option_string); 158 | } 159 | if(IS_SPIN(opt->type)){ 160 | sprintf(option_string," min %s",opt->min); 161 | strcat(option_line,option_string); 162 | } 163 | if(IS_SPIN(opt->type)){ 164 | sprintf(option_string," max %s",opt->max); 165 | strcat(option_line,option_string); 166 | } 167 | for(j=0;jvar_nb;j++){ 168 | sprintf(option_string," var %s",opt->var[j]); 169 | strcat(option_line,option_string); 170 | } 171 | } 172 | 173 | // send_uci_options() 174 | 175 | static void send_uci_options() { 176 | 177 | option_t * opt; 178 | char option_line[StringSize]=""; 179 | gui_send(GUI,"id name %s", Uci->name); 180 | gui_send(GUI,"id author %s", Uci->author); 181 | option_start_iter(Uci->option); 182 | while((opt=option_next(Uci->option))){ 183 | format_uci_option_line(option_line,opt); 184 | gui_send(GUI,"%s",option_line); 185 | } 186 | option_start_iter(Option); 187 | while((opt=option_next(Option))){ 188 | if(opt->mode &UCI){ 189 | format_uci_option_line(option_line,opt); 190 | gui_send(GUI,"%s",option_line); 191 | } 192 | } 193 | gui_send(GUI,"uciok"); 194 | } 195 | 196 | // parse_setoption() 197 | 198 | 199 | 200 | static void parse_setoption(const char string[]) { 201 | char *name; 202 | char *pg_name; 203 | char *value; 204 | char * string_copy; 205 | string_copy=my_strdup(string); 206 | if(match(string_copy,"setoption name * value *")){ 207 | name=Star[0]; 208 | value=Star[1]; 209 | if(match(name, "Polyglot *")){ 210 | pg_name=Star[0]; 211 | polyglot_set_option(pg_name,value); 212 | }else{ 213 | engine_send(Engine,"%s",string); 214 | } 215 | }else{ 216 | engine_send(Engine,"%s",string); 217 | } 218 | free(string_copy); 219 | } 220 | 221 | 222 | // uci2uci_gui_step() 223 | 224 | void uci2uci_gui_step(char string[]) { 225 | int move; 226 | if(FALSE){ 227 | }else if(match(string,"uci")){ 228 | send_uci_options(); 229 | return; 230 | }else if(match(string,"setoption *")){ 231 | parse_setoption(string); 232 | return; 233 | }else if(match(string,"position *")){ 234 | parse_position(string); 235 | Init=FALSE; 236 | }else if(match(string,"go *")){ 237 | if(Init){ 238 | board_from_fen(UCIboard,StartFen); 239 | Init=FALSE; 240 | } 241 | SavedMove=MoveNone; 242 | if(!strstr(string,"infinite") 243 | && UCIboard->move_nb 6 | #include 7 | 8 | #include "util.h" 9 | #include "board.h" 10 | #include "engine.h" 11 | #include "fen.h" 12 | #include "gui.h" 13 | #include "move.h" 14 | #include "move_do.h" 15 | #include "move_legal.h" 16 | #include "parse.h" 17 | #include "option.h" 18 | #include "book.h" 19 | #include "main.h" 20 | #include "uci.h" 21 | 22 | // constants 23 | 24 | static const int StringSize = 4096; 25 | 26 | // variables 27 | 28 | static board_t UCIboard[1]; 29 | static bool Init=true; 30 | static int SavedMove=MoveNone; 31 | 32 | // prototypes 33 | 34 | static void send_uci_options(); 35 | 36 | // parse_position() 37 | 38 | static void parse_position(const char string[]) { 39 | 40 | /* This is borrowed from Toga II. This code is quite hacky and will be 41 | rewritten using the routines in parse.cpp. 42 | */ 43 | 44 | const char * fen; 45 | char * moves; 46 | const char * ptr; 47 | char move_string[256]; 48 | int move; 49 | char * string_copy; 50 | 51 | // init 52 | 53 | string_copy=my_strdup(string); 54 | 55 | fen = strstr(string_copy,"fen "); 56 | moves = strstr(string_copy,"moves "); 57 | 58 | // start position 59 | 60 | if (fen != NULL) { // "fen" present 61 | 62 | if (moves != NULL) { // "moves" present 63 | ASSERT(moves>fen); 64 | moves[-1] = '\0'; // dirty, but so is UCI 65 | } 66 | 67 | board_from_fen(UCIboard,fen+4); // CHANGE ME 68 | 69 | } else { 70 | 71 | // HACK: assumes startpos 72 | 73 | board_from_fen(UCIboard,StartFen); 74 | } 75 | 76 | // moves 77 | 78 | if (moves != NULL) { // "moves" present 79 | 80 | ptr = moves + 6; 81 | 82 | while (*ptr != '\0') { 83 | 84 | while (*ptr == ' ') ptr++; 85 | 86 | move_string[0] = *ptr++; 87 | move_string[1] = *ptr++; 88 | move_string[2] = *ptr++; 89 | move_string[3] = *ptr++; 90 | 91 | if (*ptr == '\0' || *ptr == ' ') { 92 | move_string[4] = '\0'; 93 | } else { // promote 94 | move_string[4] = *ptr++; 95 | move_string[5] = '\0'; 96 | } 97 | move = move_from_can(move_string,UCIboard); 98 | 99 | move_do(UCIboard,move); 100 | 101 | } 102 | } 103 | free(string_copy); 104 | } 105 | 106 | 107 | // send_book_move() 108 | 109 | static void send_book_move(int move){ 110 | char move_string[256]; 111 | my_log("POLYGLOT *BOOK MOVE*\n"); 112 | move_to_can(move,UCIboard,move_string,256); 113 | // bogus info lines 114 | gui_send(GUI,"info depth 1 time 0 nodes 0 nps 0 cpuload 0"); 115 | gui_send(GUI,"bestmove %s",move_string); 116 | } 117 | 118 | // format_uci_option_line() 119 | 120 | static void format_uci_option_line(char * option_line,option_t *opt){ 121 | char option_string[StringSize]; 122 | int j; 123 | strcpy(option_line,""); 124 | strcat(option_line,"option name"); 125 | if(opt->mode&PG){ 126 | strcat(option_line," Polyglot"); 127 | } 128 | sprintf(option_string," %s",opt->name); 129 | strcat(option_line,option_string); 130 | sprintf(option_string," type %s",opt->type); 131 | strcat(option_line,option_string); 132 | if(strcmp(opt->type,"button")){ 133 | sprintf(option_string," default %s",opt->default_); 134 | strcat(option_line,option_string); 135 | } 136 | if(!strcmp(opt->type,"spin")){ 137 | sprintf(option_string," min %s",opt->min); 138 | strcat(option_line,option_string); 139 | } 140 | if(!strcmp(opt->type,"spin")){ 141 | sprintf(option_string," max %s",opt->max); 142 | strcat(option_line,option_string); 143 | } 144 | for(j=0;jvar_nb;j++){ 145 | sprintf(option_string," var %s",opt->var[j]); 146 | strcat(option_line,option_string); 147 | } 148 | } 149 | 150 | // send_uci_options() 151 | 152 | static void send_uci_options() { 153 | int i; 154 | option_t *p=Option; 155 | char option_line[StringSize]=""; 156 | gui_send(GUI,"id name %s", Uci->name); 157 | gui_send(GUI,"id author %s", Uci->author); 158 | for(i=0;ioption_nb;i++){ 159 | format_uci_option_line(option_line,Uci->option+i); 160 | gui_send(GUI,"%s",option_line); 161 | } 162 | while(p->name){ 163 | if(p->mode &UCI){ 164 | format_uci_option_line(option_line,p); 165 | gui_send(GUI,"%s",option_line); 166 | } 167 | p++; 168 | } 169 | gui_send(GUI,"uciok"); 170 | } 171 | 172 | // parse_setoption() 173 | 174 | 175 | 176 | static void parse_setoption(const char string[]) { 177 | char *name; 178 | char *pg_name; 179 | char *value; 180 | char * string_copy; 181 | string_copy=my_strdup(string); 182 | if(match(string_copy,"setoption name * value *")){ 183 | name=Star[0]; 184 | value=Star[1]; 185 | if(match(name, "Polyglot *")){ 186 | pg_name=Star[0]; 187 | polyglot_set_option(pg_name,value); 188 | }else{ 189 | engine_send(Engine,"%s",string); 190 | } 191 | }else{ 192 | engine_send(Engine,"%s",string); 193 | } 194 | free(string_copy); 195 | } 196 | 197 | 198 | // uci2uci_gui_step() 199 | 200 | void uci2uci_gui_step(char string[]) { 201 | int move; 202 | if(false){ 203 | }else if(match(string,"uci")){ 204 | send_uci_options(); 205 | return; 206 | }else if(match(string,"setoption *")){ 207 | parse_setoption(string); 208 | return; 209 | }else if(match(string,"position *")){ 210 | parse_position(string); 211 | Init=false; 212 | }else if(match(string,"go *")){ 213 | if(Init){ 214 | board_from_fen(UCIboard,StartFen); 215 | Init=false; 216 | } 217 | SavedMove=MoveNone; 218 | if(!strstr(string,"infinite")){ 219 | move=book_move(UCIboard,option_get_bool("BookRandom")); 220 | if (move != MoveNone && move_is_legal(move,UCIboard)) { 221 | if(strstr(string,"ponder")){ 222 | SavedMove=move; 223 | return; 224 | }else{ 225 | send_book_move(move); 226 | return; 227 | } 228 | } 229 | } 230 | }else if(match(string,"ponderhit") || match(string,"stop")){ 231 | if(SavedMove!=MoveNone){ 232 | send_book_move(SavedMove); 233 | SavedMove=MoveNone; 234 | return; 235 | } 236 | }else if(match(string,"quit")){ 237 | my_log("POLYGLOT *** \"quit\" from GUI ***\n"); 238 | quit(); 239 | } 240 | engine_send(Engine,"%s",string); 241 | } 242 | 243 | void uci2uci_engine_step(char string[]) { 244 | gui_send(GUI,string); 245 | } 246 | 247 | // end of uci2uci.cpp 248 | -------------------------------------------------------------------------------- /uci2uci.h: -------------------------------------------------------------------------------- 1 | 2 | // uci2uci.h 3 | 4 | #ifndef UCI2UCI_H 5 | #define UCI2UCI_H 6 | 7 | // functions 8 | 9 | extern void uci2uci_gui_step(char string[]); 10 | extern void uci2uci_engine_step(char string[]); 11 | 12 | #endif // !defined UCI2UCI_H 13 | 14 | // end of uci2uci.h 15 | -------------------------------------------------------------------------------- /util.h: -------------------------------------------------------------------------------- 1 | 2 | // util.h 3 | 4 | #ifndef UTIL_H 5 | #define UTIL_H 6 | 7 | // includes 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // defines 17 | 18 | #ifndef EXIT_SUCCES 19 | #define EXIT_SUCCES 0 20 | #endif 21 | 22 | #ifndef STDIN_FILENO 23 | #define STDIN_FILENO 0 24 | #endif 25 | 26 | #ifndef STDOUT_FILENO 27 | #define STDOUT_FILENO 1 28 | #endif 29 | 30 | 31 | #undef FALSE 32 | #define FALSE 0 33 | 34 | #undef TRUE 35 | #define TRUE 1 36 | 37 | #ifdef DEBUG 38 | # undef DEBUG 39 | # define DEBUG TRUE 40 | #else 41 | # define DEBUG FALSE 42 | #endif 43 | 44 | #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) 45 | # define S64_FORMAT "%I64d" 46 | # define U64_FORMAT "%016I64X" 47 | #else 48 | # define S64_FORMAT "%lld" 49 | # define U64_FORMAT "%016llX" 50 | #endif 51 | 52 | // macros 53 | 54 | #ifdef _MSC_VER 55 | # define S64(u) (u##i64) 56 | # define U64(u) (u##ui64) 57 | #else 58 | # define S64(u) (u##LL) 59 | # define U64(u) (u##ULL) 60 | #endif 61 | 62 | #undef ASSERT 63 | #ifdef assert 64 | # define ASSERT(a) assert(a) 65 | #else 66 | #ifdef DEBUG 67 | # define ASSERT(a) { if (!(a)) my_fatal("file \"%s\", line %d, assertion \"" #a "\" failed\n",__FILE__,__LINE__); } 68 | #else 69 | # define ASSERT(a) 70 | #endif 71 | #endif 72 | 73 | #ifdef _WIN32 74 | #define snprintf _snprintf 75 | #endif 76 | 77 | #define FormatBufferSize 4096 78 | #define StringSize 4096 79 | 80 | #ifdef _MSC_VER 81 | #define vsnprintf _vsnprintf 82 | #endif 83 | 84 | #define CONSTRUCT_ARG_STRING(format,buf) \ 85 | { \ 86 | va_list arg_list; \ 87 | int written; \ 88 | va_start(arg_list,format); \ 89 | written=vsnprintf(buf, \ 90 | sizeof(buf), \ 91 | format, \ 92 | arg_list); \ 93 | va_end(arg_list); \ 94 | buf[sizeof(buf)-1]='\0'; \ 95 | if(written>=sizeof(buf) || written<0){ \ 96 | my_fatal("write_buffer overflow: file \"%s\", line %d\n", \ 97 | __FILE__,__LINE__); \ 98 | } \ 99 | } \ 100 | 101 | #define TO_BOOL(string) ((my_string_case_equal(string,"false") || \ 102 | my_string_equal(string,"0"))?FALSE:TRUE) 103 | 104 | #define IS_BOOL(string) (my_string_case_equal(string,"false")|| \ 105 | my_string_case_equal(string,"true") || \ 106 | my_string_case_equal(string,"1") || \ 107 | my_string_case_equal(string,"0")) 108 | // types 109 | 110 | typedef signed char sint8; 111 | typedef unsigned char uint8; 112 | 113 | typedef signed short sint16; 114 | typedef unsigned short uint16; 115 | 116 | typedef signed int sint32; 117 | typedef unsigned int uint32; 118 | 119 | typedef int bool; 120 | 121 | #ifdef _MSC_VER 122 | typedef signed __int64 sint64; 123 | typedef unsigned __int64 uint64; 124 | #else 125 | typedef signed long long int sint64; 126 | typedef unsigned long long int uint64; 127 | #endif 128 | 129 | typedef struct { 130 | double start_real; 131 | double elapsed_real; 132 | bool running; 133 | } my_timer_t; 134 | 135 | 136 | // functions 137 | 138 | extern void util_init (); 139 | 140 | extern void my_random_init (); 141 | extern int my_random_int (int n); 142 | extern double my_random_double (); 143 | 144 | extern sint64 my_atoll (const char string[]); 145 | 146 | extern int my_round (double x); 147 | 148 | extern void * my_malloc (size_t size); 149 | extern void * my_realloc (void * address, size_t size); 150 | extern void my_free (void * address); 151 | 152 | extern void my_log_open (const char file_name[]); 153 | extern void my_log_close (); 154 | 155 | extern void my_log (const char format[], ...); 156 | extern void my_fatal (const char format[], ...); 157 | 158 | extern bool my_file_read_line (FILE * file, char string[], int size); 159 | extern void my_path_join (char *join_path, const char *path, const char *file); 160 | 161 | extern int my_mkdir (const char *path); 162 | 163 | extern bool my_string_empty (const char string[]); 164 | extern bool my_string_whitespace (const char string[]); 165 | extern bool my_string_equal (const char string_1[], const char string_2[]); 166 | extern bool my_string_case_equal (const char string_1[], const char string_2[]); 167 | extern const char* my_string_case_contains(const char haystack[], 168 | const char needle[]); 169 | 170 | 171 | extern bool my_string_to_lower (char dst[], const char src[]); 172 | 173 | extern char * my_strdup (const char string[]); 174 | 175 | extern void my_string_clear (const char * * variable); 176 | extern void my_string_set (const char * * variable, const char string[]); 177 | 178 | extern double now_real (); 179 | 180 | extern void my_timer_reset (my_timer_t * timer); 181 | extern void my_timer_start (my_timer_t * timer); 182 | extern void my_timer_stop (my_timer_t * timer); 183 | 184 | extern double my_timer_elapsed_real (const my_timer_t * timer); 185 | 186 | extern char * my_error(); 187 | 188 | extern void my_dequote (char *out, 189 | const char *in, 190 | const char *special); 191 | extern void my_quote (char *out, 192 | const char *in, 193 | const char *special); 194 | 195 | extern void my_sleep (int msec); 196 | 197 | #endif // !defined UTIL_H 198 | 199 | // end of util.h 200 | -------------------------------------------------------------------------------- /xboard2uci.h: -------------------------------------------------------------------------------- 1 | 2 | // xboard2uci.h 3 | 4 | #ifndef XBOARD2UCI_H 5 | #define XBOARD2UCI_H 6 | 7 | // includes 8 | 9 | #include "util.h" 10 | 11 | // types 12 | 13 | // functions 14 | 15 | extern void xboard2uci_init (); 16 | extern void xboard2uci_gui_step (char string[]); 17 | extern void xboard2uci_engine_step (char string[]); 18 | extern void xboard2uci_send_options (); 19 | 20 | 21 | #endif // !defined XBOARD2UCI_H 22 | 23 | // end of xboard2uci.h 24 | 25 | --------------------------------------------------------------------------------