add board_print, board_print_mask

This commit is contained in:
2024-03-02 07:10:23 +01:00
parent dc6ceb3407
commit a499893f32
9 changed files with 185 additions and 73 deletions

View File

@@ -72,6 +72,8 @@ CPPFLAGS += -DDEBUG_EVAL # eval functions
CPPFLAGS += -DDEBUG_PIECE # piece list management
CPPFLAGS += -DDEBUG_SEARCH # move search
CPPFLAGS += -DDIAGRAM_SYM # diagram with symbols
# remove extraneous spaces (due to spaces before comments)
CPPFLAGS := $(strip $(CPPFLAGS))
@@ -248,9 +250,9 @@ $(CCLSROOT):
# also, if cclsfile is newer than sources, no need to clean objects file
# (and to run bear).
# maybe run cleanobj cleanlibobj in commands ?
$(CCLSFILE): cleanobj cleanbrlib $(SRC) $(LIBSRC) | $(CCLSROOT)
$(CCLSFILE): cleanobj cleanbrlib libs | $(CCLSROOT)
@echo "Generating ccls compile commands file ($@)."
@$(BEAR) -- $(MAKE) compile testing
@$(BEAR) -- $(MAKE) testing
##################################### valgrind (mem check)
.PHONY: memcheck

View File

