1 /***************************************************************************
2 board.h - Board related definitions
4 begin : Fri Jan 11 2002
5 copyright : (C) 2002-2007 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 ***************************************************************************/
27 #include "board_defs.h"
29 /*******************************************************************************
30 the chessboard is a 16x8 arrays, and is accessed with 1-byte indices
31 whose binary form look like: 0yyy0xxx
32 where xxx and yyy are 3bits (0 ... 7) that store the x and y coords.
33 The 4th and 8th bits are 0 if the index is valid, so an invalid (out
34 of board) index can be detected testing with 10001000 (0x88)
36 Chess pieces. Keep these numbers because in a few place it is useful to be
37 able to assume that QUEEN=ROOK|BISHOP, ie if you want to test if a piece
38 moves diagonally you have just to test (piece&(~ROOK) == BISHOP)
39 *******************************************************************************/
42 #define TRACK_ATTACKS 0
46 public:/* a struct to keep the info of which pawns are in a row */
51 Array
<uint8_t, 7>::Type pos
;//uint8_t pos[7];
54 /* structure to track material */
59 Array
<uint8_t, 11>::Type pos
; //uint8_t pos[11];
62 /* structure to track attacks */
68 Array
<uint8_t, 10>::Type piece
;//uint8_t piece[10];
72 // data and function pointer for move generation
73 static KnightMove
* knightmoves
;
74 static uint8_t kingmoves
[];
75 static uint8_t bishmoves
[];
76 static uint8_t rookmoves
[];
79 static uint8_t b_center
[];
80 static uint8_t b_start
[];
81 static uint8_t b_castle_adj
[];
82 static uint8_t up_dir
[];
83 static char piecename
[];
85 // move generators prototypes
86 void find_rook_moves(uint8_t where
);
87 void find_bishop_moves(uint8_t where
);
88 void find_queen_moves(uint8_t where
);
89 void find_knight_moves(uint8_t where
);
90 void find_pawn_moves(uint8_t where
);
91 void find_king_moves(uint8_t where
);
92 static void init_knight_moves();
94 DEF2_A88(data
, mat_idx
);
95 DEF2_A88(pins
, oth_pins
);
96 DEF2_A88(castle_adj
, boh_nothing
);
99 DEF2_A88(b_attacks
, w_attacks
);
100 static uint8_t attack_dirs
[];
102 void add_attacks(uint8_t piece
, uint8_t where
);
103 void del_attacks(uint8_t piece
, uint8_t where
);
104 void replace_attacks(uint8_t piece1
, uint8_t piece2
, uint8_t where
);
105 void recalc_attacks();
106 void print_attacks();
107 void check_attacks();
108 #endif //TRACK_ATTACKS
110 /* flags of the current position */
111 uint8_t castle_passing_mask
;
114 /* stack to save the flags */
115 uint8_t *flags_stack
;
118 /* color on move (and the other one) */
119 uint8_t color_to_move
;
122 /* mark black/white king position */
123 Array
<uint8_t, 2>::Type king_pos
;//uint8_t king_pos[2];
124 Array
<uint8_t, 2>::Type first_rank
;//uint8_t first_rank[2];
125 Array
<uint8_t, 2>::Type second_rank
;//uint8_t second_rank[2];
126 Array
<uint8_t, 2>::Type passant_rank
;//uint8_t passant_rank[2];
127 Array
<uint8_t, 2>::Type seventh_rank
;//uint8_t seventh_rank[2];
129 /* 0xff if not calculated yet, 0/1/2 if no check, check or double check */
132 /* pawn tracking relative data and functions */
133 Array
<Array
<LinePawns
, 8>::Type
, 2>::Type line_pawns
;
135 void add_pawn(uint8_t col
, uint8_t where
);
136 void del_pawn(uint8_t col
, uint8_t where
);
137 void recalc_line_pawns();
138 void check_line_pawns();
140 /* material tracking relative data and functions */
141 Array
<MatTrack
, 12>::Type mat_tracking
;//MatTrack mat_tracking[12];
143 void mat_add(uint8_t piece
, uint8_t pos
);
144 void mat_remove(uint8_t piece
, uint8_t pos
);
145 void mat_move(uint8_t piece
, uint8_t from
, uint8_t to
);
146 void recalc_mat_tracking();
147 void check_mat_tracking();
149 /* hash relative data and functions */
154 static HashKey hash_keys
[14*128];
155 static HashKey hash_key_toggle
;
156 static void init_hash_keys(const char *file
);
158 /* data for move generation */
161 /* num of moves from the start, for fen loading/saving */
168 void set_as_default();
169 bool under_attack(uint8_t pos
, uint8_t attacker
);
170 void find_check_and_pins();
171 void find_other_pins();
172 bool move_is_check(const Move
& m
);
173 int list_attackers(uint8_t pos
, uint8_t attacker_col
,
174 A88 pins
, AttackList
* a
);
175 int propagate_see(uint8_t victim
, int numa
, AttackList
* a
,
176 int numd
, AttackList
* d
);
177 int move_see_val(const Move
& m
);
178 int find_moves(Move
* m
);
179 int find_captures(Move
* m
);
181 uint16_t compress_move(const Move
& m
) const;
182 Move
uncompress_move(uint16_t m
) const;
183 void do_move(const Move
& m
);
184 void undo_move(const Move
& m
);
186 void undo_null_move();
187 HashKey
move_hash(const Move
& m
) const;
189 int16_t evaluate(uint8_t eng_col
, int16_t alpha
= -INF
, int16_t beta
= INF
);
190 int16_t dummy_evaluate();
192 void print_board(); //prints colored board to stdout
193 void read_board(char* brd
, char* color
, char* castle
,
194 char* passing
, int moves_to_draw
, int num_moves
);
195 void read_board(char *str
); //reads from f a Forsythe-Edwards notation board
196 void write_board(char *str
); //writes to f a Forsythe-Edwards notation board