buggy code for queen turn
authorLaurent Mazet <mazet@softndesign.org>
Tue, 9 Jul 2024 06:56:59 +0000 (08:56 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 9 Jul 2024 06:56:59 +0000 (08:56 +0200)
checkers.c
display.c
display.h
function.c
function.h

index 9c5141b34951fcbcdfe527e65c31b58b665209bd..da3a8d63ab2a766fe0674d2ca79f2ab70f9689a0 100644 (file)
@@ -199,6 +199,7 @@ int main (int argc, char *argv[])
 
     /* event loop */
     int mode = 0;
+    int queen = -1;
     int stop = 0;
     int player = board->turn;
     while (!stop) {
@@ -212,12 +213,12 @@ int main (int argc, char *argv[])
             sprintf (msg, "Player %s: ", (player == 0) ? "One": "Two");
             switch (mode) {
             case 0:
-                jump = cursorwindow (board, xcursor, ycursor, 0);
+                jump = cursorwindow (board, xcursor, ycursor, queen, 0);
                 nbmaxjumps = globalmaxjumps (board, player);
                 sprintf (msg + strlen (msg), "Begin turn (%d)", nbmaxjumps);
                 break;
             case 1:
-                jump = cursorwindow (board, xcursor, ycursor, 1);
+                jump = cursorwindow (board, xcursor, ycursor, queen, 1);
                 sprintf (msg + strlen (msg), "Choose next (%d)", nbmaxjumps);
                 break;
             case 2:
@@ -255,6 +256,7 @@ int main (int argc, char *argv[])
             case 1:
                 if (cache->board == NULL) {
                     mode = 0;
+                    queen = -1;
                 }
                 break;
             }
@@ -266,7 +268,11 @@ int main (int argc, char *argv[])
                 domove (board, &xcursor, &ycursor, 0);
                 break;
             case 1:
-                if ((jump) && (testjump (board, xcursor, ycursor, 0))) {
+                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);
@@ -289,7 +295,11 @@ int main (int argc, char *argv[])
                 domove (board, &xcursor, &ycursor, 1);
                 break;
             case 1:
-                if ((jump) && (testjump (board, xcursor, ycursor, 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);
@@ -312,7 +322,11 @@ int main (int argc, char *argv[])
                 domove (board, &xcursor, &ycursor, 2);
                 break;
             case 1:
-                if ((jump) && (testjump (board, xcursor, ycursor, 2))) {
+                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);
@@ -335,7 +349,11 @@ int main (int argc, char *argv[])
                 domove (board, &xcursor, &ycursor, 3);
                 break;
             case 1:
-                if ((jump) && (testjump (board, xcursor, ycursor, 3))) {
+                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);
index 2954a42d453fafc367a55976cc81fe36ebe8ef90..fa51bfd00a3eb6a8c343f56cfc17ade3f7835342 100644 (file)
--- a/display.c
+++ b/display.c
@@ -357,7 +357,7 @@ void boardwindow (board_t *board)
     }
 }
 
-int cursorwindow (board_t *board, int x, int y, int mode)
+int cursorwindow (board_t *board, int x, int y, int queen, int mode)
 {
     int jump = 0;
     char id = getvalue (board, x, y);
@@ -368,47 +368,96 @@ int cursorwindow (board_t *board, int x, int y, int mode)
     case '1':
         _element (board, x, y, (mode) ? '3' : '5');
         break;
+    case '6':
+        _element (board, x, y, (mode) ? '8' : ';');
+        break;
+    case '7':
+        _element (board, x, y, (mode) ? '9' : ';');
+        break;
     case ' ':
     case '.':
         _element (board, x, y, 'Z');
         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':
+        switch (id) {
+        case '0':
+        case '1':
+            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;
+                }
+            }
+            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');
                 }
-                break;
-            case '1':
-                if (getvalue (board, x + 1, y - 1) == '.') {
+            } 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 (getvalue (board, x - 1, y - 1) == '.') {
+                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');
                 }
-                break;
             }
+            break;
         }
     }
     return jump;
