diff options
Diffstat (limited to 'src/text')
-rw-r--r-- | src/text/main.c | 52 |
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; |