tile moves and jumps
authorLaurent Mazet <mazet@softndesign.org>
Sun, 30 Jun 2024 14:29:18 +0000 (16:29 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 30 Jun 2024 14:29:18 +0000 (16:29 +0200)
checkers.c
display.c
display.h
function.c
function.h

index 6ab7858142b1d9e0c6395b2add20df35d272e150..53dde10bce328fe76b28bb05d8a7237352d85e39 100644 (file)
@@ -181,10 +181,12 @@ int main (int argc, char *argv[])
     int player = 0;
     while (!stop) {
         char *ptr = NULL;
+        int jump = 0;
 
         boardwindow (board);
         if (1) { /* not end of game */
-            cursorwindow (board, xcursor, ycursor, mode);
+            jump = cursorwindow (board, xcursor, ycursor, mode);
+            msgwindow (player == 0 ? "Player One": "Player Two", xmsg, ymsg, lmsg);
         } else {
             msgwindow ("End of game", xmsg, ymsg, lmsg);
         }
@@ -206,8 +208,23 @@ int main (int argc, char *argv[])
             if (mode == 0) {
                 xcursor = (xcursor + 1) % board->width;
                 ycursor = (ycursor + board->height - 1) % board->height;
-            } else {
+            } else if ((jump) && (testjump (board, xcursor, ycursor, 0))) {
+                *getcell (board, xcursor, ycursor) = '.';
+                *getcell (board, xcursor + 1, ycursor - 1) = '.';
+                *getcell (board, xcursor + 2, ycursor - 2) = '0' + player;
+                xcursor += 2;
+                ycursor -= 2;
+                if (!testalljumps (board, xcursor, ycursor)) {
+                    mode = 0;
+                    player ^= 1;
+                }
+            } else if ((player == 1) && (!jump) && (getvalue (board, xcursor + 1, ycursor - 1) == '.')) {
+                *getcell (board, xcursor, ycursor) = '.';
+                *getcell (board, xcursor + 1, ycursor - 1) = '0' + player;
+                xcursor++;
+                ycursor--;
                 mode = 0;
+                player ^= 1;
             }
             break;
         case KEY_LEFT:
@@ -215,8 +232,23 @@ int main (int argc, char *argv[])
             if (mode == 0) {
                 xcursor = (xcursor + board->width - 1) % board->width;
                 ycursor = (ycursor + board->height - 1) % board->height;
-            } else {
+            } else if ((jump) && (testjump (board, xcursor, ycursor, 1))) {
+                *getcell (board, xcursor, ycursor) = '.';
+                *getcell (board, xcursor - 1, ycursor - 1) = '.';
+                *getcell (board, xcursor - 2, ycursor - 2) = '0' + player;
+                xcursor -= 2;
+                ycursor -= 2;
+                if (!testalljumps (board, xcursor, ycursor)) {
+                    mode = 0;
+                    player ^= 1;
+                }
+            } else if ((player == 1) && (!jump) && (getvalue (board, xcursor - 1, ycursor - 1) == '.')) {
+                *getcell (board, xcursor, ycursor) = '.';
+                *getcell (board, xcursor - 1, ycursor - 1) = '0' + player;
+                xcursor--;
+                ycursor--;
                 mode = 0;
+                player ^= 1;
             }
             break;
         case KEY_DOWN:
@@ -224,8 +256,23 @@ int main (int argc, char *argv[])
             if (mode == 0) {
                 xcursor = (xcursor + board->width - 1) % board->width;
                 ycursor = (ycursor + 1) % board->height;
-            } else {
+            } else if ((jump) && (testjump (board, xcursor, ycursor, 2))) {
+                *getcell (board, xcursor, ycursor) = '.';
+                *getcell (board, xcursor - 1, ycursor + 1) = '.';
+                *getcell (board, xcursor - 2, ycursor + 2) = '0' + player;
+                xcursor -= 2;
+                ycursor += 2;
+                if (!testalljumps (board, xcursor, ycursor)) {
+                    mode = 0;
+                    player ^= 1;
+                }
+            } else if ((player == 0) && (!jump) && (getvalue (board, xcursor - 1, ycursor + 1) == '.')) {
+                *getcell (board, xcursor, ycursor) = '.';
+                *getcell (board, xcursor - 1, ycursor + 1) = '0' + player;
+                xcursor--;
+                ycursor++;
                 mode = 0;
+                player ^= 1;
             }
             break;
         case KEY_RIGHT:
@@ -233,8 +280,23 @@ int main (int argc, char *argv[])
             if (mode == 0) {
                 xcursor = (xcursor + 1) % board->width;
                 ycursor = (ycursor + 1) % board->height;
-            } else {
+            } else if ((jump) && (testjump (board, xcursor, ycursor, 3))) {
+                *getcell (board, xcursor, ycursor) = '.';
+                *getcell (board, xcursor + 1, ycursor + 1) = '.';
+                *getcell (board, xcursor + 2, ycursor + 2) = '0' + player;
+                xcursor += 2;
+                ycursor += 2;
+                if (!testalljumps (board, xcursor, ycursor)) {
+                    mode = 0;
+                    player ^= 1;
+                }
+            } else if ((player == 0) && (!jump) && (getvalue (board, xcursor + 1, ycursor + 1) == '.')) {
+                *getcell (board, xcursor, ycursor) = '.';
+                *getcell (board, xcursor + 1, ycursor + 1) = '0' + player;
+                xcursor++;
+                ycursor++;
                 mode = 0;
+                player ^= 1;
             }
             break;
         case KEY_ESC:
index a25b644ba31a3091e0ed44b41d5450bcc13dbf58..8595908b1182e4ddc1933e6897c9bae294bac4f5 100644 (file)
--- a/display.c
+++ b/display.c
@@ -248,6 +248,9 @@ void _element2 (board_t *board, int x, int y, int symb)
     case '5':
         set_color (bred);
         break;
+    case 'S':
+        set_color (byellow);
+        break;
     case 'Z':
         set_color (bblue);
         break;
@@ -348,9 +351,11 @@ void boardwindow (board_t *board)
     }
 }
 
