1 /***************************************************************************
2 board_defs.h - Board related definitions
4 begin : Sun Sep 28 2007
5 copyright : (C) 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 ***************************************************************************/
18 #ifndef __BOARD_DEFS_H__
19 #define __BOARD_DEFS_H__
25 /*******************************************************************************
26 the chessboard is a 16x8 arrays, and is accessed with 1-byte indices
27 whose binary form look like: 0yyy0xxx
28 where xxx and yyy are 3bits (0 ... 7) that store the x and y coords.
29 The 4th and 8th bits are 0 if the index is valid, so an invalid (out
30 of board) index can be detected testing with 10001000 (0x88)
32 Chess pieces. Keep these numbers because in a few place it is useful to be
33 able to assume that QUEEN=ROOK|BISHOP, ie if you want to test if a piece
34 moves diagonally you have just to test (piece&(~ROOK) == BISHOP)
35 *******************************************************************************/
43 #define STONE ((uint8_t)0xff)
44 #define ENDL 0,0,0,0,0,0,0,0
45 #define SENDL STONE,STONE,STONE,STONE,STONE,STONE,STONE,STONE
47 #define INVALID ((uint8_t)0xff)
50 #define WHITE ((uint8_t)0x80)
51 #define BLACK ((uint8_t)0x40)
53 // flags to mark pinning information
54 #define NFREE ((uint8_t)0x20)
55 #define CLFREE ((~NFREE)+(~(NFREE<<8))+(~(NFREE<<16))+(~(NFREE<<24)))
56 #define DIAG ((uint8_t)0x80)
57 #define COLM ((uint8_t)0x40)
59 #define I(a) ((uint8_t)(a))
62 #define UP ((uint8_t)0x10)
63 #define DOWN ((uint8_t)0xf0)
64 #define RIGHT ((uint8_t)0x01)
65 #define LEFT ((uint8_t)0xff)
67 #define RIGHT_OF(a) ((uint8_t)(RIGHT+a))
68 #define LEFT_OF(a) ((uint8_t)(LEFT+a))
69 #define UP_OF(a) ((uint8_t)(UP+a))
70 #define DOWN_OF(a) ((uint8_t)(DOWN+a))
71 #define RIGHTUP_OF(a) ((uint8_t)(RIGHT+UP+a))
72 #define LEFTUP_OF(a) ((uint8_t)(LEFT+UP+a))
73 #define RIGHTDOWN_OF(a) ((uint8_t)(RIGHT+DOWN+a))
74 #define LEFTDOWN_OF(a) ((uint8_t)(LEFT+DOWN+a))
76 // macros to work with 1-byte coordinates
77 #define POS_XY(x,y) ((uint8_t)((y)<<4)+(x))
78 #define OTHER_COLOR(a) (((uint8_t)0xc0) - (a))
79 #define IS_OF_COLOR(a,c) ((a) & (c))
80 #define COLOR_OF(a) (((uint8_t)0xc0) & (a))
81 #define PIECE_OF(a) (((uint8_t)0x0f) & (a))
82 #define ROW_OF(a) (a & ((uint8_t)0xf0))
83 #define COL_OF(a) (a & ((uint8_t)0x0f))
84 #define Y(a) ((uint8_t)(((a) & ((uint8_t)0xf0))>>4))
85 #define X(a) ((a) & ((uint8_t)0x0f))
86 #define CAN_MOVE(a) (! ((a) & NFREE))
87 #define IS_VOID(a) (!(a))
88 #define IS_PIECE(a,b) (((a) & ((uint8_t)0x0f)) ==(b))
89 #define OUT_OF_BOARD(a) ((a) & ((uint8_t)0x88))
90 #define IS_WHITE(a) ((uint8_t)((a)>>7))
91 #define PIECE_TO_12(p) ({uint8_t _p = (p); uint8_t _r = PIECE_OF(_p)-1 + 6*IS_WHITE(_p);ASSERT(_r<12);_r;})
92 #define SQUARE_TO_64(s) ({uint8_t _s = (s); ASSERT(!OUT_OF_BOARD(_s)); X(_s) + (Y(_s)<<3);})
93 #define SQUARE_FROM_64(s) ({uint8_t _s = (s); (0x07&_s) + ((0x38&_s)<<1);})
94 #define DISTANCE(a, b) ({int _x = X(a)-X(b); int _y = Y(a)-Y(b); MAX(ABS(_x), ABS(_y));})
95 #define DISTANCE_2(a, b) ({int _x = X(a)-X(b); int _y = Y(a)-Y(b); _x*_x + _y*_y;})
97 // flag of the move (flags) field to store
98 // if the move is en-passant, castle, a pawn 2-push or a promotion
100 #define CASTLEKINGSIDE 2
101 #define CASTLEQUEENSIDE 3
104 #define PROMOTE_FIRST 5
105 #define PROMOTEROOK (ROOK+4)
106 #define PROMOTEBISHOP (BISHOP+4)
107 #define PROMOTEQUEEN (QUEEN+4)
108 #define PROMOTEKNIGHT (KNIGHT+4)
109 #define PROMOTE_LAST 8
111 //flags of the castle-passing mask
112 #define NOT_PASSING ((uint8_t)0x08)
113 #define WCANCASTLEKS ((uint8_t)0x10)
114 #define WCANCASTLEQS ((uint8_t)0x20)
115 #define BCANCASTLEKS ((uint8_t)0x40)
116 #define BCANCASTLEQS ((uint8_t)0x80)
118 /* 0 -> 11 pieces representation */
134 #define WN WHITE|KNIGHT
135 #define WB WHITE|BISHOP
136 #define WR WHITE|ROOK
137 #define WP WHITE|PAWN
138 #define WQ WHITE|QUEEN
139 #define WK WHITE|KING
140 #define BN BLACK|KNIGHT
141 #define BB BLACK|BISHOP
142 #define BR BLACK|ROOK
143 #define BP BLACK|PAWN
144 #define BQ BLACK|QUEEN
145 #define BK BLACK|KING
147 /* define chess squares values */
148 #define A1 POS_XY(0,0)
149 #define A2 POS_XY(0,1)
150 #define A3 POS_XY(0,2)
151 #define A4 POS_XY(0,3)
152 #define A5 POS_XY(0,4)
153 #define A6 POS_XY(0,5)
154 #define A7 POS_XY(0,6)
155 #define A8 POS_XY(0,7)
156 #define B1 POS_XY(1,0)
157 #define B2 POS_XY(1,1)
158 #define B3 POS_XY(1,2)
159 #define B4 POS_XY(1,3)
160 #define B5 POS_XY(1,4)
161 #define B6 POS_XY(1,5)
162 #define B7 POS_XY(1,6)
163 #define B8 POS_XY(1,7)
164 #define C1 POS_XY(2,0)
165 #define C2 POS_XY(2,1)
166 #define C3 POS_XY(2,2)
167 #define C4 POS_XY(2,3)
168 #define C5 POS_XY(2,4)
169 #define C6 POS_XY(2,5)
170 #define C7 POS_XY(2,6)
171 #define C8 POS_XY(2,7)
172 #define D1 POS_XY(3,0)
173 #define D2 POS_XY(3,1)
174 #define D3 POS_XY(3,2)
175 #define D4 POS_XY(3,3)
176 #define D5 POS_XY(3,4)
177 #define D6 POS_XY(3,5)
178 #define D7 POS_XY(3,6)
179 #define D8 POS_XY(3,7)
180 #define E1 POS_XY(4,0)
181 #define E2 POS_XY(4,1)
182 #define E3 POS_XY(4,2)
183 #define E4 POS_XY(4,3)
184 #define E5 POS_XY(4,4)
185 #define E6 POS_XY(4,5)
186 #define E7 POS_XY(4,6)
187 #define E8 POS_XY(4,7)
188 #define F1 POS_XY(5,0)
189 #define F2 POS_XY(5,1)
190 #define F3 POS_XY(5,2)
191 #define F4 POS_XY(5,3)
192 #define F5 POS_XY(5,4)
193 #define F6 POS_XY(5,5)
194 #define F7 POS_XY(5,6)
195 #define F8 POS_XY(5,7)
196 #define G1 POS_XY(6,0)
197 #define G2 POS_XY(6,1)
198 #define G3 POS_XY(6,2)
199 #define G4 POS_XY(6,3)
200 #define G5 POS_XY(6,4)
201 #define G6 POS_XY(6,5)
202 #define G7 POS_XY(6,6)
203 #define G8 POS_XY(6,7)
204 #define H1 POS_XY(7,0)
205 #define H2 POS_XY(7,1)
206 #define H3 POS_XY(7,2)
207 #define H4 POS_XY(7,3)
208 #define H5 POS_XY(7,4)
209 #define H6 POS_XY(7,5)
210 #define H7 POS_XY(7,6)
211 #define H8 POS_XY(7,7)
213 #endif //__BOARD_DEFS_H__