refactor position: now contains board (not board *)

This commit is contained in:
2021-11-12 20:22:20 +01:00
parent bd7d9f8551
commit 451afea6b5
11 changed files with 54 additions and 17 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
core core
vgcore.*
GPATH GPATH
GRTAGS GRTAGS
GTAGS GTAGS

View File

@@ -26,7 +26,7 @@ BIN=fen pool piece move debug eval bits
CFLAGS += -std=gnu99 CFLAGS += -std=gnu99
CFLAGS += -O2 #CFLAGS += -O2
CFLAGS += -g CFLAGS += -g
CFLAGS += -Wall CFLAGS += -Wall
CFLAGS += -Wextra CFLAGS += -Wextra
@@ -67,7 +67,7 @@ $(DEPS): $(SRC) $(INC)
include $(DEPS) include $(DEPS)
clean: clean:
rm -rf $(OBJ) core $(BIN) $(DEPS) rm -rf $(OBJ) core $(BIN)
#$(OBJ): $(OBJDIR)/%.o: $(SRCDIR)/%.c #$(OBJ): $(OBJDIR)/%.o: $(SRCDIR)/%.c
# @mkdir -p $(@D) # @mkdir -p $(@D)

View File

@@ -4,10 +4,10 @@
src/debug.h src/board.h src/piece.h src/list.h src/pool.h src/debug.h src/board.h src/piece.h src/list.h src/pool.h
./obj/fen.o:: src/fen.c src/debug.h src/chessdefs.h src/bits.h src/position.h \ ./obj/fen.o:: src/fen.c src/debug.h src/chessdefs.h src/bits.h src/position.h \
src/board.h src/piece.h src/list.h src/pool.h src/fen.h src/board.h src/piece.h src/list.h src/pool.h src/fen.h
./obj/move.o:: src/move.c src/chessdefs.h src/bits.h src/debug.h src/piece.h \ ./obj/move.o:: src/move.c src/chessdefs.h src/bits.h src/debug.h src/board.h \
src/board.h src/position.h src/pool.h src/list.h src/move.h src/piece.h src/list.h src/pool.h src/move.h src/position.h
./obj/piece.o:: src/piece.c src/chessdefs.h src/bits.h src/debug.h src/piece.h \ ./obj/piece.o:: src/piece.c src/chessdefs.h src/bits.h src/debug.h src/piece.h \
src/board.h src/position.h src/pool.h src/list.h src/list.h src/pool.h src/board.h src/position.h
./obj/pool.o:: src/pool.c src/list.h src/pool.h src/debug.h ./obj/pool.o:: src/pool.c src/list.h src/pool.h src/debug.h
./obj/position.o:: src/position.c src/chessdefs.h src/bits.h src/debug.h \ ./obj/position.o:: src/position.c src/chessdefs.h src/bits.h src/debug.h \
src/position.h src/board.h src/piece.h src/list.h src/pool.h src/fen.h src/position.h src/board.h src/piece.h src/list.h src/pool.h src/fen.h

View File

@@ -17,7 +17,6 @@
#include <stdint.h> #include <stdint.h>
#include "chessdefs.h" #include "chessdefs.h"
#include "piece.h" #include "piece.h"
#include "position.h"
typedef struct board_s { typedef struct board_s {
piece_t piece; piece_t piece;

View File

@@ -14,6 +14,7 @@
#include <malloc.h> #include <malloc.h>
#include <ctype.h> #include <ctype.h>
#include "chessdefs.h" #include "chessdefs.h"
#include "board.h"
#include "piece.h" #include "piece.h"
#include "move.h" #include "move.h"
#include "list.h" #include "list.h"
@@ -149,6 +150,7 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
move->to = to; move->to = to;
move->taken = board[to].piece; move->taken = board[to].piece;
move->flags = M_NORMAL; move->flags = M_NORMAL;
move->pos = NULL;
if (move->taken) if (move->taken)
move->flags |= M_CAPTURE; move->flags |= M_CAPTURE;
list_add(&move->list, &pos->moves); list_add(&move->list, &pos->moves);
@@ -472,6 +474,16 @@ int moves_gen(pos_t *pos, bool color, bool doit)
return count; return count;
} }
int move_doit(pos_t *pos, move_t *move)
{
# ifdef DEBUG_MOVE_TOTO
log_f(1, "color:%s doit:%d\n", color? "Black": "White", doit);
# endif
if (pos && move)
return 1;
return 0;
}
#ifdef BIN_move #ifdef BIN_move
#include "fen.h" #include "fen.h"
int main(int ac, char**av) int main(int ac, char**av)

