From 1929d4bb1f687b45c4504ac88528708865f080f5 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 7 Feb 2024 22:08:24 +0100 Subject: [PATCH] bb migration: add util, update fen/fen-test + partial pos + piece --- Makefile | 15 +++ brlib | 2 +- src/bitboard.h | 64 +++++---- src/board.h | 71 ++++------ src/chessdefs.h | 136 +++++++++---------- src/fen.c | 344 ++++++++++++++++++++++++++++++----------------- src/fen.h | 10 +- src/piece.c | 204 ++++++++++++++-------------- src/piece.h | 51 +++---- src/position.c | 347 ++++++++++++++++++++++++------------------------ src/position.h | 104 ++++++++++----- src/util.c | 44 ++++++ src/util.h | 60 +++++++++ test/fen-test.c | 10 +- util.c | 0 15 files changed, 860 insertions(+), 602 deletions(-) create mode 100644 src/util.c create mode 100644 src/util.h create mode 100644 util.c diff --git a/Makefile b/Makefile index 06b2bfb..9eb16ae 100644 --- a/Makefile +++ b/Makefile @@ -48,6 +48,8 @@ LIBS := -l$(LIB) -lreadline -lncurses ##################################### pre-processor flags CPPFLAGS := -I$(BRINCDIR) +CPPFLAGS += -DBUG_ON +CPPFLAGS += -DWARN_ON #CPPFLAGS += -DDEBUG # global CPPFLAGS += -DDEBUG_DEBUG # enable log() functions #CPPFLAGS += -DDEBUG_DEBUG_C # enable verbose log() settings @@ -252,6 +254,16 @@ VALGRINDFLAGS += --suppressions=etc/libreadline.supp memcheck: targets @$(VALGRIND) $(VALGRINDFLAGS) $(BINDIR)/brchess +##################################### test binaries +TEST = bin/fen-test +FENTESTOBJS = obj/fen.o obj/position.o obj/piece.o obj/util.o + +testing: $(TEST) + +bin/fen-test: test/fen-test.c $(FENTESTOBJS) + $(CC) $(LDFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(LIBS) $(FENTESTOBJS) -o $@ + + ##################################### Makefile debug .PHONY: showflags wft @@ -271,3 +283,6 @@ wtf: @#echo LIBOBJ=$(LIBOBJ) @#echo DEP=$(DEP) @#echo LIBSRC=$(LIBSRC) + +zob: + $(CC) $(LDFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(LIBS) src/util.c -o util diff --git a/brlib b/brlib index 635e4c8..8cb08da 160000 --- a/brlib +++ b/brlib @@ -1 +1 @@ -Subproject commit 635e4c8d9ee18d982c97f34b5417d3d7113010d8 +Subproject commit 8cb08dacb3d02e5b6d417f4c58a3783ac72aab03 diff --git a/src/bitboard.h b/src/bitboard.h index 1533b93..1e4e8b2 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -11,18 +11,15 @@ * */ -#ifndef BITBOARD_H -#define BITBOARD_H +#ifndef _BITBOARD_H +#define _BITBOARD_H -#include "br.h" +#include "brlib.h" #include "chessdefs.h" #include "piece.h" #include "bitops.h" -#define mask(s) ( 1ULL << (s) ) -#define C64(const_u64) const_u64##ULL - -typedef enum square { +typedef enum { A1, B1, C1, D1, E1, F1, G1, H1, A2, B2, C2, D2, E2, F2, G2, H2, A3, B3, C3, D3, E3, F3, G3, H3, @@ -31,23 +28,21 @@ typedef enum square { A6, B6, C6, D6, E6, F6, G6, H6, A7, B7, C7, D7, E7, F7, G7, H7, A8, B8, C8, D8, E8, F8, G8, H8, - SQ_N, - SQ_0 = 0 + SQUARE_MAX = 64, + SQUARE_NONE = 64 } square; -typedef enum file { +typedef enum { FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, - FILE_N, - FILE_0 = 0 + FILE_MAX, } file; -typedef enum rank { +typedef enum { RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, - RANK_N, - RANK_0 = 0 + RANK_MAX, } rank; -typedef enum sq_bb { +typedef enum { //A1 = 0x01ULL, B1 = 0x02ULL, C1 = 1UL << 2, D1 = 1UL << 3, //E1 = 1UL << 4, F1 = 1UL << 5, G1 = 1UL << 6, H1 = 1UL << A1bb = mask(A1), A2bb = mask(A2), A3bb = mask(A3), A4bb = mask(A4), @@ -68,7 +63,7 @@ typedef enum sq_bb { H5bb = mask(H5), H6bb = mask(H6), H7bb = mask(H7), H8bb = mask(H8), } sq_bb; -typedef enum file_bb { +typedef enum { FILE_Abb = 0x0101010101010101ULL, FILE_Bbb = 0x0202020202020202ULL, FILE_Cbb = 0x0404040404040404ULL, @@ -79,7 +74,7 @@ typedef enum file_bb { FILE_Hbb = 0x8080808080808080ULL, } file_bb; -typedef enum rank_bb { +typedef enum { RANK_1bb = 0x00000000000000ffULL, RANK_2bb = 0x000000000000ff00ULL, RANK_3bb = 0x0000000000ff0000ULL, @@ -90,16 +85,31 @@ typedef enum rank_bb { RANK_8bb = 0xff00000000000000ULL } rank_bb; -#define NORTH 8 -#define EAST 1 -#define SOUTH -NORTH -#define WEST -EAST +typedef enum { + NORTH = 8, + EAST = 1, + SOUTH = -NORTH, + WEST = -EAST, -#define NORTH_EAST (NORTH + EAST) -#define SOUTH_EAST (SOUTH + EAST) -#define SOUTH_WEST (SOUTH + WEST) -#define NORTH_WEST (NORTH + WEST) + NORTH_EAST = (NORTH + EAST), + SOUTH_EAST = (SOUTH + EAST), + SOUTH_WEST = (SOUTH + WEST), + NORTH_WEST = (NORTH + WEST), +} direction; + +static inline square BB(file file, rank rank) +{ + return (rank << 3) + file; +} +static inline file BBfile(square square) +{ + return square & 7; +} +static inline rank BBrank(square square) +{ + return square >> 3; +} void bitboard_init(void); -#endif /* BITBOARD_H */ +#endif /* _BITBOARD_H */ diff --git a/src/board.h b/src/board.h index 6543eb2..03cd28c 100644 --- a/src/board.h +++ b/src/board.h @@ -14,67 +14,44 @@ #ifndef BOARD_H #define BOARD_H -#include -#include "chessdefs.h" -#include "piece.h" +#include "brlib.h" -typedef struct board_s { - piece_t piece; - piece_list_t *s_piece; - //struct list_head *s_piece; -} board_t; /* 0x88 board */ -#define BOARDSIZE (8*8*2) +#include "chessdefs.h" +//#include "piece.h" /* definitions for 0x88 representation */ -#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 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 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)) +//#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */ +//#define SQ88_OK(s) (!SQ88_NOK(s)) /* definitions for bitboard representation */ -#define BB(f, r) (1ULL << (8 * (r) + (f))) /* from rank,file 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 */ +//#define BB(f, r) (1ULL << (8 * (r) + (f))) /* from rank,file to bitboard */ -#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */ -#define SQ88_OK(s) (!SQ88_NOK(s)) -/* piece human notation - */ -#define CHAR_EMPTY ' ' -#define CHAR_PAWN 'P' -#define CHAR_KNIGHT 'N' -#define CHAR_BISHOP 'B' -#define CHAR_ROOK 'R' -#define CHAR_QUEEN 'Q' -#define CHAR_KING 'K' +//#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 */ + +//#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */ +//#define SQ88_OK(s) (!SQ88_NOK(s)) /* from human to machine */ -#define C2FILE(c) (tolower(c) - 'a') -#define C2RANK(c) (tolower(c) - '1') -/* from machine to human +/* + * #define C2FILE(c) (tolower(c) - 'a') + * #define C2RANK(c) (tolower(c) - '1') + * /\* from machine to human + * *\/ + * #define FILE2C(f) ((f) + 'a') + * #define RANK2C(r) ((r) + '1') */ -#define FILE2C(f) ((f) + 'a') -#define RANK2C(r) ((r) + '1') - -enum x88_square { - x88_A1=0x00, x88_B1, x88_C1, x88_D1, x88_E1, x88_F1, x88_G1, x88_H1, - x88_A2=0x10, x88_B2, x88_C2, x88_D2, x88_E2, x88_F2, x88_G2, x88_H2, - x88_A3=0x20, x88_B3, x88_C3, x88_D3, x88_E3, x88_F3, x88_G3, x88_H3, - x88_A4=0x30, x88_B4, x88_C4, x88_D4, x88_E4, x88_F4, x88_G4, x88_H4, - x88_A5=0x40, x88_B5, x88_C5, x88_D5, x88_E5, x88_F5, x88_G5, x88_H5, - x88_A6=0x50, x88_B6, x88_C6, x88_D6, x88_E6, x88_F6, x88_G6, x88_H6, - x88_A7=0x60, x88_B7, x88_C7, x88_D7, x88_E7, x88_F7, x88_G7, x88_H7, - x88_A8=0x70, x88_B8, x88_C8, x88_D8, x88_E8, x88_F8, x88_G8, x88_H8, -}; #endif /* BOARD_H */ diff --git a/src/chessdefs.h b/src/chessdefs.h index 889520e..d03b9bc 100644 --- a/src/chessdefs.h +++ b/src/chessdefs.h @@ -11,74 +11,54 @@ * */ -#ifndef CHESSDEFS_H -#define CHESSDEFS_H +#ifndef _CHESSDEFS_H +#define _CHESSDEFS_H -#include "br.h" +#include "brlib.h" /* brlib types */ + +#define mask(i) ( 1ULL << (i) ) +#define C64(const_u64) const_u64##ULL +#define U64(const_s64) const_s64##LL /* piece_t bits structure - * MSB 8 7 6 5 4 3 2 1 LSB - * 1: color (0 for white) - * 2-7: bit set for pawn (2), knight, bishop, rook, queen, king (7) + * piece is on bits 1-3, color on bit 4: + * .... CPPP + * C: 0 for white, 1: black + * PPP: pawn (1), knight, bishop, rook, queen, king (6) */ -typedef u8 piece_t; +typedef enum { + WHITE, BLACK, + NCOLOR_MAX +} color; -enum { - E_EMPTY = 0, - E_COLOR, /* LSB */ - E_PAWN, - E_KNIGHT, - E_BISHOP, - E_ROOK, - E_QUEEN, - E_KING, -}; +typedef enum { + ALL_PIECES = 0, /* 'all pieces' bitboard */ + PAWN = 1, KNIGHT, BISHOP, ROOK, QUEEN, KING, + PIECE_TYPE_MAX = 7 /* bit 4 */ +} piece_type; -/* pos_t bitboards tables - */ -enum { - BB_ALL = 0, /* OR of all bitboards */ - BB_UNUSED, /* future use ? */ - BB_PAWN = E_PAWN, - BB_KNIGHT, - BB_BISHOP, - BB_ROOK, - BB_QUEEN, - BB_KING, - BB_END -}; +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 bitmask in piece_t - */ -enum { - EMPTY = 0, - 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 */ - QUEEN = 1 << (E_QUEEN - 1), /* 0x20 00100000 */ - KING = 1 << (E_KING - 1), /* 0x40 01000000 */ -}; +#define OPPONENT(p) !(p) -#define PIECETOBB(p) (ffs64(PIECE(p))) /* from piece_t to bb piece array */ +#define MASK_PIECE 0x07 /* 00000111 */ +#define MASK_COLOR 0x08 /* 00001000 */ -#define WHITE 0 /* 0x00 00000000 */ -#define BLACK 1 /* 0x01 00000001 */ -#define OPPONENT(p) !(p) +#define COLOR(p) ((p) >> 3) /* bitmask */ +#define PIECE(p) ((p) & MASK_PIECE) +#define MAKE_PIECE(p, c) ((p) | (c) << 3) -#define MASK_COLOR 0x01 /* 00000001 */ -#define MASK_PIECE 0x7E /* 01111110 */ +#define IS_WHITE(p) (!COLOR(p)) +#define IS_BLACK(p) (COLOR(p)) -#define COLOR(p) ((p) & MASK_COLOR) /* bitmask */ -#define PIECE(p) ((p) & MASK_PIECE) -#define E_PIECE(p) (ffs64(PIECE(p))) /* convert mask to E_XX */ - -#define IS_WHITE(p) (!COLOR(p)) -#define IS_BLACK(p) (COLOR(p)) - -#define SET_WHITE(p) ((p) &= ~MASK_COLOR) -#define SET_BLACK(p) ((p) |= MASK_COLOR) -#define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p)) +#define SET_WHITE(p) ((p) &= ~MASK_COLOR) +#define SET_BLACK(p) ((p) |= MASK_COLOR) +#define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p)) /* flip a 0-63 square: * Vertical: G8 (62) becomes G1 (6) @@ -86,20 +66,25 @@ enum { */ #define FLIP_V(sq) ((sq) ^ 56) #define FLIP_H(sq) ((sq) ^ 7) -/* square_t bits structure : rrrrffff - * ffff: file - * rrrr: rank - */ -typedef uchar square_t; + +typedef u64 bitboard; +typedef ushort board; +#define BOARDSIZE (8*8) +/* from human to machine */ +#define C2FILE(c) (tolower(c) - 'a') +#define C2RANK(c) (tolower(c) - '1') +/* from machine to human */ +#define FILE2C(f) ((f) + 'a') +#define RANK2C(r) ((r) + '1') /* castle_t bits structure */ -typedef unsigned char castle_t; - -#define CASTLE_WK (1 << 0) /* 0x01 00000001 */ -#define CASTLE_WQ (1 << 1) /* 0x02 00000010 */ -#define CASTLE_BK (1 << 2) /* 0x04 00000100 */ -#define CASTLE_BQ (1 << 3) /* 0x08 00001000 */ +typedef enum { + CASTLE_WK = (1 << 0), /* 0x01 00000001 */ + CASTLE_WQ = (1 << 1), /* 0x02 00000010 */ + CASTLE_BK = (1 << 2), /* 0x04 00000100 */ + CASTLE_BQ = (1 << 3), /* 0x08 00001000 */ +} castle; #define CASTLE_W (CASTLE_WK | CASTLE_WQ) /* 00000011 W castle mask */ #define CASTLE_B (CASTLE_BK | CASTLE_BQ) /* 00001100 B castle mask */ @@ -112,18 +97,17 @@ typedef unsigned char castle_t; /* bitboard */ -typedef u64 bitboard_t; - +//typedef u64 bitboard_t; /* eval type */ -typedef s32 eval_t; +//typedef s32 eval_t; /* forward typedefs */ -typedef struct piece_list_s piece_list_t; -typedef struct board_s board_t; -typedef struct pos_s pos_t; -typedef struct move_s move_t; +//typedef struct piece_list_s piece_list_t; +//typedef struct board_s board_t; +//typedef struct pos_s pos_t; +//typedef struct move_s move_t; -#endif +#endif /* _CHESSDEFS_H */ diff --git a/src/fen.c b/src/fen.c index 86d3206..c9ccf15 100644 --- a/src/fen.c +++ b/src/fen.c @@ -1,6 +1,6 @@ /* fen.c - fen notation. * - * Copyright (C) 2021 Bruno Raoult ("br") + * Copyright (C) 2021-2024 Bruno Raoult ("br") * Licensed under the GNU General Public License v3.0 or later. * Some rights reserved. See COPYING. * @@ -18,158 +18,264 @@ #include #include +#include #include "chessdefs.h" #include "position.h" -#include "board.h" #include "fen.h" #include "piece.h" +#include "util.h" -/* Starting Position : - * rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 - * After 1.e4 : - * rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1 - * After 1... c5 : - * rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2 - * After 2. Nf3: - * rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2 - * - * 1 : White uppercase +/* FEN description: + * 1 : pieces on board (no space allowed): + * - rank 8 first, '/' between ranks + * - piece is usual piece notation(PNBRQK), black lowercase. + * - empty: number of consecutive empty squares (digit) * 2 : next move (w or b) * 3 : Castling capabilities: "-" if none, KQ/kq if white/black can castle * on K or Q side - * 4 : en-passant: if pawn just moved 2 squares, indicate target square (e.g. - * for e2-e4 this field is e3) + * 4 : en-passant: "-" if none. If pawn just moved 2 squares, indicate target + * en-passant square (e.g. for e2-e4 this field is e3) * 5 : half moves since last capture or pawn advance (for 50 moves rule) * 6 : full moves, starts at 1, increments after black move * + * Examples: + * + * starting position: + * rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 + * after 1.e4 e6 2.e5 d5 + * rnbqkbnr/ppp2ppp/4p3/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 3 + * 3.Nc3 Nc6 4.Rb1 Rb8 5.Nf3 h5 6.Be2 + * 1rbqkbnr/ppp2pp1/2n1p3/3pP2p/8/2N2N2/PPPPBPPP/1RBQK2R b Kk - 1 6 + * 6...Be7 + * 1rbqk1nr/ppp1bpp1/2n1p3/3pP2p/8/2N2N2/PPPPBPPP/1RBQK2R w Kk - 2 7 + * 7.Nxd5 h4 8.g4 + * 1rbqk1nr/ppp1bpp1/2n1p3/3NP3/6Pp/5N2/PPPPBP1P/1RBQK2R b Kk g3 0 8 */ -// warning, we expect a valid fen input -pos_t *fen2pos(pos_t *pos, char *fen) -{ - char *p = fen; - short rank, file, skip, color, bbpiece; - piece_t piece; - board_t *board = pos->board; -# define SKIP_BLANK(p) for(;*(p) == ' '; (p)++) +/* chess startup position FEN */ +const char *startfen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + +/* next must follow 'piece_type' enum values (pawn is 1, king is 6) */ +static const char *pieces_str = " PNBRQK"; +/* And this one must follow 'castle' enum order (also same as usual FEN) */ +static const char *castle_str = "KQkq"; + +#define SKIP_BLANK(p) for(;isspace(*(p)); (p)++) + +/** + * startpos - create a game start position + * @pos: a position pointer or NULL + * + * See @fen2pos function. + * + * @return: the pos position. + */ +position *startpos(position *pos) +{ + return fen2pos(pos, startfen); +} + +/** + * fen2pos - make a position from a fen string + * @pos: a position pointer or NULL + * @fen: a valid fen string + * + * If @pos is NULL, a position will be allocated with malloc(1), + * that should be freed by caller. + * + * @return: the pos position, or NULL if error. + */ +position *fen2pos(position *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_clear(&tmppos); - pos_clear(pos); /* 1) get piece placement information */ - for (rank = 7, file = 0; *p && *p != ' '; ++p) { - color = isupper(*p)? WHITE: BLACK; - char cp = toupper(*p); - switch (cp) { - case CHAR_PAWN: - bbpiece = BB_PAWN; - piece = PAWN; - goto set_square; - case CHAR_KNIGHT: - bbpiece = BB_KNIGHT; - piece = KNIGHT; - goto set_square; - case CHAR_BISHOP: - bbpiece = BB_BISHOP; - piece = BISHOP; - goto set_square; - case CHAR_ROOK: - bbpiece = BB_ROOK; - piece = ROOK; - goto set_square; - case CHAR_QUEEN: - bbpiece = BB_QUEEN; - piece = QUEEN; - goto set_square; - case CHAR_KING: - bbpiece = BB_KING; - piece = KING; - //pos->bb[color][BB_KING] = BB(file, rank); - //goto set_square; - set_square: -# ifdef DEBUG_FEN - log_i(5, "f=%d r=%d *p=%c piece=%c color=%d\n", - file, rank, *p, cp, color); -# endif - pos->bb[color][bbpiece] |= BB(file, rank); - pos->occupied[color] |= BB(file, rank); - SET_COLOR(piece, color); - board[SQ88(file, rank)].piece = piece; - board[SQ88(file, rank)].s_piece = - piece_add(pos, piece, SQ88(file, rank)); - file++; - break; - case '/': - rank--; - file = 0; - break; - default: - skip = cp - '0'; - while (skip--) { - board[SQ88(file++, rank)].piece = EMPTY; - } + for (rank = 7, file = 0; *cur && !isspace(*cur); ++cur) { + if (*cur == '/') { /* next rank */ + rank--; + file = 0; + continue; + } + if (isdigit(*cur)) { /* empty square(s) */ + file += *cur - '0'; + continue; + } + color = isupper(*cur)? WHITE: BLACK; + if ((p = strchr(pieces_str, toupper(*cur)))) { /* valid piece letter */ +# ifdef DEBUG_FEN + log_i(5, "f=%d r=%d *p=%c piece=%c color=%d ppos=%ld\n", + file, rank, *cur, *p, color, p - pieces_str); +# endif + pos_set_sq(&tmppos, p - pieces_str, color, file, rank); + file++; + } else { /* error */ + err_line = __LINE__, err_char = *cur, err_pos = cur - fen; + goto end; } } -# ifdef DEBUG_FEN - for (rank = 7; rank >= 0; --rank) { - for (file = 0; file < 8; ++file) { - log(5, "%02x ", board[SQ88(file, rank)].piece); - } - log(5, "\n"); - } -# endif + SKIP_BLANK(cur); - /* 2) next move color + /* 2) next turn color */ - SKIP_BLANK(p); - SET_COLOR(pos->turn, *p == 'w' ? WHITE : BLACK); - p++; + tmppos.turn = *cur++ == 'w' ? WHITE : BLACK; + SKIP_BLANK(cur); - /* 3) castle status + /* 3) castle rights */ - SKIP_BLANK(p); - pos->castle = 0; - if (*p != '-') { - for (; *p && *p != ' '; ++p) { - switch (*p) { - case 'K': - pos->castle |= CASTLE_WK; - break; - case 'k': - pos->castle |= CASTLE_BK; - break; - case 'Q': - pos->castle |= CASTLE_WQ; - break; - case 'q': - pos->castle |= CASTLE_BQ; - break; + if (*cur == '-') { + cur++; + } else { + for (; *cur && !isspace(*cur); ++cur) { + if ((p = strchr(castle_str, *cur))) { /* valid castle letter */ + tmppos.castle |= 1 << (p - castle_str); + } else { + err_line = __LINE__, err_char = *cur, err_pos = cur - fen; + goto end; } } } - p++; + SKIP_BLANK(cur); /* 4) en passant */ - SKIP_BLANK(p); - pos->en_passant = 0; - if (*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; + tmppos.en_passant = 0; + if (*cur == '-') { + cur++; } else { - p++; + tmppos.en_passant = BB(C2FILE(*cur), C2RANK(*(cur+1))); + cur += 2; } + SKIP_BLANK(cur); - /* 5) half moves since last capture or pawn move and - * 6) current move number + /* 5) half moves since last capture or pawn move (50 moves rule) */ - SKIP_BLANK(p); - //log_i(5, "pos=%d\n", (int)(p-fen)); - sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove); + sscanf(cur, "%hd%n", &tmppos.clock_50, &consumed); + cur += consumed; + SKIP_BLANK(cur); + + /* 6) current full move number, starting with 1 + */ + printf ("remain=[%s]\n", cur); + sscanf(cur, "%hd", &tmp); + printf ("tmp=[%d]\n", tmp); + if (tmp <= 0) /* fix faulty numbers*/ + tmp = 1; + printf ("tmp2=[%d]\n", tmp); + tmp = 2 * (tmp - 1) + (tmppos.turn == BLACK); /* plies, +1 if black turn */ + printf ("tmp3=[%d]\n", tmp); + + tmppos.plycount = tmp; + # ifdef DEBUG_FEN - log_i(5, "50 rule=%d current move=%d\n", pos->clock_50, pos->curmove); + for (rank = 7; rank >= 0; --rank) { + for (file = 0; file < 8; ++file) { + log(5, "%02x ", tmppos.board[BB(file, rank)]); + } + log(5, "\n"); + } + log(5, "turn=%d 50_rule=%d curply=%d\n", + tmppos.turn, tmppos.clock_50, tmppos.plycount); # endif + +end: + if (warn(err_line, "FEN error line %d: pos=%d char=%#x(%c)\n", + err_line, err_pos, err_char, err_char)) { + return NULL; + } + if (!pos) + pos = pos_new(); + else + pos_clear(pos); + *pos = tmppos; return pos; } + +/** + * pos2fen - make a FEN string from a position. + * @pos: a position pointer + * @fen: destination FEN char*, or NULL + * + * If @fen is NULL, a 100 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 + * be on secure side, it should be at least 90 bytes. See: + * https://chess.stackexchange.com/questions/30004/longest-possible-fen + * For convenience, use FENSTRLEN. + * + * @return: the pos position, or NULL if error. + */ +char *pos2fen(const position *pos, char *fen) +{ + int cur = 0; + + if (!fen) + fen = safe_malloc(92); + + /* 1) position + */ + for (int rank = RANK_8; rank >= RANK_1; --rank) { + printf("r=%d 1=%d\n", rank, RANK_1); + for (int file = FILE_A; file <= FILE_H;) { + printf(" f=%d H=%d\n", file, FILE_H); + square sq = BB(file, rank); + printf(" sq=%d\n", sq); + piece piece = PIECE(pos->board[sq]); + color color = COLOR(pos->board[sq]); + if (pos->board[sq] == EMPTY) { + int len = 0; + for (; file <= FILE_H && pos->board[BB(file,rank)] == EMPTY; file++) + len++; + fen[cur++] = '0' + len; + } else { + char c = pieces_str[piece]; + fen[cur++] = color == WHITE? c: tolower(c); + file++; + } + fen[cur]=0; puts(fen); + } + fen[cur++] = rank == RANK_1? ' ': '/'; + } + + /* 2) next turn color + */ + fen[cur++] = pos->turn == WHITE? 'w': 'b'; + fen[cur++] = ' '; + + /* 3) castle rights + */ + 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]; + } + fen[cur++] = ' '; + + /* 4) en passant + */ + if (!pos->en_passant) { + fen[cur++] = '-'; + } else { + fen[cur++] = FILE2C(BBfile(pos->en_passant)); + fen[cur++] = RANK2C(BBrank(pos->en_passant)); + } + fen[cur++] = ' '; + + /* 5) moves since last capture or pawn move (50 moves rule) + * 6) current full move number, starting with 1 + */ + sprintf(fen+cur, "%d %d", pos->clock_50, + 1 + (pos->plycount - (pos->turn == BLACK)) / 2); + return fen; +} diff --git a/src/fen.h b/src/fen.h index a57b5a7..7a626eb 100644 --- a/src/fen.h +++ b/src/fen.h @@ -1,6 +1,6 @@ /* fen.h - fen notation. * - * Copyright (C) 2021 Bruno Raoult ("br") + * Copyright (C) 2021-2024 Bruno Raoult ("br") * Licensed under the GNU General Public License v3.0 or later. * Some rights reserved. See COPYING. * @@ -16,6 +16,12 @@ #include "position.h" -pos_t *fen2pos(pos_t *pos, char *fen); +#define FENSTRLEN 92 /* secure FEN string size */ + +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); #endif /* FEN_H */ diff --git a/src/piece.c b/src/piece.c index b6e4eeb..5024f48 100644 --- a/src/piece.c +++ b/src/piece.c @@ -1,6 +1,6 @@ /* piece.c - piece list management. * - * Copyright (C) 2021 Bruno Raoult ("br") + * Copyright (C) 2021-2024 Bruno Raoult ("br") * Licensed under the GNU General Public License v3.0 or later. * Some rights reserved. See COPYING. * @@ -13,106 +13,118 @@ #include #include +#include +#include -#include -#include -#include +//#include +//#include +//#include #include "chessdefs.h" #include "piece.h" -#include "board.h" -#include "bitboard.h" -#include "position.h" +//#include "board.h" +//#include "bitboard.h" +//#include "position.h" -static pool_t *pieces_pool; +//static pool_t *pieces_pool; -struct piece_details piece_details[] = { - [E_EMPTY] = { ' ', ' ', " ", " ", "", 0 }, - [E_PAWN] = { 'P', 'p', "♙", "♟", "Pawn", PAWN_VALUE }, - [E_KNIGHT] = { 'N', 'n', "♘", "♞", "Knight", KNIGHT_VALUE }, - [E_BISHOP] = { 'B', 'b', "♗", "♝", "Bishop", BISHOP_VALUE }, - [E_ROOK] = { 'R', 'r', "♖", "♜", "Rook", ROOK_VALUE }, - [E_QUEEN] = { 'Q', 'q', "♕", "♛", "Queen", QUEEN_VALUE }, - [E_KING] = { 'K', 'k', "♔", "♚", "King", KING_VALUE } +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 } }; -void piece_list_print(struct list_head *list) -{ - struct list_head *p_cur, *tmp; - piece_list_t *piece; - - list_for_each_safe(p_cur, tmp, list) { - piece = list_entry(p_cur, piece_list_t, list); - - printf("%s%c%c ", P_SYM(piece->piece), - FILE2C(F88(piece->square)), - RANK2C(R88(piece->square))); - } - printf("\n"); -} - -pool_t *piece_pool_init() -{ - if (!pieces_pool) - pieces_pool = pool_create("pieces", 128, sizeof(piece_list_t)); - return pieces_pool; -} - -void piece_pool_stats() -{ - if (pieces_pool) - pool_stats(pieces_pool); -} - -piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square) -{ - piece_list_t *new; - short color = COLOR(piece); - -# 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(F88(square)), RANK2C(R88(square))); -# endif - if ((new = pool_get(pieces_pool))) { - /* first piece is always king */ - if (PIECE(piece) == KING) - list_add(&new->list, &pos->pieces[color]); - else - list_add_tail(&new->list, &pos->pieces[color]); - new->piece = piece; - new->square = square; - new->castle = 0; - new-> value = piece_details[PIECE(piece)].value; - } - - return new; -} - -void piece_del(struct list_head *ptr) -{ - piece_list_t *piece = list_entry(ptr, piece_list_t, list); -# ifdef DEBUG_PIECE - log_f(3, "piece=%02x square=%02x\n", piece->piece, piece->square); -# endif - list_del(ptr); - pool_add(pieces_pool, piece); - return; -} - -int pieces_del(pos_t *pos, short color) -{ - struct list_head *p_cur, *tmp, *head; - int count = 0; - - head = &pos->pieces[color]; - - list_for_each_safe(p_cur, tmp, head) { - piece_del(p_cur); - count++; - } -# ifdef DEBUG_PIECE - log_f(3, "color=%d removed=%d\n", color, count); -# endif - return count; -} +/* + * void piece_list_print(struct list_head *list) + * { + * struct list_head *p_cur, *tmp; + * piece_list_t *piece; + * + * list_for_each_safe(p_cur, tmp, list) { + * piece = list_entry(p_cur, piece_list_t, list); + * + * printf("%s%c%c ", P_SYM(piece->piece), + * FILE2C(F88(piece->square)), + * RANK2C(R88(piece->square))); + * } + * printf("\n"); + * } + * + * pool_t *piece_pool_init() + * { + * if (!pieces_pool) + * pieces_pool = pool_create("pieces", 128, sizeof(piece_list_t)); + * return pieces_pool; + * } + * + * void piece_pool_stats() + * { + * if (pieces_pool) + * pool_stats(pieces_pool); + * } + * + * piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square) + * { + * piece_list_t *new; + * short color = COLOR(piece); + * + * # 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(F88(square)), RANK2C(R88(square))); + * # endif + * if ((new = pool_get(pieces_pool))) { + * /\* first piece is always king *\/ + * if (PIECE(piece) == KING) + * list_add(&new->list, &pos->pieces[color]); + * else + * list_add_tail(&new->list, &pos->pieces[color]); + * new->piece = piece; + * new->square = square; + * new->castle = 0; + * new-> value = piece_details[PIECE(piece)].value; + * } + * + * return new; + * } + * + * void piece_del(struct list_head *ptr) + * { + * piece_list_t *piece = list_entry(ptr, piece_list_t, list); + * # ifdef DEBUG_PIECE + * log_f(3, "piece=%02x square=%02x\n", piece->piece, piece->square); + * # endif + * list_del(ptr); + * pool_add(pieces_pool, piece); + * return; + * } + * + * int pieces_del(pos_t *pos, short color) + * { + * struct list_head *p_cur, *tmp, *head; + * int count = 0; + * + * head = &pos->pieces[color]; + * + * list_for_each_safe(p_cur, tmp, head) { + * piece_del(p_cur); + * count++; + * } + * # ifdef DEBUG_PIECE + * log_f(3, "color=%d removed=%d\n", color, count); + * # endif + * return count; + * } + */ diff --git a/src/piece.h b/src/piece.h index 48e3462..da2b366 100644 --- a/src/piece.h +++ b/src/piece.h @@ -1,6 +1,6 @@ /* piece.h - piece definitions. * - * Copyright (C) 2021 Bruno Raoult ("br") + * Copyright (C) 2021-2024 Bruno Raoult ("br") * Licensed under the GNU General Public License v3.0 or later. * Some rights reserved. See COPYING. * @@ -20,16 +20,17 @@ #include "list.h" #include "pool.h" -#define PIECE_DEFAULT_VALUE 0 /* initial default values */ -#define PAWN_VALUE 100 -#define KNIGHT_VALUE 300 -#define BISHOP_VALUE 300 -#define ROOK_VALUE 500 -#define QUEEN_VALUE 900 +#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 +/* typedef struct piece_list_s { piece_t piece; square_t square; @@ -37,35 +38,39 @@ typedef struct piece_list_s { s64 value; struct list_head list; } piece_list_t; +*/ /* some default values for pieces */ -extern struct piece_details { +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 *name; - s64 value; + s64 opn_value; + s64 mid_value; + s64 end_value; } piece_details[]; -#define P_NAME(p) piece_details[E_PIECE(p)].name -#define P_LETTER(p) piece_details[E_PIECE(p)].abbrev_w -#define P_SYM(p) piece_details[E_PIECE(p)].symbol_b -#define P_CSHORT(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].abbrev_w: \ - piece_details[E_PIECE(p)].abbrev_b) -#define P_CSYM(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].symbol_w: \ - piece_details[E_PIECE(p)].symbol_b) -#define P_VALUE(p) (piece_details[E_PIECE(p)].value) +#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) + /* use short name or symbol - no effect */ #define P_USE_UTF 1 -void piece_list_print(struct list_head *list); -pool_t *piece_pool_init(); -void piece_pool_stats(); -piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square); -void piece_del(struct list_head *ptr); -int pieces_del(pos_t *pos, short color); +//void piece_list_print(struct list_head *list); +//pool_t *piece_pool_init(); +//void piece_pool_stats(); +//piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square); +//void piece_del(struct list_head *ptr); +//int pieces_del(pos_t *pos, short color); #endif diff --git a/src/position.c b/src/position.c index 051d174..61f6881 100644 --- a/src/position.c +++ b/src/position.c @@ -1,6 +1,6 @@ /* position.c - position management. * - * Copyright (C) 2021 Bruno Raoult ("br") + * Copyright (C) 2021-2024 Bruno Raoult ("br") * Licensed under the GNU General Public License v3.0 or later. * Some rights reserved. See COPYING. * @@ -16,16 +16,23 @@ #include #include #include +#include + +#include "bitops.h" #include "chessdefs.h" #include "position.h" -#include "move.h" +//#include "move.h" #include "fen.h" #include "piece.h" -#include "eval.h" +#include "util.h" +//#include "eval.h" static pool_t *pos_pool; +const char *rankstr = "12345678"; +const char *filestr = "ABCDEFGH"; + #define BYTE_PRINT "%c%c%c%c%c%c%c%c" #define BYTE2BIN(b) ((b) & 0x01 ? '1' : '0'), \ ((b) & 0x02 ? '1' : '0'), \ @@ -36,88 +43,115 @@ static pool_t *pos_pool; ((b) & 0x40 ? '1' : '0'), \ ((b) & 0x80 ? '1' : '0') -inline void bitboard_print(bitboard_t bb) +/* +inline void bitboard_print_raw(bitboard_t bb, char *title) { int i; - printf("%#018lx\n", bb); + printf("%s%s %#018lx\n", title? title: "", title? " - ": "", bb); for (i=56; i>=0; i-=8) printf("\t"BYTE_PRINT"\n", BYTE2BIN(bb>>i)); } - -inline void bitboard_print2(bitboard_t bb1, bitboard_t bb2) +*/ +/* +inline void bitboard_print2_raw(bitboard_t bb1, bitboard_t bb2, char *title) { int i; + printf("%s%s", title? title: "", title? ":\n": ""); + printf("\tW: %#018lx\tB: %#018lx\n", bb1, bb2); for (i=56; i>=0; i-=8) printf("\t"BYTE_PRINT"\t\t"BYTE_PRINT"\n", BYTE2BIN(bb1>>i), BYTE2BIN(bb2>>i)); } - +*/ /** * pos_pieces_print() - Print position pieces * @pos: &position */ -void pos_pieces_print(pos_t *pos) +void pos_pieces_print(position *pos) { - printf("White pieces (%d): \t", popcount64(pos->occupied[WHITE])); - piece_list_print(&pos->pieces[WHITE]); - printf("Black pieces (%d): \t", popcount64(pos->occupied[BLACK])); - piece_list_print(&pos->pieces[BLACK]); + int bit, count, cur; + char pname; + u64 tmp; + bitboard p; + for (int color = WHITE; color <= BLACK; ++color) { + for (int piece = KING; piece >= PAWN; --piece) { + p = pos->bb[color][piece]; + count = popcount64(p); + cur = 0; + pname = P_LETTER(piece); + printf("%c(0)%s", pname, count? ":": ""); + if (count) { + bit_for_each64(bit, tmp, p) { + char cf = BBfile(bit), cr = BBrank(bit); + printf("%s%c%c", cur? ",": "", FILE2C(cf), RANK2C(cr)); + cur++; + } + + } + printf(" "); + } + printf("\n"); + //printf("White pieces (%d): \t", popcount64(pos->occupied[WHITE])); + //piece_list_print(&pos->pieces[WHITE]); + //printf("Black pieces (%d): \t", popcount64(pos->occupied[BLACK])); + //piece_list_print(&pos->pieces[BLACK]); + } } /** * pos_bitboards_print() - Print position bitboards * @pos: &position */ -void pos_bitboards_print(pos_t *pos) -{ - printf("Bitboards occupied :\n"); - bitboard_print2(pos->occupied[WHITE], pos->occupied[BLACK]); - printf("Bitboards controlled :\n"); - bitboard_print2(pos->controlled[WHITE], pos->controlled[BLACK]); - -} +//void pos_bitboards_print(pos_t *pos) +//{ +// printf("Bitboards occupied :\n"); +// bitboard_print2(pos->occupied[WHITE], pos->occupied[BLACK]); +// printf("Bitboards controlled :\n"); +// bitboard_print2(pos->controlled[WHITE], pos->controlled[BLACK]); +// +//} /** * pos_print() - Print position on stdout. * @pos: &position */ -void pos_print(pos_t *pos) +void pos_print(position *pos) { int rank, file; - piece_t piece; - board_t *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); + piece 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); printf(" +---+---+---+---+---+---+---+---+\n"); for (rank = 7; rank >= 0; --rank) { printf("%c |", rank + '1'); for (file = 0; file < 8; ++file) { - piece = board[SQ88(file, rank)].piece; - printf(" %s |", P_CSYM(piece)); + pc = board[BB(file, rank)]; + printf(" %s |", P_CSYM(pc)); } printf("\n +---+---+---+---+---+---+---+---+\n"); } 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(F88(wk->square)), - RANK2C(R88(wk->square)), - FILE2C(F88(bk->square)), - RANK2C(R88(bk->square))); + /* + * printf("Kings: W:%c%c B:%c%c\n", + * FILE2C(F88(wk->square)), + * RANK2C(R88(wk->square)), + * FILE2C(F88(bk->square)), + * RANK2C(R88(bk->square))); + */ printf("Possible en-passant: [%#x] ", pos->en_passant); if (pos->en_passant == 0) printf("None.\n"); else - 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("%c%c.\n", + FILE2C(BBfile(pos->en_passant)), + RANK2C(BBrank(pos->en_passant))); printf("castle [%#x] : ", pos->castle); @@ -130,97 +164,94 @@ void pos_print(pos_t *pos) if (pos->castle & CASTLE_BQ) printf("q"); - printf("\n50 half-moves-rule = %d\n", pos->clock_50); - printf("Current move = %d\n", pos->curmove); - printf("Squares controlled: W:%d B:%d\n", popcount64(pos->controlled[WHITE]), - popcount64(pos->controlled[BLACK])); - printf("Mobility: W:%u B:%u\n", pos->mobility[WHITE], - pos->mobility[BLACK]); + printf("\nplies=%d clock50=%d\n", pos->plycount, pos->clock_50); + //printf("Current move = %d\n", pos->curmove); + //printf("Squares controlled: W:%d B:%d\n", popcount64(pos->controlled[WHITE]), + // popcount64(pos->controlled[BLACK])); + //printf("Mobility: W:%u B:%u\n", pos->mobility[WHITE], + // pos->mobility[BLACK]); } /** * pos_check() - extensive position consistenci check. * @pos: &position */ -void pos_check(pos_t *pos) +/* + * void pos_check(position *pos) + * { + * int rank, file; + * piece_t piece; + * board_t *board = pos->board; + * + * /\* check that board and bitboard reflect same information *\/ + * for (rank = 7; rank >= 0; --rank) { + * for (file = 0; file < 8; ++file) { + * piece_list_t *ppiece; + * printf("checking %c%c ", file+'a', rank+'1'); + * + * piece = board[SQ88(file, rank)].piece; + * ppiece= board[SQ88(file, rank)].s_piece; + * printf("piece=%s ", P_CSYM(piece)); + * if (ppiece) + * printf("ppiece=%s/sq=%#x ", P_CSYM(ppiece->piece), ppiece->square); + * switch(PIECE(piece)) { + * case PAWN: + * printf("pawn" ); + * break; + * case KNIGHT: + * printf("knight "); + * break; + * case BISHOP: + * printf("bishop "); + * break; + * case ROOK: + * printf("rook "); + * break; + * case QUEEN: + * printf("queen "); + * break; + * case KING: + * printf("king "); + * break; + * } + * printf("\n"); + * } + * } + * } + */ + +position *pos_clear(position *pos) { - int rank, file; - piece_t piece; - board_t *board = pos->board; - - /* check that board and bitboard reflect same information */ - for (rank = 7; rank >= 0; --rank) { - for (file = 0; file < 8; ++file) { - piece_list_t *ppiece; - printf("checking %c%c ", file+'a', rank+'1'); - - piece = board[SQ88(file, rank)].piece; - ppiece= board[SQ88(file, rank)].s_piece; - printf("piece=%s ", P_CSYM(piece)); - if (ppiece) - printf("ppiece=%s/sq=%#x ", P_CSYM(ppiece->piece), ppiece->square); - switch(PIECE(piece)) { - case PAWN: - printf("pawn" ); - break; - case KNIGHT: - printf("knight "); - break; - case BISHOP: - printf("bishop "); - break; - case ROOK: - printf("rook "); - break; - case QUEEN: - printf("queen "); - break; - case KING: - printf("king "); - break; - } - printf("\n"); - } - } -} - -pos_t *pos_clear(pos_t *pos) -{ - int file, rank; - board_t *board = pos->board; - - for (file = 0; file < 8; ++file) { - for (rank = 0; rank < 8; ++rank) { - /*printf("file = %d rank = %d SQ88 = %#2x = %d addr=%p\n", file, rank, - SQ88(file, rank), SQ88(file, rank), - &board[SQ88(file, rank)].piece); - */ - board[SQ88(file, rank)].piece = EMPTY; - } - } + printf("size(pos_board=%lu elt=%lu\n", sizeof(pos->board), sizeof(int)); + //for (square square = A1; square <= H8; ++square) + // pos->board[square] = EMPTY; SET_WHITE(pos->turn); pos->node_count = 0; - pos->castle = 0; + pos->turn = 0; pos->clock_50 = 0; - pos->curmove = 0; - pos->eval = 0; + pos->plycount = 0; pos->en_passant = 0; - pos->occupied[WHITE] = 0; - pos->occupied[BLACK] = 0; - for (int color=0; color<2; ++color) - for (int piece = BB_ALL; piece < BB_END; ++piece) + pos->castle = 0; + memset(pos->board, 0, sizeof(pos->board)); + //pos->curmove = 0; + //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) pos->bb[color][piece] = 0; - pos->controlled[WHITE] = 0; - pos->controlled[BLACK] = 0; - pos->mobility[WHITE] = 0; - pos->mobility[BLACK] = 0; - pos->moves_generated = false; - pos->moves_counted = false; + pos->controlled[WHITE] = 0; + pos->controlled[BLACK] = 0; + } + //pos->mobility[WHITE] = 0; + //pos->mobility[BLACK] = 0; + //pos->moves_generated = false; + //pos->moves_counted = false; /* remove pieces / moves */ - pieces_del(pos, WHITE); - pieces_del(pos, BLACK); - moves_del(pos); + //pieces_del(pos, WHITE); + //pieces_del(pos, BLACK); + //moves_del(pos); return pos; } @@ -229,35 +260,22 @@ pos_t *pos_clear(pos_t *pos) * pos_del() - delete a position. * @pos: &position. */ -void pos_del(pos_t *pos) +/* + * void pos_del(pos_t *pos) + * { + * pieces_del(pos, WHITE); + * pieces_del(pos, BLACK); + * moves_del(pos); + * pool_add(pos_pool, pos); + * } + */ + + +position *pos_new(void) { - pieces_del(pos, WHITE); - pieces_del(pos, BLACK); - moves_del(pos); - pool_add(pos_pool, pos); -} - -pos_t *pos_startpos(pos_t *pos) -{ - static char *startfen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; - - return fen2pos(pos, startfen); -} - -pos_t *pos_get() -{ - pos_t *pos = pool_get(pos_pool); - if (pos) { - INIT_LIST_HEAD(&pos->pieces[WHITE]); - INIT_LIST_HEAD(&pos->pieces[BLACK]); - - INIT_LIST_HEAD(&pos->moves[WHITE]); - INIT_LIST_HEAD(&pos->moves[BLACK]); - pos_clear(pos); - } else { - fprintf(stderr, "zobaaa\n"); - } - return pos; + position *pos = safe_malloc(sizeof(position)); + //assert(pos); + return pos_clear(pos); } /** @@ -275,44 +293,29 @@ pos_t *pos_get() * * @return: The new position. * - * TODO: merge with pos_get - NULL for init, non null for duplicate + * TODO: merge with pos_new - NULL for init, non null for duplicate */ -pos_t *pos_dup(pos_t *pos) +position *pos_dup(position *pos) { - struct list_head *p_cur, *piece_list; - piece_list_t *oldpiece; - board_t *board; - pos_t *new = pool_get(pos_pool); + position *newpos= pool_get(pos_pool); - if (new) { - board = new->board; - *new = *pos; - for (int color = 0; color < 2; ++color) { - INIT_LIST_HEAD(&new->pieces[color]); - INIT_LIST_HEAD(&new->moves[color]); - /* duplicate piece list */ - piece_list = &pos->pieces[color]; /* white/black piece list */ - - list_for_each(p_cur, piece_list) { - oldpiece = list_entry(p_cur, piece_list_t, list); - board[oldpiece->square].s_piece = - piece_add(new, oldpiece->piece, oldpiece->square); - } - } - new->bestmove = NULL; - new->node_count = 0; - new->eval = EVAL_INVALID; - new->moves_generated = false; - new->moves_counted = false; - new->check[WHITE] = new->check[BLACK] = 0; + if (newpos) { + //board = new->board; + *newpos = *pos; + //new->bestmove = NULL; + newpos->node_count = 0; + //new->eval = EVAL_INVALID; + //new->moves_generated = false; + //new->moves_counted = false; + //new->check[WHITE] = new->check[BLACK] = 0; } - return new; + return newpos; } pool_t *pos_pool_init() { if (!pos_pool) - pos_pool = pool_create("positions", 128, sizeof(pos_t)); + pos_pool = pool_create("positions", 128, sizeof(position)); return pos_pool; } diff --git a/src/position.h b/src/position.h index d41a0db..c803e60 100644 --- a/src/position.h +++ b/src/position.h @@ -1,6 +1,6 @@ /* position.h - position management definitions. * - * Copyright (C) 2021 Bruno Raoult ("br") + * Copyright (C) 2021-2024 Bruno Raoult ("br") * Licensed under the GNU General Public License v3.0 or later. * Some rights reserved. See COPYING. * @@ -16,51 +16,85 @@ #include -#include "pool.h" -#include "list.h" +#include "brlib.h" +//#include "pool.h" +//#include "list.h" #include "bitops.h" +#include "bitboard.h" #include "board.h" #include "chessdefs.h" -typedef struct pos_s { +typedef struct { u64 node_count; /* evaluated nodes */ - piece_t turn; /* we use only color bit */ - castle_t castle; + int turn; /* WHITE or BLACK */ u16 clock_50; - u16 curmove; - eval_t eval; - int check[2]; - int eval_simple_phase; - eval_t eval_simple; - move_t *bestmove; - bool moves_generated; - bool moves_counted; - board_t board[BOARDSIZE]; + u16 plycount; /* plies so far, start is 0 */ + square en_passant; + castle castle; - square_t en_passant; + //eval_t eval; + //int check[2]; + //int eval_simple_phase; + //eval_t eval_simple; + //move_t *bestmove; + //bool moves_generated; + //bool moves_counted; - bitboard_t bb[2][BB_END]; /* use: pieces[BLACK][BB_PAWN] */ - bitboard_t occupied[2]; /* OR of bb[COLOR][x] */ - bitboard_t controlled[2]; - u16 mobility[2]; - struct list_head pieces[2]; /* pieces list, King is first */ - struct list_head moves[2]; -} pos_t; + bitboard bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */ + bitboard controlled[2]; + //u16 mobility[2]; + //struct list_head pieces[2]; /* pieces list, King is first */ + //struct list_head moves[2]; + piece board[BOARDSIZE]; +} position; -void bitboard_print(bitboard_t bb); -void bitboard_print2(bitboard_t bb1, bitboard_t bb2); -void pos_pieces_print(pos_t *pos); -void pos_bitboards_print(pos_t *pos); -void pos_print(pos_t *pos); -pos_t *pos_clear(pos_t *pos); -void pos_del(pos_t *pos); -pos_t *pos_startpos(pos_t *pos); -pos_t *pos_create(); +/** + * pos_set_sq - unconditionally set a piece on a square + * @pos: position + * @p, @c: type and color of piece to add + * @f, @r: destination file and rank + * + * Both position bords and bitboards are modified. + */ +static inline void pos_set_sq(position *pos, piece_type p, color c, file f, rank r) +{ + piece piece = MAKE_PIECE(p, c); + square square = BB(f, r); + pos->board[square] = piece; + pos->bb[c][ALL_PIECES] |= 1 << square; + pos->bb[c][p] |= 1 << square; +} + +/** + * pos_clr_sq - unconditionally remove a piece from square + * @pos: position + * @f, @r: destination file and rank + * + * Both position bords and bitboards are modified. + */ +static inline void pos_clr_sq(position *pos, file f, rank r) +{ + square square = BB(f, r); + piece_type piece = PIECE(pos->board[square]); + color color = COLOR(pos->board[square]); + pos->board[square] = EMPTY; + pos->bb[color][piece] &= ~(1 << square); + pos->bb[color][ALL_PIECES] &= ~(1 << square); +} + +//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 pos_bitboards_print(pos_t *pos); +void pos_print(position *pos); +position *pos_clear(position *pos); +//void pos_del(position *pos); +position *pos_startpos(position *pos); +position *pos_new(); pool_t *pos_pool_init(); void pos_pool_stats(); -pos_t *pos_get(); -pos_t *pos_dup(pos_t *pos); -void pos_check(pos_t *pos); +position *pos_dup(position *pos); +//void pos_check(position *pos); #endif /* POSITION_H */ diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..a0eaaf4 --- /dev/null +++ b/src/util.c @@ -0,0 +1,44 @@ +/* util.c - various util functions. + * + * Copyright (C) 2024 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include +#include + +#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); + exit(0); +} +*/ diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..50aca72 --- /dev/null +++ b/src/util.h @@ -0,0 +1,60 @@ +/* util.h - various util functions. + * + * Copyright (C) 2024 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef _UTIL_H +#define _UTIL_H + +#include +#include + +#include "bug.h" + +#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 +#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 cb19846..051ce24 100644 --- a/test/fen-test.c +++ b/test/fen-test.c @@ -1,20 +1,22 @@ #include "debug.h" #include "pool.h" + #include "../src/position.h" #include "../src/fen.h" int main(int ac, char**av) { - pos_t *pos; + position *pos; + debug_init(5, stderr, true); - piece_pool_init(); pos_pool_init(); - pos = pos_get(); + pos = pos_new(); if (ac == 1) { - pos_startpos(pos); + startpos(pos); } else { fen2pos(pos, av[1]); } pos_print(pos); + printf("fen=[%s]\n", pos2fen(pos, NULL)); } diff --git a/util.c b/util.c new file mode 100644 index 0000000..e69de29