From: Laurent Mazet Date: Sun, 30 Jun 2024 14:29:18 +0000 (+0200) Subject: tile moves and jumps X-Git-Tag: v1.0~34 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=fcfe8c341b3cd85e5f3540ecca63b7793e46740b;p=checkers.git tile moves and jumps --- diff --git a/checkers.c b/checkers.c index 6ab7858..53dde10 100644 --- a/checkers.c +++ b/checkers.c @@ -181,10 +181,12 @@ int main (int argc, char *argv[]) int player = 0; while (!stop) { char *ptr = NULL; + int jump = 0; boardwindow (board); if (1) { /* not end of game */ - cursorwindow (board, xcursor, ycursor, mode); + jump = cursorwindow (board, xcursor, ycursor, mode); + msgwindow (player == 0 ? "Player One": "Player Two", xmsg, ymsg, lmsg); } else { msgwindow ("End of game", xmsg, ymsg, lmsg); } @@ -206,8 +208,23 @@ int main (int argc, char *argv[]) if (mode == 0) { xcursor = (xcursor + 1) % board->width; ycursor = (ycursor + board->height - 1) % board->height; - } else { + } 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; + 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--; mode = 0; + player ^= 1; } break; case KEY_LEFT: @@ -215,8 +232,23 @@ int main (int argc, char *argv[]) if (mode == 0) { xcursor = (xcursor + board->width - 1) % board->width; ycursor = (ycursor + board->height - 1) % board->height; - } else { + } 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; + 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--; mode = 0; + player ^= 1; } break; case KEY_DOWN: @@ -224,8 +256,23 @@ int main (int argc, char *argv[]) if (mode == 0) { xcursor = (xcursor + board->width - 1) % board->width; ycursor = (ycursor + 1) % board->height; - } else { + } 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; + 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++; mode = 0; + player ^= 1; } break; case KEY_RIGHT: @@ -233,8 +280,23 @@ int main (int argc, char *argv[]) if (mode == 0) { xcursor = (xcursor + 1) % board->width; ycursor = (ycursor + 1) % board->height; - } else { + } 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; + 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++; mode = 0; + player ^= 1; } break; case KEY_ESC: diff --git a/display.c b/display.c index a25b644..8595908 100644 --- a/display.c +++ b/display.c @@ -248,6 +248,9 @@ void _element2 (board_t *board, int x, int y, int symb) case '5': set_color (bred); break; + case 'S': + set_color (byellow); + break; case 'Z': set_color (bblue); break; @@ -348,9 +351,11 @@ void boardwindow (board_t *board) } } -void cursorwindow (board_t *board, int x, int y, int mode) +int cursorwindow (board_t *board, int x, int y, int mode) { - switch (getvalue (board, x, y)) { + int jump = 0; + char id = getvalue (board, x, y); + switch (id) { case '0': _element (board, x, y, (mode) ? '2' : '4'); break; @@ -363,7 +368,44 @@ void cursorwindow (board_t *board, int x, int y, int mode) 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': + 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; + } + } } + return jump; } char *savewindow (int length, int xoffset, int yoffset) diff --git a/display.h b/display.h index 4423829..0d10547 100644 --- a/display.h +++ b/display.h @@ -10,7 +10,7 @@ int helpwindow (char *msg, int xoffset, int yoffset); void boardwindow (board_t *board); -void cursorwindow (board_t *board, int x, int y, int mode); +int cursorwindow (board_t *board, int x, int y, int mode); char *savewindow (int length, int xoffset, int yoffset); diff --git a/function.c b/function.c index 28d1003..9c3cf8a 100644 --- a/function.c +++ b/function.c @@ -233,4 +233,47 @@ 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 testjump (board_t *board, int x, int y, int mode) +{ + int ret = 0; + + char id = getvalue (board, x, y); + switch (mode) { + case 0: + if ((x + 2 < board->width) && (y - 2 >= 0)) { + ret = (getvalue (board, x + 1, y - 1) == '0' + '1' - id) && (getvalue (board, x + 2, y - 2) == '.'); + } + break; + case 1: + if ((x - 2 >= 0) && (y - 2 >= 0)) { + ret = (getvalue (board, x - 1, y - 1) == '0' + '1' - id) && (getvalue (board, x - 2, y - 2) == '.'); + } + break; + case 2: + if ((x - 2 >= 0) && (y + 2 < board->height)) { + ret = (getvalue (board, x - 1, y + 1) == '0' + '1' - id) && (getvalue (board, x - 2, y + 2) == '.'); + } + break; + case 3: + if ((x + 2 < board->width) && (y + 2 < board->height)) { + ret = (getvalue (board, x + 1, y + 1) == '0' + '1' - id) && (getvalue (board, x + 2, y + 2) == '.'); + } + break; + } + return ret; +} + +int testalljumps (board_t *board, int x, int y) +{ + int ret = 0; + int m; + for (m = 0; m < 4; m++) { + if (testjump (board, x, y, m)) { + ret = 1; + break; + } + } + return ret; +} + /* vim: set ts=4 sw=4 et: */ diff --git a/function.h b/function.h index 3c941ef..fbea508 100644 --- a/function.h +++ b/function.h @@ -39,6 +39,10 @@ char *getcell (board_t *board, int x, int y); char getvalue (board_t *board, int x, int y); +int testjump (board_t *board, int x, int y, int mode); + +int testalljumps (board_t *board, int x, int y); + #endif /* __FUNCTION_H__ */ /* vim: set ts=4 sw=4 et: */