diff --git a/src/move.c b/src/move.c index 2079385..6e235bf 100644 --- a/src/move.c +++ b/src/move.c @@ -32,38 +32,6 @@ static struct vector { [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() { if (!moves_pool) @@ -71,15 +39,13 @@ pool_t *moves_pool_init() return moves_pool; } - - 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)), RANK2C(GET_R(move->from))); if (move->taken) - printf("x%c", piece2char(move->taken)); + printf("x%s", P_SYM(move->taken)); else printf("-"); printf("%c%c", diff --git a/src/piece.c b/src/piece.c index b08122c..a88ee9c 100644 --- a/src/piece.c +++ b/src/piece.c @@ -18,37 +18,15 @@ static pool_t *pieces_pool; -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; - -} +struct piece_details piece_details[] = { + [EMPTY] = { ' ', ' ', " ", " ", "", 0 }, + [PAWN] = { 'P', 'p', "♙", "♟", "Pawn", PAWN_VALUE }, + [KNIGHT] = { 'N', 'n', "♘", "♞", "Knight", KNIGHT_VALUE }, + [BISHOP] = { 'B', 'b', "♗", "♝", "Bishop", BISHOP_VALUE }, + [ROOK] = { 'R', 'r', "♖", "♜", "Rook", ROOK_VALUE }, + [QUEEN] = { 'Q', 'q', "♕", "♛", "Queen", QUEEN_VALUE }, + [KING] = { 'K', 'k', "♔", "♚", "King", KING_VALUE } +}; 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) { 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)), 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++; } printf("Total pieces = %d\n", i); diff --git a/src/piece.h b/src/piece.h index 650424e..41d1c58 100644 --- a/src/piece.h +++ b/src/piece.h @@ -19,6 +19,7 @@ #include "list.h" #include "position.h" #include "pool.h" +#include "ctype.h" #define PIECE_DEFAULT_VALUE 0 @@ -38,6 +39,27 @@ typedef struct { struct list_head list; } 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 pieces_print_pos_pieces(pos_t *pos); pool_t *piece_pool_init(); diff --git a/src/position.c b/src/position.c index 921ed8f..a583970 100644 --- a/src/position.c +++ b/src/position.c @@ -20,61 +20,7 @@ #include "chessdefs.h" #include "position.h" #include "fen.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; - -} +#include "piece.h" /* void pos_print - Print position on stdout. * @pos: Position address (pos_t * ) @@ -92,7 +38,7 @@ void pos_print(pos_t *pos) printf("%c |", rank + '1'); for (file = 0; file < 8; ++file) { piece = board[SQ88(file, rank)].piece; - printf(" %c |", piece2char(piece)); + printf(" %s |", P_CSYM(piece)); } printf("\n +---+---+---+---+---+---+---+---+\n"); } diff --git a/src/position.h b/src/position.h index dc85bfa..5e0a7e5 100644 --- a/src/position.h +++ b/src/position.h @@ -32,8 +32,6 @@ typedef struct position { board_t *board; } pos_t; -/* TODO: replace piece2string/piece2char with static array*/ -char *piece2string(piece_t piece); void pos_print(pos_t *pos); pos_t *pos_init(pos_t *pos); pos_t *pos_startpos(pos_t *pos);