From fb2f537b218a208976a3b8eae1f169e944f86b38 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sat, 29 Jun 2024 13:21:35 +0200 Subject: [PATCH] undo feature --- function.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++- function.h | 8 +++++++- solitaire.c | 19 +++++++++++++------ todo.txt | 2 +- type.h | 6 ++++++ 5 files changed, 80 insertions(+), 9 deletions(-) diff --git a/function.c b/function.c index 216b3d5..c601999 100644 --- a/function.c +++ b/function.c @@ -236,7 +236,7 @@ int testjump (board_t *board, int x, int y, int mode) return ret; } -void dojump (board_t *board, int x, int y, int mode) +void dojump (board_t *board, int x, int y, int mode, move_t *move) { *getcell (board, x, y) = 'O'; switch (mode) { @@ -257,6 +257,33 @@ void dojump (board_t *board, int x, int y, int mode) *getcell (board, x + 2, y) = 'X'; break; } + + move->xpet = x; + move->ypet = y; + move->mode = mode; +} + +void undojump (board_t *board, move_t *move) +{ + *getcell (board, move->xpet, move->ypet) = 'X'; + switch (move->mode) { + case 0: + *getcell (board, move->xpet, move->ypet - 1) = 'X'; + *getcell (board, move->xpet, move->ypet - 2) = 'O'; + break; + case 1: + *getcell (board, move->xpet - 1, move->ypet) = 'X'; + *getcell (board, move->xpet - 2, move->ypet) = 'O'; + break; + case 2: + *getcell (board, move->xpet, move->ypet + 1) = 'X'; + *getcell (board, move->xpet, move->ypet + 2) = 'O'; + break; + case 3: + *getcell (board, move->xpet + 1, move->ypet) = 'X'; + *getcell (board, move->xpet + 2, move->ypet) = 'O'; + break; + } } int endofgame (board_t *board) @@ -273,4 +300,29 @@ int endofgame (board_t *board) return 1; } +move_t *initmove (board_t *board) +{ + int i = 0; + int nbmaxmoves = -1; + int stop = 0; + while (!stop) { + switch (board->tab[i++]) { + case '\0': + stop = 1; + break; + case 'X': + nbmaxmoves++; + break; + } + } + move_t *moves = (move_t *) calloc (nbmaxmoves, sizeof (move_t)); + CHECKALLOC (moves); + return moves; +} + +void freemove (move_t *moves) +{ + free (moves); +} + /* vim: set ts=4 sw=4 et: */ diff --git a/function.h b/function.h index 61aa019..0797cfa 100644 --- a/function.h +++ b/function.h @@ -39,10 +39,16 @@ char getvalue (board_t *board, int x, int y); int testjump (board_t *board, int x, int y, int mode); -void dojump (board_t *board, int x, int y, int mode); +void dojump (board_t *board, int x, int y, int mode, move_t *move); + +void undojump (board_t *board, move_t *move); int endofgame (board_t *board); +move_t *initmove (board_t *board); + +void freemove (move_t *moves); + #endif /* __FUNCTION_H__ */ /* vim: set ts=4 sw=4 et: */ diff --git a/solitaire.c b/solitaire.c index 8a0b0d1..0abeb74 100644 --- a/solitaire.c +++ b/solitaire.c @@ -156,7 +156,7 @@ int main (int argc, char *argv[]) int ysave = yboard + (board->ysize - 1) / 2; char *savename = NULL; - /* init windows */ + /* help window */ int lhelp = helpwindow (help, xhelp, yhelp); /* window positions */ @@ -164,8 +164,12 @@ int main (int argc, char *argv[]) int ymsg = max (yboard + xoffset + 1 + board->ysize, yhelp + lhelp + yoffset + 1); int lmsg = xhelp - xmsg + strmaxlen (help, '\n'); + /* init move history */ + move_t *moves = initmove (board); + /* event loop */ int mode = 0; + int lastmove = 0; int stop = 0; while (!stop) { char *ptr = NULL; @@ -194,7 +198,7 @@ int main (int argc, char *argv[]) if (mode == 0) { ycursor = (ycursor + board->height - 1) % board->height; } else if (testjump (board, xcursor, ycursor, 0)) { - dojump (board, xcursor, ycursor, 0); + dojump (board, xcursor, ycursor, 0, moves + lastmove++); mode = 0; ycursor -= 2; } @@ -204,7 +208,7 @@ int main (int argc, char *argv[]) if (mode == 0) { xcursor = (xcursor + board->width - 1) % board->width; } else if (testjump (board, xcursor, ycursor, 1)) { - dojump (board, xcursor, ycursor, 1); + dojump (board, xcursor, ycursor, 1, moves + lastmove++); mode = 0; xcursor -= 2; } @@ -214,7 +218,7 @@ int main (int argc, char *argv[]) if (mode == 0) { ycursor = (ycursor + 1) % board->height; } else if (testjump (board, xcursor, ycursor, 2)) { - dojump (board, xcursor, ycursor, 2); + dojump (board, xcursor, ycursor, 2, moves + lastmove++); mode = 0; ycursor += 2; } @@ -224,7 +228,7 @@ int main (int argc, char *argv[]) if (mode == 0) { xcursor = (xcursor + 1) % board->width; } else if (testjump (board, xcursor, ycursor, 3)) { - dojump (board, xcursor, ycursor, 3); + dojump (board, xcursor, ycursor, 3, moves + lastmove++); mode = 0; xcursor += 2; } @@ -249,7 +253,9 @@ int main (int argc, char *argv[]) case 127: case '\b': case 'x': - mode = 0; + if (lastmove > 0) { + undojump (board, moves + --lastmove); + } break; //case ERR: //default: @@ -259,6 +265,7 @@ int main (int argc, char *argv[]) endwin (); freeboard (board); + freemove (moves); return 0; } diff --git a/todo.txt b/todo.txt index 35432bc..4e4efc7 100644 --- a/todo.txt +++ b/todo.txt @@ -2,7 +2,7 @@ = restoration - trace de la fenetre de sauvegarde = info de fin de partie -- marche arriere += marche arriere = board 1x2 = board 2x2 = board 2x3 diff --git a/type.h b/type.h index 34ea365..8e55aea 100644 --- a/type.h +++ b/type.h @@ -12,6 +12,12 @@ typedef struct { int yoffset; } board_t; +typedef struct { + int xpet; + int ypet; + int mode; +} move_t; + #endif /* __TYPE_H__ */ /* vim: set ts=4 sw=4 et: */ -- 2.30.2