注意:我为文本墙道歉,但我仍然无法使用自己的代码。我已经挖掘了其他类似的问题,但我还没有看到为什么我无法使用此代码来搜索所有内容。我将接受半隐秘的答案,至少指出我正确的方向感谢。
为了练习java,并回答一个关于扫雷的运行问题,我决定尝试构建一个扫雷机器人,以确定最佳可能的胜率对于给定的游戏开始。我仍然在努力在代码的游戏部分中获得正确的行为,特别是我在游戏中显示没有相邻地雷的方块。我最终在显示的空间的边缘有0,而不是我的代码搜索它周围的瓷砖,如下所示:
Printing Field:
-
-
-
-
-
-
-
-
-
- 1 1 2 1
- 0 0 0 0 0 1
- 1 0 0 0 1 2 3
- 1 0 0 0 0 0 0 0
- 2 1 0 0 0 0 0
- 1 0 0 0 0 0
- 1 0 0 0 0 0
我正在跟踪二维Character数组中的实际字段,然后在相同大小的布尔数组中跟踪显示的空间。我将下面的方法包括在我正在运行的另一种方法找到了一个' 0',这标志着一个没有炸弹的空间。我有另一种方法可以处理将显示的部分打印到当前字段,如果有人好奇,可以分享。
private void checkAround(int x, int y)
{
//this pattern is used to parse out the comma to get the coordinates.
String regex = "[,]";
Pattern pat = Pattern.compile(regex);
ArrayList<String> to_check = new ArrayList<String>(); //the list of coordinates to check.
to_check.add("" + x + "," + y);
revealed_field[x][y] = true; //marking the coordinates as revealed.
do //iterates through the to_check list until nothing is in it.
{
//these next 2 sections handle pulling the next item from to_check
String temp = to_check.get(0);
to_check.remove(0);
String[] xy = pat.split(temp);
int a = Integer.parseInt(xy[0]); //local declaration of x
int b = Integer.parseInt(xy[1]); //local declaration of y
//if either a or b are outside the bounds of the respective axis, we skip the coordinate and move on.
if (b > height-1 || b < 0) continue;
if (a > width-1 || a < 0) continue;
if (bomb_field[b][a] == '0') //if this is not adjacent to a bomb
{
if (a > 0 && revealed_field[b][a-1] == false) //adds the square above to to_check
{
to_check.add("" + (b) + "," + (a-1));
revealed_field[b][a-1] = true;
}
if (b > 0 && revealed_field[b-1][a] == false) //adds the square left to to_check
{
to_check.add("" + (b-1) + "," + (a));
revealed_field[b-1][a] = true;
}
if (b < height-1 && revealed_field[b+1][a] == false) //adds the square right to to_check
{
to_check.add("" + (b+1) + "," + (a));
revealed_field[b+1][a] = true;
}
if (a < width-1 && revealed_field[b][a+1] == false) //adds the square below to to_check
{
to_check.add("" + (b) + "," + (a+1));
revealed_field[b][a+1] = true;
}
}
}
while (to_check.size() > 0); //ends once we have nothing to check.
}
答案 0 :(得分:0)
您首先将b
存储在列表中,然后将a
存储在列表中的条目中。但是您要将条目解释为首先包含a
,然后b
。
例如,如果起点是x = 4,则y = 10: