simplify piece_details structure/functions (need to rewrite)

This commit is contained in:
2024-03-15 09:08:49 +01:00
parent 3e477f7442
commit 0f06ccb8db
4 changed files with 81 additions and 138 deletions

View File

@@ -65,9 +65,9 @@ void board_print(const piece_t *board)
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): " ");
printf(" %s |", pc? piece_to_sym(pc): " ");
# else
printf(" %s |", pc? piece_to_sym_color(pc): " ");
printf(" %s |", pc? piece_to_fen(pc): " ");
# endif
}
printf("\n +---+---+---+---+---+---+---+---+\n");
@@ -96,7 +96,7 @@ void board_print_mask(const piece_t *board, const bitboard_t mask)
bitboard_t set = mask(sq) & mask;
printf("%s", set? REVERSE : " ");
# ifdef DIAGRAM_SYM
printf("%s", pc? piece_to_sym_color(pc): " ");
printf("%s", pc? piece_to_sym(pc): " ");
# else
printf("%s", pc? piece_to_char_color(pc): " ");
# endif
@@ -118,7 +118,7 @@ void board_print_raw(const piece_t *board, const int type)
for (file_t f = FILE_A; f <= FILE_H; ++f) {
piece_t p = board[sq_make(f, r)];
if (type) {
printf("%s ", p == EMPTY? ".": piece_to_char_color(p));
printf("%s ", p == EMPTY? ".": piece_to_char(p));
} else {
printf("%02o ", p);
}

View File

@@ -184,9 +184,9 @@ pos_t *fen2pos(pos_t *pos, const char *fen)
file += *cur - '0';
continue;
}
if ((piece = char_color_to_piece(*cur)) != EMPTY) {
if ((piece = piece_from_fen(*cur)) != EMPTY) {
# ifdef DEBUG_FEN
log_i(5, "f=%d r=%d *p=%c piece=%#04x t=%d c=%d\n", file, rank, *cur,
printf("f=%d r=%d *p=%c piece=%#04x t=%d c=%d\n", file, rank, *cur,
piece, PIECE(piece), COLOR(piece));
# endif
pos_set_sq(&tmppos, sq_make(file, rank), piece);
@@ -296,13 +296,23 @@ char *pos2fen(const pos_t *pos, char *fen)
for (file_t f = FILE_A; f <= FILE_H;) {
square_t sq = sq_make(f, r);
piece_t piece = pos->board[sq];
if (pos->board[sq] == EMPTY) {
# ifdef DEBUG_FEN
printf("r=%d f=%d p=%d pos=%d\n", r, f, piece, cur);
# endif
if (piece == EMPTY) {
int len = 0;
for (; f <= FILE_H && pos->board[sq_make(f, r)] == EMPTY; f++)
len++;
# ifdef DEBUG_FEN
printf("empty=%d char=%c\n", len, '0' + len);
# endif
fen[cur++] = '0' + len;
} else {
fen[cur++] = *piece_to_char_color(piece);
fen[cur++] = *piece_to_fen(piece);
# ifdef DEBUG_FEN
printf("f1=%d r=%d c=%c t=%d c=%d \n", f, r,
*(piece_to_fen(piece)), PIECE(piece), COLOR(piece));
# endif
f++;
}
}

View File

@@ -25,22 +25,22 @@
* piece_details
*/
const struct piece_details piece_details[PIECE_MAX] = {
/* Abb Fen Sym SymC Name start value */
[EMPTY] = { "", "", "", "", "", 0, 0, 0 },
[W_PAWN] = { "", "P", "", "", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
[W_KNIGHT] = { "N", "N", "", "", "Knight", N_VAL_OPN, N_VAL_MID, N_VAL_END },
[W_BISHOP] = { "B", "B", "", "", "Bishop", B_VAL_OPN, B_VAL_MID, B_VAL_END },
[W_ROOK] = { "R", "R", "", "", "Rook", R_VAL_OPN, R_VAL_MID, R_VAL_END },
[W_QUEEN] = { "Q", "Q", "", "", "Queen", Q_VAL_OPN, Q_VAL_MID, Q_VAL_END },
[W_KING] = { "K", "K", "", "", "King", K_VAL_OPN, K_VAL_MID, K_VAL_END },
[7] = { "", "", "", "", "", 0, 0, 0 },
[8] = { "", "", "", "", "", 0, 0, 0 },
[B_PAWN] = { "", "p", "", "", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
[B_KNIGHT] = { "N", "n", "", "", "Knight", P_VAL_OPN, N_VAL_MID, N_VAL_END },
[B_BISHOP] = { "B", "b", "", "", "Bishop", P_VAL_OPN, B_VAL_MID, B_VAL_END },
[B_ROOK] = { "R", "r", "", "", "Rook", P_VAL_OPN, R_VAL_MID, R_VAL_END },
[B_QUEEN] = { "Q", "q", "", "", "Queen", P_VAL_OPN, Q_VAL_MID, Q_VAL_END },
[B_KING] = { "K", "k", "", "", "King", P_VAL_OPN, K_VAL_MID, K_VAL_END },
/* cap low fen sym name values */
[EMPTY] = { "", "", "", "", "", 0, 0, 0 },
[W_PAWN] = { "", "", "P", "", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
[W_KNIGHT] = { "N", "n", "N", "", "Knight", N_VAL_OPN, N_VAL_MID, N_VAL_END },
[W_BISHOP] = { "B", "b", "B", "", "Bishop", B_VAL_OPN, B_VAL_MID, B_VAL_END },
[W_ROOK] = { "R", "r", "R", "", "Rook", R_VAL_OPN, R_VAL_MID, R_VAL_END },
[W_QUEEN] = { "Q", "q", "Q", "", "Queen", Q_VAL_OPN, Q_VAL_MID, Q_VAL_END },
[W_KING] = { "K", "k", "K", "", "King", K_VAL_OPN, K_VAL_MID, K_VAL_END },
[7] = { "", "", "", "", "", 0, 0, 0 },
[8] = { "", "", "", "", "", 0, 0, 0 },
[B_PAWN] = { "", "", "p", "", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
[B_KNIGHT] = { "N", "n", "n", "", "Knight", P_VAL_OPN, N_VAL_MID, N_VAL_END },
[B_BISHOP] = { "B", "b", "b", "", "Bishop", P_VAL_OPN, B_VAL_MID, B_VAL_END },
[B_ROOK] = { "R", "r", "r", "", "Rook", P_VAL_OPN, R_VAL_MID, R_VAL_END },
[B_QUEEN] = { "Q", "q", "q", "", "Queen", P_VAL_OPN, Q_VAL_MID, Q_VAL_END },
[B_KING] = { "K", "k", "k", "", "King", P_VAL_OPN, K_VAL_MID, K_VAL_END },
};
const char pieces_str[6+6+1] = "PNBRQKpnbrqk";
@@ -51,24 +51,28 @@ bool piece_ok(piece_t p)
return !(p & ~(MASK_COLOR | MASK_PIECE)) && pt && (pt <= KING);
}
char *piece_to_char(piece_t p)
char *piece_to_cap(piece_t p)
{
return piece_details[p].abbr;
return piece_details[p].cap;
}
char *piece_to_char_color(piece_t p)
char *piece_to_char(piece_t p)
{
return piece_details[p].c_abbr;
return piece_details[p].fen;
}
char *piece_to_low(piece_t p)
{
return piece_details[p].low;
}
//char *piece_to_sym(piece_t p)
//{
// return piece_details[PIECE(p)].sym;
//}
char *piece_to_sym(piece_t p)
{
return piece_details[PIECE(p)].sym;
}
char *piece_to_sym_color(piece_t p)
{
return piece_details[p].c_sym;
return piece_details[p].sym;
}
char *piece_to_name(piece_t p)
@@ -76,98 +80,20 @@ char *piece_to_name(piece_t p)
return piece_details[p].name;
}
piece_t char_to_piece(char c)
piece_type_t piece_t_from_char(char c)
{
char *p = strchr(pieces_str, c);
return p? (p - pieces_str) % 6 + 1: EMPTY;
return p? (p - pieces_str) % 6 + 1: NO_PIECE_TYPE;
}
piece_t char_color_to_piece(char c)
//piece_type_t piece_from_promotion(char c, color_t color)
//{
// piece_type_t piece = piece_t_from_char(c);
// return piece? SET_COLOR()p? (p - pieces_str) % 6 + 1: NO_PIECE_TYPE;
//}
piece_t piece_from_char(char c)
{
piece_t piece = char_to_piece(c);
return isupper(c)? piece: SET_BLACK(piece);
piece_type_t piece = piece_t_from_char(c);
return isupper(c)? SET_WHITE(piece): SET_BLACK(piece);
}
/*
* void piece_list_print(struct list_head *list)
* {
* 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);
*
* printf("%s%c%c ", P_SYM(piece->piece),
* FILE2C(F88(piece->square)),
* RANK2C(R88(piece->square)));
* }
* printf("\n");
* }
*
* pool_t *piece_pool_init()
* {
* if (!pieces_pool)
* pieces_pool = pool_create("pieces", 128, sizeof(piece_list_t));
* return pieces_pool;
* }
*
* void piece_pool_stats()
* {
* if (pieces_pool)
* pool_stats(pieces_pool);
* }
*
* piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square)
* {
* piece_list_t *new;
* short color = COLOR(piece);
*
* # ifdef DEBUG_PIECE
* log_f(3, "piece=%02x square=%02x\n", piece, square);
* log_f(5, "Adding %s %s on %c%c\n", color? "Black": "White",
* P_NAME(piece), FILE2C(F88(square)), RANK2C(R88(square)));
* # endif
* if ((new = pool_get(pieces_pool))) {
* /\* first piece is always king *\/
* if (PIECE(piece) == KING)
* list_add(&new->list, &pos->pieces[color]);
* else
* list_add_tail(&new->list, &pos->pieces[color]);
* new->piece = piece;
* new->square = square;
* new->castle = 0;
* new-> value = piece_details[PIECE(piece)].value;
* }
*
* return new;
* }
*
* void piece_del(struct list_head *ptr)
* {
* piece_list_t *piece = list_entry(ptr, piece_list_t, list);
* # ifdef DEBUG_PIECE
* log_f(3, "piece=%02x square=%02x\n", piece->piece, piece->square);
* # endif
* list_del(ptr);
* pool_add(pieces_pool, piece);
* return;
* }
*
* int pieces_del(pos_t *pos, short color)
* {
* struct list_head *p_cur, *tmp, *head;
* int count = 0;
*
* head = &pos->pieces[color];
*
* list_for_each_safe(p_cur, tmp, head) {
* piece_del(p_cur);
* count++;
* }
* # ifdef DEBUG_PIECE
* log_f(3, "color=%d removed=%d\n", color, count);
* # endif
* return count;
* }
*/

View File

@@ -31,6 +31,7 @@ typedef enum {
typedef enum {
ALL_PIECES = 0, /* 'all pieces' bitboard */
NO_PIECE_TYPE = 0,
PAWN = 1, KNIGHT, BISHOP, ROOK, QUEEN, KING,
PIECE_TYPE_MAX = 7 /* bit 4 */
} piece_type_t;
@@ -80,10 +81,10 @@ typedef enum __piece_e {
* s64 end_value;
*/
extern const struct piece_details {
char *abbr; /* used for game notation */
char *c_abbr; /* lowercase = black */
char *sym; /* used for game notation */
char *c_sym; /* different W & B */
char *cap; /* used for game notation */
char *low; /* used also for UCI promotion */
char *fen; /* cap=white, low=black */
char *sym; /* UTF-8 symbol */
char *name; /* piece name */
s64 opn_value; /* value opening */
s64 mid_value; /* value midgame */
@@ -104,20 +105,26 @@ extern const char pieces_str[6+6+1]; /* to search from fen/user inp
#define IS_WHITE(p) (!COLOR(p))
#define IS_BLACK(p) (COLOR(p))
#define SET_WHITE(p) ((p) &= ~MASK_COLOR)
#define SET_BLACK(p) ((p) |= MASK_COLOR)
#define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p))
#define SET_WHITE(p) (piece_t)((p) &= ~MASK_COLOR)
#define SET_BLACK(p) (piece_t)((p) |= MASK_COLOR)
#define SET_COLOR(p, c) (piece_t)(!(c)? SET_WHITE(p): SET_BLACK(p))
extern bool piece_ok(piece_t p);
bool piece_ok(piece_t p);
extern char *piece_to_char(piece_t p);
extern char *piece_to_char_color(piece_t p);
extern char *piece_to_sym(piece_t p);
extern char *piece_to_sym_color(piece_t p);
extern char *piece_to_name(piece_t p);
char *piece_to_cap(piece_t p);
char *piece_to_low(piece_t p);
char *piece_to_fen(piece_t p);
char *piece_to_sym(piece_t p);
char *piece_to_name(piece_t p);
extern piece_t char_to_piece(char c);
extern piece_t char_color_to_piece(char c);
#define piece_to_char(c) piece_to_fen(c)
//#define piece_to_char_t(p) piece_to_uci(p)
//piece_type_t char_to_piece(char c);
piece_type_t piece_t_from_char(char c);
piece_t piece_from_fen(char c);
#define piece_from_char(c) piece_from_fen(c)
/* use short name or symbol - no effect
*/