- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试实现一种算法来清除围棋游戏中的死子。
我听说 floodfill 是实现此目标的最佳选择,因为递归使用它最有效且更容易实现。
我在我的代码中使用它时遇到问题,想知道我应该如何实现它。
这是我的一门类(class),它非常不言自明。
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
是另一个字符
任何帮助都会很棒!
最佳答案
虽然其他答案在技术上是正确的,但您还缺少更多与 go 相关的逻辑。我认为你需要做的是(在 B 步):
for each W neighbour of the move:
check that W group to see if it has any liberties (spaces)
remove it if not
洪水填充对于查找一组石头的范围很有用,但您的例程需要的远不止于此(我在这里进行了简化,并且还试图猜测该例程的用途-请参阅此答案下方的评论).
鉴于上述情况,识别一组中所有石头的洪水填充将是这样的(请注意,它使用第二个数组进行填充,因为您不想更改 pos
只是为了找到一个组):
public void findGroup(int x, int y, char colour, char[][] mask) {
// if this square is the colour expected and has not been visited before
if (pos[x][y] == colour && mask[x][y] == ' ') {
// save this group member
mask[x][y] = pos[x][y];
// look at the neighbours
findGroup(x+1, y, colour, mask);
findGroup(x-1, y, colour, mask);
findGroup(x, y+1, colour, mask);
findGroup(x, y-1, colour, mask);
}
}
您可以调用它来识别单个组(并将其复制到掩码中),因此它将帮助您识别与 B Action 相邻的 W 组成员(例如),但这只是一小部分您需要的全部逻辑。
最后,请注意,如果您想对一组中的每颗 gem 做某事,您有两种选择。你可以像上面那样调用一个例程,然后循环 mask
来找到组,或者你可以把你想做的 Action 直接放在例程中(在这种情况下你仍然使用 mask
控制泛洪的程度填写测试 && mask[x][y] == ' '
但你不使用它作为结果 - 所有的工作在例程返回时完成)。
(按照所有规则编写程序以正确处理 go,实际上非常复杂 - 您还有很多工作要做...:o)
关于JAVA - 围棋游戏算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10087136/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我正在对新标签进行一些试验,并且已经遇到了第一个障碍。我想我应该通过实现经典棋盘游戏 Go/Baduk/Weiqi 的一个版本来开始尝试。 我使用 moveTo() 和 lineTo() 绘制了 xy
我注意到标准 Go 库 (Y0) 中定义了用于一阶、二阶和 N 阶贝塞尔函数(Y0、Y1、Yn)的函数。我似乎无法确定这些数学函数的实际应用会使它们变得如此重要以至于包含在标准库中。 有人能帮帮我吗?
我对 kubernetes 的 golang API 有一些疑问。 我应该使用哪一个? k8s.io/client-go 还是 k8s.io/kubernetes/pkg/client?有什么区别?
我正在处理需要 big.Float 类型的数字,我需要将它们分开。我知道 big.Int 有一个 .Div() 函数,但如果我是正确的,那会截断值并失去我使用 big.Float. 相关代码 func
http://golang.org/pkg/sort/ 这是来自 Go 的例子。 // OrderedBy returns a Sorter that sorts using the less fu
我正在尝试使用两种或多种编程语言实现一个带有 float 的程序。该程序确实说了 50k 次迭代,最终将误差降低到非常小的值。 为确保我的结果具有可比性,我想确保在不同语言中使用相同精度的数据类型。请
http://play.golang.org/p/xjs-jwMsr7 我有这个功能 func (e *MyError) Error() string { return fmt.Sprint
code : type ByteSlice []byte func (p *ByteSlice) Append(data []byte) { slice := *p slice = appen
我一直在 Node 和 Go 中尝试使用 WebSockets 和 HTTP/2 库。我的基本设置是创建客户端和服务器,从服务器重复发送文件并测量时间,直到每个文件在客户端可用。 令我惊讶的是,HTT
我是一名优秀的程序员,十分优秀!