diff options
| author | tslil <tslil@posteo.de> | 2020-12-31 23:05:00 -0500 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-08-28 19:37:41 +0100 |
| commit | 33ae4e18686199822aad9f9341d008f839862d9a (patch) | |
| tree | 2aceb707c820ca52fa028e3884e4fd1c9fe8f295 /src | |
| parent | 26f77822201aae05c493e1f1b71631d89b1c564c (diff) | |
Line-mode mostly complete
Diffstat (limited to 'src')
| -rw-r--r-- | src/ctaklm.c | 105 |
1 files changed, 63 insertions, 42 deletions
diff --git a/src/ctaklm.c b/src/ctaklm.c index 52c29da..0162ab1 100644 --- a/src/ctaklm.c +++ b/src/ctaklm.c @@ -2,7 +2,7 @@ #include <stdlib.h> #include <string.h> -#include <3rd-party/linenoise.h> +#include <linenoise.h> #include <tak.h> const char *blk = "\033[41m", *wht = "\033[44m"; @@ -154,51 +154,71 @@ print_info(void) { } char *gamelog = 0; -uint8_t auto_board = 0xFF, auto_info = 0xFF; +uint8_t auto_board = 0xFF, auto_info = 0xFF, won = 0x00; + +static void +append_to_gamelog(char *line, const uint8_t win_line) { + // I _could_ dynamically compute the size but ... don't let + // `perfect' be the enemy of `good' ? + char prepend[8]; + + if (win_line) { + prepend[0] = '\n'; + prepend[1] = 0; + } else if (ply & 1) { + snprintf(prepend, 7, "%s%d. ", + (ply == 1) ? "" : "\n", ply/2+1); + } else { + strcpy(prepend, " "); + } + // Make room for this line + gamelog = realloc(gamelog, + strlen(gamelog) + + strlen(prepend) + + strlen(line) + 1); + strcat(gamelog,prepend); + strcat(gamelog,line); +} + +static void +end_game(char *line, char *win) { + won = 0xFF; + append_to_gamelog(line, 0); + append_to_gamelog("R-R", 1); + print_board(); + puts("Game over:"); + puts(gamelog); + puts("\nEnter `new' to play again."); +} static void handle_turn(char *line) { - switch (do_ptn(line)) { - case PTN_INVALID : { - puts("Invalid PTN."); - break; - } - case ACT_ILLEGAL : { - puts("Illegal action."); - break; - } - case ACT_OVERFLOW : { - puts("Stack move would cause internal overflow, and we can't allow for that :)"); - break; - } - case WIN_DRAGON: - case WIN_FLAT_BLACK: - case WIN_FLAT_WHITE: - case WIN_ROAD_BLACK: - case WIN_ROAD_WHITE: { - puts(gamelog); - break; - } - // Valid, append to game log - default: { - // I _could_ dynamically compute the size but ... don't let - // `perfect' be the enemy of `good' ? - char prepend[8]; - if (ply & 1) { - snprintf(prepend, 7, "%s%d. ", - (ply == 1) ? "" : "\n", ply/2+1); - } else { - strcpy(prepend, " "); + if (won) { + puts("Game over, enter `new' to play again."); + } else { + switch (do_ptn(line)) { + // Errors + case PTN_INVALID : { puts("Invalid PTN."); break; } + case ACT_ILLEGAL : { puts("Illegal action."); break; } + case ACT_OVERFLOW : { + puts("Move would cause internal overflow, select another."); + break; + } + // Valid, append to game log + case PTN_VALID: + case ACT_OK: { + append_to_gamelog(line, 0); + if (auto_info) print_info(); + if (auto_board) print_board(); + break; } - // Make room for this line - gamelog = realloc(gamelog, - strlen(gamelog) - + strlen(prepend) - + strlen(line) + 1); - strcat(gamelog,prepend); - strcat(gamelog,line); - if (auto_info) print_info(); - if (auto_board) print_board(); + // A win + case WIN_DRAGON: { end_game(line,"R-R"); break; } + case WIN_DRAW: { end_game(line,"1/2-1/2"); break; } + case WIN_FLAT_BLACK: { end_game(line,"0-F"); break; } + case WIN_FLAT_WHITE: { end_game(line, "F-0"); break; } + case WIN_ROAD_BLACK: { end_game(line,"0-R"); break; } + case WIN_ROAD_WHITE: { end_game(line, "R-0"); break; } } } } @@ -206,6 +226,7 @@ handle_turn(char *line) { static void new_game(uint8_t size) { reset_state(size); + won = 0x00; printf("New %dx%d game!\n",size,size); if (gamelog) gamelog = realloc(gamelog, sizeof(char)); else gamelog = malloc(sizeof(char)); |
