diff options
Diffstat (limited to 'src/core/sok-solve.c')
-rw-r--r-- | src/core/sok-solve.c | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/src/core/sok-solve.c b/src/core/sok-solve.c index 34a8320..45d227c 100644 --- a/src/core/sok-solve.c +++ b/src/core/sok-solve.c @@ -7,7 +7,8 @@ static bool sok_solve_step( sok_ctx_t * const ctx, sok_cache_t * const cache, - void (*on_error)(const char * const) + const sok_solve_cbs_t * const cbs, + void * const user_data ) { // check for success if (sok_ctx_is_done(ctx)) { @@ -22,15 +23,19 @@ sok_solve_step( // add to cache if (!sok_cache_add(cache, ctx)) { - if (on_error) { + if (cbs && cbs->on_error) { // log error - on_error("sok_cache_add() failed"); + cbs->on_error("sok_cache_add() failed", user_data); } // return failure return false; } + if (cbs && cbs->on_step && !cbs->on_step(ctx, user_data)) { + // return failure + return false; + } /* * // make sure level is solvable * if (sok_ctx_is_lost(ctx)) { @@ -45,16 +50,16 @@ sok_solve_step( } // recurse, check for success - if (sok_solve_step(ctx, cache, on_error)) { + if (sok_solve_step(ctx, cache, cbs, user_data)) { // return success return true; } // undo move if (!sok_ctx_undo(ctx)) { - if (on_error) { + if (cbs && cbs->on_error) { // log error - on_error("sok_ctx_undo() failed"); + cbs->on_error("sok_ctx_undo() failed", user_data); } // return failure @@ -66,20 +71,25 @@ sok_solve_step( return false; } -bool +bool sok_solve( sok_ctx_t * const ctx, - void (*on_error)(const char * const) + const sok_solve_cbs_t * const cbs, + void *user_data ) { // init cache sok_cache_t cache; if (!sok_cache_init(&cache, SOK_CACHE_DEFAULT_CAPACITY)) { - on_error("sok_cache_init() failed"); + if (cbs && cbs->on_error) { + cbs->on_error("sok_cache_init() failed", user_data); + } + + // return failure return false; } // solve - const bool r = sok_solve_step(ctx, &cache, on_error); + const bool r = sok_solve_step(ctx, &cache, cbs, user_data); // finalize cache sok_cache_fini(&cache); |