Fixed event processing in the gui
[rattatechess.git] / board.h
blob8d6e981832a0e60ad5315f5291842398399d0212
1 /***************************************************************************
2 board.h - Board related definitions
3 -------------------
4 begin : Fri Jan 11 2002
5 copyright : (C) 2002-2007 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 #ifndef __BOARD_H__
19 #define __BOARD_H__
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <inttypes.h>
24 #include "utils.h"
25 #include "hash.h"
26 #include "move.h"
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
44 class PACKED Board
46 public:/* a struct to keep the info of which pawns are in a row */
47 class LinePawns
49 public:
50 uint8_t count;
51 Array<uint8_t, 7>::Type pos;//uint8_t pos[7];
54 /* structure to track material */
55 class MatTrack
57 public:
58 uint8_t count;
59 Array<uint8_t, 11>::Type pos; //uint8_t pos[11];
62 /* structure to track attacks */
63 class AttackList
65 public:
66 uint8_t count;
67 uint8_t curr;
68 Array<uint8_t, 10>::Type piece;//uint8_t piece[10];
71 // utility data
72 static uint8_t b_center[];
73 static uint8_t b_start[];
74 static uint8_t b_castle_adj[];
75 static uint8_t up_dir[];
76 static char piecename[];
78 DEF2_A88(data, mat_idx);
79 DEF2_A88(pins, oth_pins);
80 DEF2_A88(castle_adj, boh_nothing);
82 #if TRACK_ATTACKS
83 /* attack tracking relative data */
84 DEF2_A88(b_attacks, w_attacks);
85 static uint8_t attack_dirs[];
86 #endif //TRACK_ATTACKS
88 /* flags of the current position */
89 uint8_t castle_passing_mask;
90 uint8_t fifty;
92 /* color on move (and the other one) */
93 uint8_t color_to_move;
94 uint8_t other_color;
96 /* mark black/white king position */
97 Array<uint8_t, 2>::Type king_pos;//uint8_t king_pos[2];
98 Array<uint8_t, 2>::Type first_rank;//uint8_t first_rank[2];
99 Array<uint8_t, 2>::Type second_rank;//uint8_t second_rank[2];
100 Array<uint8_t, 2>::Type passant_rank;//uint8_t passant_rank[2];
101 Array<uint8_t, 2>::Type seventh_rank;//uint8_t seventh_rank[2];
103 /* 0xff if not calculated yet, 0/1/2 if no check, check or double check */
104 uint8_t under_check;
106 /* pawn tracking relative data */
107 Array<Array<LinePawns, 8>::Type, 2>::Type line_pawns;
109 /* material tracking relative data */
110 Array<MatTrack, 12>::Type mat_tracking;
112 /* hash relative data and functions */
113 HashKey hash;
114 static HashKey hash_keys[14*128];
115 static HashKey hash_key_toggle;
117 /* data for move generation */
118 Move* mvg_curr;
119 static KnightMove* knightmoves;
120 static uint8_t kingmoves[];
121 static uint8_t bishmoves[];
122 static uint8_t rookmoves[];
124 /* num of moves from the start, for fen loading/saving */
125 int num_moves;
127 /* simple values, PAWN=1, KNIGHT=3, etc */
128 static int16_t simple_values[];
132 /* move generators prototypes */
133 void find_rook_moves(uint8_t where);
134 void find_bishop_moves(uint8_t where);
135 void find_queen_moves(uint8_t where);
136 void find_knight_moves(uint8_t where);
137 void find_pawn_moves(uint8_t where);
138 void find_king_moves(uint8_t where);
139 static void init_knight_moves();
141 /* pawn tracking relative functions */
142 void add_pawn(uint8_t col, uint8_t where);
143 void del_pawn(uint8_t col, uint8_t where);
144 void recalc_line_pawns();
145 void check_line_pawns();
147 /* material tracking relative functions */
148 void mat_add(uint8_t piece, uint8_t pos);
149 void mat_remove(uint8_t piece, uint8_t pos);
150 void mat_move(uint8_t piece, uint8_t from, uint8_t to);
151 void recalc_mat_tracking();
152 void check_mat_tracking();
154 #if TRACK_ATTACKS
155 /* attack tracking relative functions */
156 void add_attacks(uint8_t piece, uint8_t where);
157 void del_attacks(uint8_t piece, uint8_t where);
158 void replace_attacks(uint8_t piece1, uint8_t piece2, uint8_t where);
159 void recalc_attacks();
160 void print_attacks();
161 void check_attacks();
162 #endif //TRACK_ATTACKS
164 /* hash relative functions */
165 static void init_hash_keys(const char *file);
167 Board();
169 void init_globals();
170 void set_as_default();
171 bool under_attack(uint8_t pos, uint8_t attacker);
172 void find_check_and_pins();
173 void find_other_pins();
174 bool move_is_check(const Move& m);
175 int list_attackers(uint8_t pos, uint8_t attacker_col,
176 A88 pins, AttackList* a);
177 int propagate_see(uint8_t victim, int numa, AttackList* a,
178 int numd, AttackList* d);
179 int move_see_val(const Move& m);
180 int find_moves(Move* m);
181 int find_captures(Move* m);
183 int16_t evaluate(uint8_t eng_col, int16_t alpha = -INF, int16_t beta = INF);
184 int16_t dummy_evaluate();
185 bool insufficient_material();
187 uint16_t compress_move(const Move& m) const;
188 Move uncompress_move(uint16_t m) const;
189 void do_move(const Move& m, SaveBuf& s);
190 void undo_move(const Move& m, const SaveBuf& s);
191 void do_null_move(SaveBuf& s);
192 void undo_null_move(const SaveBuf& s);
194 HashKey move_hash(const Move& m) const;
195 void recalc_hash();
197 Move move_from_string(const char* str);
198 static char* move_to_coord(char* str, const Move& mv);
199 char* move_to_alg(char* str, const Move& mv);
201 void print_board(); //prints colored board to stdout
202 void read_board(char* brd, char* color, char* castle,
203 char* passing, int moves_to_draw, int num_moves);
204 void read_board(char *str); //reads from f a Forsythe-Edwards notation board
205 char* to_fen(char *str); //writes to f a Forsythe-Edwards notation board
208 #endif //__BOARD_H__