1 /***************************************************************************
2 board_io.cpp - description
4 begin : Wed Mar 13 2002
5 copyright : (C) 2002-2005 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
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();
37 unsigned char piece
= data
[POS_XY(i
, j
)];
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
)]);
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()) );
65 printf("pawn positions:\n");
69 max
= MAX(max
, line_pawns
[i
][w
].count
);
71 for(int q
=max
-1;q
>=0;q
--)
77 if(q
<line_pawns
[i
][w
].count
)
78 printf("%d", line_pawns
[i
][w
].pos
[q
]);
86 printf("-------- --------\n");
88 static char name
[] = "brqnpkRBQNPK";
89 printf("piece positions:\n");
92 printf("%c:", name
[i
]);
93 for(int j
=0;j
<mat_tracking
[i
].count
;j
++)
95 'a' + X(mat_tracking
[i
].pos
[j
]),
96 '1' + Y(mat_tracking
[i
].pos
[j
]));
102 char* Board::to_fen(char* 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
)];
119 if(space_counter
> 0)
121 str
+= sprintf(str
, "%d", space_counter
);
125 char p
= (piece
==STONE
) ? '#' : Board::piecename
[PIECE_OF(piece
)];
126 if(COLOR_OF(piece
) == BLACK
)
128 str
+= sprintf(str
, "%c", p
);
131 if(space_counter
> 0)
133 str
+= sprintf(str
, "%d", space_counter
);
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' );
159 str
+= sprintf(str
," -");
161 str
+= sprintf(str
," %d %d",fifty
,num_moves
+1);
167 void Board::read_board(char* brd
, char* color
, char* castle
,
168 char* passing
, int moves_to_draw
, int num_mv
)
174 fifty
= moves_to_draw
;
181 if(cc
>='1' && cc
<='8')
189 currl
= 0x70 - i
*0x10;
194 data
[currl
++] = STONE
;
203 data
[currl
++] = i
|WHITE
;
206 else if((piecename
[i
]^0x20)==cc
)
210 data
[currl
++] = i
|BLACK
;
215 color_to_move
= (*color
=='w')?WHITE
:BLACK
;
216 other_color
= OTHER_COLOR(color_to_move
);
219 castle_passing_mask
= 0;
225 castle_passing_mask
|=WCANCASTLEKS
;
228 castle_passing_mask
|=WCANCASTLEQS
;
231 castle_passing_mask
|=BCANCASTLEKS
;
234 castle_passing_mask
|=BCANCASTLEQS
;
240 castle_passing_mask
|= (passing
[0]-'a');
242 castle_passing_mask
|= NOT_PASSING
;
244 num_moves
= MAX(num_mv
-1, 0);
249 recalc_mat_tracking();
253 #endif //TRACK_ATTACKS
256 void Board::read_board(char* str
)
259 char color
[2],castle
[5],passing
[3];
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
);