edit game_undo + struct game_redo + copy hist

This commit is contained in:
thibault perrier committed 2022-01-06 02:20:36 +01:00
1 parent 52458140cc
commit 87e5ebe741
3 files changed
+26 -8

No files matched your search

+3 -3
View File
@@ -56,7 +56,7 @@ game game_copy(cgame g1) {
for (int j = 0; j < g2->width; j++) { for (int j = 0; j < g2->width; j++) {
g2->tab_cell[i][j] = g1->tab_cell[i][j]; g2->tab_cell[i][j] = g1->tab_cell[i][j];
} }
// g2->hist = history_copy(g1); g2->hist = history_copy(g1->hist);
} }
return g2; return g2;
} }
@@ -164,9 +164,9 @@ bool game_check_move(cgame g, uint i, uint j, square s) {
void game_play_move(game g, uint i, uint j, square s) { void game_play_move(game g, uint i, uint j, square s) {
if (g->hist == NULL) { if (g->hist == NULL) {
g->hist = history_create_empty(); g->hist = history_create_empty();
g->hist = history_insert_first(g->hist, game_get_square(g, i, j), i, j); g->hist = history_insert_first(g->hist, game_get_state(g, i, j), i, j);
} else { } else {
g->hist = history_next(history_insert_after(g->hist, g->hist, game_get_square(g, i, j), i, j)); g->hist = history_next(history_insert_after(g->hist, g->hist, game_get_state(g, i, j), i, j));
g->hist = history_delete_all_after(g->hist, g->hist); g->hist = history_delete_all_after(g->hist, g->hist);
} }
game_set_square(g, i, j, s); game_set_square(g, i, j, s);
+5 -5
View File
@@ -56,17 +56,17 @@ bool game_is_wrapping(cgame g) {
} }
void game_undo(game g) { void game_undo(game g) {
if (history_prev(g->hist) == NULL) { if (g->hist == NULL) {
printf("waiting for your first move\n"); printf("you are at your first move\n");
} else { } else {
game_set_square(g, history_i(history_prev(g->hist)), history_j(history_prev(g->hist)), history_state(history_prev(g->hist))); game_set_square(g, history_i(g->hist), history_j(g->hist), history_state(g->hist));
game_update_flags(g); game_update_flags(g);
g->hist = history_prev(g->hist); g->hist = history_append(g->hist, g->hist->state, g->hist->i, g->hist->j);
} }
} }
void game_redo(game g) { void game_redo(game g) {
if (history_next(g->hist) == NULL) { if ((g->hist == NULL && (history_next(g->hist) == NULL)) || g->hist == NULL) {
printf("waiting for you to undo first"); 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_set_square(g, history_i(history_next(g->hist)), history_j(history_next(g->hist)), history_state(history_next(g->hist)));
+18
View File
@@ -247,6 +247,22 @@ bool test_game_undo(){
return true; return true;
} }
/* ********** game_redo ********** */
bool test_game_redo(){
game g1 = game_new_empty();
game_play_move(g1, 0, 0, S_LIGHTBULB);
game_play_move(g1, 0, 1, S_MARK);
game_play_move(g1, 0, 0, S_BLANK);
game g2 = game_copy(g1);
game_undo(g1);
game_redo(g1);
if (!game_equal(g1, g2)){
return false;
}
return true;
}
/* ********** usage ********** */ /* ********** usage ********** */
void usage(int argc, char* argv[]) { void usage(int argc, char* argv[]) {
fprintf(stderr, "Usage: %s <testname>\n", argv[0]); fprintf(stderr, "Usage: %s <testname>\n", argv[0]);
@@ -277,6 +293,8 @@ int main(int argc, char* argv[]) {
success = test_game_get_black_number(); success = test_game_get_black_number();
} else if (strcmp("game_undo", argv[1]) == 0) { } else if (strcmp("game_undo", argv[1]) == 0) {
success = test_game_undo(); success = test_game_undo();
} else if (strcmp("game_redo", argv[1]) == 0) {
success = test_game_redo();
} else { } else {
fprintf(stderr, "test \"%s\" is not a valid test name.\n", argv[1]); fprintf(stderr, "test \"%s\" is not a valid test name.\n", argv[1]);
return EXIT_FAILURE; return EXIT_FAILURE;