chess

Terminal based Chess trainer using Anki
git clone git://git.dimitrijedobrota.com/chess.git
Log | Files | Refs

commit 60b4ed03f61f6791bcd1ebc5998059f994ffa3e7
parent 6690067cb0f6b8e533ca4f71689c91066665b3ec
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sat, 17 Sep 2022 21:15:25 +0200

Move annotation, pawn promotion, better move display

Diffstat:
Minclude/board.h | 3+++
Minclude/display.h | 3++-
Msrc/board.c | 23++++++++++++++++++-----
Msrc/display.c | 14+++++++-------
Msrc/main.c | 5+++--
5 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/include/board.h b/include/board.h @@ -1,6 +1,7 @@ #ifndef BOARD_H #define BOARD_H +#include <cii/except.h> #include "anki.h" #define T Board_T @@ -9,6 +10,8 @@ typedef struct G *G; typedef struct T *T; +extern const Except_T BOARDE_MOVE; + enum AC { AC_QUIT = -3, AC_SUSPEND, AC_INDEX }; #define BUFF_SIZE 10 diff --git a/include/display.h b/include/display.h @@ -48,7 +48,8 @@ struct movesInfo_T { typedef struct movesStyle_T *movesStyle_T; struct movesStyle_T { - int padding; + int move_padding; + int num_padding; int foreground; int background; int active; diff --git a/src/board.c b/src/board.c @@ -5,6 +5,7 @@ #include <string.h> #include <cii/assert.h> +#include <cii/except.h> #include <cii/mem.h> #include "board.h" @@ -15,12 +16,13 @@ #define MAX_MOVE 10 #define MAX_PLAY 1024 +const Except_T BOARDE_MOVE = {"Board: Invalid move"}; + const char def_board[8][8] = {"rnbqkbnr", "pppppppp", " ", " ", " ", " ", "PPPPPPPP", "RNBQKBNR"}; static char piece_lookup[] = {'K', 'Q', 'R', 'B', 'N', 'P', 'k', 'q', 'r', 'b', 'n', 'p'}; static int piece_value[] = {100, 8, 5, 3, 3, 1, 100, 8, 5, 3, 3, 1}; - struct G { char arr[20]; int size; @@ -224,17 +226,26 @@ T Board_play(T self, char *m, int white) { __board_set_at(new, 'f', castle_rank, conv('R')); __board_set_at(new, 'g', castle_rank, conv('K')); __board_set_at(new, 'h', castle_rank, ' '); + return new; } else if (strcmp(m, "O-O-O") == 0) { __board_set_at(new, 'a', castle_rank, ' '); __board_set_at(new, 'c', castle_rank, conv('K')); __board_set_at(new, 'd', castle_rank, conv('R')); __board_set_at(new, 'e', castle_rank, ' '); + return new; } else { // Decoding move MESS Start int l = strlen(m); - if (m[l - 1] == '+' || m[l - 1] == '#') + while (m[l - 1] == '+' || m[l - 1] == '#' || m[l - 1] == '!' || + m[l - 1] == '?') l--; + char promoted = 0; + if (m[l - 2] == '=') { + promoted = conv(m[l - 1]); + l -= 2; + } + char file = m[l - 2]; int rank = m[l - 1] - '0'; @@ -270,13 +281,15 @@ T Board_play(T self, char *m, int white) { if (take) __board_send_grave(new, __board_get_at(self, file, rank), mover); __board_set_at(new, file_s, rank_s, ' '); - __board_set_at(new, file, rank, moved); - break; + __board_set_at(new, file, rank, promoted ? promoted : moved); + return new; } rank_s++; } } - return new; + + RAISE(BOARDE_MOVE); + return NULL; } int piece_get_index(char l) { diff --git a/src/display.c b/src/display.c @@ -129,7 +129,7 @@ int game_handleInput(data_T data, struct tb_event ev) { game->buffer[--game->buffer_crnt] = '\0'; return INPUT_HANDLED; default: { - char *valid = "abcdefgh12345678KQRBNPx#+O-!?"; + char *valid = "abcdefgh12345678KQRBNPx#+O-!?="; if (strchr(valid, ev.ch)) { if (game->buffer_crnt < BUFF_SIZE) game->buffer[game->buffer_crnt++] = ev.ch; @@ -153,7 +153,7 @@ void move_display(game_T game, movesStyle_T style, size_t move_index, color = (game->move_fail == move_index) ? TB_RED : color; background = (game->display_current == move_index) ? style->active : style->background; - tb_printf(x, y, color, background, "%*s", style->padding, move); + tb_printf(x, y, color, background, "%*s", style->move_padding, move); } void moves_display(widget_T widget) { @@ -174,16 +174,16 @@ void moves_display(widget_T widget) { widgetList_cacl(list, display_row); pane_clear(pane, 0); - int x = centerHorisontal(pane, 4 * style->padding), y = pane_y(pane); + int x = centerHorisontal(pane, 2 * style->move_padding + style->num_padding), + y = pane_y(pane); size_t index_end = MIN(list->index_start + list->display_num, (game->move_current + 1) / 2); for (i = list->index_start; i < index_end; i++, y++) { - tb_printf(x, y, style->foreground, style->background, "%*d.", - style->padding - 1, i + 1); + tb_printf(x, y, style->foreground, style->background, "%d.", i + 1); move_display(game, style, i * 2 + 1, game->moves[i * 2 + 1], - x + style->padding, y); + x + style->num_padding, y); if (i * 2 + 2 <= game->move_current) move_display(game, style, i * 2 + 2, game->moves[i * 2 + 2], - x + 2 * style->padding, y); + x + style->num_padding + style->move_padding, y); } } diff --git a/src/main.c b/src/main.c @@ -79,11 +79,12 @@ struct widget_T board_widget = { .info = &boardInfo, }; -#define MOVE_PADDING 6 +#define MOVE_PADDING 8 struct movesInfo_T movesInfo; struct movesStyle_T movesStyle = { - .padding = MOVE_PADDING, + .move_padding = MOVE_PADDING, + .num_padding = 4, .foreground = TB_WHITE, .background = 0, .active = TB_BLUE,