diff --git a/src/board.h b/src/board.h index c51fbdd..50da4f9 100644 --- a/src/board.h +++ b/src/board.h @@ -27,9 +27,12 @@ typedef struct board_s { /* definitions for 0x88 representation */ -#define SQ88(f, r) (16 * (r) + (f)) /* from rank,file to sq88 */ -#define FILE88(s) ((s) & 7) /* from sq88 to file */ -#define RANK88(s) ((s) >> 4) /* from sq88 to rank */ +#define SQ88(f, r) (((r) << 4) | (f)) /* from rank,file to sq88 */ +#define F88(s) ((s) & 0x0f) /* from sq88 to file */ +#define R88(s) ((s) >> 4) /* from sq88 to rank */ + +#define SETF88(s, r) ((s) &= 0xf0, (s) |= (r)) +#define SETR88(s, f) ((s) &= 0x0f, (s) |= (f)<<4) #define SQ88_NOK(s) ((s) & 0x88) /* invalid square */ #define SQ88_OK(s) (!SQ88_NOK(s)) @@ -37,7 +40,7 @@ typedef struct board_s { /* definitions for bitboard representation */ #define BB(f, r) (1ULL << (8 * (r) + (f))) /* from rank,file to bitboard */ -#define SQ88_2_BB(s) (BB(FILE88(s), RANK88(s))) /* from sq88 to bitboard */ +#define SQ88_2_BB(s) (BB(F88(s), R88(s))) /* from sq88 to bitboard */ #define FILEBB(b) ((b) % 8) /* from sq88 to file */ #define RANKBB(b) ((b) / 8) /* from sq88 to rank */ diff --git a/src/chessdefs.h b/src/chessdefs.h index 9301278..53e49b8 100644 --- a/src/chessdefs.h +++ b/src/chessdefs.h @@ -36,7 +36,7 @@ enum { */ enum { EMPTY = 0, - PAWN = 1 << (E_PAWN - 1), /* 0x02 00000010 */ + PAWN = 1 << (E_PAWN - 1), /* 1<<(2-1) = 0x02 00000010 */ KNIGHT = 1 << (E_KNIGHT - 1), /* 0x04 00000100 */ BISHOP = 1 << (E_BISHOP - 1), /* 0x08 00001000 */ ROOK = 1 << (E_ROOK - 1), /* 0x10 00010000 */ @@ -68,12 +68,6 @@ enum { */ typedef unsigned char square_t; -#define GET_R(s) ((s) >> 4) -#define GET_F(s) ((s) & 0x0f) -#define SET_R(s, f) ((s) &= 0x0f, (s) |= (f)<<4) -#define SET_F(s, r) ((s) &= 0xf0, (s) |= (r)) -#define SQUARE(f, r) ((r) << 4 | (f)) - /* castle_t bits structure */ typedef unsigned char castle_t; diff --git a/src/fen.c b/src/fen.c index 7b8d550..90536f9 100644 --- a/src/fen.c +++ b/src/fen.c @@ -88,7 +88,7 @@ pos_t *fen2pos(pos_t *pos, char *fen) SET_COLOR(piece, color); board[SQ88(file, rank)].piece = piece; board[SQ88(file, rank)].s_piece = - piece_add(pos, piece, SQUARE(file, rank)); + piece_add(pos, piece, SQ88(file, rank)); file++; break; case '/': @@ -146,8 +146,10 @@ pos_t *fen2pos(pos_t *pos, char *fen) SKIP_BLANK(p); pos->en_passant = 0; if (*p != '-') { - SET_F(pos->en_passant, C2FILE(*p++)); - SET_R(pos->en_passant, C2RANK(*p++)); + //SET_F(pos->en_passant, C2FILE(*p++)); + //SET_R(pos->en_passant, C2RANK(*p++)); + pos->en_passant = SQ88(C2FILE(*p), C2RANK(*(p+1))); + pos += 2; } else { p++; } diff --git a/src/move.c b/src/move.c index d5c233b..02b0d51 100644 --- a/src/move.c +++ b/src/move.c @@ -82,8 +82,8 @@ int move_print(move_t *move, move_flags_t flags) goto end; } else { printf("%s%c%c", P_SYM(move->piece), - FILE2C(GET_F(move->from)), - RANK2C(GET_R(move->from))); + FILE2C(F88(move->from)), + RANK2C(R88(move->from))); if (move->taken) { printf("x"); if (flags & M_PR_LONG) @@ -92,8 +92,8 @@ int move_print(move_t *move, move_flags_t flags) printf("-"); } printf("%c%c", - FILE2C(GET_F(move->to)), - RANK2C(GET_R(move->to))); + FILE2C(F88(move->to)), + RANK2C(R88(move->to))); if (flags & M_PR_LONG && move->flags & M_EN_PASSANT) printf("e.p."); if (move->promotion) @@ -143,10 +143,10 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from, # ifdef DEBUG_MOVE log_i(3, "piece_color=%d turn=%d from=%c%c to=%c%c\n", COLOR(piece), pos->turn, - FILE2C(GET_F(from)), - RANK2C(GET_R(from)), - FILE2C(GET_F(to)), - RANK2C(GET_R(to))); + FILE2C(F88(from)), + RANK2C(R88(from)), + FILE2C(F88(to)), + RANK2C(R88(to))); # endif if (COLOR(piece) != pos->turn) return NULL; @@ -155,7 +155,7 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from, if (board[to].piece & KING) { # ifdef DEBUG_MOVE log_i(2, "invalid position: opponent king [%c%c] is in check.\n", - FILE2C(GET_F(to)), RANK2C(GET_R(to))); + FILE2C(F88(to)), RANK2C(R88(to))); # endif return NULL; } @@ -195,8 +195,8 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from, # ifdef DEBUG_MOVE log_i(3, "added move from %c%c to %c%c\n", - FILE2C(GET_F(move->from)), RANK2C(GET_R(move->from)), - FILE2C(GET_F(move->to)), RANK2C(GET_R(move->to))); + FILE2C(F88(move->from)), RANK2C(R88(move->from)), + FILE2C(F88(move->to)), RANK2C(R88(move->to))); # endif return move; } @@ -207,8 +207,8 @@ void move_del(struct list_head *ptr) # ifdef DEBUG_MOVE log_i(3, "delete move from %c%c to %c%c\n", - FILE2C(GET_F(move->from)), RANK2C(GET_R(move->from)), - FILE2C(GET_F(move->to)), RANK2C(GET_R(move->to))); + FILE2C(F88(move->from)), RANK2C(R88(move->from)), + FILE2C(F88(move->to)), RANK2C(R88(move->to))); # endif /* TODO: remove move->pos if non null */ @@ -248,7 +248,7 @@ static move_t *move_pawn_add(pos_t *pos, piece_t piece, square_t from, if (color != pos->turn) return NULL; - if (RANK88(from) == rank7) { /* promotion */ + if (R88(from) == rank7) { /* promotion */ for (promote = QUEEN; promote > PAWN; promote >>= 1) { if ((move = move_add(pos, piece, from, to))) { move->flags |= M_PROMOTION; @@ -306,7 +306,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit) P_NAME(piece), dir, square, - FILE2C(GET_F(square)), RANK2C(GET_R(square))); + FILE2C(F88(square)), RANK2C(R88(square))); # endif /* normal push. We do not test for valid destination square here, @@ -322,7 +322,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit) if (doit && (move = move_pawn_add(pos, piece | color, square, new, rank7))) count++; /* push 2 squares */ - if (move && RANK88(square) == rank2) { + if (move && R88(square) == rank2) { new += dir * 16; if (SQ88_OK(new) && !board[new].piece) { # ifdef DEBUG_MOVE @@ -339,16 +339,16 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit) } /* en passant - not accounted for mobility. Correct ? */ - if (doit && pos->en_passant && RANK88(square) == rank5) { - unsigned char ep_file = FILE88(pos->en_passant); - unsigned char sq_file = FILE88(square); + if (doit && pos->en_passant && R88(square) == rank5) { + unsigned char ep_file = F88(pos->en_passant); + unsigned char sq_file = F88(square); # ifdef DEBUG_MOVE log_i(4, "possible en passant on rank %#x (current = %#0x)\n", ep_file, sq_file); # endif if (sq_file == ep_file - 1 || sq_file == ep_file + 1) { - square_t t_square = SQUARE(ep_file, rank5); /* taken pawn square */ + square_t t_square = SQ88(ep_file, rank5); /* taken pawn square */ piece_t taken = board[t_square].piece; move = move_pawn_add(pos, piece | color , square, pos->en_passant, rank7); count++; @@ -425,17 +425,17 @@ int pseudo_moves_castle(pos_t *pos) printf("Cannot castle K side: controlled\n"); goto next; } - move = move_add(pos, board[SQUARE(4, rank1)].piece, - SQUARE(4, rank1), SQUARE(6, rank1)); + move = move_add(pos, board[SQ88(4, rank1)].piece, + SQ88(4, rank1), SQ88(6, rank1)); if (move) { newpos = move->newpos; move->flags |= M_CASTLE_K; /* move King rook to column F */ - newpos->board[SQUARE(5, rank1)] = newpos->board[SQUARE(7, rank1)]; - SET_F(newpos->board[SQUARE(5, rank1)].s_piece->square, 5); - newpos->board[SQUARE(7, rank1)].piece = 0; - newpos->board[SQUARE(7, rank1)].s_piece = NULL; + newpos->board[SQ88(5, rank1)] = newpos->board[SQ88(7, rank1)]; + SETF88(newpos->board[SQ88(5, rank1)].s_piece->square, 5); + newpos->board[SQ88(7, rank1)].piece = 0; + newpos->board[SQ88(7, rank1)].s_piece = NULL; count++; } @@ -451,16 +451,16 @@ next: printf("Cannot castle Q side: controlled\n"); goto end; } - move = move_add(pos, board[SQUARE(4, rank1)].piece, - SQUARE(4, rank1), SQUARE(2, rank1)); + move = move_add(pos, board[SQ88(4, rank1)].piece, + SQ88(4, rank1), SQ88(2, rank1)); if (move) { newpos = move->newpos; move->flags |= M_CASTLE_Q; /* move King rook to column F */ - newpos->board[SQUARE(3, rank1)] = newpos->board[SQUARE(0, rank1)]; - SET_F(newpos->board[SQUARE(3, rank1)].s_piece->square, 3); - newpos->board[SQUARE(0, rank1)].piece = 0; - newpos->board[SQUARE(0, rank1)].s_piece = NULL; + newpos->board[SQ88(3, rank1)] = newpos->board[SQ88(0, rank1)]; + SETF88(newpos->board[SQ88(3, rank1)].s_piece->square, 3); + newpos->board[SQ88(0, rank1)].piece = 0; + newpos->board[SQ88(0, rank1)].s_piece = NULL; count++; } @@ -492,7 +492,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit) IS_WHITE(color)? "white": "black", P_NAME(piece), square, - FILE2C(GET_F(square)), RANK2C(GET_R(square))); + FILE2C(F88(square)), RANK2C(R88(square))); log_i(5, "vector=%ld ndirs=%d slide=%d\n", vector-vectors, ndirs, slide); # endif @@ -509,7 +509,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit) bb_new = SQ88_2_BB(new); # ifdef DEBUG_MOVE log_i(2, "trying %#x=%c%c bb=%#lx\n", - new, FILE2C(GET_F(new)), RANK2C(GET_R(new)), + new, FILE2C(F88(new)), RANK2C(R88(new)), bb_new); //bitboard_print(new_bitboard); # endif @@ -521,7 +521,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit) # ifdef DEBUG_MOVE log_i(2, "%s king cannot move to %c%c\n", IS_WHITE(color)? "white": "black", - FILE2C(GET_F(new)), RANK2C(GET_R(new))); + FILE2C(F88(new)), RANK2C(R88(new))); # endif break; } @@ -531,7 +531,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit) //bitboard_print(pos->occupied[OPPONENT(color)]); # ifdef DEBUG_MOVE log_i(2, "BB: skipping %#llx [%c%c] (same color piece)\n", - new, FILE2C(GET_F(new)), RANK2C(GET_R(new))); + new, FILE2C(F88(new)), RANK2C(R88(new))); # endif break; } diff --git a/src/piece.c b/src/piece.c index 65a14e6..599feed 100644 --- a/src/piece.c +++ b/src/piece.c @@ -41,8 +41,8 @@ void piece_list_print(struct list_head *list) piece = list_entry(p_cur, piece_list_t, list); printf("%s%c%c ", P_SYM(piece->piece), - FILE2C(GET_F(piece->square)), - RANK2C(GET_R(piece->square))); + FILE2C(F88(piece->square)), + RANK2C(R88(piece->square))); } printf("\n"); } @@ -68,7 +68,7 @@ piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square) # ifdef DEBUG_PIECE log_f(3, "piece=%02x square=%02x\n", piece, square); log_f(5, "Adding %s %s on %c%c\n", color? "Black": "White", - P_NAME(piece), FILE2C(GET_F(square)), RANK2C(GET_R(square))); + P_NAME(piece), FILE2C(F88(square)), RANK2C(R88(square))); # endif if ((new = pool_get(pieces_pool))) { list_add_tail(&new->list, &pos->pieces[color]); diff --git a/src/position.c b/src/position.c index c7da93e..2dd7efd 100644 --- a/src/position.c +++ b/src/position.c @@ -85,18 +85,19 @@ void pos_print(pos_t *pos) printf(" A B C D E F G H\n\n"); printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black"); printf("Kings: W:%c%c B:%c%c\n", - FILE2C(GET_F(pos->king[WHITE])), - RANK2C(GET_R(pos->king[WHITE])), - FILE2C(GET_F(pos->king[BLACK])), - RANK2C(GET_R(pos->king[BLACK]))); + FILE2C(F88(pos->king[WHITE])), + RANK2C(R88(pos->king[WHITE])), + FILE2C(F88(pos->king[BLACK])), + RANK2C(R88(pos->king[BLACK]))); printf("Possible en-passant: [%#x] ", pos->en_passant); if (pos->en_passant == 0) printf("None.\n"); else - printf("%d %d = %c%c\n", GET_F(pos->en_passant), - GET_R(pos->en_passant), - FILE2C(GET_F(pos->en_passant)), - RANK2C(GET_R(pos->en_passant))); + printf("%d %d = %c%c\n", + F88(pos->en_passant), + R88(pos->en_passant), + FILE2C(F88(pos->en_passant)), + RANK2C(R88(pos->en_passant))); printf("castle [%#x] : ", pos->castle);