execute
[rattatechess.git] / board.cpp
blobc79e511631ca588c3f4f3fe47d78485abab0d5a3
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 set_as_default();
28 //-------------------------------------------------------------------------------------
29 //-------------------------------------------------------------------------------------
31 void Board::set_as_default()
33 memcpy(&data[0],b_start,128);
34 memcpy(&castle_adj[0],b_castle_adj,128);
36 num_moves=0;
38 castle_passing_mask = 0xfc;
39 fifty = 0;
40 color_to_move = WHITE;
41 other_color = BLACK;
42 under_check = 0xff;
43 king_pos[1] = E1;
44 king_pos[0] = E8;
45 first_rank[1] = 0x00;
46 first_rank[0] = 0x70;
47 second_rank[1] = 0x10;
48 second_rank[0] = 0x60;
49 passant_rank[1] = 0x40;
50 passant_rank[0] = 0x30;
51 seventh_rank[1] = 0x60;
52 seventh_rank[0] = 0x10;
53 //cicci = 0;
55 recalc_line_pawns();
56 recalc_mat_tracking();
57 recalc_hash();
58 #if TRACK_ATTACKS
59 recalc_attacks();
60 #endif
63 #if 0
64 void Board::set_as_default()
66 static uint8_t b_start55[] =
68 SENDL,SENDL,
69 SENDL,SENDL,
70 SENDL,SENDL,
71 WR,WN,WB,WQ,WK,STONE,STONE,STONE,SENDL,
72 WP,WP,WP,WP,WP,STONE,STONE,STONE,SENDL,
73 __,__,__,__,__,STONE,STONE,STONE,SENDL,
74 BP,BP,BP,BP,BP,STONE,STONE,STONE,SENDL,
75 BR,BN,BB,BQ,BK,STONE,STONE,STONE,SENDL
77 static uint8_t b_castle_adj55[] =
79 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
80 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
81 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
82 0xd0,0xf0,0xf0,0xf0,0xc0,0xf0,0xf0,0xf0,ENDL,
83 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
84 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
85 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
86 0x70,0xf0,0xf0,0xf0,0x30,0xf0,0xf0,0xf0,ENDL
89 memcpy(data,b_start55,128);
90 memset(data+128, 0xff, 128);
91 memcpy(castle_adj,b_castle_adj55,128);
93 num_moves=0;
95 flags_stack_ptr = 0;
96 num_old_hashes = 0;
98 castle_passing_mask = 0xfc;
99 fifty = 0;
100 color_to_move = WHITE;
101 other_color = BLACK;
102 under_check = 0xff;
103 king_pos[1] = E3;
104 king_pos[0] = E8;
105 first_rank[1] = 0x30;
106 first_rank[0] = 0x70;
107 second_rank[1] = 0x40;
108 second_rank[0] = 0x60;
109 passant_rank[1] = 0x40;
110 passant_rank[0] = 0x60;
111 seventh_rank[1] = 0x60;
112 seventh_rank[0] = 0x40;
114 recalc_line_pawns();
115 recalc_mat_tracking();
116 recalc_hash();
117 recalc_attacks();
120 void Board::set_as_default()
122 static uint8_t b_start68[] =
124 SENDL,SENDL,
125 WR,WN,WB,WQ,WK,WB,WN,WR,SENDL,
126 WP,WP,WP,WP,WP,WP,WP,WP,SENDL,
127 __,__,__,__,__,__,__,__,SENDL,
128 __,__,__,__,__,__,__,__,SENDL,
129 BP,BP,BP,BP,BP,BP,BP,BP,SENDL,
130 BR,BN,BB,BQ,BK,BB,BN,BR,SENDL,
131 SENDL,SENDL
133 static uint8_t b_castle_adj68[] =
135 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
136 0xd0,0xf0,0xf0,0xf0,0xc0,0xf0,0xf0,0xe0,ENDL,
137 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
138 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
139 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
140 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL,
141 0x70,0xf0,0xf0,0xf0,0x30,0xf0,0xf0,0xb0,ENDL,
142 0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,ENDL
145 memcpy(data,b_start68,128);
146 memset(data+128, 0xff, 128);
147 memcpy(castle_adj,b_castle_adj68,128);
149 num_moves=0;
151 flags_stack_ptr = 0;
152 num_old_hashes = 0;
154 castle_passing_mask = 0xfc;
155 fifty = 0;
156 color_to_move = WHITE;
157 other_color = BLACK;
158 under_check = 0xff;
159 king_pos[1] = E2;
160 king_pos[0] = E7;
161 first_rank[1] = 0x10;
162 first_rank[0] = 0x60;
163 second_rank[1] = 0x20;
164 second_rank[0] = 0x50;
165 passant_rank[1] = 0x30;
166 passant_rank[0] = 0x40;
167 seventh_rank[1] = 0x50;
168 seventh_rank[0] = 0x20;
170 recalc_line_pawns();
171 recalc_mat_tracking();
172 recalc_hash();
173 recalc_attacks();
175 #endif
179 /*******************************************************************************
180 initialize a table with all possible horse moves
181 *******************************************************************************/
183 void Board::init_knight_moves()
185 char poss_hmoves[8][2] = {{1,2},{-1,2},{1,-2},{-1,-2},
186 {2,1},{-2,1},{2,-1},{-2,-1}};
187 if(knightmoves)
188 return;
190 knightmoves = new KnightMove[128];
192 // For every square just check all moves;
193 for(int x=0;x<8;x++)
194 for(int y=0;y<8;y++)
196 KnightMove* current = &knightmoves [ POS_XY(x,y) ];
197 unsigned char c = 0;
199 for(int i=0;i<8;i++)
201 int a = x + poss_hmoves[i][0];
202 int b = y + poss_hmoves[i][1];
204 if( (a >= 0) && (a < 8) && (b >= 0) && (b < 8) )
205 current->jump[c++] = POS_XY(a,b);
207 for(int i=c;i<8;i++)
208 current->jump[i] = 0xff;
209 current->numm = c-1;
213 //-------------------------------------------------------------------------------------
216 /* create the hashkeys.cpp file from a raw file with random data (ie /dev/urandom) */
217 void Board::init_hash_keys(const char* file)
219 HashKey *hash_keys = new HashKey[14*128];
220 FILE *f = fopen(file, "r");
221 if(!f)
223 printf("Error, could not read hash keys file!\n");
224 return;
227 //printf("loaded \"hashkeys.dat\"\n");
228 if(fread(hash_keys, sizeof(HashKey), 14*128, f) != 14*128)
229 printf("Error, could not read hash keys file!\n");
230 fclose(f);
232 FILE *s = fopen("hashkeys.cpp", "w");
234 fprintf(s, "/* Automatically generated, do not edit */\n");
235 fprintf(s, "#include \"board.h\"\n");
236 fprintf(s, "HashKey Board::hash_keys[14*128] = {\n");
238 for(int i=0;i<14*128;i++)
240 fprintf(s, " HashKey( 0x%016llxULL, 0x%08x )",
241 (unsigned long long)hash_keys[i].check, hash_keys[i].index);
242 if(i!=14*128-1)
243 fprintf(s, ",");
244 if((i+1)%2==0)
245 fprintf(s, "\n");
247 fprintf(s, "};\n");
248 fclose(s);
250 printf("hashkeys.cpp generated!\n");