View File

@@ -60,4 +60,6 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *piece, bool doit);
int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit); int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit);
int moves_gen(pos_t *pos, bool color, bool doit); int moves_gen(pos_t *pos, bool color, bool doit);
int move_doit(pos_t *pos, move_t *move);
#endif /* MODE_H */ #endif /* MODE_H */

View File

@@ -12,10 +12,13 @@
*/ */
#include <malloc.h> #include <malloc.h>
#include <ctype.h>
#include "chessdefs.h" #include "chessdefs.h"
#include "piece.h" #include "piece.h"
#include "ctype.h" #include "board.h"
#include "debug.h" #include "debug.h"
#include "position.h"
static pool_t *pieces_pool; static pool_t *pieces_pool;

View File

@@ -17,9 +17,7 @@
#include <ctype.h> #include <ctype.h>
#include "chessdefs.h" #include "chessdefs.h"
#include "board.h"
#include "list.h" #include "list.h"
#include "position.h"
#include "pool.h" #include "pool.h"
#define PIECE_DEFAULT_VALUE 0 #define PIECE_DEFAULT_VALUE 0

View File

@@ -168,14 +168,35 @@ pos_t *pos_get()
pos_t *pos = pool_get(pos_pool); pos_t *pos = pool_get(pos_pool);
if (pos) { if (pos) {
//printf("sizeof(board)=%lu\n", sizeof (board_t)); //printf("sizeof(board)=%lu\n", sizeof (board_t));
pos->board = malloc(sizeof (board_t)*BOARDSIZE); //pos->board = malloc(sizeof (board_t)*BOARDSIZE);
//printf("board mem: %p-%p\n", pos->board, pos->board+sizeof (board_t));
//if (pos->board)
pos_clear(pos);
//else {
// free(pos);
// pos = NULL;
//}
}
return pos;
}
/* TODO: merge with pos_get - NULL for init, non null for duplicate */
pos_t *pos_dup(pos_t *pos)
{
pos_t *new = pool_get(pos_pool);
if (new) {
//printf("sizeof(board)=%lu\n", sizeof (board_t));
//new->board = malloc(sizeof (board_t)*BOARDSIZE);
//if (!new->board) {
// pool_add(pos_pool, new);
// return NULL;
//}
*new = *pos;
INIT_LIST_HEAD(&new->pieces[WHITE]);
INIT_LIST_HEAD(&new->pieces[BLACK]);
INIT_LIST_HEAD(&new->moves);
//printf("board mem: %p-%p\n", pos->board, pos->board+sizeof (board_t)); //printf("board mem: %p-%p\n", pos->board, pos->board+sizeof (board_t));
if (pos->board)
pos_clear(pos);
else {
free(pos);
pos = NULL;
}
} }
return pos; return pos;
} }

View File

@@ -27,7 +27,7 @@ typedef struct pos_s {
u16 curmove; u16 curmove;
u16 mobility[2]; u16 mobility[2];
eval_t eval; eval_t eval;
board_t *board; board_t board[BOARDSIZE];
square_t en_passant; square_t en_passant;
square_t king[2]; square_t king[2];
@@ -46,5 +46,6 @@ pos_t *pos_startpos(pos_t *pos);
pos_t *pos_create(); pos_t *pos_create();
pool_t *pos_pool_init(); pool_t *pos_pool_init();
pos_t *pos_get(); pos_t *pos_get();
pos_t *pos_dup(pos_t *pos);
#endif #endif