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);
}
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:
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:
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:
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:
case '5':
set_color (bred);
break;
+ case 'S':
+ set_color (byellow);
+ break;
case 'Z':
set_color (bblue);
break;
}
}
-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;
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)
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);
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: */
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: */