@@ -21,7 +21,7 @@
#include "board.h"
#include "piece.h"
typedef u64 bitboard_t;
//typedef u64 bitboard_t;
/* mapping square -> bitboard */
extern bitboard_t bb_sq[64];
@@ -39,6 +39,8 @@ extern bitboard_t bb_rank[64], bb_file[64], bb_diag[64], bb_anti[64];
/* knight and king moves */
extern bitboard_t bb_knight[64], bb_king[64];
/* Unsure if it will work with all compilers. Use #define instead ?
*/
enum {
A1bb = mask(A1), A2bb = mask(A2), A3bb = mask(A3), A4bb = mask(A4),
A5bb = mask(A5), A6bb = mask(A6), A7bb = mask(A7), A8bb = mask(A8),

View File

@@ -11,10 +11,12 @@
*
*/
#include <stdio.h>
#include <ctype.h>
#include "brlib.h"
#include "board.h"
#include "bitboard.h"
static const char *sq_strings[] = {
"a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
@@ -50,3 +52,57 @@ square_t sq_from_string(const char *sqstr)
rank_t r = C2RANK(sqstr[1]);
return sq_coord_ok(f) && sq_coord_ok(r) ? sq_make(f, r): SQUARE_NONE;
}
/**
* board_print() - Print a board
* @board: &board_t to print
*/
void board_print(piece_t *board)
{
printf(" +---+---+---+---+---+---+---+---+\n");
for (int rank = 7; rank >= 0; --rank) {
printf("%c |", rank + '1');
for (int file = 0; file < 8; ++file) {
piece_t pc = board[sq_make(file, rank)];
# ifdef DIAGRAM_SYM
printf(" %s |", pc? piece_to_sym_color(pc): " ");
# else
printf(" %s |", pc? piece_to_sym_color(pc): " ");
# endif
}
printf("\n +---+---+---+---+---+---+---+---+\n");
}
printf(" A B C D E F G H\n");
}
/**
* board_print_mask() - Print a board position with some reversed squares
* @board: &board_t to print
* @mask: a bitboard indicating reverse color displayed squares
*
* Squares corresponding to @mask will be displayed in reverse colors.
*/
void board_print_mask(piece_t *board, bitboard_t mask)
{
// 6: blink
# define REVERSE "\e[7m▌"
# define RESET "▐\e[0m"
printf(" +---+---+---+---+---+---+---+---+\n");
for (int rank = 7; rank >= 0; --rank) {
printf("%c |", rank + '1');
for (int file = 0; file < 8; ++file) {
square_t sq = sq_make(file, rank);
piece_t pc = board[sq];
bitboard_t set = mask(sq) & mask;
printf("%s", set? REVERSE : " ");
# ifdef DIAGRAM_SYM
printf("%s", pc? piece_to_sym_color(pc): " ");
# else
printf("%s", pc? piece_to_char_color(pc): " ");
# endif
printf("%s|", set? RESET : " ");
}
printf("\n +---+---+---+---+---+---+---+---+\n");
}
printf(" A B C D E F G H\n");
}

View File

@@ -11,12 +11,14 @@
*
*/
#ifndef BOARD_H
#define BOARD_H
#ifndef _BOARD_H
#define _BOARD_H
#include "brlib.h"
#include "chessdefs.h"
#include "piece.h"
#include "bitboard.h"
/* a square is defined as
* rrrfff
@@ -24,43 +26,6 @@
#define SQ_FILEMASK (007) /* warning, octal */
#define SQ_RANKMASK (070)
typedef enum {
_SSQUARE_ = -1, /* force signed enum */
A1 = 0, B1, C1, D1, E1, F1, G1, H1,
A2, B2, C2, D2, E2, F2, G2, H2,
A3, B3, C3, D3, E3, F3, G3, H3,
A4, B4, C4, D4, E4, F4, G4, H4,
A5, B5, C5, D5, E5, F5, G5, H5,
A6, B6, C6, D6, E6, F6, G6, H6,
A7, B7, C7, D7, E7, F7, G7, H7,
A8, B8, C8, D8, E8, F8, G8, H8,
SQUARE_MAX = 64,
SQUARE_NONE = 64
} square_t;
typedef enum {
_SFILE_ = -1, /* force signed enum */
FILE_A = 0, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H,
FILE_MAX,
} file_t;
typedef enum {
_SRANK_ = -1, /* force signed enum */
RANK_1 = 0, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8,
RANK_MAX,
} rank_t;
typedef enum {
NORTH = 8,
EAST = 1,
SOUTH = -NORTH,
WEST = -EAST,
NORTH_EAST = (NORTH + EAST),
SOUTH_EAST = (SOUTH + EAST),
SOUTH_WEST = (SOUTH + WEST),
NORTH_WEST = (NORTH + WEST),
} dir_t;
/* flip a 0-63 square:
* Vertical: G8 (62) becomes G1 (6)
@@ -100,4 +65,7 @@ static __always_inline rank_t sq_rank(square_t square)
extern const char *sq_to_string(const square_t sq);
extern square_t sq_from_string(const char *sq_string);
#endif /* BOARD_H */
extern void board_print(piece_t *board);
extern void board_print_mask(piece_t *board, bitboard_t mask);
#endif /* _BOARD_H */

View File

@@ -18,7 +18,7 @@
#define ONE 1ull
#define C64(const_u64) const_u64##ULL
#define mask(i) ( ONE << (i) )
#define mask(i) ( (unsigned long long) (ONE << (i)) )
//typedef ushort board;
#define BOARDSIZE (8*8)
@@ -51,19 +51,54 @@ typedef enum {
typedef struct __pos_s pos_t;
typedef struct __movelist_s movelist_t;
/* bitboard
/* basic types
*/
//typedef u64 bitboard_t;
typedef u64 bitboard_t;
/* eval type
*/
//typedef s32 eval_t;
/* forward typedefs
/* forward enum definition is impossible in C11, to simplify
* cross-dependancies, all important enum are moved here.
*/
//typedef struct piece_list_s piece_list_t;
//typedef struct board_s board_t;
//typedef struct pos_s pos_t;
//typedef struct move_s move_t;
typedef enum {
_SSQUARE_ = -1, /* force signed enum */
A1 = 0, B1, C1, D1, E1, F1, G1, H1,
A2, B2, C2, D2, E2, F2, G2, H2,
A3, B3, C3, D3, E3, F3, G3, H3,
A4, B4, C4, D4, E4, F4, G4, H4,
A5, B5, C5, D5, E5, F5, G5, H5,
A6, B6, C6, D6, E6, F6, G6, H6,
A7, B7, C7, D7, E7, F7, G7, H7,
A8, B8, C8, D8, E8, F8, G8, H8,
SQUARE_MAX = 64,
SQUARE_NONE = 64
} square_t;
typedef enum {
_SFILE_ = -1, /* force signed enum */
FILE_A = 0, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H,
FILE_MAX,
} file_t;
typedef enum {
_SRANK_ = -1, /* force signed enum */
RANK_1 = 0, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8,
RANK_MAX,
} rank_t;
typedef enum {
NORTH = 8,
EAST = 1,
SOUTH = -NORTH,
WEST = -EAST,
NORTH_EAST = (NORTH + EAST),
SOUTH_EAST = (SOUTH + EAST),
SOUTH_WEST = (SOUTH + WEST),
NORTH_WEST = (NORTH + WEST),
} dir_t;
#endif /* _CHESSDEFS_H */

View File

@@ -35,7 +35,7 @@ typedef enum {
PIECE_TYPE_MAX = 7 /* bit 4 */
} piece_type_t;
typedef enum {
typedef enum __piece_e {
EMPTY = 0,
NO_PIECE = 0,
W_PAWN = PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,

View File

@@ -27,12 +27,7 @@
#include "fen.h"
#include "piece.h"
#include "util.h"
//#include "eval.h"
//static pool_t *pos_pool;
//const char *rankstr = "12345678";
//const char *filestr = "ABCDEFGH";
#include "board.h"
/****************************************************
* #define BYTE_PRINT "%c%c%c%c%c%c%c%c" *
@@ -49,7 +44,10 @@
/**
* pos_new() - allocate a new position
*
* position is not initialized
* Position is not initialized.
* If BUG_ON compilation is defined, the program will fail.
*
* @Return: The new position or NULL.
*/
pos_t *pos_new(void)
{
@@ -69,7 +67,7 @@ pos_t *pos_new(void)
* - moves_generated ans moves_counted are unset
* - check is set to zero
*
* @return: The new position.
* @Return: The new position.
*
* TODO: merge with pos_new - NULL for init, non null for duplicate
*/
@@ -147,24 +145,70 @@ pos_t *pos_clear(pos_t *pos)
*/
void pos_print(pos_t *pos)
{
int rank, file;
piece_t pc, *board = pos->board;
//int rank, file;
//piece_t *board = pos->board;
char fen[92];
//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);
board_print(pos->board);
/*
* printf(" +---+---+---+---+---+---+---+---+\n");
* for (rank = 7; rank >= 0; --rank) {
* printf("%c |", rank + '1');
* for (file = 0; file < 8; ++file) {
* pc = board[sq_make(file, rank)];
* printf(" %s |", pc? piece_to_sym_color(pc): " ");
* }
* printf("\n +---+---+---+---+---+---+---+---+\n");
* }
* printf(" A B C D E F G H\n");
*/
printf("fen %s\n", pos2fen(pos, fen));
//printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black");
/*
* printf("Kings: W:%c%c B:%c%c\n",
* FILE2C(F88(wk->square)),
* RANK2C(R88(wk->square)),
* FILE2C(F88(bk->square)),
* RANK2C(R88(bk->square)));
*/
//printf("plies=%d clock50=%d\n", pos->plycount, pos->clock_50);
//printf("Current move = %d\n", pos->curmove);
//printf("Squares controlled: W:%d B:%d\n", popcount64(pos->controlled[WHITE]),
// popcount64(pos->controlled[BLACK]));
//printf("Mobility: W:%u B:%u\n", pos->mobility[WHITE],
// pos->mobility[BLACK]);
}
printf(" +---+---+---+---+---+---+---+---+\n");
for (rank = 7; rank >= 0; --rank) {
printf("%c |", rank + '1');
for (file = 0; file < 8; ++file) {
pc = board[sq_make(file, rank)];
printf(" %s |", pc? piece_to_sym_color(pc): " ");
}
printf("\n +---+---+---+---+---+---+---+---+\n");
}
printf(" A B C D E F G H\n");
/**
* pos_print_mask() - Print position and fen on stdout, with highlighted squares.
* @pos: &position
* @mask: mask of highlighted squares.
*/
void pos_print_mask(pos_t *pos, bitboard_t mask)
{
//int rank, file;
//piece_t pc, *board = pos->board;
char fen[92];
//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);
board_print_mask(pos->board, mask);
/*
* printf(" +---+---+---+---+---+---+---+---+\n");
* for (rank = 7; rank >= 0; --rank) {
* printf("%c |", rank + '1');
* for (file = 0; file < 8; ++file) {
* pc = board[sq_make(file, rank)];
* printf(" %s |", pc? piece_to_sym_color(pc): " ");
* }
* printf("\n +---+---+---+---+---+---+---+---+\n");
* }
* printf(" A B C D E F G H\n");
*/
printf("fen %s\n", pos2fen(pos, fen));
//printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black");
/*

View File

@@ -23,6 +23,7 @@
#include "bitboard.h"
#include "piece.h"
#include "move.h"
#include "board.h"
typedef struct __pos_s {
u64 node_count; /* evaluated nodes */
@@ -97,6 +98,7 @@ extern void pos_del(pos_t *pos);
extern pos_t *pos_clear(pos_t *pos);
extern void pos_print(pos_t *pos);
extern void pos_print_mask(pos_t *pos, bitboard_t mask);
extern void pos_pieces_print(pos_t *pos);
extern void pos_print_board_raw(const pos_t *pos, int type);

View File

@@ -10,7 +10,7 @@
int main(int ac, char**av)
{
pos_t *pos;
bitboard_t mask = A1bb | C3bb | A8bb | G7bb | H8bb | H1bb;
const char *fen;
char revfen[128];
int comp;
@@ -26,6 +26,9 @@ int main(int ac, char**av)
fen2pos(pos, fen);
}
pos_print(pos);
pos_print_mask(pos, mask);
printf("ULL=#%lx %#lx %#lx %#lx #%lx\n", A5bb, H5bb, H6bb, H7bb, H8bb);
printf("ULL=%llx %llx %llx\n", mask(A5), mask(H7), mask(H8));
pos2fen(pos, revfen);
//printf("reverse fen=[%s]\n", pos2fen(pos, NULL));