From cb4fc93ffaf636e7c6fc62f669d0bf4a4a0e95ce Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 9 Jul 2024 08:56:59 +0200 Subject: [PATCH] buggy code for queen turn --- checkers.c | 30 +++++++++++++---- display.c | 99 ++++++++++++++++++++++++++++++++++++++++-------------- display.h | 2 +- function.c | 80 +++++++++++++++++++++++++++++++++++++++++++ function.h | 8 +++++ 5 files changed, 187 insertions(+), 32 deletions(-) diff --git a/checkers.c b/checkers.c index 9c5141b..da3a8d6 100644 --- a/checkers.c +++ b/checkers.c @@ -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); diff --git a/display.c b/display.c index 2954a42..fa51bfd 100644 --- 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; diff --git a/display.h b/display.h index 0d10547..a298325 100644 --- 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); diff --git a/function.c b/function.c index 0d02412..9e5e259 100644 --- a/function.c +++ b/function.c @@ -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)); diff --git a/function.h b/function.h index f72e8ef..0ae101e 100644 --- a/function.h +++ b/function.h @@ -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); -- 2.30.2