per test
[rattatechess.git] / board_io.cpp
blob20172a2490ba45cde0641eb06717662b9ce7034b
1 /***************************************************************************
2 board_io.cpp - description
3 -------------------
4 begin : Wed Mar 13 2002
5 copyright : (C) 2002-2005 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include "board.h"
19 #include "engine.h"
20 #include <string.h>
22 /* print a fancy colored board */
23 void Board::print_board()
25 static const char out_wpiece[] = "\e[1;33m";
26 static const char out_bpiece[] = "\e[1;32m";
27 static const char out_wsquare[]= "\e[1;47m";
28 static const char out_bsquare[]= "\e[1;43m";
30 find_check_and_pins();
31 find_other_pins();
33 for(int j=7;j>=0;j--)
35 for(int i=0;i<8;i++)
37 unsigned char piece = data[POS_XY(i, j)];
39 printf("%s%s %c ",
40 piece == STONE ? "\e[1;40m" :
41 oth_pins[POS_XY(i, j)] ? "\e[1;44m" :
42 (CAN_MOVE(pins[POS_XY(i, j)]) ? ((j+i)&1 ? out_wsquare : out_bsquare):
43 (IS_OF_COLOR(piece,color_to_move)?"\e[1;45m":"\e[1;41m")),
44 COLOR_OF(piece)==WHITE ? out_wpiece : out_bpiece,
45 piece==STONE?' ':piecename[PIECE_OF(piece)]);
47 printf("\e[0m\n");
51 printf("#on move: %s ", color_to_move==WHITE?"white":"black");
52 printf("castling: %s%s%s%s ",
53 castle_passing_mask&WCANCASTLEKS ? "K" : " ",
54 castle_passing_mask&WCANCASTLEQS ? "Q" : " ",
55 castle_passing_mask&BCANCASTLEKS ? "k" : " ",
56 castle_passing_mask&BCANCASTLEQS ? "q" : " " );
57 printf("en-passant: %d\n", castle_passing_mask&NOT_PASSING?
58 -1: castle_passing_mask&0x07);
59 printf("#static value....: %d\n", (int)evaluate(WHITE, -INF, INF) );
60 printf("#hash key........: %s\n", hash.to_string(TmpStr().data()) );
61 printf("#fen.............: %s\n", to_fen(BigStr().data()) );
63 //print_attacks();
64 #if 0
65 printf("pawn positions:\n");
66 int max = 0;
67 for(int i=0;i<2;i++)
68 for(int w=0;w<8;w++)
69 max = MAX(max, line_pawns[i][w].count);
71 for(int q=max-1;q>=0;q--)
73 for(int i=0;i<2;i++)
75 for(int w=0;w<8;w++)
77 if(q<line_pawns[i][w].count)
78 printf("%d", line_pawns[i][w].pos[q]);
79 else
80 printf(" ");
82 printf(" ");
84 printf("\n");
86 printf("-------- --------\n");
88 static char name[] = "brqnpkRBQNPK";
89 printf("piece positions:\n");
90 for(int i=0;i<12;i++)
92 printf("%c:", name[i]);
93 for(int j=0;j<mat_tracking[i].count;j++)
94 printf(" %c%c",
95 'a' + X(mat_tracking[i].pos[j]),
96 '1' + Y(mat_tracking[i].pos[j]));
97 printf("\n");
99 #endif
102 char* Board::to_fen(char* str)
104 char *retv = str;
105 int space_counter = 0;
107 for(int y=7; y>=0; y--)
109 for(int x=0; x<8; x++)
111 uint8_t piece = data[POS_XY(x,y)];
113 if(piece == 0)
115 space_counter++;
116 continue;
119 if(space_counter > 0)
121 str += sprintf(str, "%d", space_counter);
122 space_counter = 0;
125 char p = (piece==STONE) ? '#' : Board::piecename[PIECE_OF(piece)];
126 if(COLOR_OF(piece) == BLACK)
127 p ^= 0x20;
128 str += sprintf(str, "%c", p);
131 if(space_counter > 0)
133 str += sprintf(str, "%d", space_counter);
134 space_counter = 0;
137 if(y != 0)
138 str += sprintf(str,"/");
141 str += sprintf(str, " %c ", (color_to_move==WHITE) ? 'w':'b');
143 if(castle_passing_mask & WCANCASTLEKS)
144 str += sprintf(str, "K");
145 if(castle_passing_mask & WCANCASTLEQS)
146 str += sprintf(str, "Q");
147 if(castle_passing_mask & BCANCASTLEKS)
148 str += sprintf(str, "k");
149 if(castle_passing_mask & BCANCASTLEQS)
150 str += sprintf(str, "q");
151 if(!(castle_passing_mask & 0xf0))
152 str += sprintf(str,"-");
154 if(!(castle_passing_mask&NOT_PASSING))
155 str += sprintf(str, " %c%c",
156 (castle_passing_mask&0x07)+'a',
157 (color_to_move==WHITE)?'6':'3' );
158 else
159 str += sprintf(str," -");
161 str += sprintf(str," %d %d",fifty,num_moves+1);
163 return retv;
167 void Board::read_board(char* brd, char* color, char* castle,
168 char* passing, int moves_to_draw, int num_mv)
170 char* pars = brd;
171 int i = 0;
172 int currl = 0x70;
174 fifty = moves_to_draw;
176 zero_a88(data);
178 while(pars[0]!='\0')
180 char cc=*(pars++);
181 if(cc>='1' && cc<='8')
183 currl += cc-'0';
184 continue;
186 if(cc=='/')
188 i++;
189 currl = 0x70 - i*0x10;
190 continue;
192 if(cc=='#')
194 data[currl++] = STONE;
195 continue;
197 for(int i=1;i<7;i++)
199 if(piecename[i]==cc)
201 if(i==KING)
202 king_pos[1] = currl;
203 data[currl++] = i|WHITE;
204 continue;
206 else if((piecename[i]^0x20)==cc)
208 if(i==KING)
209 king_pos[0] = currl;
210 data[currl++] = i|BLACK;
211 continue;
215 color_to_move = (*color=='w')?WHITE:BLACK;
216 other_color = OTHER_COLOR(color_to_move);
218 pars = castle;
219 castle_passing_mask = 0;
220 while(pars[0]!='\0')
222 switch(*pars)
224 case 'K':
225 castle_passing_mask|=WCANCASTLEKS;
226 break;
227 case 'Q':
228 castle_passing_mask|=WCANCASTLEQS;
229 break;
230 case 'k':
231 castle_passing_mask|=BCANCASTLEKS;
232 break;
233 case 'q':
234 castle_passing_mask|=BCANCASTLEQS;
235 break;
237 pars++;
239 if(passing[0]!='-')
240 castle_passing_mask |= (passing[0]-'a');
241 else
242 castle_passing_mask |= NOT_PASSING;
244 num_moves = MAX(num_mv-1, 0);
246 under_check=0xff;
248 recalc_line_pawns();
249 recalc_mat_tracking();
250 recalc_hash();
251 #if TRACK_ATTACKS
252 recalc_attacks();
253 #endif //TRACK_ATTACKS
256 void Board::read_board(char* str)
258 char brd[81];
259 char color[2],castle[5],passing[3];
260 int moves_to_draw;
261 int numm;
263 sscanf(str,"%s%s%s%s%d%d",
264 brd,color,castle,passing,&moves_to_draw,&numm);
266 read_board(brd,color,castle,passing,moves_to_draw,numm);