Removed killer, just use history. small cleanup.
[rattatechess.git] / board.cpp
blob37689abc9a3acf17b7bb39159e2b49b1452831cc
1 /***************************************************************************
2 chess.cpp - description
3 -------------------
4 begin : Fri Jan 11 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 "engine.h"
19 #include <stdlib.h>
20 #include <string.h>
22 Board::Board()
24 init_knight_moves();
25 flags_stack = new uint8_t[4096];
26 old_hashes = new HashKey[4096];
28 set_as_default();
31 //-------------------------------------------------------------------------------------
32 //-------------------------------------------------------------------------------------
34 void Board::set_as_default()
36 memcpy(&data[0],b_start,128);
37 memcpy(&castle_adj[0],b_castle_adj,128);
39 num_moves=0;
41 flags_stack_ptr = 0;
42 num_old_hashes = 0;
44 castle_passing_mask = 0xfc;
45 fifty = 0;
46 color_to_move = WHITE;
47 other_color = BLACK;
48 under_check = 0xff;
49 king_pos[1] = E1;
50 king_pos[0] = E8;
51 first_rank[1] = 0x00;
52 first_rank[0] = 0x70;
53 second_rank[1] = 0x10;
54 second_rank[0] = 0x60;
55 passant_rank[1] = 0x40;
56 passant_rank[0] = 0x30;
57 seventh_rank[1] = 0x60;
58 seventh_rank[0] = 0x10;
59 //cicci = 0;
61 recalc_line_pawns();
62 recalc_mat_tracking();
63 recalc_hash();
64 #if TRACK_ATTACKS
65 recalc_attacks();
66 #endif
69 #if 0
70 void Board::set_as_default()
72 static uint8_t b_start55[] =
74 SENDL,SENDL,
75 SENDL,SENDL,
76 SENDL,SENDL,
77 WR,WN,WB,WQ,WK,STONE,STONE,STONE,SENDL,
78 WP,WP,WP,WP,WP,STONE,STONE,STONE,SENDL,
79 __,__,__,__,__,STONE,STONE,STONE,SENDL,
80 BP,BP,BP,BP,BP,STONE,STONE,STONE,SENDL,
81 BR,BN,BB,BQ,BK,STONE,STONE,STONE,SENDL
83 static uint8_t b_castle_adj55[] =
85 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
86 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
87 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
88 0xd0,0xf0,0xf0,0xf0,0xc0,0xf0,0xf0,0xf0,ENDL,
89 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
90 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
91 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
92 0x70,0xf0,0xf0,0xf0,0x30,0xf0,0xf0,0xf0,ENDL
95 memcpy(data,b_start55,128);
96 memset(data+128, 0xff, 128);
97 memcpy(castle_adj,b_castle_adj55,128);
99 num_moves=0;
101 flags_stack_ptr = 0;
102 num_old_hashes = 0;
104 castle_passing_mask = 0xfc;
105 fifty = 0;
106 color_to_move = WHITE;
107 other_color = BLACK;
108 under_check = 0xff;
109 king_pos[1] = E3;
110 king_pos[0] = E8;
111 first_rank[1] = 0x30;
112 first_rank[0] = 0x70;
113 second_rank[1] = 0x40;
114 second_rank[0] = 0x60;
115 passant_rank[1] = 0x40;
116 passant_rank[0] = 0x60;
117 seventh_rank[1] = 0x60;
118 seventh_rank[0] = 0x40;
120 recalc_line_pawns();
121 recalc_mat_tracking();
122 recalc_hash();
123 recalc_attacks();
126 void Board::set_as_default()
128 static uint8_t b_start68[] =
130 SENDL,SENDL,
131 WR,WN,WB,WQ,WK,WB,WN,WR,SENDL,
132 WP,WP,WP,WP,WP,WP,WP,WP,SENDL,
133 __,__,__,__,__,__,__,__,SENDL,
134 __,__,__,__,__,__,__,__,SENDL,
135 BP,BP,BP,BP,BP,BP,BP,BP,SENDL,
136 BR,BN,BB,BQ,BK,BB,BN,BR,SENDL,
137 SENDL,SENDL
139 static uint8_t b_castle_adj68[] =
141 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
142 0xd0,0xf0,0xf0,0xf0,0xc0,0xf0,0xf0,0xe0,ENDL,
143 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
144 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
145 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
146 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
147 0x70,0xf0,0xf0,0xf0,0x30,0xf0,0xf0,0xb0,ENDL,
148 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL
151 memcpy(data,b_start68,128);
152 memset(data+128, 0xff, 128);
153 memcpy(castle_adj,b_castle_adj68,128);
155 num_moves=0;
157 flags_stack_ptr = 0;
158 num_old_hashes = 0;
160 castle_passing_mask = 0xfc;
161 fifty = 0;
162 color_to_move = WHITE;
163 other_color = BLACK;
164 under_check = 0xff;
165 king_pos[1] = E2;
166 king_pos[0] = E7;
167 first_rank[1] = 0x10;
168 first_rank[0] = 0x60;
169 second_rank[1] = 0x20;
170 second_rank[0] = 0x50;
171 passant_rank[1] = 0x30;
172 passant_rank[0] = 0x40;
173 seventh_rank[1] = 0x50;
174 seventh_rank[0] = 0x20;
176 recalc_line_pawns();
177 recalc_mat_tracking();
178 recalc_hash();
179 recalc_attacks();
181 #endif
185 /*******************************************************************************
186 initialize a table with all possible horse moves
187 *******************************************************************************/
189 void Board::init_knight_moves()
191 char poss_hmoves[8][2] = {{1,2},{-1,2},{1,-2},{-1,-2},
192 {2,1},{-2,1},{2,-1},{-2,-1}};
193 if(knightmoves)
194 return;
196 knightmoves = new KnightMove[128];
198 // For every square just check all moves;
199 for(int x=0;x<8;x++)
200 for(int y=0;y<8;y++)
202 KnightMove* current = &knightmoves [ POS_XY(x,y) ];
203 unsigned char c = 0;
205 for(int i=0;i<8;i++)
207 int a = x + poss_hmoves[i][0];
208 int b = y + poss_hmoves[i][1];
210 if( (a >= 0) && (a < 8) && (b >= 0) && (b < 8) )
211 current->jump[c++] = POS_XY(a,b);
213 for(int i=c;i<8;i++)
214 current->jump[i] = 0xff;
215 current->numm = c-1;
219 //-------------------------------------------------------------------------------------
222 /* create the hashkeys.cpp file from a raw file with random data (ie /dev/urandom) */
223 void Board::init_hash_keys(const char* file)
225 HashKey *hash_keys = new HashKey[14*128];
226 FILE *f = fopen(file, "r");
227 if(!f)
229 printf("Error, could not read hash keys file!\n");
230 return;
233 //printf("loaded \"hashkeys.dat\"\n");
234 if(fread(hash_keys, sizeof(HashKey), 14*128, f) != 14*128)
235 printf("Error, could not read hash keys file!\n");
236 fclose(f);
238 FILE *s = fopen("hashkeys.cpp", "w");
240 fprintf(s, "/* Automatically generated, do not edit */\n");
241 fprintf(s, "#include \"board.h\"\n");
242 fprintf(s, "HashKey Board::hash_keys[14*128] = {\n");
244 for(int i=0;i<14*128;i++)
246 fprintf(s, " HashKey( 0x%016llxULL, 0x%08x )",
247 (unsigned long long)hash_keys[i].check, hash_keys[i].index);
248 if(i!=14*128-1)
249 fprintf(s, ",");
250 if((i+1)%2==0)
251 fprintf(s, "\n");
253 fprintf(s, "};\n");
254 fclose(s);
256 printf("hashkeys.cpp generated!\n");