diff --git a/.gitignore b/.gitignore index 2f5fb48..caf89ef 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,9 @@ vgcore.* *.i *.old *.save +/GPATH +/GRTAGS +/GTAGS /.ccls-root /.ccls-cache/ /obj/ diff --git a/brlib b/brlib index 8cb08da..0e9b25d 160000 --- a/brlib +++ b/brlib @@ -1 +1 @@ -Subproject commit 8cb08dacb3d02e5b6d417f4c58a3783ac72aab03 +Subproject commit 0e9b25dd681d1daeddf9f073884532ea59962c10 diff --git a/src/bitboard.h b/src/bitboard.h index 1e4e8b2..885b405 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -20,7 +20,8 @@ #include "bitops.h" typedef enum { - A1, B1, C1, D1, E1, F1, G1, H1, + _SSQUARE_ = -1, /* force signed enum */ + A1 = 0, B1, C1, D1, E1, F1, G1, H1, A2, B2, C2, D2, E2, F2, G2, H2, A3, B3, C3, D3, E3, F3, G3, H3, A4, B4, C4, D4, E4, F4, G4, H4, @@ -30,17 +31,19 @@ typedef enum { A8, B8, C8, D8, E8, F8, G8, H8, SQUARE_MAX = 64, SQUARE_NONE = 64 -} square; +} square_t; typedef enum { - FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, + _SFILE_ = -1, /* force signed enum */ + FILE_A = 0, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_MAX, -} file; +} file_t; typedef enum { - RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, + _SRANK_ = -1, /* force signed enum */ + RANK_1 = 0, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_MAX, -} rank; +} rank_t; typedef enum { //A1 = 0x01ULL, B1 = 0x02ULL, C1 = 1UL << 2, D1 = 1UL << 3, @@ -95,17 +98,17 @@ typedef enum { SOUTH_EAST = (SOUTH + EAST), SOUTH_WEST = (SOUTH + WEST), NORTH_WEST = (NORTH + WEST), -} direction; +} dir_t; -static inline square BB(file file, rank rank) +static inline square_t BB(file_t file, rank_t rank) { return (rank << 3) + file; } -static inline file BBfile(square square) +static inline file_t BBfile(square_t square) { return square & 7; } -static inline rank BBrank(square square) +static inline rank_t BBrank(square_t square) { return square >> 3; } diff --git a/src/chessdefs.h b/src/chessdefs.h index d03b9bc..e605841 100644 --- a/src/chessdefs.h +++ b/src/chessdefs.h @@ -28,21 +28,21 @@ */ typedef enum { WHITE, BLACK, - NCOLOR_MAX -} color; + COLOR_MAX +} color_t; typedef enum { ALL_PIECES = 0, /* 'all pieces' bitboard */ PAWN = 1, KNIGHT, BISHOP, ROOK, QUEEN, KING, PIECE_TYPE_MAX = 7 /* bit 4 */ -} piece_type; +} piece_type_t; typedef enum { EMPTY, W_PAWN = PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING, B_PAWN = PAWN | 8, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING, PIECE_MAX -} piece; +} piece_t; #define OPPONENT(p) !(p) @@ -84,7 +84,7 @@ typedef enum { CASTLE_WQ = (1 << 1), /* 0x02 00000010 */ CASTLE_BK = (1 << 2), /* 0x04 00000100 */ CASTLE_BQ = (1 << 3), /* 0x08 00001000 */ -} castle; +} castle_rights_t; #define CASTLE_W (CASTLE_WK | CASTLE_WQ) /* 00000011 W castle mask */ #define CASTLE_B (CASTLE_BK | CASTLE_BQ) /* 00001100 B castle mask */ diff --git a/src/fen.c b/src/fen.c index c9ccf15..9fcd163 100644 --- a/src/fen.c +++ b/src/fen.c @@ -71,7 +71,7 @@ static const char *castle_str = "KQkq"; * * @return: the pos position. */ -position *startpos(position *pos) +pos_t *startpos(pos_t *pos) { return fen2pos(pos, startfen); } @@ -86,13 +86,13 @@ position *startpos(position *pos) * * @return: the pos position, or NULL if error. */ -position *fen2pos(position *pos, const char *fen) +pos_t *fen2pos(pos_t *pos, const char *fen) { const char *cur = fen; char *p; short rank, file, color, tmp; int consumed, err_line = 0, err_pos, err_char; - position tmppos; + pos_t tmppos; pos_clear(&tmppos); @@ -203,7 +203,7 @@ end: * @pos: a position pointer * @fen: destination FEN char*, or NULL * - * If @fen is NULL, a 100 bytes memory will be allocated with malloc(1), + * If @fen is NULL, a 92 bytes memory will be allocated with malloc(1), * that should be freed by caller. * * Note: If @fen is given, no check is done on its length, but to @@ -213,7 +213,7 @@ end: * * @return: the pos position, or NULL if error. */ -char *pos2fen(const position *pos, char *fen) +char *pos2fen(const pos_t *pos, char *fen) { int cur = 0; @@ -222,14 +222,14 @@ char *pos2fen(const position *pos, char *fen) /* 1) position */ - for (int rank = RANK_8; rank >= RANK_1; --rank) { + for (rank_t rank = RANK_8; rank >= RANK_1; --rank) { printf("r=%d 1=%d\n", rank, RANK_1); - for (int file = FILE_A; file <= FILE_H;) { + for (file_t file = FILE_A; file <= FILE_H;) { printf(" f=%d H=%d\n", file, FILE_H); - square sq = BB(file, rank); + square_t sq = BB(file, rank); printf(" sq=%d\n", sq); - piece piece = PIECE(pos->board[sq]); - color color = COLOR(pos->board[sq]); + piece_t piece = PIECE(pos->board[sq]); + color_t color = COLOR(pos->board[sq]); if (pos->board[sq] == EMPTY) { int len = 0; for (; file <= FILE_H && pos->board[BB(file,rank)] == EMPTY; file++) @@ -255,7 +255,6 @@ char *pos2fen(const position *pos, char *fen) if (pos->castle == 0) { fen[cur++] = '-'; } else { - //static char * for (int i = 0; i < 4; ++i) if (pos->castle & mask(i)) fen[cur++] = castle_str[i]; diff --git a/src/fen.h b/src/fen.h index 7a626eb..db55884 100644 --- a/src/fen.h +++ b/src/fen.h @@ -20,8 +20,8 @@ extern const char *startfen; /* startup position */ -extern position *startpos(position *pos); -extern position *fen2pos(position *pos, const char *fen); -extern char *pos2fen(const position *pos, char *fen); +extern pos_t *startpos(pos_t *pos); +extern pos_t *fen2pos(pos_t *pos, const char *fen); +extern char *pos2fen(const pos_t *pos, char *fen); #endif /* FEN_H */ diff --git a/src/piece.c b/src/piece.c index 5024f48..78c5113 100644 --- a/src/piece.c +++ b/src/piece.c @@ -26,26 +26,29 @@ //#include "bitboard.h" //#include "position.h" -//static pool_t *pieces_pool; - +/** + * piece_details + */ const struct piece_details piece_details[] = { - /* W B SW SB Name start value */ - [EMPTY] = { ' ', ' ', " ", " ", "", - 0, 0, 0 }, - [PAWN] = { 'P', 'p', "♙", "♟", "Pawn", - PAWN_VALUE, PAWN_VALUE, PAWN_VALUE }, - [KNIGHT] = { 'N', 'n', "♘", "♞", "Knight", - KNIGHT_VALUE, KNIGHT_VALUE, KNIGHT_VALUE }, - [BISHOP] = { 'B', 'b', "♗", "♝", "Bishop", - BISHOP_VALUE, BISHOP_VALUE, BISHOP_VALUE }, - [ROOK] = { 'R', 'r', "♖", "♜", "Rook", - ROOK_VALUE, ROOK_VALUE, ROOK_VALUE }, - [QUEEN] = { 'Q', 'q', "♕", "♛", "Queen", - QUEEN_VALUE, QUEEN_VALUE, QUEEN_VALUE }, - [KING] = { 'K', 'k', "♔", "♚", "King", - KING_VALUE, KING_VALUE, KING_VALUE } + /* Abb Sym Name start value */ + [EMPTY] = { ' ', " ", " ", 0, 0, 0 }, + [W_PAWN] = { 'P', "♙", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END }, + [W_KNIGHT] = { 'N', "♘", "Knight", N_VAL_OPN, N_VAL_MID, N_VAL_END }, + [W_BISHOP] = { 'B', "♗", "Bishop", B_VAL_OPN, B_VAL_MID, B_VAL_END }, + [W_ROOK] = { 'R', "♖", "Rook", R_VAL_OPN, R_VAL_MID, R_VAL_END }, + [W_QUEEN] = { 'Q', "♕", "Queen", Q_VAL_OPN, Q_VAL_MID, Q_VAL_END }, + [W_KING] = { 'K', "♔", "King", K_VAL_OPN, K_VAL_MID, K_VAL_END }, + [7] = { '7', "�", "Inv 7", 0, 0, 0 }, + [8] = { '7', "�", "Inv 8", 0, 0, 0 }, + [B_PAWN] = { 'p', "♟", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END }, + [B_KNIGHT] = { 'n', "♞", "Knight", P_VAL_OPN, N_VAL_MID, N_VAL_END }, + [B_BISHOP] = { 'b', "♝", "Bishop", P_VAL_OPN, B_VAL_MID, B_VAL_END }, + [B_ROOK] = { 'r', "♜", "Rook", P_VAL_OPN, R_VAL_MID, R_VAL_END }, + [B_QUEEN] = { 'q', "♛", "Queen", P_VAL_OPN, Q_VAL_MID, Q_VAL_END }, + [B_KING] = { 'k', "♚", "King", P_VAL_OPN, K_VAL_MID, K_VAL_END }, }; +const char *fenpieces_idx = " PNBRQ pnbrq"; /* * void piece_list_print(struct list_head *list) * { diff --git a/src/piece.h b/src/piece.h index da2b366..9b7a7f5 100644 --- a/src/piece.h +++ b/src/piece.h @@ -21,14 +21,31 @@ #include "pool.h" -/* initial default values */ -#define EMPTY_VALUE 0 -#define PAWN_VALUE 100 -#define KNIGHT_VALUE 300 -#define BISHOP_VALUE 300 -#define ROOK_VALUE 500 -#define QUEEN_VALUE 900 -#define KING_VALUE 20000 +/* default values for opening, midgame, endgame + */ +#define E_VAL_OPN 0 /* empty */ +#define P_VAL_OPN 100 +#define N_VAL_OPN 300 +#define B_VAL_OPN 300 +#define R_VAL_OPN 500 +#define Q_VAL_OPN 900 +#define K_VAL_OPN 20000 + +#define E_VAL_MID 0 +#define P_VAL_MID 100 +#define N_VAL_MID 300 +#define B_VAL_MID 300 +#define R_VAL_MID 500 +#define Q_VAL_MID 900 +#define K_VAL_MID 20000 + +#define E_VAL_END 0 +#define P_VAL_END 100 +#define N_VAL_END 300 +#define B_VAL_END 300 +#define R_VAL_END 500 +#define Q_VAL_END 900 +#define K_VAL_END 20000 /* typedef struct piece_list_s { @@ -43,24 +60,20 @@ typedef struct piece_list_s { /* some default values for pieces */ extern const struct piece_details { - char abbrev_w; /* used for game notation */ - char abbrev_b; - char *symbol_w; - char *symbol_b; /* used for game notation */ + char abbrev; /* used for game notation */ + //char abbrev_b; + char *symbol; + //char *symbol_b; /* used for game notation */ char *name; s64 opn_value; s64 mid_value; s64 end_value; } piece_details[]; +#define P_ABBR(p) piece_details[PIECE(p)].abbrev +#define P_SYM(p) piece_details[PIECE(p)].symbol #define P_NAME(p) piece_details[PIECE(p)].name -#define P_LETTER(p) piece_details[PIECE(p)].abbrev_w -#define P_SYM(p) piece_details[PIECE(p)].symbol_b -#define P_CSHORT(p) (IS_WHITE(p)? piece_details[PIECE(p)].abbrev_w: \ - piece_details[PIECE(p)].abbrev_b) -#define P_CSYM(p) (IS_WHITE(p)? piece_details[PIECE(p)].symbol_w: \ - piece_details[PIECE(p)].symbol_b) -#define P_VALUE(p) (piece_details[PIECE(p)].value) +#define P_VAL(p) piece_details[PIECE(p)].opn_value /* use short name or symbol - no effect */ diff --git a/src/position.c b/src/position.c index 61f6881..8e48e89 100644 --- a/src/position.c +++ b/src/position.c @@ -70,7 +70,7 @@ inline void bitboard_print2_raw(bitboard_t bb1, bitboard_t bb2, char *title) * pos_pieces_print() - Print position pieces * @pos: &position */ -void pos_pieces_print(position *pos) +void pos_pieces_print(pos_t *pos) { int bit, count, cur; char pname; @@ -101,6 +101,25 @@ void pos_pieces_print(position *pos) } } +/** + * raw_bitboard_print - print simple bitboard representation + * @bb: the bitboard + * @tit: a string or NULL + */ +void raw_bitboard_printc(const bitboard bb, const char *tit, piece_t p) +{ + if (tit) + printf("%s\n", tit); + for (rank_t r = RANK_8; r >= RANK_1; --r) { + printf("%d ", r); + for (file_t f = FILE_A; f <= FILE_H; ++f) + printf(" %c", bb & (BB(f, r)) ? p: '.'); + printf(" A B C D E F G H\n"); + } + printf(" \n"); + return; +} + /** * pos_bitboards_print() - Print position bitboards * @pos: &position @@ -118,10 +137,10 @@ void pos_pieces_print(position *pos) * pos_print() - Print position on stdout. * @pos: &position */ -void pos_print(position *pos) +void pos_print(pos_t *pos) { int rank, file; - piece pc, *board = pos->board; + piece_t pc, *board = pos->board; //piece_list_t *wk = list_first_entry(&pos->pieces[WHITE], piece_list_t, list), // *bk = list_first_entry(&pos->pieces[BLACK], piece_list_t, list); @@ -220,7 +239,7 @@ void pos_print(position *pos) * } */ -position *pos_clear(position *pos) +pos_t *pos_clear(pos_t *pos) { printf("size(pos_board=%lu elt=%lu\n", sizeof(pos->board), sizeof(int)); //for (square square = A1; square <= H8; ++square) @@ -238,8 +257,8 @@ position *pos_clear(position *pos) //pos->eval = 0; //pos->occupied[WHITE] = 0; //pos->occupied[BLACK] = 0; - for (color color = WHITE; color <= BLACK; ++color) { - for (piece_type piece = 0; piece <= KING; ++piece) + for (color_t color = WHITE; color <= BLACK; ++color) { + for (piece_type_t piece = 0; piece <= KING; ++piece) pos->bb[color][piece] = 0; pos->controlled[WHITE] = 0; pos->controlled[BLACK] = 0; @@ -271,9 +290,9 @@ position *pos_clear(position *pos) */ -position *pos_new(void) +pos_t *pos_new(void) { - position *pos = safe_malloc(sizeof(position)); + pos_t *pos = safe_malloc(sizeof(pos_t)); //assert(pos); return pos_clear(pos); } @@ -295,9 +314,9 @@ position *pos_new(void) * * TODO: merge with pos_new - NULL for init, non null for duplicate */ -position *pos_dup(position *pos) +pos_t *pos_dup(pos_t *pos) { - position *newpos= pool_get(pos_pool); + pos_t *newpos= pool_get(pos_pool); if (newpos) { //board = new->board; @@ -315,7 +334,7 @@ position *pos_dup(position *pos) pool_t *pos_pool_init() { if (!pos_pool) - pos_pool = pool_create("positions", 128, sizeof(position)); + pos_pool = pool_create("positions", 128, sizeof(pos_t)); return pos_pool; } diff --git a/src/position.h b/src/position.h index c803e60..4090575 100644 --- a/src/position.h +++ b/src/position.h @@ -30,8 +30,8 @@ typedef struct { int turn; /* WHITE or BLACK */ u16 clock_50; u16 plycount; /* plies so far, start is 0 */ - square en_passant; - castle castle; + square_t en_passant; + castle_rights_t castle; //eval_t eval; //int check[2]; @@ -46,8 +46,8 @@ typedef struct { //u16 mobility[2]; //struct list_head pieces[2]; /* pieces list, King is first */ //struct list_head moves[2]; - piece board[BOARDSIZE]; -} position; + piece_t board[BOARDSIZE]; +} pos_t; /** * pos_set_sq - unconditionally set a piece on a square @@ -57,10 +57,10 @@ typedef struct { * * Both position bords and bitboards are modified. */ -static inline void pos_set_sq(position *pos, piece_type p, color c, file f, rank r) +static inline void pos_set_sq(pos_t *pos, piece_type_t p, color_t c, file_t f, rank_t r) { - piece piece = MAKE_PIECE(p, c); - square square = BB(f, r); + piece_t piece = MAKE_PIECE(p, c); + square_t square = BB(f, r); pos->board[square] = piece; pos->bb[c][ALL_PIECES] |= 1 << square; pos->bb[c][p] |= 1 << square; @@ -73,11 +73,11 @@ static inline void pos_set_sq(position *pos, piece_type p, color c, file f, rank * * Both position bords and bitboards are modified. */ -static inline void pos_clr_sq(position *pos, file f, rank r) +static inline void pos_clr_sq(pos_t *pos, file_t f, rank_t r) { - square square = BB(f, r); - piece_type piece = PIECE(pos->board[square]); - color color = COLOR(pos->board[square]); + square_t square = BB(f, r); + piece_type_t piece = PIECE(pos->board[square]); + color_t color = COLOR(pos->board[square]); pos->board[square] = EMPTY; pos->bb[color][piece] &= ~(1 << square); pos->bb[color][ALL_PIECES] &= ~(1 << square); @@ -85,16 +85,19 @@ static inline void pos_clr_sq(position *pos, file f, rank r) //void bitboard_print(bitboard_t bb, char *title); //void bitboard_print2(bitboard_t bb1, bitboard_t bb2, char *title); -void pos_pieces_print(position *pos); + +void raw_bitboard_print(const bitboard bitboard, const char *title); +void pos_pieces_print(pos_t *pos); + //void pos_bitboards_print(pos_t *pos); -void pos_print(position *pos); -position *pos_clear(position *pos); +void pos_print(pos_t *pos); +pos_t *pos_clear(pos_t *pos); //void pos_del(position *pos); -position *pos_startpos(position *pos); -position *pos_new(); +pos_t *pos_startpos(pos_t *pos); +pos_t *pos_new(); pool_t *pos_pool_init(); void pos_pool_stats(); -position *pos_dup(position *pos); +pos_t *pos_dup(pos_t *pos); //void pos_check(position *pos); #endif /* POSITION_H */ diff --git a/src/util.c b/src/util.c index a0eaaf4..6eb3ea4 100644 --- a/src/util.c +++ b/src/util.c @@ -17,25 +17,6 @@ #include "util.h" #include "bitboard.h" -/** - * bitboard_print_simple - print simple bitboard representation - * @bitboard: the bitboard - * @title: a string or NULL - */ -void raw_bitboard_print(const bitboard bitboard, const char *title) -{ - if (title) - printf("%s\n", title); - for (rank r = RANK_8; r >= RANK_1; --r) { - printf("%d ", r); - for (file f = FILE_A; f <= FILE_H; ++f) - printf(" %c", bitboard & (BB(f, r)) ? 'X': '.'); - printf(" A B C D E F G H\n"); - } - printf(" \n"); - return; -} - /*int main() { char *foo = safe_malloc(1000); diff --git a/src/util.h b/src/util.h index 50aca72..2ba7edb 100644 --- a/src/util.h +++ b/src/util.h @@ -21,40 +21,19 @@ #include "chessdefs.h" -/* -#define bug_on(expr) do { \ - if (unlikely(expr)) { \ - fprintf(stderr, \ - "** BUG IN %s[%s:%d]: assertion \"" #expr "\" failed.\n", \ - __func__, __FILE__,__LINE__); \ - abort(); \ - } \ - } while (0) - -#define warn_on(expr) ({ \ - int _ret = !!(expr); \ - if (unlikely(_ret)) { \ - fprintf(stderr, \ - "** WARN ON %s[%s:%d]: assertion \"" #expr "\" failed.\n", \ - __func__, __FILE__,__LINE__); \ - } \ - _ret; \ - }) -*/ #undef safe_malloc +#undef safe_free + + #define safe_malloc(size) ({ \ void *_ret = malloc(size); \ bug_on(_ret == NULL); \ _ret; \ }) -#undef safe_free #define safe_free(ptr) do { \ bug_on(ptr == NULL); \ free(_ret); \ } while (0) - -void raw_bitboard_print(const bitboard bitboard, const char *title); - #endif /* UTIL_H */ diff --git a/test/fen-test.c b/test/fen-test.c index 051ce24..c6f85b0 100644 --- a/test/fen-test.c +++ b/test/fen-test.c @@ -6,7 +6,7 @@ int main(int ac, char**av) { - position *pos; + pos_t *pos; debug_init(5, stderr, true);