From cded799e70de205bf4b41654c71b80110fd26b78 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Mon, 1 Jul 2024 23:20:21 +0200 Subject: [PATCH] functions for turn evaluation --- checkers.c | 56 +++++------------------- function.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++ function.h | 10 +++++ 3 files changed, 147 insertions(+), 44 deletions(-) diff --git a/checkers.c b/checkers.c index 53dde10..c40efe0 100644 --- a/checkers.c +++ b/checkers.c @@ -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; } diff --git a/function.c b/function.c index 9c3cf8a..c130747 100644 --- a/function.c +++ b/function.c @@ -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: */ diff --git a/function.h b/function.h index fbea508..ef405c3 100644 --- a/function.h +++ b/function.h @@ -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: */ -- 2.30.2