index 0d10547687baa4371ab2a8d06fc57fe0c03f0b49..a298325e38738ffc4231ab5e05df9774edaa7415 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);
 
-int cursorwindow (board_t *board, int x, int y, int mode);
+int cursorwindow (board_t *board, int x, int y, int queen, int mode);
 
 char *savewindow (int length, int xoffset, int yoffset);
 
index 0d02412520c6e0966c44c7e121cc66790cfce4de..9e5e25995e35f2ce1928a00bb01e6ea8305049d1 100644 (file)
@@ -434,6 +434,86 @@ int globalmaxjumps (board_t *board, int id)
     return maxjumps;
 }
 
+int testqueenmove (board_t *board, int xcursor, int ycursor, int queen, int mode)
+{
+    int ret = 0;
+    if ((mode == queen) || (queen == -1)) {
+        switch (mode) {
+        case 0:
+            ret = (getvalue (board, xcursor + 1, ycursor - 1) == '.');
+            break;
+        case 1:
+            ret = (getvalue (board, xcursor - 1, ycursor - 1) == '.');
+            break;
+        case 2:
+            ret = (getvalue (board, xcursor + 1, ycursor + 1) == '.');
+            break;
+        case 3:
+            ret = (getvalue (board, xcursor + 1, ycursor + 1) == '.');
+            break;
+        }
+    }
+    return ret;
+}
+
+int testqueenjump (board_t *board, int xcursor, int ycursor, int mode)
+{
+    int ret = 0;
+    int delta = 0;
+    switch (mode) {
+    case 0:
+        while (!(ret = testjump (board, xcursor + delta, ycursor - delta, 0))) {
+            delta++;
+            if (getvalue (board, xcursor + delta, ycursor - delta) != '.') {
+                break;
+            }
+        }
+        break;
+    case 1:
+        while (!(ret = testjump (board, xcursor - delta, ycursor - delta, 1))) {
+            delta++;
+            if (getvalue (board, xcursor - delta, ycursor - delta) != '.') {
+                break;
+            }
+        }
+        break;
+    case 2:
+        while (!(ret = testjump (board, xcursor - delta, ycursor + delta, 2))) {
+            delta++;
+            if (getvalue (board, xcursor - delta, ycursor + delta) != '.') {
+                break;
+            }
+        }
+        break;
+    case 3:
+        while (!(ret = testjump (board, xcursor + delta, ycursor + delta, 3))) {
+            delta++;
+            if (getvalue (board, xcursor + delta, ycursor + delta) != '.') {
+                break;
+            }
+        }
+        break;
+    }
+    return (ret) ? delta + 1 : ret;
+}
+
+int testqueenturn (board_t *board, int xcursor, int ycursor, int queen, int mode)
+{
+    int ret = 0;
+    if ((queen == mode) || (queen == -1)) {
+        ret = (testqueenmove (board, xcursor, ycursor, queen, mode) ||
+            testqueenjump (board, xcursor, ycursor, mode));
+    } else {
+        ret = testqueenjump (board, xcursor, ycursor, mode);
+    }
+    return ret;
+}
+
+void doqueenturn (board_t *board, int *xcursor, int *ycursor, int *queen, int mode)
+{
+    return;
+}
+
 cache_t *initcache (board_t *board)
 {
     cache_t *cache = (cache_t *) calloc (board->width / 2 * (board->height - 1) / 2, sizeof (cache_t));
index f72e8ef06481bd89fa381c56bbe5ad65489e9c05..0ae101e7efea1c8602ddfa371ab3609d6a6c29e9 100644 (file)
@@ -57,6 +57,14 @@ int evalmaxjumps (board_t *board, int x, int y, int id);
 
 int globalmaxjumps (board_t *board, int id);
 
+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);
+
+void doqueenturn (board_t *board, int *xcursor, int *ycursor, int *queen, int mode);
+
 cache_t *initcache (board_t *board);
 
 board_t *pushcache (cache_t *cache, board_t *board, int xcursor, int ycursor);