// Does not return correct value on the 0-0 or 1-0 spaces on an empty board, or 1-0 when a stone is on 1-1. int isLegalMove(int x, int y) { int j; int visited[361]; int queued[361]; int bqueue[361]; int qpos = 0; int curIndex = 0; int curColor; /* ko check */ /* end ko check */ /* liberty check */ /* Do a BFS among same-color stones, and return 1 as soon as a liberty is * found. Return 0 at end of method. * Argh, note: this returns false when the player kills a group by placing * a piece in a false eye. */ for (j = 0; j < 361; j++) { bqueue[j] = -1; queued[j] = 0; visited[j] = 0; } bqueue[qpos++] = 19*y + x; curColor = game.turn; while (bqueue[curIndex] > 0) { if (!visited[bqueue[curIndex]]) { int p[4]; p[0] = ((bqueue[curIndex] % 19 > 0)? bqueue[curIndex] - 1 : -1); p[1] = ((bqueue[curIndex] % 19 < 18)? bqueue[curIndex] + 1 : -1); p[2] = ((bqueue[curIndex] > 18)? bqueue[curIndex] - 19: -1); p[3] = ((bqueue[curIndex] < 342)? bqueue[curIndex] + 19: -1); visited[bqueue[curIndex]] = 1; if ( curColor == EMPTY ) { return 1; } for(j = 0; j < 4; j++) { // keep going deeper if and only if the current node is your color if ( !(p[j] < 0 || visited[p[j]] || queued[p[j]] || curColor != game.turn) ) { // add it to the queue queued[p[j]] = 1; bqueue[qpos++] = p[j]; } } curIndex++; curColor = game.goban[bqueue[curIndex] % 19][bqueue[curIndex] / 19]; } } /* end liberty check */ return 0;}