From bec7cbdc10b1c83634df5d7b9448646e6168d5eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophyl=20Hoguet?= Date: Thu, 23 Dec 2021 14:15:47 +0100 Subject: [PATCH] patch + update game_text --- game.c | 10 +++++----- game_ext.c | 7 +++---- game_test_nnunes.c | 7 ++++--- game_text.c | 8 ++++++++ history.c | 4 ++-- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/game.c b/game.c index 9a74a96..bb5e6c2 100644 --- a/game.c +++ b/game.c @@ -56,7 +56,7 @@ game game_copy(cgame g1) { for (int j = 0; j < g2->width; j++) { g2->tab_cell[i][j] = g1->tab_cell[i][j]; } - //g2->hist = history_copy(g1); + // g2->hist = history_copy(g1); } return g2; } @@ -162,15 +162,15 @@ bool game_check_move(cgame g, uint i, uint j, square s) { } void game_play_move(game g, uint i, uint j, square s) { - game_set_square(g, i, j, s); - game_update_flags(g); if (g->hist == NULL) { g->hist = history_create_empty(); - g->hist = history_insert_first(g->hist, s, i, j); + g->hist = history_insert_first(g->hist, game_get_square(g, i, j), i, j); } else { - g->hist = history_next(history_insert_after(g->hist, g->hist, s, i, j)); + g->hist = history_next(history_insert_after(g->hist, g->hist, game_get_square(g, i, j), i, j)); g->hist = history_delete_all_after(g->hist, g->hist); } + game_set_square(g, i, j, s); + game_update_flags(g); } void delete_flags(game g) { diff --git a/game_ext.c b/game_ext.c index d731453..0ed18f4 100644 --- a/game_ext.c +++ b/game_ext.c @@ -57,7 +57,7 @@ bool game_is_wrapping(cgame g) { void game_undo(game g) { if (history_prev(g->hist) == NULL) { - printf("waiting for your first move"); + printf("waiting for your first move\n"); } else { game_set_square(g, history_i(history_prev(g->hist)), history_j(history_prev(g->hist)), history_state(history_prev(g->hist))); game_update_flags(g); @@ -66,10 +66,9 @@ void game_undo(game g) { } void game_redo(game g) { - if (history_next(g->hist) == NULL){ + if (history_next(g->hist) == NULL) { printf("waiting for you to undo first"); - } - else{ + } else { game_set_square(g, history_i(history_next(g->hist)), history_j(history_next(g->hist)), history_state(history_next(g->hist))); game_update_flags(g); g->hist = history_next(g->hist); diff --git a/game_test_nnunes.c b/game_test_nnunes.c index 5eb8c9f..7536ddb 100644 --- a/game_test_nnunes.c +++ b/game_test_nnunes.c @@ -5,6 +5,7 @@ #include #include "game.h" #include "game_aux.h" +#include "game_ext.h" bool test_dummy(void) { return true; @@ -360,7 +361,7 @@ bool test_game_has_error(void) { /* ************** game_nb_rows ************** */ bool test_game_nb_rows(void) { game g = game_new_empty(); - if (game_nb_rows(g) != g->height){ + if (game_nb_rows(g) != g->height) { return false; } return true; @@ -369,7 +370,7 @@ bool test_game_nb_rows(void) { /* ************** game_nb_cols ************** */ bool test_game_nb_cols(void) { game g = game_new_empty(); - if (game_nb_cols(g) != g->width){ + if (game_nb_cols(g) != g->width) { return false; } return true; @@ -378,7 +379,7 @@ bool test_game_nb_cols(void) { /* ************** game_is_wrapping ************** */ bool test_game_is_wrapping(void) { game g = game_new_empty(); - if (game_is_wrapping(g) != g->wrapping){ + if (game_is_wrapping(g) != g->wrapping) { return false; } return true; diff --git a/game_text.c b/game_text.c index 921330c..88c169d 100644 --- a/game_text.c +++ b/game_text.c @@ -40,6 +40,8 @@ void printhelp(void) { printf("h : Affiche cette aide.\n"); printf("r : Réinitialise la partie.\n"); printf("q : Quitter la partie (abandon).\n"); + printf("z : Annulez le coup (undo).\n"); + printf("y : Annulez l'annulation du coup (redo).\n"); printf( "l : Place une ampoule (*light bulb*) dans la case " "(,).\n"); @@ -80,6 +82,12 @@ int main(void) { game_delete(g); printf("shame\n"); return EXIT_SUCCESS; + } else if (c == 'z') { + game_undo(g); + game_print(g); + } else if (c == 'y') { + game_redo(g); + game_print(g); } else if (c == 'l' || c == 'm' || c == 'b') { if (scanf(" %u %u", &i, &j) == 2) { if (c == 'l' && game_check_move(g, i, j, S_LIGHTBULB)) diff --git a/history.c b/history.c index 845a4aa..4f8a8da 100644 --- a/history.c +++ b/history.c @@ -54,13 +54,13 @@ square history_state(history h) { return h->state; } -square history_i(history h) { +uint history_i(history h) { if (history_is_empty(h)) errPointer(); return h->i; } -square history_j(history h) { +uint history_j(history h) { if (history_is_empty(h)) errPointer(); return h->j;