validate turn
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Tue, 2 Jul 2024 13:58:55 +0000 (15:58 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Tue, 2 Jul 2024 13:58:55 +0000 (15:58 +0200)
checkers.c
display.c

index 8f994688d64b47de2dde56e5e115964689de407d..284fd27264b458aef3da8c42f8e9f2fb6e3e4372 100644 (file)
@@ -35,7 +35,8 @@ char *help =
     "<k> Move down cursor\n"
     "<l> Move right cursor\n"
     "<q> Quit\n"
-//    "<x> Undo last move\n"
+    "<v> validate turn\n"
+    "<x> Undo last move\n"
     "<s> 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:
index b3f7a9510a8381b79212deff02e344c87f674aff..286e1147aa3502d66427eb5cf086ac06b4aae684 100644 (file)
--- 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);
 }