From 27bb06ddb6613e90ea0c5499897c06742325610b Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 14 Jul 2024 23:34:52 +0200 Subject: [PATCH] queen move and jump ok --- checkers.c | 148 ++++++++++++++++++++++++++++------------------------- display.c | 67 ++++++++++-------------- function.c | 77 +++++++++++++++++++--------- function.h | 4 +- 4 files changed, 161 insertions(+), 135 deletions(-) diff --git a/checkers.c b/checkers.c index 42b4cf2..92d86f6 100644 --- a/checkers.c +++ b/checkers.c @@ -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: diff --git a/display.c b/display.c index ebfd525..bff4120 100644 --- 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; } diff --git a/function.c b/function.c index d2fd22a..41a5722 100644 --- a/function.c +++ b/function.c @@ -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) diff --git a/function.h b/function.h index 7c83260..11fc148 100644 --- a/function.h +++ b/function.h @@ -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); -- 2.30.2