在练习中,我遵循扫雷教程,但在每个方块周围加入炸弹数量时,它不会向右计数炸弹,我不完全确定问题是什么是。我尝试重新启动,在多个编译器中打开它,移动它,什么都没有。我看了看,发现我找不到任何逻辑错误。
以下是我的点钞代码。最后一个if
语句是计算右边方格的语句。
btnAmt = 10
background
是一个包含所有地雷值的二维数组。
如果错误不在这里,我可以发布完整的代码,但我感到很沮丧,因为看似没有逻辑错误,所有其他方向都有效。
//Count neightbouring mines
for(int x = 0; x < background.length; x++){
for(int y = 0; y < background[0].length; y++){
int nCount = 0;
if(background[x][y] != MINE){
if((x > 0) && (y > 0) && (background[x-1][y-1] == MINE)){ //up and left
nCount++;
}
if(y > 0 && background[x][y-1] == MINE){ //Up
nCount++;
}
if(x < btnAmt-1 && y > 0 && background[x+1][y-1] == MINE){ // Up Right
nCount++;
}
if(x>0 && background[x-1][y] == MINE){ //Left
nCount++;
}
if(x>0 && y<btnAmt-1 && background[x-1][y+1] == MINE){ //Down and left
nCount++;
}
if(x<btnAmt-1 && y<btnAmt-1 && background[x+1][y+1] == MINE){//Down and right
nCount++;
}
if(x<btnAmt-1 && background[x+1][y] == MINE){ //Right
nCount++;
}
background[x][y] = nCount;
}
}
}
答案 0 :(得分:1)
您从未检查background[x][y+1]
。这是&#34;权利&#34;方向,你所评论的是&#34;对&#34; (background[x+1][y]
)实际上是 down 。
请记住,mat[i][j]
表示(按惯例)第i
行和矩阵j
的{{1}}列。因此,向右移动意味着向列添加1,因此mat
。
答案 1 :(得分:0)
我会尝试通过循环遍历周围的方块并检查我的计数来使其更合乎逻辑,更容易跟随自己,但在决定我的循环边界应该是什么之后,考虑所有边缘。例如,像这样:
// count neighbouring mines
for (int x = 0; x < background.length; x++) {
for (int y = 0; y < background[0].length; y++) {
// if a MINE, we don't care about the count
if (background[x][y] != MINE) {
int nCount = 0;
// find the left side of the boundary box
int xMin = Math.max(x - 1, 0);
// find the right side of the boundary box
int xMax = Math.min(x + 1, background.length - 1);
// find the y min side of the boundary box
int yMin = Math.max(y - 1, 0);
// find the y max side of the boundary box
int yMax = Math.min(y + 1, background[0].length - 1);
// now loop using the boundaries calculated above
for (int x2 = xMin; x2 <= xMax; x2++) {
for (int y2 = yMin; y2 <= yMax; y2++) {
// check to make sure not the same squre
if (x2 != x || y2 != y) {
// if MINE, then increment nCount by 1
nCount += (background[x2][y2] == MINE ? 1 : 0);
}
}
}
background[x][y] = nCount;
}
}
}
作为概念证明,以下程序使用上述代码不变,并且有效:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MineSweeperFun {
public static void main(String[] args) {
MineSweeperModel model = new MineSweeperModel(36, 18, 60);
model.displayGrid();
}
}
public class MineSweeperModel {
private static final int MINE = -1;
private int[][] backgroundGrid;
private int rows;
private int cols;
private int mineCount;
public MineSweeperModel(int rows, int cols, int mineCount) {
this.rows = rows;
this.cols = cols;
this.mineCount = mineCount;
backgroundGrid = refreshGrid();
}
private int[][] refreshGrid() {
int[][] grid = new int[rows][cols];
insertRandomMines(grid);
calculateNeighborCount(grid);
return grid;
}
private void insertRandomMines(int[][] grid) {
List<Integer> intList = new ArrayList<>();
for (int i = 0; i < rows * cols; i++) {
intList.add(i);
}
Collections.shuffle(intList);
for (int i = 0; i < mineCount; i++) {
int value = intList.remove(0);
int row = value / cols;
int col = value % cols;
grid[row][col] = MINE;
}
}
public void displayGrid() {
for (int row = 0; row < backgroundGrid.length; row++) {
for (int col = 0; col < backgroundGrid[row].length; col++) {
int value = backgroundGrid[row][col];
String txt = ".";
if (value == MINE) {
txt = "*";
} else if (value > 0) {
txt = "" + value;
}
System.out.printf("%4s", txt);
}
System.out.println();
}
}
private void calculateNeighborCount(int[][] background) {
// count neighboring mines
for (int x = 0; x < background.length; x++) {
for (int y = 0; y < background[0].length; y++) {
// if a MINE, we don't care about the count
if (background[x][y] != MINE) {
int nCount = 0;
// find the left side of the boundary box
int xMin = Math.max(x - 1, 0);
// find the right side of the boundary box
int xMax = Math.min(x + 1, background.length - 1);
// find the y min side of the boundary box
int yMin = Math.max(y - 1, 0);
// find the y max side of the boundary box
int yMax = Math.min(y + 1, background[0].length - 1);
// now loop using the boundaries calculated above
for (int x2 = xMin; x2 <= xMax; x2++) {
for (int y2 = yMin; y2 <= yMax; y2++) {
// check to make sure not the same squre
if (x2 != x || y2 != y) {
// if MINE, then increment nCount by 1
nCount += (background[x2][y2] == MINE ? 1 : 0);
}
}
}
background[x][y] = nCount;
}
}
}
}
}
示例输出:
1 1 1 1 1 . . . . . . 1 1 1 . . . .
* 1 2 * 2 . . . . . . 1 * 1 1 1 1 .
1 1 2 * 2 . . 1 1 1 . 1 1 1 1 * 1 .
. . 1 1 1 . . 1 * 1 . . . . 2 2 2 .
. . . 1 1 1 . 1 1 1 . 1 1 1 2 * 2 .
. 1 1 2 * 1 . . . . 1 2 * 2 3 * 3 1
. 1 * 3 2 1 . 1 1 1 1 * 2 2 * 3 * 1
. 1 2 * 1 . . 1 * 2 2 2 1 1 1 2 1 1
. . 1 1 1 . . 1 1 3 * 2 . . . 1 1 1
. . . . . . . . . 2 * 2 . . . 1 * 1
1 2 2 1 . . . . . 2 2 2 . 1 1 2 2 2
2 * * 2 1 . . . . 1 * 2 1 2 * 1 1 *
* 3 3 * 1 . . 1 1 2 1 2 * 2 2 2 2 1
1 1 1 1 1 . . 1 * 1 . 1 1 1 1 * 2 1
. . . 1 1 1 . 1 1 1 . . . . 1 1 2 *
. . . 1 * 1 . . . . . . . 1 1 1 1 1
. . . 1 1 1 . . 1 1 1 1 1 2 * 2 1 1
. . 1 1 1 . . . 1 * 1 1 * 3 2 3 * 1
. . 1 * 1 . . . 1 1 2 2 2 2 * 2 1 1
. . 1 1 1 1 1 1 . 1 2 * 1 1 1 1 . .
. . . . . 1 * 1 . 1 * 2 1 . . . . .
1 1 1 . . 1 1 1 . 1 2 2 1 . . . . .
1 * 1 . . . . . . . 1 * 1 . . . . .
1 1 1 . . . . 1 1 1 1 1 1 . . . . .
. . . . . . . 1 * 1 . . . . . . . .
. . . . . 1 1 2 1 1 . . . . . . . .
. . . . 1 2 * 1 . . . . . . . . . .
. . . . 1 * 2 1 . . . . . . . . . .
. 1 1 1 1 1 1 . . . . . . . . . . .
. 1 * 1 . . . . 1 1 1 1 1 2 1 1 . .
. 1 1 1 . . . . 1 * 1 1 * 2 * 1 . .
. . . 1 1 1 . . 1 2 2 2 1 2 1 1 . .
1 1 . 1 * 2 1 1 . 1 * 2 2 2 1 . . .
* 1 . 1 2 3 * 1 1 2 2 2 * * 2 1 1 .
1 1 . . 1 * 2 1 1 * 1 2 3 4 3 * 1 .
. . . . 1 1 1 . 1 1 1 1 * 2 * 2 1 .