-void cursorwindow (board_t *board, int x, int y, int mode)
+int cursorwindow (board_t *board, int x, int y, int mode)
 {
-    switch (getvalue (board, x, y)) {
+    int jump = 0;
+    char id = getvalue (board, x, y);
+    switch (id) {
     case '0':
         _element (board, x, y, (mode) ? '2' : '4');
         break;
@@ -363,7 +368,44 @@ void cursorwindow (board_t *board, int x, int y, int mode)
         break;
     }
     if (mode) {
+        if (testjump (board, x, y, 0)) {
+            _element (board, x + 2, y - 2, 'S');
+            jump = 1;
+        }
+        if (testjump (board, x, y, 1)) {
+            _element (board, x - 2, y - 2, 'S');
+            jump = 1;
+        }
+        if (testjump (board, x, y, 2)) {
+            _element (board, x - 2, y + 2, 'S');
+            jump = 1;
+        }
+        if (testjump (board, x, y, 3)) {
+            _element (board, x + 2, y + 2, 'S');
+            jump = 1;
+        }
+        if (!jump) {
+            switch (id) {
+            case '0':
+                if (getvalue (board, x - 1, y + 1) == '.') {
+                    _element (board, x - 1, y + 1, 'S');
+                }
+                if (getvalue (board, x + 1, y + 1) == '.') {
+                    _element (board, x + 1, y + 1, 'S');
+                }
+                break;
+            case '1':
+                if (getvalue (board, x + 1, y - 1) == '.') {
+                    _element (board, x + 1, y - 1, 'S');
+                }
+                if (getvalue (board, x - 1, y - 1) == '.') {
+                    _element (board, x - 1, y - 1, 'S');
+                }
+                break;
+            }
+        }
     }
+    return jump;
 }
 
 char *savewindow (int length, int xoffset, int yoffset)
index 442382917daa7539843515c6d421c174fe21db2e..0d10547687baa4371ab2a8d06fc57fe0c03f0b49 100644 (file)
--- a/display.h
+++ b/display.h
@@ -10,7 +10,7 @@ int helpwindow (char *msg, int xoffset, int yoffset);
 
 void boardwindow (board_t *board);
 
-void cursorwindow (board_t *board, int x, int y, int mode);
+int cursorwindow (board_t *board, int x, int y, int mode);
 
 char *savewindow (int length, int xoffset, int yoffset);
 
index 28d100382e765d3a1feb5753d2cbe68f2bc114f2..9c3cf8a5ea0530f62b25abae6a9cf8bedae59794 100644 (file)
@@ -233,4 +233,47 @@ char getvalue (board_t *board, int x, int y)
     return (x >= 0) && (x < board->width) && (y >= 0) && (y < board->height) ? *getcell (board, x, y) : 0;
 }
 
+int testjump (board_t *board, int x, int y, int mode)
+{
+    int ret = 0;
+
+    char id = getvalue (board, x, y);
+    switch (mode) {
+    case 0:
+        if ((x + 2 < board->width) && (y - 2 >= 0)) {
+            ret = (getvalue (board, x + 1, y - 1) == '0' + '1' - id) && (getvalue (board, x + 2, y - 2) == '.');
+        }
+        break;
+    case 1:
+        if ((x - 2 >= 0) && (y - 2 >= 0)) {
+            ret = (getvalue (board, x - 1, y - 1) == '0' + '1' - id) && (getvalue (board, x - 2, y - 2) == '.');
+        }
+        break;
+    case 2:
+        if ((x - 2 >= 0) && (y + 2 < board->height)) {
+            ret = (getvalue (board, x - 1, y + 1) == '0' + '1' - id) && (getvalue (board, x - 2, y + 2) == '.');
+        }
+        break;
+    case 3:
+        if ((x + 2 < board->width) && (y + 2 < board->height)) {
+            ret = (getvalue (board, x + 1, y + 1) == '0' + '1' - id) && (getvalue (board, x + 2, y + 2) == '.');
+        }
+        break;
+    }
+    return ret;
+}
+
+int testalljumps (board_t *board, int x, int y)
+{
+    int ret = 0;
+    int m;
+    for (m = 0; m < 4; m++) {
+        if (testjump (board, x, y, m)) {
+            ret = 1;
+            break;
+        }
+    }
+    return ret;
+}
+
 /* vim: set ts=4 sw=4 et: */
index 3c941ef199a7cbded4a25b3b09ac2cdee98238e8..fbea5085bcfa37999981de85e78a01acdbff0794 100644 (file)
@@ -39,6 +39,10 @@ char *getcell (board_t *board, int x, int y);
 
 char getvalue (board_t *board, int x, int y);
 
+int testjump (board_t *board, int x, int y, int mode);
+
+int testalljumps (board_t *board, int x, int y);
+
 #endif /* __FUNCTION_H__ */
 
 /* vim: set ts=4 sw=4 et: */