queen move and jump ok
authorLaurent Mazet <mazet@softndesign.org>
Sun, 14 Jul 2024 21:34:52 +0000 (23:34 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 14 Jul 2024 21:34:52 +0000 (23:34 +0200)
checkers.c
display.c
function.c
function.h

index 42b4cf2a4cc315fefb326af009a14b9af0db80d9..92d86f65fd67a1c5ce6bfad80be10f7b802e10e8 100644 (file)
@@ -56,6 +56,55 @@ int usage (int ret)
     return ret;
 }
 
+int petturn (board_t **board, cache_t *cache, int *xcursor, int *ycursor, int jump, int direction)
+{
+    int player = getid (*board, *xcursor, *ycursor);
+
+    int mode = 1;
+    if (ispet (*board, *xcursor, *ycursor, player, 0, -1)) {
+        if ((jump) && (testjump (*board, *xcursor, *ycursor, direction))) {
+            pushcache (cache, *board, *xcursor, *ycursor);
+            *board = copyboard (*board);
+            dopetjump (*board, xcursor, ycursor, direction);
+            if (!testalljumps (*board, *xcursor, *ycursor)) {
+                mode = 2;
+            }
+        } else {
+            int petmove = (((player == 0) && ((direction == 2) || (direction == 3))) ||
+                           ((player == 1) && ((direction == 0) || (direction == 1))));
+            if (petmove && isempty (*board, *xcursor, *ycursor, 1, direction)) {
+                pushcache (cache, *board, *xcursor, *ycursor);
+                *board = copyboard (*board);
+                dopetmove (*board, xcursor, ycursor, direction);
+                mode = 2;
+            }
+        }
+    }
+
+    return mode;
+}
+
+int queenturn (board_t **board, cache_t *cache, int *xcursor, int *ycursor, int queen, int direction)
+{
+    int player = getid (*board, *xcursor, *ycursor);
+
+    if (isqueen (*board, *xcursor, *ycursor, player, 0, -1)) {
+        if (testqueenmove (*board, *xcursor, *ycursor, queen, direction)) {
+            pushcache (cache, *board, *xcursor, *ycursor);
+            *board = copyboard (*board);
+            dopetmove (*board, xcursor, ycursor, direction);
+            queen = direction;
+        } else if (testqueenjump (*board, *xcursor, *ycursor, direction)) {
+            pushcache (cache, *board, *xcursor, *ycursor);
+            *board = copyboard (*board);
+            dopetjump (*board, xcursor, ycursor, direction);
+            queen = -1;
+        }
+    }
+
+    return queen;
+}
+
 /* main function */
 int main (int argc, char *argv[])
 {
@@ -224,6 +273,10 @@ int main (int argc, char *argv[])
             case 2:
                 sprintf (msg + strlen (msg), "Validate turn");
                 break;
+            case 3:
+                jump = cursorwindow (board, xcursor, ycursor, queen, 1);
+                sprintf (msg + strlen (msg), "Continue or validate turn");
+                break;
             }
             msgwindow (msg, xmsg, ymsg, lmsg);
         } else {
@@ -249,11 +302,14 @@ int main (int argc, char *argv[])
         case 'c':
             switch (mode) {
             case 0:
-                if (getvalue (board, xcursor, ycursor) == ((player == 0) ? '0' : '1')) {
+                if (ispet (board, xcursor, ycursor, player, 0, -1)) {
                     mode = 1;
+                } else if (isqueen (board, xcursor, ycursor, player, 0, -1)) {
+                    mode = 3;
                 }
                 break;
             case 1:
+            case 3:
                 if (cache->board == NULL) {
                     mode = 0;
                     queen = -1;
@@ -268,23 +324,10 @@ int main (int argc, char *argv[])
                 domove (board, &xcursor, &ycursor, 0);
                 break;
             case 1:
-                if (testqueenturn (board, xcursor, ycursor, queen, 0)) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    doqueenturn (board, &xcursor, &ycursor, &queen, 0);
-                } else if ((jump) && (testjump (board, xcursor, ycursor, 0))) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    dopetjump (board, &xcursor, &ycursor, 0);
-                    if (!testalljumps (board, xcursor, ycursor)) {
-                        mode = 2;
-                    }
-                } else if ((player == 1) && (!jump) && (isempty (board, xcursor, ycursor, 1, 0))) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    dopetmove (board, &xcursor, &ycursor, 0);
-                    mode = 2;
-                }
+                mode = petturn (&board, cache, &xcursor, &ycursor, jump, 0);
+                break;
+            case 3:
+                queen = queenturn (&board, cache, &xcursor, &ycursor, queen, 0);
                 break;
             }
             break;
