undo feature
authorLaurent Mazet <mazet@softndesign.org>
Sat, 29 Jun 2024 11:21:35 +0000 (13:21 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 29 Jun 2024 11:21:35 +0000 (13:21 +0200)
function.c
function.h
solitaire.c
todo.txt
type.h

index 216b3d53ccc9538d78131403da6b82e6f9ffe46b..c601999788e031f9348fafe998fe026ed83f1d2f 100644 (file)
@@ -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: */
index 61aa01940a13ac6384c678ac86e23c84d679cb85..0797cfa4211759f06b1d44de0c4428115b83cf63 100644 (file)
@@ -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: */
index 8a0b0d175862c9698ad5c97108d97918342869ab..0abeb741e0aa2982ece5b7564f06b1a3955c5b5f 100644 (file)
@@ -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;
 }
index 35432bc3ac7102362d3ffa4cded6b34e6432f2cd..4e4efc7e70933f0e118a0ba151c5bc808aa53658 100644 (file)
--- 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 34ea36521c6194f96303fe65152f755cb4c37555..8e55aea7ae1c83a4c5206b903d685a6280f9d572 100644 (file)
--- 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: */