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) {
*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)
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: */
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: */
int ysave = yboard + (board->ysize - 1) / 2;
char *savename = NULL;
- /* init windows */
+ /* help window */
int lhelp = helpwindow (help, xhelp, yhelp);
/* window positions */
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;
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;
}
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;
}
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;
}
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;
}
case 127:
case '\b':
case 'x':
- mode = 0;
+ if (lastmove > 0) {
+ undojump (board, moves + --lastmove);
+ }
break;
//case ERR:
//default:
endwin ();
freeboard (board);
+ freemove (moves);
return 0;
}
= restoration
- trace de la fenetre de sauvegarde
= info de fin de partie
-- marche arriere
+= marche arriere
= board 1x2
= board 2x2
= board 2x3
int yoffset;
} board_t;
+typedef struct {
+ int xpet;
+ int ypet;
+ int mode;
+} move_t;
+
#endif /* __TYPE_H__ */
/* vim: set ts=4 sw=4 et: */