@@ -295,23 +338,10 @@ int main (int argc, char *argv[])
                 domove (board, &xcursor, &ycursor, 1);
                 break;
             case 1:
-                if (testqueenturn (board, xcursor, ycursor, queen, 1)) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    doqueenturn (board, &xcursor, &ycursor, &queen, 1);
-                } else if ((jump) && (testjump (board, xcursor, ycursor, 1))) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    dopetjump (board, &xcursor, &ycursor, 1);
-                    if (!testalljumps (board, xcursor, ycursor)) {
-                        mode = 2;
-                    }
-                } else if ((player == 1) && (!jump) && (isempty (board, xcursor, ycursor, 1, 1))) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    dopetmove (board, &xcursor, &ycursor, 1);
-                    mode = 2;
-                }
+                mode = petturn (&board, cache, &xcursor, &ycursor, jump, 1);
+                break;
+            case 3:
+                queen = queenturn (&board, cache, &xcursor, &ycursor, queen, 1);
                 break;
             }
             break;
@@ -322,23 +352,10 @@ int main (int argc, char *argv[])
                 domove (board, &xcursor, &ycursor, 2);
                 break;
             case 1:
-                if (testqueenturn (board, xcursor, ycursor, queen, 2)) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    doqueenturn (board, &xcursor, &ycursor, &queen, 2);
-                } else if ((jump) && (testjump (board, xcursor, ycursor, 2))) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    dopetjump (board, &xcursor, &ycursor, 2);
-                    if (!testalljumps (board, xcursor, ycursor)) {
-                        mode = 2;
-                    }
-                } else if ((player == 0) && (!jump) && (isempty (board, xcursor, ycursor, 1, 2))) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    dopetmove (board, &xcursor, &ycursor, 2);
-                    mode = 2;
-                }
+                mode = petturn (&board, cache, &xcursor, &ycursor, jump, 2);
+                break;
+            case 3:
+                queen = queenturn (&board, cache, &xcursor, &ycursor, queen, 2);
                 break;
             }
             break;
@@ -349,23 +366,10 @@ int main (int argc, char *argv[])
                 domove (board, &xcursor, &ycursor, 3);
                 break;
             case 1:
-                if (testqueenturn (board, xcursor, ycursor, queen, 3)) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    doqueenturn (board, &xcursor, &ycursor, &queen, 3);
-                } else if ((jump) && (testjump (board, xcursor, ycursor, 3))) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    dopetjump (board, &xcursor, &ycursor, 3);
-                    if (!testalljumps (board, xcursor, ycursor)) {
-                        mode = 2;
-                    }
-                } else if ((player == 0) && (!jump) && (isempty (board, xcursor, ycursor, 1, 3))) {
-                    pushcache (cache, board, xcursor, ycursor);
-                    board = copyboard (board);
-                    dopetmove (board, &xcursor, &ycursor, 3);
-                    mode = 2;
-                }
+                mode = petturn (&board, cache, &xcursor, &ycursor, jump, 3);
+                break;
+            case 3:
+                queen = queenturn (&board, cache, &xcursor, &ycursor, queen, 3);
                 break;
             }
             break;
@@ -389,12 +393,16 @@ int main (int argc, char *argv[])
         case '\n':
         case '\r':
         case 'v':
