From 8ece7a6a0d01f0375a21182ba6663627dc398977 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 8 Jan 2019 22:44:14 -0500 Subject: add is_done (busted, disabled) --- src/libsok/sok-ctx.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++- src/libsok/sok-solve.c | 8 +++++ src/libsok/sok.h | 4 +++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/libsok/sok-ctx.c b/src/libsok/sok-ctx.c index d514fe3..26c6239 100644 --- a/src/libsok/sok-ctx.c +++ b/src/libsok/sok-ctx.c @@ -201,6 +201,73 @@ sok_ctx_count_goals_left( return r; } +/* + * static bool + * sok_ctx_has_corner_boxes( + * const sok_ctx_t * const ctx + * ) { + * for (size_t i = 0; i < ctx->level.num_boxes; i++) { + * // right + * const bool r = ctx_is_wall(ctx, (sok_pos_t) { + * .x = ctx->boxes[i].x + 1, + * .y = ctx->boxes[i].y, + * }); + * + * // top-right + * const bool tr = ctx_is_wall(ctx, (sok_pos_t) { + * .x = ctx->boxes[i].x + 1, + * .y = ctx->boxes[i].y - 1, + * }); + * + * // top + * const bool t = ctx_is_wall(ctx, (sok_pos_t) { + * .x = ctx->boxes[i].x, + * .y = ctx->boxes[i].y - 1, + * }); + * + * // top-left + * const bool tl = ctx_is_wall(ctx, (sok_pos_t) { + * .x = ctx->boxes[i].x - 1, + * .y = ctx->boxes[i].y - 1, + * }); + * + * // left + * const bool l = ctx_is_wall(ctx, (sok_pos_t) { + * .x = ctx->boxes[i].x - 1, + * .y = ctx->boxes[i].y, + * }); + * + * // bottom-left + * const bool bl = ctx_is_wall(ctx, (sok_pos_t) { + * .x = ctx->boxes[i].x - 1, + * .y = ctx->boxes[i].y + 1, + * }); + * + * // bottom + * const bool b = ctx_is_wall(ctx, (sok_pos_t) { + * .x = ctx->boxes[i].x, + * .y = ctx->boxes[i].y + 1, + * }); + * + * const bool br = ctx_is_wall(ctx, (sok_pos_t) { + * .x = ctx->boxes[i].x + 1, + * .y = ctx->boxes[i].y + 1, + * }); + * + * if ( + * (r && tr && t) || // top-right + * (t && tl && l) || // top-left + * (l && bl && b) || // bottom-left + * (b && br && r) // bottom-right + * ) { + * // return + * return true; + * } + * } + * + * return false; + * } + */ void sok_ctx_init(sok_ctx_t * const ctx, void *user_data) { @@ -232,8 +299,9 @@ sok_ctx_set_level( ctx->home = ctx->level.home; memcpy(ctx->boxes, ctx->level.boxes, ctx->level.num_boxes * sizeof(sok_pos_t)); - // count number of goals left + // count number of goals left and check whether there are ctx->num_goals_left = sok_ctx_count_goals_left(ctx); + // ctx->is_lost = sok_ctx_is_lost(ctx); // return success return true; @@ -322,6 +390,7 @@ sok_ctx_move_box( // update box and goal count ctx->boxes[box_ofs] = new_pos; ctx->num_goals_left = sok_ctx_count_goals_left(ctx); + // ctx->is_lost = sok_ctx_has_corner_boxes(ctx); // return success return true; @@ -338,6 +407,15 @@ sok_ctx_is_done( return ctx->num_goals_left == 0; } +/* + * bool + * sok_ctx_is_lost( + * const sok_ctx_t * const ctx + * ) { + * return ctx->is_lost; + * } + */ + bool sok_ctx_move( sok_ctx_t * const ctx, diff --git a/src/libsok/sok-solve.c b/src/libsok/sok-solve.c index 225ce9b..34a8320 100644 --- a/src/libsok/sok-solve.c +++ b/src/libsok/sok-solve.c @@ -31,6 +31,14 @@ sok_solve_step( return false; } +/* + * // make sure level is solvable + * if (sok_ctx_is_lost(ctx)) { + * // return failure + * return false; + * } + */ + for (size_t dir = 0; dir < SOK_DIR_LAST; dir++) { if (!sok_ctx_move(ctx, dir)) { continue; diff --git a/src/libsok/sok.h b/src/libsok/sok.h index a633d12..4fbae72 100644 --- a/src/libsok/sok.h +++ b/src/libsok/sok.h @@ -122,6 +122,9 @@ typedef struct { // number of open goals size_t num_goals_left; + // are there boxes in corners? + // _Bool is_lost; + // player position sok_pos_t home; @@ -137,6 +140,7 @@ void sok_ctx_init(sok_ctx_t * const ctx, void *user_data); _Bool sok_ctx_set_level(sok_ctx_t * const ctx, const char * const level); _Bool sok_ctx_is_done(const sok_ctx_t * const); +// _Bool sok_ctx_is_lost(const sok_ctx_t * const); _Bool sok_ctx_move(sok_ctx_t * const, const sok_dir_t); _Bool sok_ctx_undo(sok_ctx_t * const); -- cgit v1.2.3