remove piece2char/piece2string (global var instead)

This commit is contained in:
2021-10-31 13:44:08 +01:00
parent 2f701c4bd5
commit 16c0c06095
5 changed files with 36 additions and 131 deletions

View File

@@ -32,38 +32,6 @@ static struct vector {
[KING] = { 8, 0, { -1, -16, 1, 16, -15, -17, 15, 17}}, [KING] = { 8, 0, { -1, -16, 1, 16, -15, -17, 15, 17}},
}; };
inline static char piece2char(piece_t p)
{
piece_t piece = PIECE(p);
char res;
//printf("%#x p=%#x\n", p, PIECE(p));
switch (piece) {
case PAWN:
res = CHAR_PAWN;
break;
case KNIGHT:
res = CHAR_KNIGHT;
break;
case BISHOP:
res = CHAR_BISHOP;
break;
case ROOK:
res = CHAR_ROOK;
break;
case QUEEN:
res = CHAR_QUEEN;
break;
case KING:
res = CHAR_KING;
break;
default:
res = CHAR_EMPTY;
}
return res;
}
pool_t *moves_pool_init() pool_t *moves_pool_init()
{ {
if (!moves_pool) if (!moves_pool)
@@ -71,15 +39,13 @@ pool_t *moves_pool_init()
return moves_pool; return moves_pool;
} }
void move_print(move_t *move) void move_print(move_t *move)
{ {
printf("%c%c%c", piece2char(move->piece), printf("%s%c%c", P_SYM(move->piece),
FILE2C(GET_F(move->from)), FILE2C(GET_F(move->from)),
RANK2C(GET_R(move->from))); RANK2C(GET_R(move->from)));
if (move->taken) if (move->taken)
printf("x%c", piece2char(move->taken)); printf("x%s", P_SYM(move->taken));
else else
printf("-"); printf("-");
printf("%c%c", printf("%c%c",

View File

@@ -18,37 +18,15 @@
static pool_t *pieces_pool; static pool_t *pieces_pool;
inline static char piece2char(piece_t p) struct piece_details piece_details[] = {
{ [EMPTY] = { ' ', ' ', " ", " ", "", 0 },
piece_t piece = PIECE(p); [PAWN] = { 'P', 'p', "", "", "Pawn", PAWN_VALUE },
char res; [KNIGHT] = { 'N', 'n', "", "", "Knight", KNIGHT_VALUE },
[BISHOP] = { 'B', 'b', "", "", "Bishop", BISHOP_VALUE },
//printf("%#x p=%#x\n", p, PIECE(p)); [ROOK] = { 'R', 'r', "", "", "Rook", ROOK_VALUE },
switch (piece) { [QUEEN] = { 'Q', 'q', "", "", "Queen", QUEEN_VALUE },
case PAWN: [KING] = { 'K', 'k', "", "", "King", KING_VALUE }
res = CHAR_PAWN; };
break;
case KNIGHT:
res = CHAR_KNIGHT;
break;
case BISHOP:
res = CHAR_BISHOP;
break;
case ROOK:
res = CHAR_ROOK;
break;
case QUEEN:
res = CHAR_QUEEN;
break;
case KING:
res = CHAR_KING;
break;
default:
res = CHAR_EMPTY;
}
return res;
}
void piece_list_print(struct list_head *list) void piece_list_print(struct list_head *list)
{ {
@@ -59,14 +37,9 @@ void piece_list_print(struct list_head *list)
list_for_each_safe(p_cur, tmp, list) { list_for_each_safe(p_cur, tmp, list) {
piece = list_entry(p_cur, piece_list_t, list); piece = list_entry(p_cur, piece_list_t, list);
printf("%c%c%c ", piece2char(piece->piece), printf("%s%c%c ", P_SYM(piece->piece),
FILE2C(GET_F(piece->square)), FILE2C(GET_F(piece->square)),
RANK2C(GET_R(piece->square))); RANK2C(GET_R(piece->square)));
/*printf("\t%d: %s on %c%c\n", i,
piece2string(piece->piece),
FILE2C(GET_F(piece->square)),
RANK2C(GET_R(piece->square)));
*/
i++; i++;
} }
printf("Total pieces = %d\n", i); printf("Total pieces = %d\n", i);

View File

@@ -19,6 +19,7 @@
#include "list.h" #include "list.h"
#include "position.h" #include "position.h"
#include "pool.h" #include "pool.h"
#include "ctype.h"
#define PIECE_DEFAULT_VALUE 0 #define PIECE_DEFAULT_VALUE 0
@@ -38,6 +39,27 @@ typedef struct {
struct list_head list; struct list_head list;
} piece_list_t; } piece_list_t;
/* some default values for pieces
*/
extern struct piece_details {
char abbrev_w; /* used for game notation */
char abbrev_b;
char *symbol_w;
char *symbol_b; /* used for game notation */
char *name;
int64_t value;
} piece_details[];
#define P_NAME(p) piece_details[PIECE(p)].name
#define P_LETTER(p) piece_details[PIECE(p)].abbrev_w
#define P_SYM(p) piece_details[PIECE(p)].symbol_b
#define P_CSHORT(p) (COLOR(p) == WHITE? piece_details[PIECE(p)].abbrev_w: \
piece_details[PIECE(p)].abbrev_b)
#define P_CSYM(p) (COLOR(p) == WHITE? piece_details[PIECE(p)].symbol_w: \
piece_details[PIECE(p)].symbol_b)
/* use short name or symbol - no effect
*/
#define P_USE_UTF 1
void piece_list_print(struct list_head *list); void piece_list_print(struct list_head *list);
void pieces_print_pos_pieces(pos_t *pos); void pieces_print_pos_pieces(pos_t *pos);
pool_t *piece_pool_init(); pool_t *piece_pool_init();

View File

@@ -20,61 +20,7 @@
#include "chessdefs.h" #include "chessdefs.h"
#include "position.h" #include "position.h"
#include "fen.h" #include "fen.h"
#include "piece.h"
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;
//printf("%#x p=%#x\n", p, PIECE(p));
switch (piece) {
case PAWN:
res = CHAR_PAWN;
break;
case KNIGHT:
res = CHAR_KNIGHT;
break;
case BISHOP:
res = CHAR_BISHOP;
break;
case ROOK:
res = CHAR_ROOK;
break;
case QUEEN:
res = CHAR_QUEEN;
break;
case KING:
res = CHAR_KING;
break;
default:
res = CHAR_EMPTY;
}
if (IS_BLACK(p))
res = tolower(res);
return res;
}
/* void pos_print - Print position on stdout. /* void pos_print - Print position on stdout.
* @pos: Position address (pos_t * ) * @pos: Position address (pos_t * )
@@ -92,7 +38,7 @@ void pos_print(pos_t *pos)
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(" %s |", P_CSYM(piece));
} }
printf("\n +---+---+---+---+---+---+---+---+\n"); printf("\n +---+---+---+---+---+---+---+---+\n");
} }

View File

@@ -32,8 +32,6 @@ typedef struct position {
board_t *board; board_t *board;
} 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);