}
}
-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;
}
{
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;
}
}
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;
}