diff --git a/Makefile b/Makefile index e8cfdd6..62bc157 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ SRCDIR=./src SRC=$(wildcard $(SRCDIR)/*.c) INC=$(wildcard $(SRCDIR)/*.h) -BIN=fen pool + +BIN=fen pool piece CFLAGS += -std=gnu99 CFLAGS += -g @@ -31,3 +32,8 @@ pool: CFLAGS+=-DPOOLBIN pool: $(SRC) echo SRC=$(SRC) $(CC) $(CFLAGS) $? -o $@ + +piece: CFLAGS+=-DPIECEBIN +piece: $(SRC) + echo SRC=$(SRC) + $(CC) $(CFLAGS) $? -o $@ diff --git a/src/board.h b/src/board.h index 05f3097..fc6f641 100644 --- a/src/board.h +++ b/src/board.h @@ -19,8 +19,9 @@ typedef struct { piece_t piece; - //struct piece *s_piece; -} board_t[8*8*2]; /* 0x88 board */ + //piece_t *s_piece; +} board_t; /* 0x88 board */ +#define BOARDSIZE (8*8*2) /* definitions for 0x88 representation */ diff --git a/src/chessdefs.h b/src/chessdefs.h index e91f918..be221a1 100644 --- a/src/chessdefs.h +++ b/src/chessdefs.h @@ -14,6 +14,8 @@ #ifndef CHESSDEFS_H #define CHESSDEFS_H +#include + /* piece_t bits structure */ typedef unsigned char piece_t; @@ -53,6 +55,7 @@ typedef unsigned char square_t; #define GET_R(s) ((s) & 0x0f) #define SET_F(s, f) ((s) &= 0x0f, (s) |= (f)<<4) #define SET_R(s, r) ((s) &= 0xf0, (s) |= (r)) +#define SQUARE(f, r) ((f) << 4 | (r)) /* castle_t bits structure */ @@ -66,5 +69,8 @@ typedef unsigned char castle_t; #define CASTLE_W 0x03 /* 00000011 W castle mask */ #define CASTLE_B 0x0C /* 00001100 B castle mask */ +/* eval type + */ +typedef int64_t eval_t; #endif diff --git a/src/fen.c b/src/fen.c index 65b54a5..c7df0dd 100644 --- a/src/fen.c +++ b/src/fen.c @@ -21,6 +21,7 @@ #include "position.h" #include "board.h" #include "fen.h" +#include "piece.h" /* Starting Position : * rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 @@ -46,36 +47,41 @@ pos_t *fen2pos(pos_t *pos, char *fen) { char *p = fen; - short rank, file, skip; + short rank, file, skip, color; + piece_t piece; board_t *board = pos->board; # define SKIP_BLANK(p) for(;*(p) == ' '; (p)++) /* 1) get piece placement information */ for (rank = 7, file = 0; *p && *p != ' '; ++p) { + color = !!islower(*p); char cp = toupper(*p); switch (cp) { case CHAR_PAWN: - board[SQ88(file, rank)]->piece = PAWN; - goto color; + piece = PAWN; + goto set_square; case CHAR_KNIGHT: - board[SQ88(file, rank)]->piece = KNIGHT; - goto color; + piece = KNIGHT; + goto set_square; case CHAR_BISHOP: - board[SQ88(file, rank)]->piece = BISHOP; - goto color; + piece = BISHOP; + goto set_square; case CHAR_ROOK: - board[SQ88(file, rank)]->piece = ROOK; - goto color; + piece = ROOK; + goto set_square; case CHAR_QUEEN: - board[SQ88(file, rank)]->piece = QUEEN; - goto color; + piece = QUEEN; + goto set_square; case CHAR_KING: - board[SQ88(file, rank)]->piece = KING; - //goto color; + piece = KING; + //goto set_square; + set_square: //printf("f=%d r=%d *p=%c piece=%c\n", file, rank, *p, cp); - color: - SET_COLOR(board[SQ88(file, rank)]->piece, islower(*p)); + piece |= color; + //board[SQ88(file, rank)]->piece = piece; + board[SQ88(file, rank)].piece = piece; + piece_add(pos, piece, SQUARE(file, rank)); //board[SQ88(file, rank)]->piece |= isupper(*p)? WHITE: BLACK; file++; break; @@ -87,16 +93,16 @@ pos_t *fen2pos(pos_t *pos, char *fen) skip = cp - '0'; file += skip; while (skip--) { - board[SQ88(file, rank)]->piece = EMPTY; + board[SQ88(file, rank)].piece = EMPTY; } } } - for (rank = 7; rank >= 0; --rank) { + /*for (rank = 7; rank >= 0; --rank) { for (file = 0; file < 8; ++file) { printf("%2x ", board[SQ88(file, rank)]->piece); } putchar('\n'); - } + }*/ /* 2) next move color */ @@ -152,6 +158,7 @@ int main(int ac, char**av) { pos_t *pos; + piece_pool_init(); pos = pos_create(); if (ac == 1) { pos_startpos(pos); diff --git a/src/piece.h b/src/piece.h index 67d6075..650424e 100644 --- a/src/piece.h +++ b/src/piece.h @@ -17,8 +17,20 @@ #include "chessdefs.h" #include "board.h" #include "list.h" +#include "position.h" +#include "pool.h" -typedef struct piece { +#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 KING_VALUE 20000 + +typedef struct { piece_t piece; square_t square; short castle; @@ -26,4 +38,9 @@ typedef struct piece { struct list_head list; } piece_list_t; +void piece_list_print(struct list_head *list); +void pieces_print_pos_pieces(pos_t *pos); +pool_t *piece_pool_init(); +piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square); + #endif diff --git a/src/pool.c b/src/pool.c index 7b60b14..af866ae 100644 --- a/src/pool.c +++ b/src/pool.c @@ -39,6 +39,8 @@ pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize) { pool_t *pool; + printf("%s: name=[%s] growsize=%u eltsize=%lu\n", + __func__, name, growsize, eltsize); /* we need at least this space in struct */ if (eltsize < sizeof (struct list_head)) return NULL; @@ -88,22 +90,25 @@ void *pool_get(pool_t *pool) if (!pool) return NULL; if (!pool->available) { - printf("+++ %s [%s]: allocating new pool (old=%u)\n", __func__, pool->name, - pool->allocated); void *alloc = malloc(pool->eltsize * pool->growsize); void *cur; uint32_t i; + printf("+++ %s [%s]: growing pool from %u to %u elements.\n", __func__, + pool->name, + pool->allocated, + pool->allocated + pool->growsize); if (!alloc) return NULL; - pool->allocated += pool->growsize; //printf(" (old=%u)\n", pool->allocated); + pool->allocated += pool->growsize; + //printf(" (new=%u)\n", pool->allocated); for (i = 0; i < pool->growsize; ++i) { cur = alloc + i * pool->eltsize; - printf("%s: alloc=%p cur=%p\n", __func__, - alloc, cur); + //printf("%s: alloc=%p cur=%p\n", __func__, alloc, cur); _pool_add(pool, (struct list_head *)cur); } + pool_stats(pool); } //printf("%s: returning %p pointer\n", __func__, res); return _pool_get(pool); diff --git a/src/position.c b/src/position.c index 20ed58f..3f4a506 100644 --- a/src/position.c +++ b/src/position.c @@ -21,7 +21,28 @@ #include "position.h" #include "fen.h" -inline static char piece2char(unsigned char p) +char *piece2string(piece_t p) +{ + piece_t piece = PIECE(p); + + switch (piece) { + case PAWN: + return "Pawn"; + case KNIGHT: + return "Knight"; + case BISHOP: + return "Bishop"; + case ROOK: + return "Rook"; + case QUEEN: + return "Queen"; + case KING: + return "King"; + } + return "Unknown"; +} + +inline static char piece2char(piece_t p) { piece_t piece = PIECE(p); char res; @@ -55,6 +76,11 @@ inline static char piece2char(unsigned char p) } +/* void pos_print - Print position on stdout. + * @pos: Position address (pos_t * ) + * + * Return: None. + */ void pos_print(pos_t *pos) { int rank, file; @@ -65,7 +91,7 @@ void pos_print(pos_t *pos) for (rank = 7; rank >= 0; --rank) { printf("%c |", rank + '1'); for (file = 0; file < 8; ++file) { - piece = board[SQ88(file, rank)]->piece; + piece = board[SQ88(file, rank)].piece; printf(" %c |", piece2char(piece)); } printf("\n +---+---+---+---+---+---+---+---+\n"); @@ -101,10 +127,13 @@ pos_t *pos_init(pos_t *pos) int file, rank; board_t *board = pos->board; - for (rank = 0; rank < 8; ++rank) { - for (file = 0; file < 8; ++file) { - printf("file = %d rank = %d SQ88 = %#x\n", file, rank, SQ88(file, rank)); - board[SQ88(file, rank)]->piece = EMPTY; + 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; } } @@ -114,6 +143,8 @@ pos_t *pos_init(pos_t *pos) pos->curmove = 0; pos->en_passant = 0; pos->en_passant = 0; + INIT_LIST_HEAD(&pos->pieces_white); + INIT_LIST_HEAD(&pos->pieces_black); return pos; } @@ -129,7 +160,9 @@ pos_t *pos_create() { pos_t *pos = malloc(sizeof(pos_t)); if (pos) { - pos->board = malloc(sizeof (board_t)); + //printf("sizeof(board)=%lu\n", sizeof (board_t)); + pos->board = malloc(sizeof (board_t)*BOARDSIZE); + //printf("board mem: %p-%p\n", pos->board, pos->board+sizeof (board_t)); if (pos->board) pos_init(pos); else { @@ -137,7 +170,5 @@ pos_t *pos_create() pos = NULL; } } - INIT_LIST_HEAD(&pos->w_pieces); - INIT_LIST_HEAD(&pos->b_pieces); return pos; } diff --git a/src/position.h b/src/position.h index 3cf95ca..11baf7c 100644 --- a/src/position.h +++ b/src/position.h @@ -25,10 +25,14 @@ typedef struct position { square_t en_passant; short clock_50; short curmove; + eval_t eval; + struct list_head pieces_white; + struct list_head pieces_black; board_t *board; - struct list_head w_pieces, b_pieces; } pos_t; +/* TODO: replace piece2string/piece2char with static array*/ +char *piece2string(piece_t piece); void pos_print(pos_t *pos); pos_t *pos_init(pos_t *pos); pos_t *pos_startpos(pos_t *pos);