aboutsummaryrefslogtreecommitdiff
path: root/src/text
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-01-08 14:26:16 -0500
committerPaul Duncan <pabs@pablotron.org>2019-01-08 14:26:16 -0500
commit8c17e9d9473ced08c37aad7705e0a9f9c4873577 (patch)
tree3fe399dd68c6d44e70baa9fd4b0838d73a4cb70b /src/text
parentfa56925a5de7384c03f4775822444095d38bf3ac (diff)
downloadsok-8c17e9d9473ced08c37aad7705e0a9f9c4873577.tar.bz2
sok-8c17e9d9473ced08c37aad7705e0a9f9c4873577.zip
add sok_ctx_hash, cache, solver fixes
Diffstat (limited to 'src/text')
-rw-r--r--src/text/main.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/text/main.c b/src/text/main.c
index 80bdc65..de5b5f3 100644
--- a/src/text/main.c
+++ b/src/text/main.c
@@ -752,6 +752,42 @@ print_level(
printf("%s\n", print_buf);
}
+static void
+solve_on_error(
+ const char * const err
+) {
+ fprintf(stderr, "Error solving level: %s\n", err);
+ exit(EXIT_FAILURE);
+}
+
+static void
+print_moves(
+ const sok_ctx_t * const ctx,
+ const size_t skip_moves
+) {
+ printf("Moves (%zu): ", ctx->num_moves - skip_moves);
+ for (size_t i = skip_moves; i < ctx->num_moves; i++) {
+ switch (ctx->moves[i].dir) {
+ case SOK_DIR_UP:
+ fputs("u", stdout);
+ break;
+ case SOK_DIR_DOWN:
+ fputs("d", stdout);
+ break;
+ case SOK_DIR_LEFT:
+ fputs("l", stdout);
+ break;
+ case SOK_DIR_RIGHT:
+ fputs("r", stdout);
+ break;
+ default:
+ fprintf(stderr, "Error: invalid move: %u", ctx->moves[i].dir);
+ exit(EXIT_FAILURE);
+ }
+ }
+ printf("\n");
+}
+
int main(int argc, char *argv[]) {
size_t level = (argc > 1) ? atoi(argv[1]) : 0;
@@ -836,6 +872,22 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
}
+
+ break;
+ case 's':
+ {
+ // get current number of moves
+ const size_t old_num_moves = ctx.num_moves;
+
+ if (sok_solve(&ctx, solve_on_error)) {
+ // found solution, print it
+ print_moves(&ctx, old_num_moves);
+ } else {
+ fprintf(stderr, "W: Couldn't solve level\n");
+ }
+ }
+
+ break;
default:
// ignore
break;