-            if (mode == 2) {
+            switch (mode) {
+            case 2:
+            case 3:
                 player = (player == 0) ? 1 : 0;
                 emptycache (cache);
                 cleanafterjump (board);
                 pettoqueen (board);
+                queen = -1;
                 mode = 0;
+                break;
             }
             break;
         case KEY_BACKSPACE:
index ebfd5250ef478a7145b348746211589daf9adc73..bff4120293f2ed1cc4502a41f3eec21c6ff1a784 100644 (file)
--- a/display.c
+++ b/display.c
@@ -402,60 +402,45 @@ int cursorwindow (board_t *board, int x, int y, int queen, int mode)
             if (!jump) {
                 switch (id) {
                 case '0':
-                    if (getvalue (board, x - 1, y + 1) == '.') {
+                    if (isempty (board, x, y, 1, 2)) {
                         _element (board, x - 1, y + 1, 'S');
                     }
-                    if (getvalue (board, x + 1, y + 1) == '.') {
+                    if (isempty (board, x, y, 1, 3)) {
                         _element (board, x + 1, y + 1, 'S');
                     }
                     break;
                 case '1':
-                    if (getvalue (board, x + 1, y - 1) == '.') {
+                    if (isempty (board, x, y, 1, 0)) {
                         _element (board, x + 1, y - 1, 'S');
                     }
-                    if (getvalue (board, x - 1, y - 1) == '.') {
-                    _element (board, x - 1, y - 1, 'S');
+                    if (isempty (board, x, y, 1, 1)) {
+                        _element (board, x - 1, y - 1, 'S');
                     }
                     break;
                 }
             }
             break;
-        case 6:
-        case 7:
-            if (queen == -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');
-                }
-                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');
-                }
-            } else {
-                if ((queen == 0) && (getvalue (board, x + 1, y - 1) == '.')) {
-                    _element (board, x + 1, y - 1, 'S');
-                } else if (testjump (board, x, y, 0)) {
-                    _element (board, x + 2, y - 2, 'S');
-                }
-                if ((queen == 1) && (getvalue (board, x - 1, y - 1) == '.')) {
-                    _element (board, x - 1, y - 1, 'S');
-                } else if (testjump (board, x, y, 1)) {
-                    _element (board, x - 2, y - 2, 'S');
-                }
-                if ((queen == 2) && (getvalue (board, x - 1, y + 1) == '.')) {
-                    _element (board, x - 1, y + 1, 'S');
-                } else if (testjump (board, x, y, 2)) {
-                    _element (board, x - 2, y + 2, 'S');
-                }
-                if ((queen == 3) && (getvalue (board, x + 1, y + 1) == '.')) {
-                    _element (board, x + 1, y + 1, 'S');
-                } else if (testjump (board, x, y, 3)) {
-                    _element (board, x + 2, y + 2, 'S');
-                }
+        case '6':
+        case '7':
+            if (((queen ==-1) || (queen == 0)) && isempty (board, x, y, 1, 0)) {
+                _element (board, x + 1, y - 1, 'S');
+            } else if (testjump (board, x, y, 0)) {
+                _element (board, x + 2, y - 2, 'S');
+            }
+            if (((queen == -1) || (queen == 1)) && isempty (board, x, y, 1, 1)) {
+                _element (board, x - 1, y - 1, 'S');
+            } else if (testjump (board, x, y, 1)) {
+                _element (board, x - 2, y - 2, 'S');
+            }
+            if (((queen == -1) || (queen == 2)) && isempty (board, x, y, 1, 2)) {
+                _element (board, x - 1, y + 1, 'S');
+            } else if (testjump (board, x, y, 2)) {
+                _element (board, x - 2, y + 2, 'S');
+            }
+            if (((queen == -1) || (queen == 3)) && isempty (board, x, y, 1, 3)) {
+                _element (board, x + 1, y + 1, 'S');
+            } else if (testjump (board, x, y, 3)) {
+                _element (board, x + 2, y + 2, 'S');
             }
             break;
         }
index d2fd22a4796f0180441ede071b77d5923de2df90..41a5722c7cb50775d38e8ccc474b56430a83ead3 100644 (file)
@@ -239,6 +239,21 @@ 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 getid (board_t *board, int x, int y)
+{
+    int id = -1;
+    switch (getvalue (board, x, y)) {
+    case '0':
+    case '6':
+        id = 0;
+        break;
+    case '1':
+    case '7':
+        id = 1;
+    }
+    return id;
+}
+
 void domove (board_t *board, int *x, int *y, int mode)
 {
     switch (mode) {
@@ -359,19 +374,32 @@ void pettoqueen (board_t *board)
 int _testcell (board_t *board, int xcursor, int ycursor, char value, int delta, int mode)
 {
     int ret = 0;
-    switch (mode) {
-    case 0:
-        ret = (getvalue (board, xcursor + delta, ycursor - delta) == value);
-        break;
-    case 1:
-        ret = (getvalue (board, xcursor - delta, ycursor - delta) == value);
-        break;
-    case 2:
-        ret = (getvalue (board, xcursor + delta, ycursor + delta) == value);
-        break;
-    case 3:
-        ret = (getvalue (board, xcursor + delta, ycursor + delta) == value);
-        break;
+    if (mode == -1) {
+        while (++mode < ((delta) ? 4 : 1)) {
+            ret = ret || _testcell (board, xcursor, ycursor, value, delta, mode);
+        }
+    } else {
+        switch (mode) {
+        case 0:
+            xcursor += delta;
+            ycursor -= delta;
+            break;
+        case 1:
+            xcursor -= delta;
+            ycursor -= delta;
+            break;
+        case 2:
+            xcursor -= delta;
+            ycursor += delta;
+            break;
+        case 3:
+            xcursor += delta;
+            ycursor += delta;
+            break;
+        }
+        ret = ((xcursor >= 0) && (xcursor < board->width) &&
+               (ycursor >= 0) && (ycursor < board->height) &&
+               (getvalue (board, xcursor, ycursor) == value));
     }
     return ret;
 }
@@ -415,7 +443,7 @@ int _testjump (board_t *board, int x, int y, int delta, int mode)
         break;
     }
     if (ret) {
-        int id = (getvalue (board, x, y) == '0') ? 0 : 1;
+        int id = getid (board, x, y);
         ret = ispet (board, x, y, 1 - id, 1 + delta, mode) && isempty (board, x, y, 2 + delta, mode);
     }
     return ret;
@@ -477,7 +505,7 @@ int globalmaxjumps (board_t *board, int id)
 int testqueenmove (board_t *board, int xcursor, int ycursor, int queen, int mode)
 {
     int ret = 0;
-    if ((mode == queen) || (queen == -1)) {
+    if ((queen == mode) || (queen == -1)) {
         ret = isempty (board, xcursor, ycursor, 1, mode);
     }
     return ret;
@@ -485,9 +513,6 @@ int testqueenmove (board_t *board, int xcursor, int ycursor, int queen, int mode
 
 int testqueenjump (board_t *board, int xcursor, int ycursor, int mode)
 {
-    if (getvalue (board, xcursor, ycursor) / 2 != '6' / 2) {
-        return 0;
-    }
     int ret = 0;
     int delta = 0;
     while (!(ret = _testjump (board, xcursor, ycursor, delta, mode))) {
@@ -499,13 +524,13 @@ int testqueenjump (board_t *board, int xcursor, int ycursor, int mode)
     return (ret) ? delta + 1 : ret;
 }
 
-int testqueenturn (board_t *board, int xcursor, int ycursor, int queen, int mode)
+int testqueenturn (board_t *board, int xcursor, int ycursor, int id, int queen, int mode)
 {
     int ret = 0;
-    if (getvalue (board, xcursor, ycursor) / 2 == '6' / 2) {
+    if (isqueen (board, xcursor, ycursor, id, 0, -1)) {
         if ((queen == mode) || (queen == -1)) {
-            ret = (testqueenmove (board, xcursor, ycursor, queen, mode) ||
-                testqueenjump (board, xcursor, ycursor, mode));
+            ret = testqueenmove (board, xcursor, ycursor, queen, mode);
+            ret = ret || testqueenjump (board, xcursor, ycursor, mode);
         } else {
             ret = testqueenjump (board, xcursor, ycursor, mode);
         }
@@ -515,7 +540,13 @@ int testqueenturn (board_t *board, int xcursor, int ycursor, int queen, int mode
 
 void doqueenturn (board_t *board, int *xcursor, int *ycursor, int *queen, int mode)
 {
-    return;
+    if (testqueenmove (board, *xcursor, *ycursor, *queen, mode)) {
+        dopetmove (board, xcursor, ycursor, mode);
+        *queen = mode;
+    } else if (testqueenjump (board, *xcursor, *ycursor, mode)) {
+        dopetjump (board, xcursor, ycursor, mode);
+        *queen = -1;
+    }
 }
 
 cache_t *initcache (board_t *board)
index 7c832606adaacc917c7f5a42c5b279bffdb64b71..11fc14800d2bee362af6a46ff33523e7dde06249 100644 (file)
@@ -39,6 +39,8 @@ char *getcell (board_t *board, int x, int y);
 
 char getvalue (board_t *board, int x, int y);
 
+int getid (board_t *board, int x, int y);
+
 void domove (board_t *board, int *x, int *y, int mode);
 
 void dopetjump (board_t *board, int *x, int *y, int mode);
@@ -67,7 +69,7 @@ int testqueenmove (board_t *board, int xcursor, int ycursor, int queen, int mode
 
 int testqueenjump (board_t *board, int xcursor, int ycursor, int mode);
 
-int testqueenturn (board_t *board, int xcursor, int ycursor, int queen, int mode);
+int testqueenturn (board_t *board, int xcursor, int ycursor, int id, int queen, int mode);
 
 void doqueenturn (board_t *board, int *xcursor, int *ycursor, int *queen, int mode);