Various tunings.
[rattatechess.git] / board_io.cpp
blobaf518288ece1c9ef50ed43651f6a4eae15ef05ab
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 #if 1
60 printf("#static value....: %d\n",(int)evaluate(WHITE,-INF,INF));
61 printf("#hash key........: %08x%08x:%08x\n",
62 hash.check_hi, hash.check_lo, hash.index);
63 char buf[256];
64 write_board(buf);
65 printf("#fen.............: %s\n", buf);
66 #endif
68 //print_attacks();
69 #if 0
70 printf("pawn positions:\n");
71 int max = 0;
72 for(int i=0;i<2;i++)
73 for(int w=0;w<8;w++)
74 max = MAX(max, line_pawns[i][w].count);
76 for(int q=max-1;q>=0;q--)
78 for(int i=0;i<2;i++)
80 for(int w=0;w<8;w++)
82 if(q<line_pawns[i][w].count)
83 printf("%d", line_pawns[i][w].pos[q]);
84 else
85 printf(" ");
87 printf(" ");
89 printf("\n");
91 printf("-------- --------\n");
93 static char name[] = "brqnpkRBQNPK";
94 printf("piece positions:\n");
95 for(int i=0;i<12;i++)
97 printf("%c:", name[i]);
98 for(int j=0;j<mat_tracking[i].count;j++)
99 printf(" %c%c",
100 'a' + X(mat_tracking[i].pos[j]),
101 '1' + Y(mat_tracking[i].pos[j]));
102 printf("\n");
104 #endif
107 void Board::write_board(char* str)
109 int cn=0;
110 for(int i=7;i>=0;i--)
112 for(int j=0;j<8;j++)
114 unsigned char pc=data[POS_XY(j,i)];
115 if(pc==0)
116 cn++;
117 else
119 if(cn>0)
121 str+=sprintf(str,"%d",cn);
122 cn=0;
124 char p= pc==STONE ? '#' : Board::piecename[PIECE_OF(pc)];
125 if(COLOR_OF(pc)==BLACK)
126 p^=0x20;
127 str+=sprintf(str,"%c",p);
130 if(cn>0)
132 str+=sprintf(str,"%d",cn);
133 cn=0;
135 if(i!=0)
136 str+=sprintf(str,"/");
139 str+=sprintf(str," %c ",color_to_move==WHITE ? 'w':'b');
141 if(castle_passing_mask&WCANCASTLEKS)
142 str+=sprintf(str,"K");
143 if(castle_passing_mask&WCANCASTLEQS)
144 str+=sprintf(str,"Q");
145 if(castle_passing_mask&BCANCASTLEKS)
146 str+=sprintf(str,"k");
147 if(castle_passing_mask&BCANCASTLEQS)
148 str+=sprintf(str,"q");
149 if(!(castle_passing_mask&0xf0))
150 str+=sprintf(str,"-");
152 if(!(castle_passing_mask&NOT_PASSING))
153 str+=sprintf(str," %c%c",(castle_passing_mask&0x07)+'a',
154 color_to_move==WHITE?'6':'3');
155 else
156 str+=sprintf(str," -");
157 str+=sprintf(str," %d %d",fifty,num_moves+1);
161 void Board::read_board(char* brd, char* color, char* castle,
162 char* passing, int moves_to_draw, int num_mv)
164 char* pars = brd;
165 int i = 0;
166 int currl = 0x70;
168 fifty = moves_to_draw;
170 zero_a88(data);
172 while(pars[0]!='\0')
174 char cc=*(pars++);
175 if(cc>='1' && cc<='8')
177 currl += cc-'0';
178 continue;
180 if(cc=='/')
182 i++;
183 currl = 0x70 - i*0x10;
184 continue;
186 if(cc=='#')
188 data[currl++] = STONE;
189 continue;
191 for(int i=1;i<7;i++)
193 if(piecename[i]==cc)
195 if(i==KING)
196 king_pos[1] = currl;
197 data[currl++] = i|WHITE;
198 continue;
200 else if((piecename[i]^0x20)==cc)
202 if(i==KING)
203 king_pos[0] = currl;
204 data[currl++] = i|BLACK;
205 continue;
209 color_to_move = (*color=='w')?WHITE:BLACK;
210 other_color = OTHER_COLOR(color_to_move);
212 pars = castle;
213 castle_passing_mask = 0;
214 while(pars[0]!='\0')
216 switch(*pars)
218 case 'K':
219 castle_passing_mask|=WCANCASTLEKS;
220 break;
221 case 'Q':
222 castle_passing_mask|=WCANCASTLEQS;
223 break;
224 case 'k':
225 castle_passing_mask|=BCANCASTLEKS;
226 break;
227 case 'q':
228 castle_passing_mask|=BCANCASTLEQS;
229 break;
231 pars++;
233 if(passing[0]!='-')
234 castle_passing_mask |= (passing[0]-'a');
235 else
236 castle_passing_mask |= NOT_PASSING;
238 num_moves = MAX(num_mv-1, 0);
240 under_check=0xff;
241 flags_stack_ptr = 0;
242 num_old_hashes = 0;
244 recalc_line_pawns();
245 recalc_mat_tracking();
246 recalc_hash();
247 #if TRACK_ATTACKS
248 recalc_attacks();
249 #endif //TRACK_ATTACKS
252 void Board::read_board(char* str)
254 char brd[81];
255 char color[2],castle[5],passing[3];
256 int moves_to_draw;
257 int numm;
259 sscanf(str,"%s%s%s%s%d%d",
260 brd,color,castle,passing,&moves_to_draw,&numm);
262 read_board(brd,color,castle,passing,moves_to_draw,numm);