我想实现一个算法来清除我的Go游戏中的死亡石头。JAVA - Go游戏算法
听说floodfill是最好的实现这是使用它递归将是最effiecient也更容易实现。
我在使用它在我的代码中的麻烦,想知道我应该怎么去实现它。
这是我的一个类,它很自我解释。
import java.io.*;
public class GoGame implements Serializable {
int size;
char[][] pos; // This is the array that stores whether a Black (B) or White (W) piece is stored, otherwise its an empty character.
public GoGame(int s){
size = s;
}
public void init() {
pos = new char[size][size];
for (int i=0;i<size;i++) {
for (int j=0;j<size;j++) {
pos[i][j] = ' ';
}
}
}
public void ClearAll() {
for (int i=0;i<size;i++) {
for (int j=0;j<size;j++) {
pos[i][j] = ' ';
}
}
}
public void clear(int x, int y) {
pos[x][y]=' ';
}
public void putB(int x, int y) { //places a black stone on the board+array
pos[x][y]='B';
floodfill(x,y,'B','W');
}
public void putW(int x, int y) { //places a white stone on the board+array
pos[x][y]='W';
floodfill(x,y,'W','B');
}
public char get(int x, int y) {
return pos[x][y];
}
public void floodfill(int x, int y, char placed, char liberty){
floodfill(x-1, y, placed, liberty);
floodfill(x+1, y, placed, liberty);
floodfill(x, y-1, placed, liberty);
floodfill(x, y+1, placed, liberty);
}
}
x
和y
是方形的坐标,placed
是石头的性格放下,liberty
是其他字符
任何帮助将是惊人的!
术语“死亡”可以指一组没有自由的石头,但更多的时候,“死亡”组只是一个可以被迫移除的组。这些宝石仍然会有自由,但无论如何都会在游戏结束时被移除。没有什么绝对的办法可以告诉哪些石头已经死亡,因为规则在这个问题上并没有明确的定义 - 所以只需要玩家同意哪些石头已经死亡。但他们仍然需要能够确定哪些群体已经死亡 - 所以我认为OP真正要问的是如何识别群体。 – 2012-04-10 15:43:22
是的,我同意 - 我只是想让事情比较简单。我给出的代码标识了一个组。 – 2012-04-10 15:59:13