Flood fill algorithm for Game of Go
Posted
by
Jackson Borghi
on Game Development
See other posts from Game Development
or by Jackson Borghi
Published on 2012-11-05T04:45:38Z
Indexed on
2012/11/05
17:20 UTC
Read the original article
Hit count: 490
I'm having a hell of a time trying to figure out how to make captured stones disappear. I've read everywhere that I should use the flood fill algorithm, but I haven't had any luck with that so far. Any help would be amazing! Here is my code:
package Go;
import static java.lang.Math.*;
import static stdlib.StdDraw.*;
import java.awt.Color;
public class Go2 {
public static Color opposite(Color player) {
if (player == WHITE) {
return BLACK;
}
return WHITE;
}
public static void drawGame(Color[][] board) {
Color[][][] unit = new Color[400][19][19];
for (int h = 0; h < 400; h++) {
for (int x = 0; x < 19; x++) {
for (int y = 0; y < 19; y++) {
unit[h][x][y] = YELLOW;
}
}
}
setXscale(0, 19);
setYscale(0, 19);
clear(YELLOW);
setPenColor(BLACK);
line(0, 0, 0, 19);
line(19, 19, 19, 0);
line(0, 19, 19, 19);
line(0, 0, 19, 0);
for (double i = 0; i < 19; i++) {
line(0.0, i, 19, i);
line(i, 0.0, i, 19);
}
for (int x = 0; x < 19; x++) {
for (int y = 0; y < 19; y++) {
if (board[x][y] != YELLOW) {
setPenColor(board[x][y]);
filledCircle(x, y, 0.47);
setPenColor(GRAY);
circle(x, y, 0.47);
}
}
}
int h = 0;
}
public static void main(String[] args) {
int px;
int py;
Color[][] temp = new Color[19][19];
Color[][] board = new Color[19][19];
Color player = WHITE;
for (int i = 0; i < 19; i++) {
for (int h = 0; h < 19; h++) {
board[i][h] = YELLOW;
temp[i][h] = YELLOW;
}
}
while (true) {
drawGame(board);
while (!mousePressed()) {
}
px = (int) round(mouseX());
py = (int) round(mouseY());
board[px][py] = player;
while (mousePressed()) {
}
floodFill(px, py, player, board, temp);
System.out.print("XXXXX = "+ temp[px][py]);
if (checkTemp(temp, board, px, py)) {
for (int x = 0; x < 19; x++) {
for (int y = 0; y < 19; y++) {
if (temp[x][y] == GRAY) {
board[x][y] = YELLOW;
}
}
}
}
player = opposite(player);
}
}
private static boolean checkTemp(Color[][] temp, Color[][] board, int x,
int y) {
if (x < 19 && x > -1 && y < 19 && y > -1) {
if (temp[x + 1][y] == YELLOW || temp[x - 1][y] == YELLOW
|| temp[x][y - 1] == YELLOW || temp[x][y + 1] == YELLOW) {
return false;
}
}
if (x == 18) {
if (temp[x - 1][y] == YELLOW || temp[x][y - 1] == YELLOW
|| temp[x][y + 1] == YELLOW) {
return false;
}
}
if (y == 18) {
if (temp[x + 1][y] == YELLOW || temp[x - 1][y] == YELLOW
|| temp[x][y - 1] == YELLOW) {
return false;
}
}
if (y == 0) {
if (temp[x + 1][y] == YELLOW || temp[x - 1][y] == YELLOW
|| temp[x][y + 1] == YELLOW) {
return false;
}
}
if (x == 0) {
if (temp[x + 1][y] == YELLOW || temp[x][y - 1] == YELLOW
|| temp[x][y + 1] == YELLOW) {
return false;
}
}
else {
if (x < 19) {
if (temp[x + 1][y] == GRAY) {
checkTemp(temp, board, x + 1, y);
}
}
if (x >= 0) {
if (temp[x - 1][y] == GRAY) {
checkTemp(temp, board, x - 1, y);
}
}
if (y < 19) {
if (temp[x][y + 1] == GRAY) {
checkTemp(temp, board, x, y + 1);
}
}
if (y >= 0) {
if (temp[x][y - 1] == GRAY) {
checkTemp(temp, board, x, y - 1);
}
}
}
return true;
}
private static void floodFill(int x, int y, Color player, Color[][] board,
Color[][] temp) {
if (board[x][y] != player) {
return;
} else {
temp[x][y] = GRAY;
System.out.println("x = " + x + " y = " + y);
if (x < 19) {
floodFill(x + 1, y, player, board, temp);
}
if (x >= 0) {
floodFill(x - 1, y, player, board, temp);
}
if (y < 19) {
floodFill(x, y + 1, player, board, temp);
}
if (y >= 0) {
floodFill(x, y - 1, player, board, temp);
}
}
}
}
© Game Development or respective owner