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 | |
| parent | 26f77822201aae05c493e1f1b71631d89b1c564c (diff) | |
Line-mode mostly complete
| -rw-r--r-- | .clang_complete | 1 | ||||
| -rw-r--r-- | 3rd-party/linenoise.c (renamed from include/3rd-party/linenoise.c) | 0 | ||||
| -rw-r--r-- | 3rd-party/linenoise.h (renamed from include/3rd-party/linenoise.h) | 0 | ||||
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | include/tak.c | 2 | ||||
| -rw-r--r-- | include/tak.h | 2 | ||||
| -rw-r--r-- | src/ctaklm.c | 105 |
7 files changed, 69 insertions, 46 deletions
diff --git a/.clang_complete b/.clang_complete index 3e2e760..e9c2aee 100644 --- a/.clang_complete +++ b/.clang_complete @@ -1 +1,2 @@ -Iinclude/ +-I3rd-party/ diff --git a/include/3rd-party/linenoise.c b/3rd-party/linenoise.c index cfe51e7..cfe51e7 100644 --- a/include/3rd-party/linenoise.c +++ b/3rd-party/linenoise.c diff --git a/include/3rd-party/linenoise.h b/3rd-party/linenoise.h index 6dfee73..6dfee73 100644 --- a/include/3rd-party/linenoise.h +++ b/3rd-party/linenoise.h @@ -1,8 +1,9 @@ IDIR=include -CFLAGS=-g -Wall -I$(IDIR) +TPDIR=3rd-party +CFLAGS=-g -Wall -I$(IDIR) -I$(TPDIR) LIBS= -SRCS=$(wildcard src/*.c) $(wildcard include/*.c) $(wildcard include/3rd-party/*.c) +SRCS=$(wildcard src/*.c) $(wildcard include/*.c) $(wildcard 3rd-party/*.c) OBJS=$(SRCS:.c=.o) PROG=ctaklm diff --git a/include/tak.c b/include/tak.c index 7fa4f14..e8bc846 100644 --- a/include/tak.c +++ b/include/tak.c @@ -288,7 +288,7 @@ check_road_colour(const enum COLOUR colour) { } if (dfs_road(dfs_stack, dfs_pntr, colour, 1)) return winner; - return WIN_NONE; + return ACT_OK; } enum E_RESULT diff --git a/include/tak.h b/include/tak.h index 6773bcd..7d96b9a 100644 --- a/include/tak.h +++ b/include/tak.h @@ -3,7 +3,7 @@ enum E_RESULT { ACT_OK, ACT_ILLEGAL, ACT_OVERFLOW, PTN_VALID, PTN_INVALID, - WIN_NONE, WIN_DRAW, WIN_FLAT_WHITE, WIN_FLAT_BLACK, + WIN_DRAW, WIN_FLAT_WHITE, WIN_FLAT_BLACK, WIN_ROAD_WHITE, WIN_ROAD_BLACK, WIN_DRAGON }; // ------------------------------------------------------------------- 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)); |
