bb migration: add util, update fen/fen-test + partial pos + piece
This commit is contained in:
15
Makefile
15
Makefile
@@ -48,6 +48,8 @@ LIBS := -l$(LIB) -lreadline -lncurses
|
|||||||
|
|
||||||
##################################### pre-processor flags
|
##################################### pre-processor flags
|
||||||
CPPFLAGS := -I$(BRINCDIR)
|
CPPFLAGS := -I$(BRINCDIR)
|
||||||
|
CPPFLAGS += -DBUG_ON
|
||||||
|
CPPFLAGS += -DWARN_ON
|
||||||
#CPPFLAGS += -DDEBUG # global
|
#CPPFLAGS += -DDEBUG # global
|
||||||
CPPFLAGS += -DDEBUG_DEBUG # enable log() functions
|
CPPFLAGS += -DDEBUG_DEBUG # enable log() functions
|
||||||
#CPPFLAGS += -DDEBUG_DEBUG_C # enable verbose log() settings
|
#CPPFLAGS += -DDEBUG_DEBUG_C # enable verbose log() settings
|
||||||
@@ -252,6 +254,16 @@ VALGRINDFLAGS += --suppressions=etc/libreadline.supp
|
|||||||
memcheck: targets
|
memcheck: targets
|
||||||
@$(VALGRIND) $(VALGRINDFLAGS) $(BINDIR)/brchess
|
@$(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
|
##################################### Makefile debug
|
||||||
.PHONY: showflags wft
|
.PHONY: showflags wft
|
||||||
|
|
||||||
@@ -271,3 +283,6 @@ wtf:
|
|||||||
@#echo LIBOBJ=$(LIBOBJ)
|
@#echo LIBOBJ=$(LIBOBJ)
|
||||||
@#echo DEP=$(DEP)
|
@#echo DEP=$(DEP)
|
||||||
@#echo LIBSRC=$(LIBSRC)
|
@#echo LIBSRC=$(LIBSRC)
|
||||||
|
|
||||||
|
zob:
|
||||||
|
$(CC) $(LDFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(LIBS) src/util.c -o util
|
||||||
|
2
brlib
2
brlib
Submodule brlib updated: 635e4c8d9e...8cb08dacb3
@@ -11,18 +11,15 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef BITBOARD_H
|
#ifndef _BITBOARD_H
|
||||||
#define BITBOARD_H
|
#define _BITBOARD_H
|
||||||
|
|
||||||
#include "br.h"
|
#include "brlib.h"
|
||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
#include "piece.h"
|
#include "piece.h"
|
||||||
#include "bitops.h"
|
#include "bitops.h"
|
||||||
|
|
||||||
#define mask(s) ( 1ULL << (s) )
|
typedef enum {
|
||||||
#define C64(const_u64) const_u64##ULL
|
|
||||||
|
|
||||||
typedef enum square {
|
|
||||||
A1, B1, C1, D1, E1, F1, G1, H1,
|
A1, B1, C1, D1, E1, F1, G1, H1,
|
||||||
A2, B2, C2, D2, E2, F2, G2, H2,
|
A2, B2, C2, D2, E2, F2, G2, H2,
|
||||||
A3, B3, C3, D3, E3, F3, G3, H3,
|
A3, B3, C3, D3, E3, F3, G3, H3,
|
||||||
@@ -31,23 +28,21 @@ typedef enum square {
|
|||||||
A6, B6, C6, D6, E6, F6, G6, H6,
|
A6, B6, C6, D6, E6, F6, G6, H6,
|
||||||
A7, B7, C7, D7, E7, F7, G7, H7,
|
A7, B7, C7, D7, E7, F7, G7, H7,
|
||||||
A8, B8, C8, D8, E8, F8, G8, H8,
|
A8, B8, C8, D8, E8, F8, G8, H8,
|
||||||
SQ_N,
|
SQUARE_MAX = 64,
|
||||||
SQ_0 = 0
|
SQUARE_NONE = 64
|
||||||
} square;
|
} square;
|
||||||
|
|
||||||
typedef enum file {
|
typedef enum {
|
||||||
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H,
|
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H,
|
||||||
FILE_N,
|
FILE_MAX,
|
||||||
FILE_0 = 0
|
|
||||||
} file;
|
} file;
|
||||||
|
|
||||||
typedef enum rank {
|
typedef enum {
|
||||||
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8,
|
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8,
|
||||||
RANK_N,
|
RANK_MAX,
|
||||||
RANK_0 = 0
|
|
||||||
} rank;
|
} rank;
|
||||||
|
|
||||||
typedef enum sq_bb {
|
typedef enum {
|
||||||
//A1 = 0x01ULL, B1 = 0x02ULL, C1 = 1UL << 2, D1 = 1UL << 3,
|
//A1 = 0x01ULL, B1 = 0x02ULL, C1 = 1UL << 2, D1 = 1UL << 3,
|
||||||
//E1 = 1UL << 4, F1 = 1UL << 5, G1 = 1UL << 6, H1 = 1UL <<
|
//E1 = 1UL << 4, F1 = 1UL << 5, G1 = 1UL << 6, H1 = 1UL <<
|
||||||
A1bb = mask(A1), A2bb = mask(A2), A3bb = mask(A3), A4bb = mask(A4),
|
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),
|
H5bb = mask(H5), H6bb = mask(H6), H7bb = mask(H7), H8bb = mask(H8),
|
||||||
} sq_bb;
|
} sq_bb;
|
||||||
|
|
||||||
typedef enum file_bb {
|
typedef enum {
|
||||||
FILE_Abb = 0x0101010101010101ULL,
|
FILE_Abb = 0x0101010101010101ULL,
|
||||||
FILE_Bbb = 0x0202020202020202ULL,
|
FILE_Bbb = 0x0202020202020202ULL,
|
||||||
FILE_Cbb = 0x0404040404040404ULL,
|
FILE_Cbb = 0x0404040404040404ULL,
|
||||||
@@ -79,7 +74,7 @@ typedef enum file_bb {
|
|||||||
FILE_Hbb = 0x8080808080808080ULL,
|
FILE_Hbb = 0x8080808080808080ULL,
|
||||||
} file_bb;
|
} file_bb;
|
||||||
|
|
||||||
typedef enum rank_bb {
|
typedef enum {
|
||||||
RANK_1bb = 0x00000000000000ffULL,
|
RANK_1bb = 0x00000000000000ffULL,
|
||||||
RANK_2bb = 0x000000000000ff00ULL,
|
RANK_2bb = 0x000000000000ff00ULL,
|
||||||
RANK_3bb = 0x0000000000ff0000ULL,
|
RANK_3bb = 0x0000000000ff0000ULL,
|
||||||
@@ -90,16 +85,31 @@ typedef enum rank_bb {
|
|||||||
RANK_8bb = 0xff00000000000000ULL
|
RANK_8bb = 0xff00000000000000ULL
|
||||||
} rank_bb;
|
} rank_bb;
|
||||||
|
|
||||||
#define NORTH 8
|
typedef enum {
|
||||||
#define EAST 1
|
NORTH = 8,
|
||||||
#define SOUTH -NORTH
|
EAST = 1,
|
||||||
#define WEST -EAST
|
SOUTH = -NORTH,
|
||||||
|
WEST = -EAST,
|
||||||
|
|
||||||
#define NORTH_EAST (NORTH + EAST)
|
NORTH_EAST = (NORTH + EAST),
|
||||||
#define SOUTH_EAST (SOUTH + EAST)
|
SOUTH_EAST = (SOUTH + EAST),
|
||||||
#define SOUTH_WEST (SOUTH + WEST)
|
SOUTH_WEST = (SOUTH + WEST),
|
||||||
#define NORTH_WEST (NORTH + 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);
|
void bitboard_init(void);
|
||||||
|
|
||||||
#endif /* BITBOARD_H */
|
#endif /* _BITBOARD_H */
|
||||||
|
71
src/board.h
71
src/board.h
@@ -14,67 +14,44 @@
|
|||||||
#ifndef BOARD_H
|
#ifndef BOARD_H
|
||||||
#define BOARD_H
|
#define BOARD_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include "brlib.h"
|
||||||
#include "chessdefs.h"
|
|
||||||
#include "piece.h"
|
|
||||||
|
|
||||||
typedef struct board_s {
|
#include "chessdefs.h"
|
||||||
piece_t piece;
|
//#include "piece.h"
|
||||||
piece_list_t *s_piece;
|
|
||||||
//struct list_head *s_piece;
|
|
||||||
} board_t; /* 0x88 board */
|
|
||||||
#define BOARDSIZE (8*8*2)
|
|
||||||
|
|
||||||
/* definitions for 0x88 representation
|
/* definitions for 0x88 representation
|
||||||
*/
|
*/
|
||||||
#define SQ88(f, r) (((r) << 4) | (f)) /* from rank,file to sq88 */
|
//#define SQ88(f, r) (((r) << 4) | (f)) /* from rank,file to sq88 */
|
||||||
#define F88(s) ((s) & 0x0f) /* from sq88 to file */
|
//#define F88(s) ((s) & 0x0f) /* from sq88 to file */
|
||||||
#define R88(s) ((s) >> 4) /* from sq88 to rank */
|
//#define R88(s) ((s) >> 4) /* from sq88 to rank */
|
||||||
|
|
||||||
#define SETF88(s, r) ((s) &= 0xf0, (s) |= (r))
|
//#define SETF88(s, r) ((s) &= 0xf0, (s) |= (r))
|
||||||
#define SETR88(s, f) ((s) &= 0x0f, (s) |= (f)<<4)
|
//#define SETR88(s, f) ((s) &= 0x0f, (s) |= (f)<<4)
|
||||||
|
|
||||||
#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */
|
//#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */
|
||||||
#define SQ88_OK(s) (!SQ88_NOK(s))
|
//#define SQ88_OK(s) (!SQ88_NOK(s))
|
||||||
|
|
||||||
/* definitions for bitboard representation
|
/* definitions for bitboard representation
|
||||||
*/
|
*/
|
||||||
#define BB(f, r) (1ULL << (8 * (r) + (f))) /* from rank,file to bitboard */
|
//#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 SQ88_NOK(s) ((s) & 0x88) /* invalid square */
|
|
||||||
#define SQ88_OK(s) (!SQ88_NOK(s))
|
|
||||||
|
|
||||||
/* piece human notation
|
//#define SQ88_2_BB(s) (BB(F88(s), R88(s))) /* from sq88 to bitboard */
|
||||||
*/
|
//#define FILEBB(b) ((b) % 8) /* from sq88 to file */
|
||||||
#define CHAR_EMPTY ' '
|
//#define RANKBB(b) ((b) / 8) /* from sq88 to rank */
|
||||||
#define CHAR_PAWN 'P'
|
|
||||||
#define CHAR_KNIGHT 'N'
|
//#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */
|
||||||
#define CHAR_BISHOP 'B'
|
//#define SQ88_OK(s) (!SQ88_NOK(s))
|
||||||
#define CHAR_ROOK 'R'
|
|
||||||
#define CHAR_QUEEN 'Q'
|
|
||||||
#define CHAR_KING 'K'
|
|
||||||
|
|
||||||
/* from human to machine
|
/* from human to machine
|
||||||
*/
|
*/
|
||||||
#define C2FILE(c) (tolower(c) - 'a')
|
/*
|
||||||
#define C2RANK(c) (tolower(c) - '1')
|
* #define C2FILE(c) (tolower(c) - 'a')
|
||||||
/* from machine to human
|
* #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 */
|
#endif /* BOARD_H */
|
||||||
|
122
src/chessdefs.h
122
src/chessdefs.h
@@ -11,67 +11,47 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef CHESSDEFS_H
|
#ifndef _CHESSDEFS_H
|
||||||
#define 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
|
/* piece_t bits structure
|
||||||
* MSB 8 7 6 5 4 3 2 1 LSB
|
* piece is on bits 1-3, color on bit 4:
|
||||||
* 1: color (0 for white)
|
* .... CPPP
|
||||||
* 2-7: bit set for pawn (2), knight, bishop, rook, queen, king (7)
|
* 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 {
|
typedef enum {
|
||||||
E_EMPTY = 0,
|
ALL_PIECES = 0, /* 'all pieces' bitboard */
|
||||||
E_COLOR, /* LSB */
|
PAWN = 1, KNIGHT, BISHOP, ROOK, QUEEN, KING,
|
||||||
E_PAWN,
|
PIECE_TYPE_MAX = 7 /* bit 4 */
|
||||||
E_KNIGHT,
|
} piece_type;
|
||||||
E_BISHOP,
|
|
||||||
E_ROOK,
|
|
||||||
E_QUEEN,
|
|
||||||
E_KING,
|
|
||||||
};
|
|
||||||
|
|
||||||
/* pos_t bitboards tables
|
typedef enum {
|
||||||
*/
|
EMPTY,
|
||||||
enum {
|
W_PAWN = PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
|
||||||
BB_ALL = 0, /* OR of all bitboards */
|
B_PAWN = PAWN | 8, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING,
|
||||||
BB_UNUSED, /* future use ? */
|
PIECE_MAX
|
||||||
BB_PAWN = E_PAWN,
|
} piece;
|
||||||
BB_KNIGHT,
|
|
||||||
BB_BISHOP,
|
|
||||||
BB_ROOK,
|
|
||||||
BB_QUEEN,
|
|
||||||
BB_KING,
|
|
||||||
BB_END
|
|
||||||
};
|
|
||||||
|
|
||||||
/* 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 PIECETOBB(p) (ffs64(PIECE(p))) /* from piece_t to bb piece array */
|
|
||||||
|
|
||||||
#define WHITE 0 /* 0x00 00000000 */
|
|
||||||
#define BLACK 1 /* 0x01 00000001 */
|
|
||||||
#define OPPONENT(p) !(p)
|
#define OPPONENT(p) !(p)
|
||||||
|
|
||||||
#define MASK_COLOR 0x01 /* 00000001 */
|
#define MASK_PIECE 0x07 /* 00000111 */
|
||||||
#define MASK_PIECE 0x7E /* 01111110 */
|
#define MASK_COLOR 0x08 /* 00001000 */
|
||||||
|
|
||||||
#define COLOR(p) ((p) & MASK_COLOR) /* bitmask */
|
#define COLOR(p) ((p) >> 3) /* bitmask */
|
||||||
#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 MAKE_PIECE(p, c) ((p) | (c) << 3)
|
||||||
|
|
||||||
#define IS_WHITE(p) (!COLOR(p))
|
#define IS_WHITE(p) (!COLOR(p))
|
||||||
#define IS_BLACK(p) (COLOR(p))
|
#define IS_BLACK(p) (COLOR(p))
|
||||||
@@ -86,20 +66,25 @@ enum {
|
|||||||
*/
|
*/
|
||||||
#define FLIP_V(sq) ((sq) ^ 56)
|
#define FLIP_V(sq) ((sq) ^ 56)
|
||||||
#define FLIP_H(sq) ((sq) ^ 7)
|
#define FLIP_H(sq) ((sq) ^ 7)
|
||||||
/* square_t bits structure : rrrrffff
|
|
||||||
* ffff: file
|
typedef u64 bitboard;
|
||||||
* rrrr: rank
|
typedef ushort board;
|
||||||
*/
|
#define BOARDSIZE (8*8)
|
||||||
typedef uchar square_t;
|
/* 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
|
/* castle_t bits structure
|
||||||
*/
|
*/
|
||||||
typedef unsigned char castle_t;
|
typedef enum {
|
||||||
|
CASTLE_WK = (1 << 0), /* 0x01 00000001 */
|
||||||
#define CASTLE_WK (1 << 0) /* 0x01 00000001 */
|
CASTLE_WQ = (1 << 1), /* 0x02 00000010 */
|
||||||
#define CASTLE_WQ (1 << 1) /* 0x02 00000010 */
|
CASTLE_BK = (1 << 2), /* 0x04 00000100 */
|
||||||
#define CASTLE_BK (1 << 2) /* 0x04 00000100 */
|
CASTLE_BQ = (1 << 3), /* 0x08 00001000 */
|
||||||
#define CASTLE_BQ (1 << 3) /* 0x08 00001000 */
|
} castle;
|
||||||
|
|
||||||
#define CASTLE_W (CASTLE_WK | CASTLE_WQ) /* 00000011 W castle mask */
|
#define CASTLE_W (CASTLE_WK | CASTLE_WQ) /* 00000011 W castle mask */
|
||||||
#define CASTLE_B (CASTLE_BK | CASTLE_BQ) /* 00001100 B castle mask */
|
#define CASTLE_B (CASTLE_BK | CASTLE_BQ) /* 00001100 B castle mask */
|
||||||
@@ -112,18 +97,17 @@ typedef unsigned char castle_t;
|
|||||||
|
|
||||||
/* bitboard
|
/* bitboard
|
||||||
*/
|
*/
|
||||||
typedef u64 bitboard_t;
|
//typedef u64 bitboard_t;
|
||||||
|
|
||||||
|
|
||||||
/* eval type
|
/* eval type
|
||||||
*/
|
*/
|
||||||
typedef s32 eval_t;
|
//typedef s32 eval_t;
|
||||||
|
|
||||||
/* forward typedefs
|
/* forward typedefs
|
||||||
*/
|
*/
|
||||||
typedef struct piece_list_s piece_list_t;
|
//typedef struct piece_list_s piece_list_t;
|
||||||
typedef struct board_s board_t;
|
//typedef struct board_s board_t;
|
||||||
typedef struct pos_s pos_t;
|
//typedef struct pos_s pos_t;
|
||||||
typedef struct move_s move_t;
|
//typedef struct move_s move_t;
|
||||||
|
|
||||||
#endif
|
#endif /* _CHESSDEFS_H */
|
||||||
|
336
src/fen.c
336
src/fen.c
@@ -1,6 +1,6 @@
|
|||||||
/* fen.c - fen notation.
|
/* 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.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
* Some rights reserved. See COPYING.
|
* Some rights reserved. See COPYING.
|
||||||
*
|
*
|
||||||
@@ -18,158 +18,264 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
#include <bug.h>
|
||||||
|
|
||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "board.h"
|
|
||||||
#include "fen.h"
|
#include "fen.h"
|
||||||
#include "piece.h"
|
#include "piece.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
/* Starting Position :
|
/* FEN description:
|
||||||
* rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
|
* 1 : pieces on board (no space allowed):
|
||||||
* After 1.e4 :
|
* - rank 8 first, '/' between ranks
|
||||||
* rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
|
* - piece is usual piece notation(PNBRQK), black lowercase.
|
||||||
* After 1... c5 :
|
* - empty: number of consecutive empty squares (digit)
|
||||||
* 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
|
|
||||||
* 2 : next move (w or b)
|
* 2 : next move (w or b)
|
||||||
* 3 : Castling capabilities: "-" if none, KQ/kq if white/black can castle
|
* 3 : Castling capabilities: "-" if none, KQ/kq if white/black can castle
|
||||||
* on K or Q side
|
* on K or Q side
|
||||||
* 4 : en-passant: if pawn just moved 2 squares, indicate target square (e.g.
|
* 4 : en-passant: "-" if none. If pawn just moved 2 squares, indicate target
|
||||||
* for e2-e4 this field is e3)
|
* 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)
|
* 5 : half moves since last capture or pawn advance (for 50 moves rule)
|
||||||
* 6 : full moves, starts at 1, increments after black move
|
* 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
|
/* chess startup position FEN */
|
||||||
pos_t *fen2pos(pos_t *pos, char *fen)
|
const char *startfen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||||
{
|
|
||||||
char *p = fen;
|
/* next must follow 'piece_type' enum values (pawn is 1, king is 6) */
|
||||||
short rank, file, skip, color, bbpiece;
|
static const char *pieces_str = " PNBRQK";
|
||||||
piece_t piece;
|
/* And this one must follow 'castle' enum order (also same as usual FEN) */
|
||||||
board_t *board = pos->board;
|
static const char *castle_str = "KQkq";
|
||||||
# define SKIP_BLANK(p) for(;*(p) == ' '; (p)++)
|
|
||||||
|
#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
|
/* 1) get piece placement information
|
||||||
*/
|
*/
|
||||||
for (rank = 7, file = 0; *p && *p != ' '; ++p) {
|
for (rank = 7, file = 0; *cur && !isspace(*cur); ++cur) {
|
||||||
color = isupper(*p)? WHITE: BLACK;
|
if (*cur == '/') { /* next rank */
|
||||||
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--;
|
rank--;
|
||||||
file = 0;
|
file = 0;
|
||||||
break;
|
continue;
|
||||||
default:
|
|
||||||
skip = cp - '0';
|
|
||||||
while (skip--) {
|
|
||||||
board[SQ88(file++, rank)].piece = EMPTY;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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
|
# ifdef DEBUG_FEN
|
||||||
for (rank = 7; rank >= 0; --rank) {
|
log_i(5, "f=%d r=%d *p=%c piece=%c color=%d ppos=%ld\n",
|
||||||
for (file = 0; file < 8; ++file) {
|
file, rank, *cur, *p, color, p - pieces_str);
|
||||||
log(5, "%02x ", board[SQ88(file, rank)].piece);
|
|
||||||
}
|
|
||||||
log(5, "\n");
|
|
||||||
}
|
|
||||||
# endif
|
# 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SKIP_BLANK(cur);
|
||||||
|
|
||||||
/* 2) next move color
|
/* 2) next turn color
|
||||||
*/
|
*/
|
||||||
SKIP_BLANK(p);
|
tmppos.turn = *cur++ == 'w' ? WHITE : BLACK;
|
||||||
SET_COLOR(pos->turn, *p == 'w' ? WHITE : BLACK);
|
SKIP_BLANK(cur);
|
||||||
p++;
|
|
||||||
|
|
||||||
/* 3) castle status
|
/* 3) castle rights
|
||||||
*/
|
*/
|
||||||
SKIP_BLANK(p);
|
if (*cur == '-') {
|
||||||
pos->castle = 0;
|
cur++;
|
||||||
if (*p != '-') {
|
} else {
|
||||||
for (; *p && *p != ' '; ++p) {
|
for (; *cur && !isspace(*cur); ++cur) {
|
||||||
switch (*p) {
|
if ((p = strchr(castle_str, *cur))) { /* valid castle letter */
|
||||||
case 'K':
|
tmppos.castle |= 1 << (p - castle_str);
|
||||||
pos->castle |= CASTLE_WK;
|
} else {
|
||||||
break;
|
err_line = __LINE__, err_char = *cur, err_pos = cur - fen;
|
||||||
case 'k':
|
goto end;
|
||||||
pos->castle |= CASTLE_BK;
|
|
||||||
break;
|
|
||||||
case 'Q':
|
|
||||||
pos->castle |= CASTLE_WQ;
|
|
||||||
break;
|
|
||||||
case 'q':
|
|
||||||
pos->castle |= CASTLE_BQ;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p++;
|
SKIP_BLANK(cur);
|
||||||
|
|
||||||
/* 4) en passant
|
/* 4) en passant
|
||||||
*/
|
*/
|
||||||
SKIP_BLANK(p);
|
tmppos.en_passant = 0;
|
||||||
pos->en_passant = 0;
|
if (*cur == '-') {
|
||||||
if (*p != '-') {
|
cur++;
|
||||||
//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 {
|
} 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
|
/* 5) half moves since last capture or pawn move (50 moves rule)
|
||||||
* 6) current move number
|
|
||||||
*/
|
*/
|
||||||
SKIP_BLANK(p);
|
sscanf(cur, "%hd%n", &tmppos.clock_50, &consumed);
|
||||||
//log_i(5, "pos=%d\n", (int)(p-fen));
|
cur += consumed;
|
||||||
sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove);
|
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
|
# 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
|
# 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;
|
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;
|
||||||
|
}
|
||||||
|
10
src/fen.h
10
src/fen.h
@@ -1,6 +1,6 @@
|
|||||||
/* fen.h - fen notation.
|
/* 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.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
* Some rights reserved. See COPYING.
|
* Some rights reserved. See COPYING.
|
||||||
*
|
*
|
||||||
@@ -16,6 +16,12 @@
|
|||||||
|
|
||||||
#include "position.h"
|
#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 */
|
#endif /* FEN_H */
|
||||||
|
204
src/piece.c
204
src/piece.c
@@ -1,6 +1,6 @@
|
|||||||
/* piece.c - piece list management.
|
/* 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.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
* Some rights reserved. See COPYING.
|
* Some rights reserved. See COPYING.
|
||||||
*
|
*
|
||||||
@@ -13,106 +13,118 @@
|
|||||||
|
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#include <debug.h>
|
//#include <debug.h>
|
||||||
#include <pool.h>
|
//#include <pool.h>
|
||||||
#include <list.h>
|
//#include <list.h>
|
||||||
|
|
||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
#include "piece.h"
|
#include "piece.h"
|
||||||
#include "board.h"
|
//#include "board.h"
|
||||||
#include "bitboard.h"
|
//#include "bitboard.h"
|
||||||
#include "position.h"
|
//#include "position.h"
|
||||||
|
|
||||||
static pool_t *pieces_pool;
|
//static pool_t *pieces_pool;
|
||||||
|
|
||||||
struct piece_details piece_details[] = {
|
const struct piece_details piece_details[] = {
|
||||||
[E_EMPTY] = { ' ', ' ', " ", " ", "", 0 },
|
/* W B SW SB Name start value */
|
||||||
[E_PAWN] = { 'P', 'p', "♙", "♟", "Pawn", PAWN_VALUE },
|
[EMPTY] = { ' ', ' ', " ", " ", "",
|
||||||
[E_KNIGHT] = { 'N', 'n', "♘", "♞", "Knight", KNIGHT_VALUE },
|
0, 0, 0 },
|
||||||
[E_BISHOP] = { 'B', 'b', "♗", "♝", "Bishop", BISHOP_VALUE },
|
[PAWN] = { 'P', 'p', "♙", "♟", "Pawn",
|
||||||
[E_ROOK] = { 'R', 'r', "♖", "♜", "Rook", ROOK_VALUE },
|
PAWN_VALUE, PAWN_VALUE, PAWN_VALUE },
|
||||||
[E_QUEEN] = { 'Q', 'q', "♕", "♛", "Queen", QUEEN_VALUE },
|
[KNIGHT] = { 'N', 'n', "♘", "♞", "Knight",
|
||||||
[E_KING] = { 'K', 'k', "♔", "♚", "King", KING_VALUE }
|
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)
|
/*
|
||||||
{
|
* void piece_list_print(struct list_head *list)
|
||||||
struct list_head *p_cur, *tmp;
|
* {
|
||||||
piece_list_t *piece;
|
* 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);
|
* 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)),
|
* printf("%s%c%c ", P_SYM(piece->piece),
|
||||||
RANK2C(R88(piece->square)));
|
* FILE2C(F88(piece->square)),
|
||||||
}
|
* RANK2C(R88(piece->square)));
|
||||||
printf("\n");
|
* }
|
||||||
}
|
* printf("\n");
|
||||||
|
* }
|
||||||
pool_t *piece_pool_init()
|
*
|
||||||
{
|
* pool_t *piece_pool_init()
|
||||||
if (!pieces_pool)
|
* {
|
||||||
pieces_pool = pool_create("pieces", 128, sizeof(piece_list_t));
|
* if (!pieces_pool)
|
||||||
return pieces_pool;
|
* pieces_pool = pool_create("pieces", 128, sizeof(piece_list_t));
|
||||||
}
|
* return pieces_pool;
|
||||||
|
* }
|
||||||
void piece_pool_stats()
|
*
|
||||||
{
|
* void piece_pool_stats()
|
||||||
if (pieces_pool)
|
* {
|
||||||
pool_stats(pieces_pool);
|
* if (pieces_pool)
|
||||||
}
|
* pool_stats(pieces_pool);
|
||||||
|
* }
|
||||||
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;
|
* {
|
||||||
short color = COLOR(piece);
|
* piece_list_t *new;
|
||||||
|
* short color = COLOR(piece);
|
||||||
# ifdef DEBUG_PIECE
|
*
|
||||||
log_f(3, "piece=%02x square=%02x\n", piece, square);
|
* # ifdef DEBUG_PIECE
|
||||||
log_f(5, "Adding %s %s on %c%c\n", color? "Black": "White",
|
* log_f(3, "piece=%02x square=%02x\n", piece, square);
|
||||||
P_NAME(piece), FILE2C(F88(square)), RANK2C(R88(square)));
|
* log_f(5, "Adding %s %s on %c%c\n", color? "Black": "White",
|
||||||
# endif
|
* P_NAME(piece), FILE2C(F88(square)), RANK2C(R88(square)));
|
||||||
if ((new = pool_get(pieces_pool))) {
|
* # endif
|
||||||
/* first piece is always king */
|
* if ((new = pool_get(pieces_pool))) {
|
||||||
if (PIECE(piece) == KING)
|
* /\* first piece is always king *\/
|
||||||
list_add(&new->list, &pos->pieces[color]);
|
* if (PIECE(piece) == KING)
|
||||||
else
|
* list_add(&new->list, &pos->pieces[color]);
|
||||||
list_add_tail(&new->list, &pos->pieces[color]);
|
* else
|
||||||
new->piece = piece;
|
* list_add_tail(&new->list, &pos->pieces[color]);
|
||||||
new->square = square;
|
* new->piece = piece;
|
||||||
new->castle = 0;
|
* new->square = square;
|
||||||
new-> value = piece_details[PIECE(piece)].value;
|
* new->castle = 0;
|
||||||
}
|
* new-> value = piece_details[PIECE(piece)].value;
|
||||||
|
* }
|
||||||
return new;
|
*
|
||||||
}
|
* return new;
|
||||||
|
* }
|
||||||
void piece_del(struct list_head *ptr)
|
*
|
||||||
{
|
* void piece_del(struct list_head *ptr)
|
||||||
piece_list_t *piece = list_entry(ptr, piece_list_t, list);
|
* {
|
||||||
# ifdef DEBUG_PIECE
|
* piece_list_t *piece = list_entry(ptr, piece_list_t, list);
|
||||||
log_f(3, "piece=%02x square=%02x\n", piece->piece, piece->square);
|
* # ifdef DEBUG_PIECE
|
||||||
# endif
|
* log_f(3, "piece=%02x square=%02x\n", piece->piece, piece->square);
|
||||||
list_del(ptr);
|
* # endif
|
||||||
pool_add(pieces_pool, piece);
|
* list_del(ptr);
|
||||||
return;
|
* pool_add(pieces_pool, piece);
|
||||||
}
|
* return;
|
||||||
|
* }
|
||||||
int pieces_del(pos_t *pos, short color)
|
*
|
||||||
{
|
* int pieces_del(pos_t *pos, short color)
|
||||||
struct list_head *p_cur, *tmp, *head;
|
* {
|
||||||
int count = 0;
|
* struct list_head *p_cur, *tmp, *head;
|
||||||
|
* int count = 0;
|
||||||
head = &pos->pieces[color];
|
*
|
||||||
|
* head = &pos->pieces[color];
|
||||||
list_for_each_safe(p_cur, tmp, head) {
|
*
|
||||||
piece_del(p_cur);
|
* list_for_each_safe(p_cur, tmp, head) {
|
||||||
count++;
|
* piece_del(p_cur);
|
||||||
}
|
* count++;
|
||||||
# ifdef DEBUG_PIECE
|
* }
|
||||||
log_f(3, "color=%d removed=%d\n", color, count);
|
* # ifdef DEBUG_PIECE
|
||||||
# endif
|
* log_f(3, "color=%d removed=%d\n", color, count);
|
||||||
return count;
|
* # endif
|
||||||
}
|
* return count;
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
41
src/piece.h
41
src/piece.h
@@ -1,6 +1,6 @@
|
|||||||
/* piece.h - piece definitions.
|
/* 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.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
* Some rights reserved. See COPYING.
|
* Some rights reserved. See COPYING.
|
||||||
*
|
*
|
||||||
@@ -20,9 +20,9 @@
|
|||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "pool.h"
|
#include "pool.h"
|
||||||
|
|
||||||
#define PIECE_DEFAULT_VALUE 0
|
|
||||||
|
|
||||||
/* initial default values */
|
/* initial default values */
|
||||||
|
#define EMPTY_VALUE 0
|
||||||
#define PAWN_VALUE 100
|
#define PAWN_VALUE 100
|
||||||
#define KNIGHT_VALUE 300
|
#define KNIGHT_VALUE 300
|
||||||
#define BISHOP_VALUE 300
|
#define BISHOP_VALUE 300
|
||||||
@@ -30,6 +30,7 @@
|
|||||||
#define QUEEN_VALUE 900
|
#define QUEEN_VALUE 900
|
||||||
#define KING_VALUE 20000
|
#define KING_VALUE 20000
|
||||||
|
|
||||||
|
/*
|
||||||
typedef struct piece_list_s {
|
typedef struct piece_list_s {
|
||||||
piece_t piece;
|
piece_t piece;
|
||||||
square_t square;
|
square_t square;
|
||||||
@@ -37,35 +38,39 @@ typedef struct piece_list_s {
|
|||||||
s64 value;
|
s64 value;
|
||||||
struct list_head list;
|
struct list_head list;
|
||||||
} piece_list_t;
|
} piece_list_t;
|
||||||
|
*/
|
||||||
|
|
||||||
/* some default values for pieces
|
/* some default values for pieces
|
||||||
*/
|
*/
|
||||||
extern struct piece_details {
|
extern const struct piece_details {
|
||||||
char abbrev_w; /* used for game notation */
|
char abbrev_w; /* used for game notation */
|
||||||
char abbrev_b;
|
char abbrev_b;
|
||||||
char *symbol_w;
|
char *symbol_w;
|
||||||
char *symbol_b; /* used for game notation */
|
char *symbol_b; /* used for game notation */
|
||||||
char *name;
|
char *name;
|
||||||
s64 value;
|
s64 opn_value;
|
||||||
|
s64 mid_value;
|
||||||
|
s64 end_value;
|
||||||
} piece_details[];
|
} piece_details[];
|
||||||
|
|
||||||
#define P_NAME(p) piece_details[E_PIECE(p)].name
|
#define P_NAME(p) piece_details[PIECE(p)].name
|
||||||
#define P_LETTER(p) piece_details[E_PIECE(p)].abbrev_w
|
#define P_LETTER(p) piece_details[PIECE(p)].abbrev_w
|
||||||
#define P_SYM(p) piece_details[E_PIECE(p)].symbol_b
|
#define P_SYM(p) piece_details[PIECE(p)].symbol_b
|
||||||
#define P_CSHORT(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].abbrev_w: \
|
#define P_CSHORT(p) (IS_WHITE(p)? piece_details[PIECE(p)].abbrev_w: \
|
||||||
piece_details[E_PIECE(p)].abbrev_b)
|
piece_details[PIECE(p)].abbrev_b)
|
||||||
#define P_CSYM(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].symbol_w: \
|
#define P_CSYM(p) (IS_WHITE(p)? piece_details[PIECE(p)].symbol_w: \
|
||||||
piece_details[E_PIECE(p)].symbol_b)
|
piece_details[PIECE(p)].symbol_b)
|
||||||
#define P_VALUE(p) (piece_details[E_PIECE(p)].value)
|
#define P_VALUE(p) (piece_details[PIECE(p)].value)
|
||||||
|
|
||||||
/* use short name or symbol - no effect
|
/* use short name or symbol - no effect
|
||||||
*/
|
*/
|
||||||
#define P_USE_UTF 1
|
#define P_USE_UTF 1
|
||||||
|
|
||||||
void piece_list_print(struct list_head *list);
|
//void piece_list_print(struct list_head *list);
|
||||||
pool_t *piece_pool_init();
|
//pool_t *piece_pool_init();
|
||||||
void piece_pool_stats();
|
//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);
|
||||||
void piece_del(struct list_head *ptr);
|
//void piece_del(struct list_head *ptr);
|
||||||
int pieces_del(pos_t *pos, short color);
|
//int pieces_del(pos_t *pos, short color);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
343
src/position.c
343
src/position.c
@@ -1,6 +1,6 @@
|
|||||||
/* position.c - position management.
|
/* 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.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
* Some rights reserved. See COPYING.
|
* Some rights reserved. See COPYING.
|
||||||
*
|
*
|
||||||
@@ -16,16 +16,23 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "bitops.h"
|
||||||
|
|
||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "move.h"
|
//#include "move.h"
|
||||||
#include "fen.h"
|
#include "fen.h"
|
||||||
#include "piece.h"
|
#include "piece.h"
|
||||||
#include "eval.h"
|
#include "util.h"
|
||||||
|
//#include "eval.h"
|
||||||
|
|
||||||
static pool_t *pos_pool;
|
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 BYTE_PRINT "%c%c%c%c%c%c%c%c"
|
||||||
#define BYTE2BIN(b) ((b) & 0x01 ? '1' : '0'), \
|
#define BYTE2BIN(b) ((b) & 0x01 ? '1' : '0'), \
|
||||||
((b) & 0x02 ? '1' : '0'), \
|
((b) & 0x02 ? '1' : '0'), \
|
||||||
@@ -36,88 +43,115 @@ static pool_t *pos_pool;
|
|||||||
((b) & 0x40 ? '1' : '0'), \
|
((b) & 0x40 ? '1' : '0'), \
|
||||||
((b) & 0x80 ? '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;
|
int i;
|
||||||
printf("%#018lx\n", bb);
|
printf("%s%s %#018lx\n", title? title: "", title? " - ": "", bb);
|
||||||
for (i=56; i>=0; i-=8)
|
for (i=56; i>=0; i-=8)
|
||||||
printf("\t"BYTE_PRINT"\n",
|
printf("\t"BYTE_PRINT"\n",
|
||||||
BYTE2BIN(bb>>i));
|
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;
|
int i;
|
||||||
|
printf("%s%s", title? title: "", title? ":\n": "");
|
||||||
|
|
||||||
printf("\tW: %#018lx\tB: %#018lx\n", bb1, bb2);
|
printf("\tW: %#018lx\tB: %#018lx\n", bb1, bb2);
|
||||||
for (i=56; i>=0; i-=8)
|
for (i=56; i>=0; i-=8)
|
||||||
printf("\t"BYTE_PRINT"\t\t"BYTE_PRINT"\n",
|
printf("\t"BYTE_PRINT"\t\t"BYTE_PRINT"\n",
|
||||||
BYTE2BIN(bb1>>i),
|
BYTE2BIN(bb1>>i),
|
||||||
BYTE2BIN(bb2>>i));
|
BYTE2BIN(bb2>>i));
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
/**
|
/**
|
||||||
* pos_pieces_print() - Print position pieces
|
* pos_pieces_print() - Print position pieces
|
||||||
* @pos: &position
|
* @pos: &position
|
||||||
*/
|
*/
|
||||||
void pos_pieces_print(pos_t *pos)
|
void pos_pieces_print(position *pos)
|
||||||
{
|
{
|
||||||
printf("White pieces (%d): \t", popcount64(pos->occupied[WHITE]));
|
int bit, count, cur;
|
||||||
piece_list_print(&pos->pieces[WHITE]);
|
char pname;
|
||||||
printf("Black pieces (%d): \t", popcount64(pos->occupied[BLACK]));
|
u64 tmp;
|
||||||
piece_list_print(&pos->pieces[BLACK]);
|
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_bitboards_print() - Print position bitboards
|
||||||
* @pos: &position
|
* @pos: &position
|
||||||
*/
|
*/
|
||||||
void pos_bitboards_print(pos_t *pos)
|
//void pos_bitboards_print(pos_t *pos)
|
||||||
{
|
//{
|
||||||
printf("Bitboards occupied :\n");
|
// printf("Bitboards occupied :\n");
|
||||||
bitboard_print2(pos->occupied[WHITE], pos->occupied[BLACK]);
|
// bitboard_print2(pos->occupied[WHITE], pos->occupied[BLACK]);
|
||||||
printf("Bitboards controlled :\n");
|
// printf("Bitboards controlled :\n");
|
||||||
bitboard_print2(pos->controlled[WHITE], pos->controlled[BLACK]);
|
// bitboard_print2(pos->controlled[WHITE], pos->controlled[BLACK]);
|
||||||
|
//
|
||||||
}
|
//}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* pos_print() - Print position on stdout.
|
* pos_print() - Print position on stdout.
|
||||||
* @pos: &position
|
* @pos: &position
|
||||||
*/
|
*/
|
||||||
void pos_print(pos_t *pos)
|
void pos_print(position *pos)
|
||||||
{
|
{
|
||||||
int rank, file;
|
int rank, file;
|
||||||
piece_t piece;
|
piece pc, *board = pos->board;
|
||||||
board_t *board = pos->board;
|
|
||||||
piece_list_t *wk = list_first_entry(&pos->pieces[WHITE], piece_list_t, list),
|
//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);
|
// *bk = list_first_entry(&pos->pieces[BLACK], piece_list_t, list);
|
||||||
|
|
||||||
|
|
||||||
printf(" +---+---+---+---+---+---+---+---+\n");
|
printf(" +---+---+---+---+---+---+---+---+\n");
|
||||||
for (rank = 7; rank >= 0; --rank) {
|
for (rank = 7; rank >= 0; --rank) {
|
||||||
printf("%c |", rank + '1');
|
printf("%c |", rank + '1');
|
||||||
for (file = 0; file < 8; ++file) {
|
for (file = 0; file < 8; ++file) {
|
||||||
piece = board[SQ88(file, rank)].piece;
|
pc = board[BB(file, rank)];
|
||||||
printf(" %s |", P_CSYM(piece));
|
printf(" %s |", P_CSYM(pc));
|
||||||
}
|
}
|
||||||
printf("\n +---+---+---+---+---+---+---+---+\n");
|
printf("\n +---+---+---+---+---+---+---+---+\n");
|
||||||
}
|
}
|
||||||
printf(" A B C D E F G H\n\n");
|
printf(" A B C D E F G H\n\n");
|
||||||
printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black");
|
printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black");
|
||||||
printf("Kings: W:%c%c B:%c%c\n",
|
/*
|
||||||
FILE2C(F88(wk->square)),
|
* printf("Kings: W:%c%c B:%c%c\n",
|
||||||
RANK2C(R88(wk->square)),
|
* FILE2C(F88(wk->square)),
|
||||||
FILE2C(F88(bk->square)),
|
* RANK2C(R88(wk->square)),
|
||||||
RANK2C(R88(bk->square)));
|
* FILE2C(F88(bk->square)),
|
||||||
|
* RANK2C(R88(bk->square)));
|
||||||
|
*/
|
||||||
printf("Possible en-passant: [%#x] ", pos->en_passant);
|
printf("Possible en-passant: [%#x] ", pos->en_passant);
|
||||||
if (pos->en_passant == 0)
|
if (pos->en_passant == 0)
|
||||||
printf("None.\n");
|
printf("None.\n");
|
||||||
else
|
else
|
||||||
printf("%d %d = %c%c\n",
|
printf("%c%c.\n",
|
||||||
F88(pos->en_passant),
|
FILE2C(BBfile(pos->en_passant)),
|
||||||
R88(pos->en_passant),
|
RANK2C(BBrank(pos->en_passant)));
|
||||||
FILE2C(F88(pos->en_passant)),
|
|
||||||
RANK2C(R88(pos->en_passant)));
|
|
||||||
|
|
||||||
printf("castle [%#x] : ", pos->castle);
|
printf("castle [%#x] : ", pos->castle);
|
||||||
|
|
||||||
@@ -130,97 +164,94 @@ void pos_print(pos_t *pos)
|
|||||||
if (pos->castle & CASTLE_BQ)
|
if (pos->castle & CASTLE_BQ)
|
||||||
printf("q");
|
printf("q");
|
||||||
|
|
||||||
printf("\n50 half-moves-rule = %d\n", pos->clock_50);
|
printf("\nplies=%d clock50=%d\n", pos->plycount, pos->clock_50);
|
||||||
printf("Current move = %d\n", pos->curmove);
|
//printf("Current move = %d\n", pos->curmove);
|
||||||
printf("Squares controlled: W:%d B:%d\n", popcount64(pos->controlled[WHITE]),
|
//printf("Squares controlled: W:%d B:%d\n", popcount64(pos->controlled[WHITE]),
|
||||||
popcount64(pos->controlled[BLACK]));
|
// popcount64(pos->controlled[BLACK]));
|
||||||
printf("Mobility: W:%u B:%u\n", pos->mobility[WHITE],
|
//printf("Mobility: W:%u B:%u\n", pos->mobility[WHITE],
|
||||||
pos->mobility[BLACK]);
|
// pos->mobility[BLACK]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* pos_check() - extensive position consistenci check.
|
* pos_check() - extensive position consistenci check.
|
||||||
* @pos: &position
|
* @pos: &position
|
||||||
*/
|
*/
|
||||||
void pos_check(pos_t *pos)
|
/*
|
||||||
{
|
* void pos_check(position *pos)
|
||||||
int rank, file;
|
* {
|
||||||
piece_t piece;
|
* int rank, file;
|
||||||
board_t *board = pos->board;
|
* piece_t piece;
|
||||||
|
* board_t *board = pos->board;
|
||||||
/* check that board and bitboard reflect same information */
|
*
|
||||||
for (rank = 7; rank >= 0; --rank) {
|
* /\* check that board and bitboard reflect same information *\/
|
||||||
for (file = 0; file < 8; ++file) {
|
* for (rank = 7; rank >= 0; --rank) {
|
||||||
piece_list_t *ppiece;
|
* for (file = 0; file < 8; ++file) {
|
||||||
printf("checking %c%c ", file+'a', rank+'1');
|
* 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;
|
* piece = board[SQ88(file, rank)].piece;
|
||||||
printf("piece=%s ", P_CSYM(piece));
|
* ppiece= board[SQ88(file, rank)].s_piece;
|
||||||
if (ppiece)
|
* printf("piece=%s ", P_CSYM(piece));
|
||||||
printf("ppiece=%s/sq=%#x ", P_CSYM(ppiece->piece), ppiece->square);
|
* if (ppiece)
|
||||||
switch(PIECE(piece)) {
|
* printf("ppiece=%s/sq=%#x ", P_CSYM(ppiece->piece), ppiece->square);
|
||||||
case PAWN:
|
* switch(PIECE(piece)) {
|
||||||
printf("pawn" );
|
* case PAWN:
|
||||||
break;
|
* printf("pawn" );
|
||||||
case KNIGHT:
|
* break;
|
||||||
printf("knight ");
|
* case KNIGHT:
|
||||||
break;
|
* printf("knight ");
|
||||||
case BISHOP:
|
* break;
|
||||||
printf("bishop ");
|
* case BISHOP:
|
||||||
break;
|
* printf("bishop ");
|
||||||
case ROOK:
|
* break;
|
||||||
printf("rook ");
|
* case ROOK:
|
||||||
break;
|
* printf("rook ");
|
||||||
case QUEEN:
|
* break;
|
||||||
printf("queen ");
|
* case QUEEN:
|
||||||
break;
|
* printf("queen ");
|
||||||
case KING:
|
* break;
|
||||||
printf("king ");
|
* case KING:
|
||||||
break;
|
* printf("king ");
|
||||||
}
|
* break;
|
||||||
printf("\n");
|
* }
|
||||||
}
|
* 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;
|
|
||||||
}
|
position *pos_clear(position *pos)
|
||||||
}
|
{
|
||||||
|
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);
|
SET_WHITE(pos->turn);
|
||||||
pos->node_count = 0;
|
pos->node_count = 0;
|
||||||
pos->castle = 0;
|
pos->turn = 0;
|
||||||
pos->clock_50 = 0;
|
pos->clock_50 = 0;
|
||||||
pos->curmove = 0;
|
pos->plycount = 0;
|
||||||
pos->eval = 0;
|
|
||||||
pos->en_passant = 0;
|
pos->en_passant = 0;
|
||||||
pos->occupied[WHITE] = 0;
|
pos->castle = 0;
|
||||||
pos->occupied[BLACK] = 0;
|
memset(pos->board, 0, sizeof(pos->board));
|
||||||
for (int color=0; color<2; ++color)
|
//pos->curmove = 0;
|
||||||
for (int piece = BB_ALL; piece < BB_END; ++piece)
|
//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->bb[color][piece] = 0;
|
||||||
pos->controlled[WHITE] = 0;
|
pos->controlled[WHITE] = 0;
|
||||||
pos->controlled[BLACK] = 0;
|
pos->controlled[BLACK] = 0;
|
||||||
pos->mobility[WHITE] = 0;
|
}
|
||||||
pos->mobility[BLACK] = 0;
|
//pos->mobility[WHITE] = 0;
|
||||||
pos->moves_generated = false;
|
//pos->mobility[BLACK] = 0;
|
||||||
pos->moves_counted = false;
|
//pos->moves_generated = false;
|
||||||
|
//pos->moves_counted = false;
|
||||||
/* remove pieces / moves */
|
/* remove pieces / moves */
|
||||||
pieces_del(pos, WHITE);
|
//pieces_del(pos, WHITE);
|
||||||
pieces_del(pos, BLACK);
|
//pieces_del(pos, BLACK);
|
||||||
moves_del(pos);
|
//moves_del(pos);
|
||||||
|
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
@@ -229,35 +260,22 @@ pos_t *pos_clear(pos_t *pos)
|
|||||||
* pos_del() - delete a position.
|
* pos_del() - delete a position.
|
||||||
* @pos: &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);
|
position *pos = safe_malloc(sizeof(position));
|
||||||
pieces_del(pos, BLACK);
|
//assert(pos);
|
||||||
moves_del(pos);
|
return pos_clear(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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -275,44 +293,29 @@ pos_t *pos_get()
|
|||||||
*
|
*
|
||||||
* @return: The new position.
|
* @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;
|
position *newpos= pool_get(pos_pool);
|
||||||
piece_list_t *oldpiece;
|
|
||||||
board_t *board;
|
|
||||||
pos_t *new = pool_get(pos_pool);
|
|
||||||
|
|
||||||
if (new) {
|
if (newpos) {
|
||||||
board = new->board;
|
//board = new->board;
|
||||||
*new = *pos;
|
*newpos = *pos;
|
||||||
for (int color = 0; color < 2; ++color) {
|
//new->bestmove = NULL;
|
||||||
INIT_LIST_HEAD(&new->pieces[color]);
|
newpos->node_count = 0;
|
||||||
INIT_LIST_HEAD(&new->moves[color]);
|
//new->eval = EVAL_INVALID;
|
||||||
/* duplicate piece list */
|
//new->moves_generated = false;
|
||||||
piece_list = &pos->pieces[color]; /* white/black piece list */
|
//new->moves_counted = false;
|
||||||
|
//new->check[WHITE] = new->check[BLACK] = 0;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
return newpos;
|
||||||
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;
|
|
||||||
}
|
|
||||||
return new;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pool_t *pos_pool_init()
|
pool_t *pos_pool_init()
|
||||||
{
|
{
|
||||||
if (!pos_pool)
|
if (!pos_pool)
|
||||||
pos_pool = pool_create("positions", 128, sizeof(pos_t));
|
pos_pool = pool_create("positions", 128, sizeof(position));
|
||||||
return pos_pool;
|
return pos_pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
104
src/position.h
104
src/position.h
@@ -1,6 +1,6 @@
|
|||||||
/* position.h - position management definitions.
|
/* 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.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
* Some rights reserved. See COPYING.
|
* Some rights reserved. See COPYING.
|
||||||
*
|
*
|
||||||
@@ -16,51 +16,85 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "pool.h"
|
#include "brlib.h"
|
||||||
#include "list.h"
|
//#include "pool.h"
|
||||||
|
//#include "list.h"
|
||||||
#include "bitops.h"
|
#include "bitops.h"
|
||||||
|
#include "bitboard.h"
|
||||||
|
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
|
|
||||||
typedef struct pos_s {
|
typedef struct {
|
||||||
u64 node_count; /* evaluated nodes */
|
u64 node_count; /* evaluated nodes */
|
||||||
piece_t turn; /* we use only color bit */
|
int turn; /* WHITE or BLACK */
|
||||||
castle_t castle;
|
|
||||||
u16 clock_50;
|
u16 clock_50;
|
||||||
u16 curmove;
|
u16 plycount; /* plies so far, start is 0 */
|
||||||
eval_t eval;
|
square en_passant;
|
||||||
int check[2];
|
castle castle;
|
||||||
int eval_simple_phase;
|
|
||||||
eval_t eval_simple;
|
|
||||||
move_t *bestmove;
|
|
||||||
bool moves_generated;
|
|
||||||
bool moves_counted;
|
|
||||||
board_t board[BOARDSIZE];
|
|
||||||
|
|
||||||
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 bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */
|
||||||
bitboard_t occupied[2]; /* OR of bb[COLOR][x] */
|
bitboard controlled[2];
|
||||||
bitboard_t controlled[2];
|
//u16 mobility[2];
|
||||||
u16 mobility[2];
|
//struct list_head pieces[2]; /* pieces list, King is first */
|
||||||
struct list_head pieces[2]; /* pieces list, King is first */
|
//struct list_head moves[2];
|
||||||
struct list_head moves[2];
|
piece board[BOARDSIZE];
|
||||||
} pos_t;
|
} position;
|
||||||
|
|
||||||
void bitboard_print(bitboard_t bb);
|
/**
|
||||||
void bitboard_print2(bitboard_t bb1, bitboard_t bb2);
|
* pos_set_sq - unconditionally set a piece on a square
|
||||||
void pos_pieces_print(pos_t *pos);
|
* @pos: position
|
||||||
void pos_bitboards_print(pos_t *pos);
|
* @p, @c: type and color of piece to add
|
||||||
void pos_print(pos_t *pos);
|
* @f, @r: destination file and rank
|
||||||
pos_t *pos_clear(pos_t *pos);
|
*
|
||||||
void pos_del(pos_t *pos);
|
* Both position bords and bitboards are modified.
|
||||||
pos_t *pos_startpos(pos_t *pos);
|
*/
|
||||||
pos_t *pos_create();
|
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();
|
pool_t *pos_pool_init();
|
||||||
void pos_pool_stats();
|
void pos_pool_stats();
|
||||||
pos_t *pos_get();
|
position *pos_dup(position *pos);
|
||||||
pos_t *pos_dup(pos_t *pos);
|
//void pos_check(position *pos);
|
||||||
void pos_check(pos_t *pos);
|
|
||||||
|
|
||||||
#endif /* POSITION_H */
|
#endif /* POSITION_H */
|
||||||
|
44
src/util.c
Normal file
44
src/util.c
Normal file
@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
*/
|
60
src/util.h
Normal file
60
src/util.h
Normal file
@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _UTIL_H
|
||||||
|
#define _UTIL_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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 */
|
@@ -1,20 +1,22 @@
|
|||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "pool.h"
|
#include "pool.h"
|
||||||
|
|
||||||
#include "../src/position.h"
|
#include "../src/position.h"
|
||||||
#include "../src/fen.h"
|
#include "../src/fen.h"
|
||||||
|
|
||||||
int main(int ac, char**av)
|
int main(int ac, char**av)
|
||||||
{
|
{
|
||||||
pos_t *pos;
|
position *pos;
|
||||||
|
|
||||||
|
|
||||||
debug_init(5, stderr, true);
|
debug_init(5, stderr, true);
|
||||||
piece_pool_init();
|
|
||||||
pos_pool_init();
|
pos_pool_init();
|
||||||
pos = pos_get();
|
pos = pos_new();
|
||||||
if (ac == 1) {
|
if (ac == 1) {
|
||||||
pos_startpos(pos);
|
startpos(pos);
|
||||||
} else {
|
} else {
|
||||||
fen2pos(pos, av[1]);
|
fen2pos(pos, av[1]);
|
||||||
}
|
}
|
||||||
pos_print(pos);
|
pos_print(pos);
|
||||||
|
printf("fen=[%s]\n", pos2fen(pos, NULL));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user