revert to color at LSB, to avoid easy mistakes

close #3
This commit is contained in:
2021-11-19 12:15:53 +01:00
parent b582798751
commit 6b2c1702f6
3 changed files with 22 additions and 25 deletions

View File

@@ -22,13 +22,13 @@ typedef u8 piece_t;
enum { enum {
E_EMPTY = 0, E_EMPTY = 0,
E_COLOR, /* LSB */
E_PAWN, E_PAWN,
E_KNIGHT, E_KNIGHT,
E_BISHOP, E_BISHOP,
E_ROOK, E_ROOK,
E_QUEEN, E_QUEEN,
E_KING, E_KING,
E_COLOR = 8
}; };
@@ -36,23 +36,22 @@ enum {
*/ */
enum { enum {
EMPTY = 0, EMPTY = 0,
PAWN = 1 << (E_PAWN - 1), /* 0x01 00000001 */ PAWN = 1 << (E_PAWN - 1), /* 0x02 00000010 */
KNIGHT = 1 << (E_KNIGHT - 1), /* 0x02 00000010 */ KNIGHT = 1 << (E_KNIGHT - 1), /* 0x04 00000100 */
BISHOP = 1 << (E_BISHOP - 1), /* 0x04 00000100 */ BISHOP = 1 << (E_BISHOP - 1), /* 0x08 00001000 */
ROOK = 1 << (E_ROOK - 1), /* 0x08 00001000 */ ROOK = 1 << (E_ROOK - 1), /* 0x10 00010000 */
QUEEN = 1 << (E_QUEEN - 1), /* 0x10 00010000 */ QUEEN = 1 << (E_QUEEN - 1), /* 0x20 00100000 */
KING = 1 << (E_KING - 1), /* 0x20 00100000 */ KING = 1 << (E_KING - 1), /* 0x40 01000000 */
}; };
#define WHITE 0 /* 0x00 00000000 */ #define WHITE 0 /* 0x00 00000000 */
#define BLACK 1 /* 0x01 00000001 */ #define BLACK 1 /* 0x01 00000001 */
#define OPPONENT(p) !(p) #define OPPONENT(p) !(p)
#define MASK_PIECE 0x3F /* 00111111 */ #define MASK_COLOR 0x01 /* 00000001 */
#define MASK_COLOR 0x80 /* 10000000 */ #define MASK_PIECE 0x7E /* 01111110 */
#define COLOR(p) ((p) & MASK_COLOR) /* bitmask */ #define COLOR(p) ((p) & MASK_COLOR) /* bitmask */
#define VCOLOR(p) (!!COLOR(p)) /* WHITE/BLACK */
#define PIECE(p) ((p) & MASK_PIECE) #define PIECE(p) ((p) & MASK_PIECE)
#define E_PIECE(p) (ffs64(PIECE(p))) /* convert mask to E_XX */ #define E_PIECE(p) (ffs64(PIECE(p))) /* convert mask to E_XX */

View File

@@ -186,8 +186,8 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
/* fix dest square */ /* fix dest square */
newpos->board[to].s_piece->square = to; newpos->board[to].s_piece->square = to;
/* replace old occupied bitboard by new one */ /* replace old occupied bitboard by new one */
newpos->occupied[VCOLOR(piece)] ^= SQ88_2_BB(from); newpos->occupied[COLOR(piece)] ^= SQ88_2_BB(from);
newpos->occupied[VCOLOR(piece)] |= SQ88_2_BB(to); newpos->occupied[COLOR(piece)] |= SQ88_2_BB(to);
/* always make "from" square empty */ /* always make "from" square empty */
newpos->board[from].piece = 0; newpos->board[from].piece = 0;
@@ -277,7 +277,6 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
{ {
piece_t piece = PIECE(ppiece->piece); piece_t piece = PIECE(ppiece->piece);
unsigned char color = COLOR(ppiece->piece); unsigned char color = COLOR(ppiece->piece);
unsigned char vcolor = VCOLOR(ppiece->piece);
square_t square = ppiece->square, new; square_t square = ppiece->square, new;
board_t *board = pos->board; board_t *board = pos->board;
unsigned char rank2, rank7, rank5; unsigned char rank2, rank7, rank5;
@@ -319,7 +318,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
log_i(4, "pushing pawn %#04x\n", square); log_i(4, "pushing pawn %#04x\n", square);
# endif # endif
//log_f(4, "pawn move mobility\n"); //log_f(4, "pawn move mobility\n");
pos->mobility[vcolor]++; pos->mobility[color]++;
if (doit && (move = move_pawn_add(pos, piece | color, square, new, rank7))) if (doit && (move = move_pawn_add(pos, piece | color, square, new, rank7)))
count++; count++;
/* push 2 squares */ /* push 2 squares */
@@ -330,7 +329,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
log_i(4, "pushing pawn %#04x 2 squares\n", square); log_i(4, "pushing pawn %#04x 2 squares\n", square);
# endif # endif
//log_f(2, "pawn move2 mobility\n"); //log_f(2, "pawn move2 mobility\n");
pos->mobility[vcolor]++; pos->mobility[color]++;
if (doit && if (doit &&
(move = move_pawn_add(pos, piece | color, square, new, rank7))) { (move = move_pawn_add(pos, piece | color, square, new, rank7))) {
count++; count++;
@@ -374,10 +373,10 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
# endif # endif
if (SQ88_NOK(new)) if (SQ88_NOK(new))
continue; continue;
pos->controlled[vcolor] |= SQ88_2_BB(new); pos->controlled[color] |= SQ88_2_BB(new);
if (board[new].piece && COLOR(board[new].piece) != color) { if (board[new].piece && COLOR(board[new].piece) != color) {
//log_f(2, "pawn capture mobility\n"); //log_f(2, "pawn capture mobility\n");
pos->mobility[vcolor]++; pos->mobility[color]++;
if (doit && if (doit &&
((move = move_pawn_add(pos, piece | color, square, new, rank7)))) ((move = move_pawn_add(pos, piece | color, square, new, rank7))))
count++; count++;
@@ -476,7 +475,6 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
{ {
piece_t piece = PIECE(ppiece->piece); piece_t piece = PIECE(ppiece->piece);
unsigned char color = COLOR(ppiece->piece); unsigned char color = COLOR(ppiece->piece);
unsigned char vcolor = VCOLOR(ppiece->piece);
struct vector *vector = vectors+piece; struct vector *vector = vectors+piece;
square_t square = ppiece->square; square_t square = ppiece->square;
unsigned char ndirs = vector->ndirs; unsigned char ndirs = vector->ndirs;
@@ -516,10 +514,10 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
//bitboard_print(new_bitboard); //bitboard_print(new_bitboard);
# endif # endif
pos->controlled[vcolor] |= bb_new;; pos->controlled[color] |= bb_new;;
/* king: do not move to opponent controlled square */ /* king: do not move to opponent controlled square */
if (piece == KING && pos->controlled[OPPONENT(vcolor)] & bb_new) { if (piece == KING && pos->controlled[OPPONENT(color)] & bb_new) {
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(2, "%s king cannot move to %c%c\n", log_i(2, "%s king cannot move to %c%c\n",
IS_WHITE(color)? "white": "black", IS_WHITE(color)? "white": "black",
@@ -528,9 +526,9 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
break; break;
} }
if (bb_new & pos->occupied[vcolor]) { if (bb_new & pos->occupied[color]) {
//bitboard_print(pos->occupied[vcolor]); //bitboard_print(pos->occupied[color]);
//bitboard_print(pos->occupied[OPPONENT(vcolor)]); //bitboard_print(pos->occupied[OPPONENT(color)]);
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(2, "BB: skipping %#llx [%c%c] (same color piece)\n", log_i(2, "BB: skipping %#llx [%c%c] (same color piece)\n",
new, FILE2C(GET_F(new)), RANK2C(GET_R(new))); new, FILE2C(GET_F(new)), RANK2C(GET_R(new)));
@@ -540,7 +538,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
/* we are sure the move is valid : we create move */ /* we are sure the move is valid : we create move */
//log_f(2, "piece mobility\n"); //log_f(2, "piece mobility\n");
pos->mobility[vcolor]++; pos->mobility[color]++;
if (doit && color == pos->turn) { if (doit && color == pos->turn) {
if ((move = move_add(pos, ppiece->piece, square, new))) { if ((move = move_add(pos, ppiece->piece, square, new))) {
count++; count++;

View File

@@ -63,7 +63,7 @@ void piece_pool_stats()
piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square) piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square)
{ {
piece_list_t *new; piece_list_t *new;
short color = VCOLOR(piece); short color = COLOR(piece);
# ifdef DEBUG_PIECE # ifdef DEBUG_PIECE
log_f(3, "piece=%02x square=%02x\n", piece, square); log_f(3, "piece=%02x square=%02x\n", piece, square);