Add player's pieces list management + change board def.
This commit is contained in:
8
Makefile
8
Makefile
@@ -5,7 +5,8 @@ SRCDIR=./src
|
|||||||
|
|
||||||
SRC=$(wildcard $(SRCDIR)/*.c)
|
SRC=$(wildcard $(SRCDIR)/*.c)
|
||||||
INC=$(wildcard $(SRCDIR)/*.h)
|
INC=$(wildcard $(SRCDIR)/*.h)
|
||||||
BIN=fen pool
|
|
||||||
|
BIN=fen pool piece
|
||||||
|
|
||||||
CFLAGS += -std=gnu99
|
CFLAGS += -std=gnu99
|
||||||
CFLAGS += -g
|
CFLAGS += -g
|
||||||
@@ -31,3 +32,8 @@ pool: CFLAGS+=-DPOOLBIN
|
|||||||
pool: $(SRC)
|
pool: $(SRC)
|
||||||
echo SRC=$(SRC)
|
echo SRC=$(SRC)
|
||||||
$(CC) $(CFLAGS) $? -o $@
|
$(CC) $(CFLAGS) $? -o $@
|
||||||
|
|
||||||
|
piece: CFLAGS+=-DPIECEBIN
|
||||||
|
piece: $(SRC)
|
||||||
|
echo SRC=$(SRC)
|
||||||
|
$(CC) $(CFLAGS) $? -o $@
|
||||||
|
@@ -19,8 +19,9 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
piece_t piece;
|
piece_t piece;
|
||||||
//struct piece *s_piece;
|
//piece_t *s_piece;
|
||||||
} board_t[8*8*2]; /* 0x88 board */
|
} board_t; /* 0x88 board */
|
||||||
|
#define BOARDSIZE (8*8*2)
|
||||||
|
|
||||||
/* definitions for 0x88 representation
|
/* definitions for 0x88 representation
|
||||||
*/
|
*/
|
||||||
|
@@ -14,6 +14,8 @@
|
|||||||
#ifndef CHESSDEFS_H
|
#ifndef CHESSDEFS_H
|
||||||
#define CHESSDEFS_H
|
#define CHESSDEFS_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
/* piece_t bits structure
|
/* piece_t bits structure
|
||||||
*/
|
*/
|
||||||
typedef unsigned char piece_t;
|
typedef unsigned char piece_t;
|
||||||
@@ -53,6 +55,7 @@ typedef unsigned char square_t;
|
|||||||
#define GET_R(s) ((s) & 0x0f)
|
#define GET_R(s) ((s) & 0x0f)
|
||||||
#define SET_F(s, f) ((s) &= 0x0f, (s) |= (f)<<4)
|
#define SET_F(s, f) ((s) &= 0x0f, (s) |= (f)<<4)
|
||||||
#define SET_R(s, r) ((s) &= 0xf0, (s) |= (r))
|
#define SET_R(s, r) ((s) &= 0xf0, (s) |= (r))
|
||||||
|
#define SQUARE(f, r) ((f) << 4 | (r))
|
||||||
|
|
||||||
/* castle_t bits structure
|
/* castle_t bits structure
|
||||||
*/
|
*/
|
||||||
@@ -66,5 +69,8 @@ typedef unsigned char castle_t;
|
|||||||
#define CASTLE_W 0x03 /* 00000011 W castle mask */
|
#define CASTLE_W 0x03 /* 00000011 W castle mask */
|
||||||
#define CASTLE_B 0x0C /* 00001100 B castle mask */
|
#define CASTLE_B 0x0C /* 00001100 B castle mask */
|
||||||
|
|
||||||
|
/* eval type
|
||||||
|
*/
|
||||||
|
typedef int64_t eval_t;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
43
src/fen.c
43
src/fen.c
@@ -21,6 +21,7 @@
|
|||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include "fen.h"
|
#include "fen.h"
|
||||||
|
#include "piece.h"
|
||||||
|
|
||||||
/* Starting Position :
|
/* Starting Position :
|
||||||
* rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
|
* rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
|
||||||
@@ -46,36 +47,41 @@
|
|||||||
pos_t *fen2pos(pos_t *pos, char *fen)
|
pos_t *fen2pos(pos_t *pos, char *fen)
|
||||||
{
|
{
|
||||||
char *p = fen;
|
char *p = fen;
|
||||||
short rank, file, skip;
|
short rank, file, skip, color;
|
||||||
|
piece_t piece;
|
||||||
board_t *board = pos->board;
|
board_t *board = pos->board;
|
||||||
# define SKIP_BLANK(p) for(;*(p) == ' '; (p)++)
|
# define SKIP_BLANK(p) for(;*(p) == ' '; (p)++)
|
||||||
|
|
||||||
/* 1) get piece placement information
|
/* 1) get piece placement information
|
||||||
*/
|
*/
|
||||||
for (rank = 7, file = 0; *p && *p != ' '; ++p) {
|
for (rank = 7, file = 0; *p && *p != ' '; ++p) {
|
||||||
|
color = !!islower(*p);
|
||||||
char cp = toupper(*p);
|
char cp = toupper(*p);
|
||||||
switch (cp) {
|
switch (cp) {
|
||||||
case CHAR_PAWN:
|
case CHAR_PAWN:
|
||||||
board[SQ88(file, rank)]->piece = PAWN;
|
piece = PAWN;
|
||||||
goto color;
|
goto set_square;
|
||||||
case CHAR_KNIGHT:
|
case CHAR_KNIGHT:
|
||||||
board[SQ88(file, rank)]->piece = KNIGHT;
|
piece = KNIGHT;
|
||||||
goto color;
|
goto set_square;
|
||||||
case CHAR_BISHOP:
|
case CHAR_BISHOP:
|
||||||
board[SQ88(file, rank)]->piece = BISHOP;
|
piece = BISHOP;
|
||||||
goto color;
|
goto set_square;
|
||||||
case CHAR_ROOK:
|
case CHAR_ROOK:
|
||||||
board[SQ88(file, rank)]->piece = ROOK;
|
piece = ROOK;
|
||||||
goto color;
|
goto set_square;
|
||||||
case CHAR_QUEEN:
|
case CHAR_QUEEN:
|
||||||
board[SQ88(file, rank)]->piece = QUEEN;
|
piece = QUEEN;
|
||||||
goto color;
|
goto set_square;
|
||||||
case CHAR_KING:
|
case CHAR_KING:
|
||||||
board[SQ88(file, rank)]->piece = KING;
|
piece = KING;
|
||||||
//goto color;
|
//goto set_square;
|
||||||
|
set_square:
|
||||||
//printf("f=%d r=%d *p=%c piece=%c\n", file, rank, *p, cp);
|
//printf("f=%d r=%d *p=%c piece=%c\n", file, rank, *p, cp);
|
||||||
color:
|
piece |= color;
|
||||||
SET_COLOR(board[SQ88(file, rank)]->piece, islower(*p));
|
//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;
|
//board[SQ88(file, rank)]->piece |= isupper(*p)? WHITE: BLACK;
|
||||||
file++;
|
file++;
|
||||||
break;
|
break;
|
||||||
@@ -87,16 +93,16 @@ pos_t *fen2pos(pos_t *pos, char *fen)
|
|||||||
skip = cp - '0';
|
skip = cp - '0';
|
||||||
file += skip;
|
file += skip;
|
||||||
while (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) {
|
for (file = 0; file < 8; ++file) {
|
||||||
printf("%2x ", board[SQ88(file, rank)]->piece);
|
printf("%2x ", board[SQ88(file, rank)]->piece);
|
||||||
}
|
}
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/* 2) next move color
|
/* 2) next move color
|
||||||
*/
|
*/
|
||||||
@@ -152,6 +158,7 @@ int main(int ac, char**av)
|
|||||||
{
|
{
|
||||||
pos_t *pos;
|
pos_t *pos;
|
||||||
|
|
||||||
|
piece_pool_init();
|
||||||
pos = pos_create();
|
pos = pos_create();
|
||||||
if (ac == 1) {
|
if (ac == 1) {
|
||||||
pos_startpos(pos);
|
pos_startpos(pos);
|
||||||
|
19
src/piece.h
19
src/piece.h
@@ -17,8 +17,20 @@
|
|||||||
#include "chessdefs.h"
|
#include "chessdefs.h"
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include "list.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;
|
piece_t piece;
|
||||||
square_t square;
|
square_t square;
|
||||||
short castle;
|
short castle;
|
||||||
@@ -26,4 +38,9 @@ typedef struct piece {
|
|||||||
struct list_head list;
|
struct list_head list;
|
||||||
} piece_list_t;
|
} 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
|
#endif
|
||||||
|
15
src/pool.c
15
src/pool.c
@@ -39,6 +39,8 @@ pool_t *pool_init(const char *name, uint32_t growsize, size_t eltsize)
|
|||||||
{
|
{
|
||||||
pool_t *pool;
|
pool_t *pool;
|
||||||
|
|
||||||
|
printf("%s: name=[%s] growsize=%u eltsize=%lu\n",
|
||||||
|
__func__, name, growsize, eltsize);
|
||||||
/* we need at least this space in struct */
|
/* we need at least this space in struct */
|
||||||
if (eltsize < sizeof (struct list_head))
|
if (eltsize < sizeof (struct list_head))
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -88,22 +90,25 @@ void *pool_get(pool_t *pool)
|
|||||||
if (!pool)
|
if (!pool)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!pool->available) {
|
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 *alloc = malloc(pool->eltsize * pool->growsize);
|
||||||
void *cur;
|
void *cur;
|
||||||
uint32_t i;
|
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)
|
if (!alloc)
|
||||||
return NULL;
|
return NULL;
|
||||||
pool->allocated += pool->growsize;
|
|
||||||
//printf(" (old=%u)\n", pool->allocated);
|
//printf(" (old=%u)\n", pool->allocated);
|
||||||
|
pool->allocated += pool->growsize;
|
||||||
|
//printf(" (new=%u)\n", pool->allocated);
|
||||||
for (i = 0; i < pool->growsize; ++i) {
|
for (i = 0; i < pool->growsize; ++i) {
|
||||||
cur = alloc + i * pool->eltsize;
|
cur = alloc + i * pool->eltsize;
|
||||||
printf("%s: alloc=%p cur=%p\n", __func__,
|
//printf("%s: alloc=%p cur=%p\n", __func__, alloc, cur);
|
||||||
alloc, cur);
|
|
||||||
_pool_add(pool, (struct list_head *)cur);
|
_pool_add(pool, (struct list_head *)cur);
|
||||||
}
|
}
|
||||||
|
pool_stats(pool);
|
||||||
}
|
}
|
||||||
//printf("%s: returning %p pointer\n", __func__, res);
|
//printf("%s: returning %p pointer\n", __func__, res);
|
||||||
return _pool_get(pool);
|
return _pool_get(pool);
|
||||||
|
@@ -21,7 +21,28 @@
|
|||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "fen.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);
|
piece_t piece = PIECE(p);
|
||||||
char res;
|
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)
|
void pos_print(pos_t *pos)
|
||||||
{
|
{
|
||||||
int rank, file;
|
int rank, file;
|
||||||
@@ -65,7 +91,7 @@ void pos_print(pos_t *pos)
|
|||||||
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;
|
piece = board[SQ88(file, rank)].piece;
|
||||||
printf(" %c |", piece2char(piece));
|
printf(" %c |", piece2char(piece));
|
||||||
}
|
}
|
||||||
printf("\n +---+---+---+---+---+---+---+---+\n");
|
printf("\n +---+---+---+---+---+---+---+---+\n");
|
||||||
@@ -101,10 +127,13 @@ pos_t *pos_init(pos_t *pos)
|
|||||||
int file, rank;
|
int file, rank;
|
||||||
board_t *board = pos->board;
|
board_t *board = pos->board;
|
||||||
|
|
||||||
for (rank = 0; rank < 8; ++rank) {
|
for (file = 0; file < 8; ++file) {
|
||||||
for (file = 0; file < 8; ++file) {
|
for (rank = 0; rank < 8; ++rank) {
|
||||||
printf("file = %d rank = %d SQ88 = %#x\n", file, rank, SQ88(file, rank));
|
/*printf("file = %d rank = %d SQ88 = %#2x = %d addr=%p\n", file, rank,
|
||||||
board[SQ88(file, rank)]->piece = EMPTY;
|
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->curmove = 0;
|
||||||
pos->en_passant = 0;
|
pos->en_passant = 0;
|
||||||
pos->en_passant = 0;
|
pos->en_passant = 0;
|
||||||
|
INIT_LIST_HEAD(&pos->pieces_white);
|
||||||
|
INIT_LIST_HEAD(&pos->pieces_black);
|
||||||
|
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
@@ -129,7 +160,9 @@ pos_t *pos_create()
|
|||||||
{
|
{
|
||||||
pos_t *pos = malloc(sizeof(pos_t));
|
pos_t *pos = malloc(sizeof(pos_t));
|
||||||
if (pos) {
|
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)
|
if (pos->board)
|
||||||
pos_init(pos);
|
pos_init(pos);
|
||||||
else {
|
else {
|
||||||
@@ -137,7 +170,5 @@ pos_t *pos_create()
|
|||||||
pos = NULL;
|
pos = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
INIT_LIST_HEAD(&pos->w_pieces);
|
|
||||||
INIT_LIST_HEAD(&pos->b_pieces);
|
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
@@ -25,10 +25,14 @@ typedef struct position {
|
|||||||
square_t en_passant;
|
square_t en_passant;
|
||||||
short clock_50;
|
short clock_50;
|
||||||
short curmove;
|
short curmove;
|
||||||
|
eval_t eval;
|
||||||
|
struct list_head pieces_white;
|
||||||
|
struct list_head pieces_black;
|
||||||
board_t *board;
|
board_t *board;
|
||||||
struct list_head w_pieces, b_pieces;
|
|
||||||
} pos_t;
|
} pos_t;
|
||||||
|
|
||||||
|
/* TODO: replace piece2string/piece2char with static array*/
|
||||||
|
char *piece2string(piece_t piece);
|
||||||
void pos_print(pos_t *pos);
|
void pos_print(pos_t *pos);
|
||||||
pos_t *pos_init(pos_t *pos);
|
pos_t *pos_init(pos_t *pos);
|
||||||
pos_t *pos_startpos(pos_t *pos);
|
pos_t *pos_startpos(pos_t *pos);
|
||||||
|
Reference in New Issue
Block a user