From 2d79715cdbb63fcbbbcf3b6bf76a71e0f2de92f2 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Thu, 11 Jul 2024 00:25:08 +0200 Subject: [PATCH] start code cleaning --- function.c | 121 ++++++++++++++++++++++++++--------------------------- function.h | 6 +++ 2 files changed, 66 insertions(+), 61 deletions(-) diff --git a/function.c b/function.c index af0c3e7..6b6d70f 100644 --- a/function.c +++ b/function.c @@ -356,42 +356,82 @@ void pettoqueen (board_t *board) } } -int testjump (board_t *board, int x, int y, int mode) +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; + } + return ret; +} + +int isempty (board_t *board, int xcursor, int ycursor, int delta, int mode) +{ + return _testcell (board, xcursor, ycursor, '.', delta, mode); +} + +int ispet (board_t *board, int xcursor, int ycursor, int id, int delta, int mode) +{ + return (id == -1) ? + (_testcell (board, xcursor, ycursor, '0', delta, mode) && + _testcell (board, xcursor, ycursor, '1', delta, mode)) : + _testcell (board, xcursor, ycursor, '0' + id, delta, mode); +} + +int isqueen (board_t *board, int xcursor, int ycursor, int id, int delta, int mode) +{ + return (id == -1) ? + (_testcell (board, xcursor, ycursor, '6', delta, mode) && + _testcell (board, xcursor, ycursor, '7', delta, mode)) : + _testcell (board, xcursor, ycursor, '6' + id, delta, mode); +} - char id = getvalue (board, x, y); +int _testjump (board_t *board, int x, int y, int delta, int mode) +{ + int ret = 0; 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) == '.'); - } + ret = ((x + 1 + delta < board->width) && (y - 1 - delta >= 0)); 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) == '.'); - } + ret = ((x - 1 - delta >= 0) && (y - 1 - delta >= 0)); 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) == '.'); - } + ret = ((x - 1 - delta >= 0) && (y + 1 + delta < board->height)); 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) == '.'); - } + ret = ((x + 1 + delta < board->width) && (y + 1 + delta < board->height)); break; } + if (ret) { + int id = getvalue (board, x, y) - '0'; + ret = ispet (board, x, y, 1 - id, 1 + delta, mode) && isempty (board, x, y, 2 + delta, mode); + } return ret; } +int testjump (board_t *board, int x, int y, int mode) +{ + return _testjump (board, x, y, 0, mode); +} + 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)) { + if (_testjump (board, x, y, 0, m)) { ret = 1; break; } @@ -438,20 +478,7 @@ 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; - } + ret = isempty (board, xcursor, ycursor, 1, mode); } return ret; } @@ -463,39 +490,11 @@ 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; - } + while (!(ret = _testjump (board, xcursor, ycursor, delta, mode))) { + delta++; + if (!isempty (board, xcursor, ycursor, delta, mode)) { + break; } - break; } return (ret) ? delta + 1 : ret; } diff --git a/function.h b/function.h index 0ae101e..7c83260 100644 --- a/function.h +++ b/function.h @@ -49,6 +49,12 @@ void cleanafterjump (board_t *board); void pettoqueen (board_t *board); +int isempty (board_t *board, int xcursor, int ycursor, int delta, int mode); + +int ispet (board_t *board, int xcursor, int ycursor, int id, int delta, int mode); + +int isqueen (board_t *board, int xcursor, int ycursor, int id, int delta, int mode); + int testjump (board_t *board, int x, int y, int mode); int testalljumps (board_t *board, int x, int y); -- 2.30.2