return ret;
}
+int petturn (board_t **board, cache_t *cache, int *xcursor, int *ycursor, int jump, int direction)
+{
+ int player = getid (*board, *xcursor, *ycursor);
+
+ int mode = 1;
+ if (ispet (*board, *xcursor, *ycursor, player, 0, -1)) {
+ if ((jump) && (testjump (*board, *xcursor, *ycursor, direction))) {
+ pushcache (cache, *board, *xcursor, *ycursor);
+ *board = copyboard (*board);
+ dopetjump (*board, xcursor, ycursor, direction);
+ if (!testalljumps (*board, *xcursor, *ycursor)) {
+ mode = 2;
+ }
+ } else {
+ int petmove = (((player == 0) && ((direction == 2) || (direction == 3))) ||
+ ((player == 1) && ((direction == 0) || (direction == 1))));
+ if (petmove && isempty (*board, *xcursor, *ycursor, 1, direction)) {
+ pushcache (cache, *board, *xcursor, *ycursor);
+ *board = copyboard (*board);
+ dopetmove (*board, xcursor, ycursor, direction);
+ mode = 2;
+ }
+ }
+ }
+
+ return mode;
+}
+
+int queenturn (board_t **board, cache_t *cache, int *xcursor, int *ycursor, int queen, int direction)
+{
+ int player = getid (*board, *xcursor, *ycursor);
+
+ if (isqueen (*board, *xcursor, *ycursor, player, 0, -1)) {
+ if (testqueenmove (*board, *xcursor, *ycursor, queen, direction)) {
+ pushcache (cache, *board, *xcursor, *ycursor);
+ *board = copyboard (*board);
+ dopetmove (*board, xcursor, ycursor, direction);
+ queen = direction;
+ } else if (testqueenjump (*board, *xcursor, *ycursor, direction)) {
+ pushcache (cache, *board, *xcursor, *ycursor);
+ *board = copyboard (*board);
+ dopetjump (*board, xcursor, ycursor, direction);
+ queen = -1;
+ }
+ }
+
+ return queen;
+}
+
/* main function */
int main (int argc, char *argv[])
{
case 2:
sprintf (msg + strlen (msg), "Validate turn");
break;
+ case 3:
+ jump = cursorwindow (board, xcursor, ycursor, queen, 1);
+ sprintf (msg + strlen (msg), "Continue or validate turn");
+ break;
}
msgwindow (msg, xmsg, ymsg, lmsg);
} else {
case 'c':
switch (mode) {
case 0:
- if (getvalue (board, xcursor, ycursor) == ((player == 0) ? '0' : '1')) {
+ if (ispet (board, xcursor, ycursor, player, 0, -1)) {
mode = 1;
+ } else if (isqueen (board, xcursor, ycursor, player, 0, -1)) {
+ mode = 3;
}
break;
case 1:
+ case 3:
if (cache->board == NULL) {
mode = 0;
queen = -1;
domove (board, &xcursor, &ycursor, 0);
break;
case 1:
- 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);
- if (!testalljumps (board, xcursor, ycursor)) {
- mode = 2;
- }
- } else if ((player == 1) && (!jump) && (isempty (board, xcursor, ycursor, 1, 0))) {
- pushcache (cache, board, xcursor, ycursor);
- board = copyboard (board);
- dopetmove (board, &xcursor, &ycursor, 0);
- mode = 2;
- }
+ mode = petturn (&board, cache, &xcursor, &ycursor, jump, 0);
+ break;
+ case 3:
+ queen = queenturn (&board, cache, &xcursor, &ycursor, queen, 0);
break;
}
break;
domove (board, &xcursor, &ycursor, 1);
break;
case 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);
- if (!testalljumps (board, xcursor, ycursor)) {
- mode = 2;
- }
- } else if ((player == 1) && (!jump) && (isempty (board, xcursor, ycursor, 1, 1))) {
- pushcache (cache, board, xcursor, ycursor);
- board = copyboard (board);
- dopetmove (board, &xcursor, &ycursor, 1);
- mode = 2;
- }
+ mode = petturn (&board, cache, &xcursor, &ycursor, jump, 1);
+ break;
+ case 3:
+ queen = queenturn (&board, cache, &xcursor, &ycursor, queen, 1);
break;
}
break;
domove (board, &xcursor, &ycursor, 2);
break;
case 1:
- 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);
- if (!testalljumps (board, xcursor, ycursor)) {
- mode = 2;
- }
- } else if ((player == 0) && (!jump) && (isempty (board, xcursor, ycursor, 1, 2))) {
- pushcache (cache, board, xcursor, ycursor);
- board = copyboard (board);
- dopetmove (board, &xcursor, &ycursor, 2);
- mode = 2;
- }
+ mode = petturn (&board, cache, &xcursor, &ycursor, jump, 2);
+ break;
+ case 3:
+ queen = queenturn (&board, cache, &xcursor, &ycursor, queen, 2);
break;
}
break;
domove (board, &xcursor, &ycursor, 3);
break;
case 1:
- 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);
- if (!testalljumps (board, xcursor, ycursor)) {
- mode = 2;
- }
- } else if ((player == 0) && (!jump) && (isempty (board, xcursor, ycursor, 1, 3))) {
- pushcache (cache, board, xcursor, ycursor);
- board = copyboard (board);
- dopetmove (board, &xcursor, &ycursor, 3);
- mode = 2;
- }
+ mode = petturn (&board, cache, &xcursor, &ycursor, jump, 3);
+ break;
+ case 3:
+ queen = queenturn (&board, cache, &xcursor, &ycursor, queen, 3);
break;
}
break;
case '\n':
case '\r':
case 'v':
- if (mode == 2) {
+ switch (mode) {
+ case 2:
+ case 3:
player = (player == 0) ? 1 : 0;
emptycache (cache);
cleanafterjump (board);
pettoqueen (board);
+ queen = -1;
mode = 0;
+ break;
}
break;
case KEY_BACKSPACE:
if (!jump) {
switch (id) {
case '0':
- if (getvalue (board, x - 1, y + 1) == '.') {
+ if (isempty (board, x, y, 1, 2)) {
_element (board, x - 1, y + 1, 'S');
}
- if (getvalue (board, x + 1, y + 1) == '.') {
+ if (isempty (board, x, y, 1, 3)) {
_element (board, x + 1, y + 1, 'S');
}
break;
case '1':
- if (getvalue (board, x + 1, y - 1) == '.') {
+ if (isempty (board, x, y, 1, 0)) {
_element (board, x + 1, y - 1, 'S');
}
- if (getvalue (board, x - 1, y - 1) == '.') {
- _element (board, x - 1, y - 1, 'S');
+ if (isempty (board, x, y, 1, 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');
- }
- } 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 ((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');
- }
+ case '6':
+ case '7':
+ if (((queen ==-1) || (queen == 0)) && isempty (board, x, y, 1, 0)) {
+ _element (board, x + 1, y - 1, 'S');
+ } else if (testjump (board, x, y, 0)) {
+ _element (board, x + 2, y - 2, 'S');
+ }
+ if (((queen == -1) || (queen == 1)) && isempty (board, x, y, 1, 1)) {
+ _element (board, x - 1, y - 1, 'S');
+ } else if (testjump (board, x, y, 1)) {
+ _element (board, x - 2, y - 2, 'S');
+ }
+ if (((queen == -1) || (queen == 2)) && isempty (board, x, y, 1, 2)) {
+ _element (board, x - 1, y + 1, 'S');
+ } else if (testjump (board, x, y, 2)) {
+ _element (board, x - 2, y + 2, 'S');
+ }
+ if (((queen == -1) || (queen == 3)) && isempty (board, x, y, 1, 3)) {
+ _element (board, x + 1, y + 1, 'S');
+ } else if (testjump (board, x, y, 3)) {
+ _element (board, x + 2, y + 2, 'S');
}
break;
}
return (x >= 0) && (x < board->width) && (y >= 0) && (y < board->height) ? *getcell (board, x, y) : 0;
}
+int getid (board_t *board, int x, int y)
+{
+ int id = -1;
+ switch (getvalue (board, x, y)) {
+ case '0':
+ case '6':
+ id = 0;
+ break;
+ case '1':
+ case '7':
+ id = 1;
+ }
+ return id;
+}
+
void domove (board_t *board, int *x, int *y, int mode)
{
switch (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;
+ if (mode == -1) {
+ while (++mode < ((delta) ? 4 : 1)) {
+ ret = ret || _testcell (board, xcursor, ycursor, value, delta, mode);
+ }
+ } else {
+ switch (mode) {
+ case 0:
+ xcursor += delta;
+ ycursor -= delta;
+ break;
+ case 1:
+ xcursor -= delta;
+ ycursor -= delta;
+ break;
+ case 2:
+ xcursor -= delta;
+ ycursor += delta;
+ break;
+ case 3:
+ xcursor += delta;
+ ycursor += delta;
+ break;
+ }
+ ret = ((xcursor >= 0) && (xcursor < board->width) &&
+ (ycursor >= 0) && (ycursor < board->height) &&
+ (getvalue (board, xcursor, ycursor) == value));
}
return ret;
}
break;
}
if (ret) {
- int id = (getvalue (board, x, y) == '0') ? 0 : 1;
+ int id = getid (board, x, y);
ret = ispet (board, x, y, 1 - id, 1 + delta, mode) && isempty (board, x, y, 2 + delta, mode);
}
return ret;
int testqueenmove (board_t *board, int xcursor, int ycursor, int queen, int mode)
{
int ret = 0;
- if ((mode == queen) || (queen == -1)) {
+ if ((queen == mode) || (queen == -1)) {
ret = isempty (board, xcursor, ycursor, 1, mode);
}
return ret;
int testqueenjump (board_t *board, int xcursor, int ycursor, int mode)
{
- if (getvalue (board, xcursor, ycursor) / 2 != '6' / 2) {
- return 0;
- }
int ret = 0;
int delta = 0;
while (!(ret = _testjump (board, xcursor, ycursor, delta, mode))) {
return (ret) ? delta + 1 : ret;
}
-int testqueenturn (board_t *board, int xcursor, int ycursor, int queen, int mode)
+int testqueenturn (board_t *board, int xcursor, int ycursor, int id, int queen, int mode)
{
int ret = 0;
- if (getvalue (board, xcursor, ycursor) / 2 == '6' / 2) {
+ if (isqueen (board, xcursor, ycursor, id, 0, -1)) {
if ((queen == mode) || (queen == -1)) {
- ret = (testqueenmove (board, xcursor, ycursor, queen, mode) ||
- testqueenjump (board, xcursor, ycursor, mode));
+ ret = testqueenmove (board, xcursor, ycursor, queen, mode);
+ ret = ret || testqueenjump (board, xcursor, ycursor, mode);
} else {
ret = testqueenjump (board, xcursor, ycursor, mode);
}
void doqueenturn (board_t *board, int *xcursor, int *ycursor, int *queen, int mode)
{
- return;
+ if (testqueenmove (board, *xcursor, *ycursor, *queen, mode)) {
+ dopetmove (board, xcursor, ycursor, mode);
+ *queen = mode;
+ } else if (testqueenjump (board, *xcursor, *ycursor, mode)) {
+ dopetjump (board, xcursor, ycursor, mode);
+ *queen = -1;
+ }
}
cache_t *initcache (board_t *board)
char getvalue (board_t *board, int x, int y);
+int getid (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);
int testqueenjump (board_t *board, int xcursor, int ycursor, int mode);
-int testqueenturn (board_t *board, int xcursor, int ycursor, int queen, int mode);
+int testqueenturn (board_t *board, int xcursor, int ycursor, int id, int queen, int mode);
void doqueenturn (board_t *board, int *xcursor, int *ycursor, int *queen, int mode);