diff --git a/game_ext.c b/game_ext.c index 55ce498..c263f35 100644 --- a/game_ext.c +++ b/game_ext.c @@ -59,7 +59,7 @@ bool game_is_wrapping(cgame g) { void game_undo(game g) { // check if g->hist is not empty or if it is possible to undo - if (g->hist == NULL || history_is_empty(history_prev(g->hist))) { + if (!can_undo(g)) { printf("you are at your first move\n"); } else { square tmp = game_get_state(g, history_i(g->hist), history_j(g->hist)); @@ -70,9 +70,16 @@ void game_undo(game g) { } } +bool can_undo(game g) { + if (g->hist == NULL || history_is_empty(history_prev(g->hist))) { + return false; + } + return true; +} + void game_redo(game g) { // check if g->hist is not empty or if it is possible to redo - if (g->hist == NULL || history_next(g->hist) == NULL) { + if (!can_redo(g)) { printf("you are at your last move\n"); } else { g->hist = history_next(g->hist); @@ -81,4 +88,11 @@ void game_redo(game g) { game_update_flags(g); g->hist->state = tmp; } +} + +bool can_redo(game g) { + if (g->hist == NULL || history_next(g->hist) == NULL) { + return false; + } + return true; } \ No newline at end of file diff --git a/game_ext.h b/game_ext.h index b38ed7e..acd8e6c 100644 --- a/game_ext.h +++ b/game_ext.h @@ -85,6 +85,8 @@ void game_undo(game g); **/ void game_redo(game g); +bool can_undo(game g); +bool can_redo(game g); /** * @} */ diff --git a/model.c b/model.c index 4e6ed20..2d06579 100644 --- a/model.c +++ b/model.c @@ -547,14 +547,14 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e) { env->pressed_redo = false; env->pressed_solve = false; } else if (SDL_PointInRect(&mouse, env->rec_undo)) { - if (!(env->g->hist == NULL || history_is_empty(history_prev(env->g->hist)))) { + if (can_undo(env->g)) { env->pressed_undo = true; env->pressed_restart = false; env->pressed_redo = false; env->pressed_solve = false; } } else if (SDL_PointInRect(&mouse, env->rec_redo)) { - if (!(env->g->hist == NULL || history_next(env->g->hist) == NULL)) { + if (can_redo(env->g)) { env->pressed_redo = true; env->pressed_undo = false; env->pressed_restart = false;