/* event loop */
int mode = 0;
+ int queen = -1;
int stop = 0;
int player = board->turn;
while (!stop) {
sprintf (msg, "Player %s: ", (player == 0) ? "One": "Two");
switch (mode) {
case 0:
- jump = cursorwindow (board, xcursor, ycursor, 0);
+ jump = cursorwindow (board, xcursor, ycursor, queen, 0);
nbmaxjumps = globalmaxjumps (board, player);
sprintf (msg + strlen (msg), "Begin turn (%d)", nbmaxjumps);
break;
case 1:
- jump = cursorwindow (board, xcursor, ycursor, 1);
+ jump = cursorwindow (board, xcursor, ycursor, queen, 1);
sprintf (msg + strlen (msg), "Choose next (%d)", nbmaxjumps);
break;
case 2:
case 1:
if (cache->board == NULL) {
mode = 0;
+ queen = -1;
}
break;
}
domove (board, &xcursor, &ycursor, 0);
break;
case 1:
- if ((jump) && (testjump (board, xcursor, ycursor, 0))) {
+ if (testqueenturn (board, xcursor, ycursor, queen, 0)) {
+ pushcache (cache, board, xcursor, ycursor);
+ board = copyboard (board);
+ doqueenturn (board, &xcursor, &ycursor, &queen, 0);
+ } else if ((jump) && (testjump (board, xcursor, ycursor, 0))) {
pushcache (cache, board, xcursor, ycursor);
board = copyboard (board);
dopetjump (board, &xcursor, &ycursor, 0);
domove (board, &xcursor, &ycursor, 1);
break;
case 1:
- if ((jump) && (testjump (board, xcursor, ycursor, 1))) {
+ if (testqueenturn (board, xcursor, ycursor, queen, 1)) {
+ pushcache (cache, board, xcursor, ycursor);
+ board = copyboard (board);
+ doqueenturn (board, &xcursor, &ycursor, &queen, 1);
+ } else if ((jump) && (testjump (board, xcursor, ycursor, 1))) {
pushcache (cache, board, xcursor, ycursor);
board = copyboard (board);
dopetjump (board, &xcursor, &ycursor, 1);
domove (board, &xcursor, &ycursor, 2);
break;
case 1:
- if ((jump) && (testjump (board, xcursor, ycursor, 2))) {
+ if (testqueenturn (board, xcursor, ycursor, queen, 2)) {
+ pushcache (cache, board, xcursor, ycursor);
+ board = copyboard (board);
+ doqueenturn (board, &xcursor, &ycursor, &queen, 2);
+ } else if ((jump) && (testjump (board, xcursor, ycursor, 2))) {
pushcache (cache, board, xcursor, ycursor);
board = copyboard (board);
dopetjump (board, &xcursor, &ycursor, 2);
domove (board, &xcursor, &ycursor, 3);
break;
case 1:
- if ((jump) && (testjump (board, xcursor, ycursor, 3))) {
+ if (testqueenturn (board, xcursor, ycursor, queen, 3)) {
+ pushcache (cache, board, xcursor, ycursor);
+ board = copyboard (board);
+ doqueenturn (board, &xcursor, &ycursor, &queen, 3);
+ } else if ((jump) && (testjump (board, xcursor, ycursor, 3))) {
pushcache (cache, board, xcursor, ycursor);
board = copyboard (board);
dopetjump (board, &xcursor, &ycursor, 3);
}
}
-int cursorwindow (board_t *board, int x, int y, int mode)
+int cursorwindow (board_t *board, int x, int y, int queen, int mode)
{
int jump = 0;
char id = getvalue (board, x, y);
case '1':
_element (board, x, y, (mode) ? '3' : '5');
break;
+ case '6':
+ _element (board, x, y, (mode) ? '8' : ';');
+ break;
+ case '7':
+ _element (board, x, y, (mode) ? '9' : ';');
+ break;
case ' ':
case '.':
_element (board, x, y, 'Z');
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':
+ switch (id) {
+ case '0':
+ case '1':
+ 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;
+ }
+ }
+ break;
+ case 6:
+ case 7:
+ if (queen == -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');
+ }
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) == '.') {
+ } else {
+ if ((queen == 0) && (getvalue (board, x + 1, y - 1) == '.')) {
_element (board, x + 1, y - 1, 'S');
+ } else if (testjump (board, x, y, 0)) {
+ _element (board, x + 2, y - 2, 'S');
}
- if (getvalue (board, x - 1, y - 1) == '.') {
+ if ((queen == 1) && (getvalue (board, x - 1, y - 1) == '.')) {
_element (board, x - 1, y - 1, 'S');
+ } else if (testjump (board, x, y, 1)) {
+ _element (board, x - 2, y - 2, 'S');
+ }
+ if ((queen == 2) && (getvalue (board, x - 1, y + 1) == '.')) {
+ _element (board, x - 1, y + 1, 'S');
+ } else if (testjump (board, x, y, 2)) {
+ _element (board, x - 2, y + 2, 'S');
+ }
+ if ((queen == 3) && (getvalue (board, x + 1, y + 1) == '.')) {
+ _element (board, x + 1, y + 1, 'S');
+ } else if (testjump (board, x, y, 3)) {
+ _element (board, x + 2, y + 2, 'S');
}
- break;
}
+ break;
}
}
return jump;
void boardwindow (board_t *board);
-int cursorwindow (board_t *board, int x, int y, int mode);
+int cursorwindow (board_t *board, int x, int y, int queen, int mode);
char *savewindow (int length, int xoffset, int yoffset);
return maxjumps;
}
+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;
+ }
+ }
+ return ret;
+}
+
+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;
+ }
+ }
+ break;
+ }
+ return (ret) ? delta + 1 : ret;
+}
+
+int testqueenturn (board_t *board, int xcursor, int ycursor, int queen, int mode)
+{
+ int ret = 0;
+ if ((queen == mode) || (queen == -1)) {
+ ret = (testqueenmove (board, xcursor, ycursor, queen, mode) ||
+ testqueenjump (board, xcursor, ycursor, mode));
+ } else {
+ ret = testqueenjump (board, xcursor, ycursor, mode);
+ }
+ return ret;
+}
+
+void doqueenturn (board_t *board, int *xcursor, int *ycursor, int *queen, int mode)
+{
+ return;
+}
+
cache_t *initcache (board_t *board)
{
cache_t *cache = (cache_t *) calloc (board->width / 2 * (board->height - 1) / 2, sizeof (cache_t));
int globalmaxjumps (board_t *board, int id);
+int testqueenmove (board_t *board, int xcursor, int ycursor, int queen, int mode);
+
+int testqueenjump (board_t *board, int xcursor, int ycursor, int mode);
+
+int testqueenturn (board_t *board, int xcursor, int ycursor, int queen, int mode);
+
+void doqueenturn (board_t *board, int *xcursor, int *ycursor, int *queen, int mode);
+
cache_t *initcache (board_t *board);
board_t *pushcache (cache_t *cache, board_t *board, int xcursor, int ycursor);