functions for turn evaluation
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 1 Jul 2024 21:20:21 +0000 (23:20 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 1 Jul 2024 21:20:21 +0000 (23:20 +0200)
checkers.c
function.c
function.h

index 53dde10bce328fe76b28bb05d8a7237352d85e39..c40efe013c79656c7604c55ec07b1028579b86f8 100644 (file)
@@ -206,23 +206,15 @@ int main (int argc, char *argv[])
         case KEY_UP:
         case 'i':
             if (mode == 0) {
-                xcursor = (xcursor + 1) % board->width;
-                ycursor = (ycursor + board->height - 1) % board->height;
+                domove (board, &xcursor, &ycursor, 0);
             } 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;
+                dopetjump (board, &xcursor, &ycursor, 0);
                 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--;
+                dopetmove (board, &xcursor, &ycursor, 0);
                 mode = 0;
                 player ^= 1;
             }
@@ -230,23 +222,15 @@ int main (int argc, char *argv[])
         case KEY_LEFT:
         case 'j':
             if (mode == 0) {
-                xcursor = (xcursor + board->width - 1) % board->width;
-                ycursor = (ycursor + board->height - 1) % board->height;
+                domove (board, &xcursor, &ycursor, 1);
             } 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;
+                dopetjump (board, &xcursor, &ycursor, 1);
                 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--;
+                dopetmove (board, &xcursor, &ycursor, 1);
                 mode = 0;
                 player ^= 1;
             }
@@ -254,23 +238,15 @@ int main (int argc, char *argv[])
         case KEY_DOWN:
         case 'k':
             if (mode == 0) {
-                xcursor = (xcursor + board->width - 1) % board->width;
-                ycursor = (ycursor + 1) % board->height;
+                domove (board, &xcursor, &ycursor, 2);
             } 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;
+                dopetjump (board, &xcursor, &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++;
+                dopetmove (board, &xcursor, &ycursor, 2);
                 mode = 0;
                 player ^= 1;
             }
@@ -278,23 +254,15 @@ int main (int argc, char *argv[])
         case KEY_RIGHT:
         case 'l':
             if (mode == 0) {
-                xcursor = (xcursor + 1) % board->width;
-                ycursor = (ycursor + 1) % board->height;
+                domove (board, &xcursor, &ycursor, 3);
             } 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;
+                dopetjump (board, &xcursor, &ycursor, 3);
                 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++;
+                dopetmove (board, &xcursor, &ycursor, 3);
                 mode = 0;
                 player ^= 1;
             }
index 9c3cf8a5ea0530f62b25abae6a9cf8bedae59794..c130747377d3a6e0b01351dd9cb1ed4a6e5ec467 100644 (file)
@@ -233,6 +233,94 @@ 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;
 }
 
+void domove (board_t *board, int *x, int *y, int mode)
+{
+    switch (mode) {
+    case 0:
+        *x = (*x + 1) % board->width;
+        *y = (*y + board->height - 1) % board->height;
+        break;
+    case 1:
+        *x = (*x + board->width - 1) % board->width;
+        *y = (*y + board->height - 1) % board->height;
+        break;
+    case 2:
+        *x = (*x + board->width - 1) % board->width;
+        *y = (*y + 1) % board->height;
+        break;
+    case 3:
+        *x = (*x + 1) % board->width;
+        *y = (*y + 1) % board->height;
+        break;
+    }
+}
+
+void dopetjump (board_t *board, int *x, int *y, int mode)
+{
+    char symb = getvalue (board, *x, *y);
+    switch (mode) {
+    case 0:
+        *getcell (board, *x, *y) = '.';
+        *getcell (board, *x + 1, *y - 1) = '.';
+        *getcell (board, *x + 2, *y - 2) = symb;
+        *x += 2;
+        *y -= 2;
+        break;
+    case 1:
+        *getcell (board, *x, *y) = '.';
+        *getcell (board, *x - 1, *y - 1) = '.';
+        *getcell (board, *x - 2, *y - 2) = symb;
+        *x -= 2;
+        *y -= 2;
+        break;
+    case 2:
+        *getcell (board, *x, *y) = '.';
+        *getcell (board, *x - 1, *y + 1) = '.';
+        *getcell (board, *x - 2, *y + 2) = symb;
+        *x -= 2;
+        *y += 2;
+        break;
+    case 3:
+        *getcell (board, *x, *y) = '.';
+        *getcell (board, *x + 1, *y + 1) = '.';
+        *getcell (board, *x + 2, *y + 2) = symb;
+        *x += 2;
+        *y += 2;
+        break;
+    }
+}
+
+void dopetmove (board_t *board, int *x, int *y, int mode)
+{
+    char symb = getvalue (board, *x, *y);
+    switch (mode) {
+    case 0:
+        *getcell (board, *x, *y) = '.';
+        *getcell (board, *x + 1, *y - 1) = symb;
+        *x = *x + 1;
+        *y = *y - 1;
+        break;
+    case 1:
+        *getcell (board, *x, *y) = '.';
+        *getcell (board, *x - 1, *y - 1) = symb;
+        *x = *x - 1;
+        *y = *y - 1;
+        break;
+    case 2:
+        *getcell (board, *x, *y) = '.';
+        *getcell (board, *x - 1, *y + 1) = symb;
+        *x = *x - 1;
+        *y = *y + 1;
+        break;
+    case 3:
+        *getcell (board, *x, *y) = '.';
+        *getcell (board, *x + 1, *y + 1) = symb;
+        *x = *x + 1;
+        *y = *y + 1;
+        break;
+    }
+}
+
 int testjump (board_t *board, int x, int y, int mode)
 {
     int ret = 0;
@@ -276,4 +364,41 @@ int testalljumps (board_t *board, int x, int y)
     return ret;
 }
 
+int evalmaxjumps (board_t *board, int x, int y, int id)
+{
+    int maxjumps = 0;
+    if (getvalue (board, x, y) != '0' + id) {
+        return 0;
+    }
+
+    int m;
+    for (m = 0; m < 4; m++) {
+        if (testjump (board, x, y, m)) {
+            board_t *newboard = copyboard (board);
+            int nx = x;
+            int ny = y;
+            dopetmove (newboard, &nx, &ny, m);
+            int nb = 1 + evalmaxjumps (newboard, nx, ny, id);
+            maxjumps = max (nb, maxjumps);
+            freeboard (newboard);
+        }
+    }
+    return maxjumps;
+}
+
+int globalmaxjumps (board_t *board, int id)
+{
+    int i, j, m;
+    int maxjumps = 0;
+    for (i = 0; i < board->width; i++) {
+        for (j = 0; j < board->height; j++) {
+            for (m = 0; m < 4; m++) {
+                int nb = evalmaxjumps (board, i, j, id);
+                maxjumps = max (nb, maxjumps);
+            }
+        }
+    }
+    return maxjumps;
+}
+
 /* vim: set ts=4 sw=4 et: */
index fbea5085bcfa37999981de85e78a01acdbff0794..ef405c30a2110be20a5139e2749dabc49b78505c 100644 (file)
@@ -39,10 +39,20 @@ char *getcell (board_t *board, int x, int y);
 
 char getvalue (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);
+
+void dopetmove (board_t *board, int *x, int *y, int mode);
+
 int testjump (board_t *board, int x, int y, int mode);
 
 int testalljumps (board_t *board, int x, int y);
 
+int evalmaxjumps (board_t *board, int x, int y, int id);
+
+int globalmaxjumps (board_t *board, int id);
+
 #endif /* __FUNCTION_H__ */
 
 /* vim: set ts=4 sw=4 et: */