case KEY_UP:
case 'i':
if (mode == 0) {
- xcursor = (xcursor + 1) % board->width;
- ycursor = (ycursor + board->height - 1) % board->height;
+ domove (board, &xcursor, &ycursor, 0);
} 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;
+ dopetjump (board, &xcursor, &ycursor, 0);
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--;
+ dopetmove (board, &xcursor, &ycursor, 0);
mode = 0;
player ^= 1;
}
case KEY_LEFT:
case 'j':
if (mode == 0) {
- xcursor = (xcursor + board->width - 1) % board->width;
- ycursor = (ycursor + board->height - 1) % board->height;
+ domove (board, &xcursor, &ycursor, 1);
} 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;
+ dopetjump (board, &xcursor, &ycursor, 1);
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--;
+ dopetmove (board, &xcursor, &ycursor, 1);
mode = 0;
player ^= 1;
}
case KEY_DOWN:
case 'k':
if (mode == 0) {
- xcursor = (xcursor + board->width - 1) % board->width;
- ycursor = (ycursor + 1) % board->height;
+ domove (board, &xcursor, &ycursor, 2);
} 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;
+ dopetjump (board, &xcursor, &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++;
+ dopetmove (board, &xcursor, &ycursor, 2);
mode = 0;
player ^= 1;
}
case KEY_RIGHT:
case 'l':
if (mode == 0) {
- xcursor = (xcursor + 1) % board->width;
- ycursor = (ycursor + 1) % board->height;
+ domove (board, &xcursor, &ycursor, 3);
} 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;
+ dopetjump (board, &xcursor, &ycursor, 3);
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++;
+ dopetmove (board, &xcursor, &ycursor, 3);
mode = 0;
player ^= 1;
}
return (x >= 0) && (x < board->width) && (y >= 0) && (y < board->height) ? *getcell (board, x, y) : 0;
}
+void domove (board_t *board, int *x, int *y, int mode)
+{
+ switch (mode) {
+ case 0:
+ *x = (*x + 1) % board->width;
+ *y = (*y + board->height - 1) % board->height;
+ break;
+ case 1:
+ *x = (*x + board->width - 1) % board->width;
+ *y = (*y + board->height - 1) % board->height;
+ break;
+ case 2:
+ *x = (*x + board->width - 1) % board->width;
+ *y = (*y + 1) % board->height;
+ break;
+ case 3:
+ *x = (*x + 1) % board->width;
+ *y = (*y + 1) % board->height;
+ break;
+ }
+}
+
+void dopetjump (board_t *board, int *x, int *y, int mode)
+{
+ char symb = getvalue (board, *x, *y);
+ switch (mode) {
+ case 0:
+ *getcell (board, *x, *y) = '.';
+ *getcell (board, *x + 1, *y - 1) = '.';
+ *getcell (board, *x + 2, *y - 2) = symb;
+ *x += 2;
+ *y -= 2;
+ break;
+ case 1:
+ *getcell (board, *x, *y) = '.';
+ *getcell (board, *x - 1, *y - 1) = '.';
+ *getcell (board, *x - 2, *y - 2) = symb;
+ *x -= 2;
+ *y -= 2;
+ break;
+ case 2:
+ *getcell (board, *x, *y) = '.';
+ *getcell (board, *x - 1, *y + 1) = '.';
+ *getcell (board, *x - 2, *y + 2) = symb;
+ *x -= 2;
+ *y += 2;
+ break;
+ case 3:
+ *getcell (board, *x, *y) = '.';
+ *getcell (board, *x + 1, *y + 1) = '.';
+ *getcell (board, *x + 2, *y + 2) = symb;
+ *x += 2;
+ *y += 2;
+ break;
+ }
+}
+
+void dopetmove (board_t *board, int *x, int *y, int mode)
+{
+ char symb = getvalue (board, *x, *y);
+ switch (mode) {
+ case 0:
+ *getcell (board, *x, *y) = '.';
+ *getcell (board, *x + 1, *y - 1) = symb;
+ *x = *x + 1;
+ *y = *y - 1;
+ break;
+ case 1:
+ *getcell (board, *x, *y) = '.';
+ *getcell (board, *x - 1, *y - 1) = symb;
+ *x = *x - 1;
+ *y = *y - 1;
+ break;
+ case 2:
+ *getcell (board, *x, *y) = '.';
+ *getcell (board, *x - 1, *y + 1) = symb;
+ *x = *x - 1;
+ *y = *y + 1;
+ break;
+ case 3:
+ *getcell (board, *x, *y) = '.';
+ *getcell (board, *x + 1, *y + 1) = symb;
+ *x = *x + 1;
+ *y = *y + 1;
+ break;
+ }
+}
+
int testjump (board_t *board, int x, int y, int mode)
{
int ret = 0;
return ret;
}
+int evalmaxjumps (board_t *board, int x, int y, int id)
+{
+ int maxjumps = 0;
+ if (getvalue (board, x, y) != '0' + id) {
+ return 0;
+ }
+
+ int m;
+ for (m = 0; m < 4; m++) {
+ if (testjump (board, x, y, m)) {
+ board_t *newboard = copyboard (board);
+ int nx = x;
+ int ny = y;
+ dopetmove (newboard, &nx, &ny, m);
+ int nb = 1 + evalmaxjumps (newboard, nx, ny, id);
+ maxjumps = max (nb, maxjumps);
+ freeboard (newboard);
+ }
+ }
+ return maxjumps;
+}
+
+int globalmaxjumps (board_t *board, int id)
+{
+ int i, j, m;
+ int maxjumps = 0;
+ for (i = 0; i < board->width; i++) {
+ for (j = 0; j < board->height; j++) {
+ for (m = 0; m < 4; m++) {
+ int nb = evalmaxjumps (board, i, j, id);
+ maxjumps = max (nb, maxjumps);
+ }
+ }
+ }
+ return maxjumps;
+}
+
/* vim: set ts=4 sw=4 et: */
char getvalue (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);
+
+void dopetmove (board_t *board, int *x, int *y, int mode);
+
int testjump (board_t *board, int x, int y, int mode);
int testalljumps (board_t *board, int x, int y);
+int evalmaxjumps (board_t *board, int x, int y, int id);
+
+int globalmaxjumps (board_t *board, int id);
+
#endif /* __FUNCTION_H__ */
/* vim: set ts=4 sw=4 et: */