commit 9ec9201144172469c95a31b2fc29e40dd3f4f4ce
parent 3bc33d613e5898f992e13a8a1a9f1801a7b3b87e
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Wed, 14 Sep 2022 23:11:57 +0200
Improve input detection and handling, reload card, fix board bug
Diffstat:
4 files changed, 33 insertions(+), 43 deletions(-)
diff --git a/include/board.h b/include/board.h
@@ -13,7 +13,7 @@ enum AC { AC_QUIT = -3, AC_SUSPEND, AC_INDEX };
#define BUFF_SIZE 10
-typedef void (*review_f)(void);
+typedef int (*review_f)(void);
typedef struct game_T *game_T;
struct game_T {
card_T card;
@@ -40,7 +40,7 @@ T Board_new(void);
T Board_from_FEN(char *fen);
void Board_free(T *self);
-T Board_play(T self, char *m);
+T Board_play(T self, char *m, int white);
void Board_print(T self);
char Board_atIndex(T self, int i, int j);
@@ -55,7 +55,7 @@ int Grave_size(G self);
game_T game_new(size_t moves);
/* MOVE */
-char ** Move_list(char *pgn, size_t *size);
+char **Move_list(char *pgn, size_t *size);
#undef T
#undef G
diff --git a/src/board.c b/src/board.c
@@ -206,12 +206,12 @@ void Board_free(T *self) {
FREE(*self);
}
-T Board_play(T self, char *m) {
+T Board_play(T self, char *m, int white) {
assert(self);
assert(m);
- static char mover = 'b';
- mover = (mover == 'b') ? 'w' : 'b';
+ char mover;
+ mover = (white) ? 'w' : 'b';
T new = Board_new();
__board_copy(self, new);
diff --git a/src/display.c b/src/display.c
@@ -68,8 +68,6 @@ void board_display(widget_T widget) {
}
}
- /* draw_border(x_start - 1, y_start - 1, x_start + 16, y_start + 8, TB_RED);
- */
if (style->border) {
tb_printf(x_start, y_start - 1, 0, style->border, " ");
tb_printf(x_start, y_start + 8, 0, style->border, " ");
@@ -79,13 +77,6 @@ void board_display(widget_T widget) {
}
}
- /* if (game->pass) */
- /* tb_printf(x_start, y_start - 2, TB_GREEN, 0, "PASS"); */
- /* else if (game->fail) */
- /* tb_printf(x_start, y_start - 2, TB_GREEN, 0, "FAIL"); */
- /* else */
- /* tb_printf(x_start, y_start - 2, TB_GREEN, 0, " "); */
-
tb_printf(x_start, pane_y(pane) + pane_height(pane) - 1, TB_GREEN, 0, "%*s",
BUFF_SIZE, game->buffer);
@@ -95,28 +86,25 @@ void board_display(widget_T widget) {
int game_handleInput(data_T data, struct tb_event ev) {
game_T game = (game_T)data->payload;
- /* if() */
-
switch (ev.key) {
case TB_KEY_ARROW_LEFT:
if (game->display_current > 0)
game->display_current--;
- return 1;
+ return INPUT_HANDLED;
case TB_KEY_ARROW_RIGHT:
if (game->display_current < game->move_current)
game->display_current++;
- return 1;
+ return INPUT_HANDLED;
case TB_KEY_ARROW_UP:
game->display_current = 0;
- return 1;
+ return INPUT_HANDLED;
case TB_KEY_ARROW_DOWN:
game->display_current = game->move_current;
- return 1;
+ return INPUT_HANDLED;
case TB_KEY_ENTER:
if (game->move_current == game->moves_num) {
anki_grade(game->pass);
- game->review_next();
- return INPUT_REFORM;
+ return (game->review_next()) ? INPUT_HANDLED : INPUT_REFORM;
}
if (game->buffer_crnt) {
@@ -134,26 +122,28 @@ int game_handleInput(data_T data, struct tb_event ev) {
game->buffer_crnt = 0;
}
}
-
- return 1;
+ return INPUT_HANDLED;
case TB_KEY_BACKSPACE:
case TB_KEY_BACKSPACE2:
CLAMP(game->buffer_crnt, 1, BUFF_SIZE + 1);
game->buffer[--game->buffer_crnt] = '\0';
- return 1;
- default:
- if (!isalnum(ev.ch) && ev.ch != '+' && ev.ch != '#' && ev.ch != '-' &&
- ev.ch != '=')
- break;
-
- if (game->buffer_crnt >= BUFF_SIZE)
- break;
-
- game->buffer[game->buffer_crnt++] = ev.ch;
- return 1;
+ return INPUT_HANDLED;
+ default: {
+ char *valid = "abcdefgh12345678KQRBNPx#+O-!?";
+ if (strchr(valid, ev.ch)) {
+ if (game->buffer_crnt < BUFF_SIZE)
+ game->buffer[game->buffer_crnt++] = ev.ch;
+ return INPUT_HANDLED;
+ }
+ switch (ev.ch) {
+ case 'r':
+ case 'R':
+ return (game->review_next()) ? INPUT_HANDLED : INPUT_REFORM;
+ }
+ }
}
- return 0;
+ return INPUT_IGNORED;
}
void move_display(game_T game, movesStyle_T style, size_t move_index,
diff --git a/src/main.c b/src/main.c
@@ -153,7 +153,7 @@ void review_finished(void) {
widget_activate(&mainMenu_widget, MAIN);
}
-void review_card(void) {
+int review_card(void) {
char *fen;
size_t moves_num, i;
@@ -161,29 +161,29 @@ void review_card(void) {
if (card == NULL) {
review_finished();
- return;
+ return 0;
}
char **moves = Move_list(card_pgn(card), &moves_num);
game_T game = game_new(moves_num);
+ int white = 1;
game->boards[0] = (fen = card_fen(card)) ? Board_from_FEN(fen) : Board_new();
- for (i = 1; i <= moves_num; i++)
- game->boards[i] = Board_play(game->boards[i - 1], moves[i]);
+ for (i = 1; i <= moves_num; i++, white = !white)
+ game->boards[i] = Board_play(game->boards[i - 1], moves[i], white);
game->card = card;
game->review_next = review_card;
game->move_start = game->move_current = game->display_current =
atoi(card_start(card));
game->moves = moves;
- /* game->pass = 1; */
data_T data = data_new(game, game_handleInput);
widget_setData(&board_widget, data);
widget_setData(&moves_widget, data);
widget_setData(&title_widget, data);
- /* widgetCenter_print(widgetTitle, card_name(card)); */
+ return 1;
}
void deck_selected(char *name, int ignore) {