From 209c0517ce15437c67e55df053f7feefac5202b2 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Tue, 2 Jul 2024 15:58:55 +0200 Subject: [PATCH] validate turn --- checkers.c | 135 +++++++++++++++++++++++++++++++++-------------------- display.c | 6 +++ 2 files changed, 91 insertions(+), 50 deletions(-) diff --git a/checkers.c b/checkers.c index 8f99468..284fd27 100644 --- a/checkers.c +++ b/checkers.c @@ -35,7 +35,8 @@ char *help = " Move down cursor\n" " Move right cursor\n" " Quit\n" -// " Undo last move\n" + " validate turn\n" + " Undo last move\n" " Save file\n" ; @@ -180,15 +181,29 @@ int main (int argc, char *argv[]) int stop = 0; int player = 0; while (!stop) { + char msg[128] = {0}; char *ptr = NULL; + int nbmaxjumps = 0; int jump = 0; boardwindow (board); if (1) { /* not end of game */ - char msg[128] = {0}; - jump = cursorwindow (board, xcursor, ycursor, mode); - int nb = globalmaxjumps (board, player); - sprintf (msg, "Player %s (%d)", (player == 0) ? "One": "Two", nb); + sprintf (msg, "Player %s: ", (player == 0) ? "One": "Two"); + switch (mode) { + case 0: + jump = cursorwindow (board, xcursor, ycursor, 0); + nbmaxjumps = globalmaxjumps (board, player); + sprintf (msg + strlen (msg), "Begin turn (%d)", nbmaxjumps); + break; + case 1: + case 2: + jump = cursorwindow (board, xcursor, ycursor, 1); + sprintf (msg + strlen (msg), "Choose next (%d)", nbmaxjumps); + break; + case 3: + sprintf (msg + strlen (msg), "Validate turn"); + break; + } msgwindow (msg, xmsg, ymsg, lmsg); } else { msgwindow ("End of game", xmsg, ymsg, lmsg); @@ -197,77 +212,88 @@ int main (int argc, char *argv[]) int ch = getch (); switch (ch) { case ' ': - case '\n': - case '\r': case 'c': - if (mode == 1) { + switch (mode) { + case 0: + if (getvalue (board, xcursor, ycursor) == ((player == 0) ? '0' : '1')) { + mode = 1; + } + break; + case 1: mode = 0; - } else if ((getvalue (board, xcursor, ycursor) != ' ') && ((getvalue (board, xcursor, ycursor) - '0') % 2 == player)) { - mode = 1; + break; } break; case KEY_UP: case 'i': - if (mode == 0) { + switch (mode) { + case 0: domove (board, &xcursor, &ycursor, 0); - } else if ((jump) && (testjump (board, xcursor, ycursor, 0))) { - dopetjump (board, &xcursor, &ycursor, 0); - if (!testalljumps (board, xcursor, ycursor)) { - mode = 0; - player ^= 1; + break; + case 1: + case 2: + if ((jump) && (testjump (board, xcursor, ycursor, 0))) { + dopetjump (board, &xcursor, &ycursor, 0); + mode = (testalljumps (board, xcursor, ycursor)) ? 2 : 3; + } else if ((player == 1) && (!jump) && (getvalue (board, xcursor + 1, ycursor - 1) == '.')) { + dopetmove (board, &xcursor, &ycursor, 0); + mode = 3; } - } else if ((player == 1) && (!jump) && (getvalue (board, xcursor + 1, ycursor - 1) == '.')) { - dopetmove (board, &xcursor, &ycursor, 0); - mode = 0; - player ^= 1; + break; } break; case KEY_LEFT: case 'j': - if (mode == 0) { + switch (mode) { + case 0: domove (board, &xcursor, &ycursor, 1); - } else if ((jump) && (testjump (board, xcursor, ycursor, 1))) { - dopetjump (board, &xcursor, &ycursor, 1); - if (!testalljumps (board, xcursor, ycursor)) { - mode = 0; - player ^= 1; + break; + case 1: + case 2: + if ((jump) && (testjump (board, xcursor, ycursor, 1))) { + dopetjump (board, &xcursor, &ycursor, 1); + mode = (testalljumps (board, xcursor, ycursor)) ? 2 : 3; + } else if ((player == 1) && (!jump) && (getvalue (board, xcursor - 1, ycursor - 1) == '.')) { + dopetmove (board, &xcursor, &ycursor, 1); + mode = 3; } - } else if ((player == 1) && (!jump) && (getvalue (board, xcursor - 1, ycursor - 1) == '.')) { - dopetmove (board, &xcursor, &ycursor, 1); - mode = 0; - player ^= 1; + break; } break; case KEY_DOWN: case 'k': - if (mode == 0) { + switch (mode) { + case 0: domove (board, &xcursor, &ycursor, 2); - } else if ((jump) && (testjump (board, xcursor, ycursor, 2))) { - dopetjump (board, &xcursor, &ycursor, 2); - if (!testalljumps (board, xcursor, ycursor)) { - mode = 0; - player ^= 1; + break; + case 1: + case 2: + if ((jump) && (testjump (board, xcursor, ycursor, 2))) { + dopetjump (board, &xcursor, &ycursor, 2); + mode = (testalljumps (board, xcursor, ycursor)) ? 2 : 3; + } else if ((player == 0) && (!jump) && (getvalue (board, xcursor - 1, ycursor + 1) == '.')) { + dopetmove (board, &xcursor, &ycursor, 2); + mode = 3; } - } else if ((player == 0) && (!jump) && (getvalue (board, xcursor - 1, ycursor + 1) == '.')) { - dopetmove (board, &xcursor, &ycursor, 2); - mode = 0; - player ^= 1; + break; } break; case KEY_RIGHT: case 'l': - if (mode == 0) { + switch (mode) { + case 0: domove (board, &xcursor, &ycursor, 3); - } else if ((jump) && (testjump (board, xcursor, ycursor, 3))) { - dopetjump (board, &xcursor, &ycursor, 3); - if (!testalljumps (board, xcursor, ycursor)) { - mode = 0; - player ^= 1; + break; + case 1: + case 2: + if ((jump) && (testjump (board, xcursor, ycursor, 3))) { + dopetjump (board, &xcursor, &ycursor, 3); + mode = (testalljumps (board, xcursor, ycursor)) ? 2 : 3; + } else if ((player == 0) && (!jump) && (getvalue (board, xcursor + 1, ycursor + 1) == '.')) { + dopetmove (board, &xcursor, &ycursor, 3); + mode = 3; } - } else if ((player == 0) && (!jump) && (getvalue (board, xcursor + 1, ycursor + 1) == '.')) { - dopetmove (board, &xcursor, &ycursor, 3); - mode = 0; - player ^= 1; + break; } break; case KEY_ESC: @@ -285,6 +311,15 @@ int main (int argc, char *argv[]) free (savename); } break; + case KEY_ENTER: + case '\n': + case '\r': + case 'v': + if (mode == 3) { + player = (player == 0) ? 1 : 0; + mode = 0; + } + break; case KEY_BACKSPACE: case KEY_DELETE: case 127: diff --git a/display.c b/display.c index b3f7a95..286e114 100644 --- a/display.c +++ b/display.c @@ -466,6 +466,12 @@ void msgwindow (char *msg, int xoffset, int yoffset, int length) { set_color (white); _dobound ((length > 0) ? length : (int)strlen (msg), 1, xoffset, yoffset); + if (length > 0) { + int i; + for (i = 0; i < length; i++) { + mvaddch (yoffset, xoffset + i, ' '); + } + } mvaddstr (yoffset, xoffset + ((length > 0) ? (length - (int)strlen (msg)) / 2 : 0), msg); set_color (black); } -- 2.30.2