Various tunings.
[rattatechess.git] / board.h
blob3ae51840ddc603224b7a5dcaa5afdfb2cfa926d4
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];
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[];
78 // utility data
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);
98 #if TRACK_ATTACKS
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;
112 uint8_t fifty;
114 /* stack to save the flags */
115 uint8_t *flags_stack;
116 int flags_stack_ptr;
118 /* color on move (and the other one) */
119 uint8_t color_to_move;
120 uint8_t other_color;
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 */
130 uint8_t under_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 */
150 HashKey hash;
151 HashKey *old_hashes;
152 int num_old_hashes;
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 */
159 Move* mvg_curr;
161 /* num of moves from the start, for fen loading/saving */
162 int num_moves;
164 /* simple values, PAWN=1, KNIGHT=3, etc */
165 static int16_t simple_values[];
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 uint16_t compress_move(const Move& m) const;
184 Move uncompress_move(uint16_t m) const;
185 void do_move(const Move& m);
186 void undo_move(const Move& m);
187 void do_null_move();
188 void undo_null_move();
189 HashKey move_hash(const Move& m) const;
190 void recalc_hash();
191 int16_t evaluate(uint8_t eng_col, int16_t alpha = -INF, int16_t beta = INF);
192 int16_t dummy_evaluate();
194 void print_board(); //prints colored board to stdout
195 void read_board(char* brd, char* color, char* castle,
196 char* passing, int moves_to_draw, int num_moves);
197 void read_board(char *str); //reads from f a Forsythe-Edwards notation board
198 void write_board(char *str); //writes to f a Forsythe-Edwards notation board
201 #endif //__BOARD_H__