aboutsummaryrefslogtreecommitdiff
path: root/src/ctaklm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ctaklm.c')
-rw-r--r--src/ctaklm.c69
1 files changed, 66 insertions, 3 deletions
diff --git a/src/ctaklm.c b/src/ctaklm.c
index 9d369f8..3d4310c 100644
--- a/src/ctaklm.c
+++ b/src/ctaklm.c
@@ -190,16 +190,17 @@ end_game(char *line, char *win) {
putchar('\n');
}
-static void
+static int
handle_turn(char *line) {
// Track win state
uint8_t new_win = (won == 0xFF);
switch (do_ptn(line)) {
// Errors
- case PTN_INVALID: { puts("Invalid PTN."); break; }
- case ACT_ILLEGAL: { puts("Illegal action."); break; }
+ case PTN_INVALID: { puts("Invalid PTN."); return -1; break; }
+ case ACT_ILLEGAL: { puts("Illegal action."); return -1; break; }
case ACT_OVERFLOW: {
puts("Move would cause internal overflow, select another.");
+ return -1;
break;
}
// Game has ended
@@ -216,6 +217,7 @@ handle_turn(char *line) {
}
}
puts("Game over, enter `new' to play again.");
+ if (! new_win) return -1;
break;
}
// Valid, append to game log
@@ -227,6 +229,8 @@ handle_turn(char *line) {
break;
}
}
+
+ return 0;
}
static void
@@ -238,6 +242,59 @@ new_game(uint8_t size) {
gamelog[0] = 0;
}
+static int
+load_ptn(const char* fn) {
+ FILE *fh = NULL;
+
+ fh = fopen(fn, "r");
+ if (fh == NULL) return EXIT_FAILURE;
+
+ int space1, space2;
+ enum E_RESULT r;
+
+ ssize_t read;
+ size_t alloc_size;
+ char *line = NULL;
+ while ((read = getline(&line, &alloc_size, fh)) != -1) {
+ if (read && line[0] <= '9' && line[0] >= '0') {
+ // Trim trailing \n
+ line[read-1] = 0;
+ // Find first separator
+ space1 = 0;
+ while (space1 < read && line[space1++] != ' ');
+ // If still on line
+ if (space1 < read) {
+ // Find next separator or end of line, either way mark the split
+ space2 = space1;
+ while (space2 < read && line[space2] != ' ') space2++;
+ line[space2] = 0;
+ // Try the first piece we found
+ r = handle_turn(line+space1);
+ if (r) {
+ printf("Error on: %s\n", line+space1);
+ break;
+ }
+ // If there's a second piece, try it
+ if (space2 + 1 < read) {
+ r = handle_turn(line+space2+1);
+ if (r) {
+ printf("Error on: %s", line+space2+1);
+ break;
+ }
+ } else {
+ break;
+ }
+ }
+ }
+ }
+
+ if (line) free(line);
+ fclose(fh);
+
+
+ return EXIT_SUCCESS;
+}
+
int
main(int argc, char **argv) {
(void)(argc);
@@ -255,6 +312,12 @@ main(int argc, char **argv) {
print_info();
} else if (!strncmp(line,"log",3)) {
puts(gamelog);
+ } else if (!strncmp(line,"load",4)) {
+ if (strnlen(line,6) >= 6) {
+ load_ptn(line+5);
+ } else {
+ puts("Usage: load <file.ptn>.");
+ }
} else if (!strncmp(line,"auto",4)) {
if (!strncmp(line,"auto board",10)) {
auto_board = ~auto_board;