gpt4 book ai didi

java - 扫雷算法卡住了

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:13:40 25 4
gpt4 key购买 nike

我正在使用 Android Studio 在 Android 中编写扫雷程序,我正在尝试计算单元格的邻居以计算显示的数字。

但是计数不起作用,因为所有单元格都有相同的错误邻居数:

Here是作为超链接的示例图片(在帖子中显示图片无效)

搜索方法:

public void countNeighbors() {
if(mine) {

} else {
int total = 0;

for (int xoff = -1 ; xoff <= 1 ; xoff++) {
for (int yoff = -1 ; yoff <= 1; yoff++) {
int row = i + xoff;
int col = j + yoff;

if (row > -1 && row < grid.length && col > -1 && col < grid[row].length) {
//Log.d("SweeperLog", "Found 1 Neighbor");
Cell neighbor = grid[row][col];
if (!neighbor.mine) {
total++;
}
}
}
}
neighborCount = total;
Log.d("SweeperLog", "NeighborCount " + neighborCount);
}
}

以及在其中绘制和创建所有内容的 SweeperView.class:

public class SweeperView extends View {
public static int cols = 10;
public static int rows = 10;

public static Cell[][] grid = new Cell[cols][rows];
private float height = 600;
private float width = 600;
private int w = 60;

public SweeperView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);

/*
* Gives every grid a cell
*/
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = new Cell(i, j, w, getContext());
}
}

/*
* Count neighbor of every cell
*/
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j].countNeighbors();
}
}
}

public void checkCells(float x, float y) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j].contains(x, y)) {
grid[i][j].reveal();
}
}
}
}

@Override
protected void onDraw(Canvas canvas) {
/*
* Draws the outer rectangle
*/
//Paint paint = new Paint(Color.GRAY);
//paint.setStyle(Paint.Style.STROKE);
//canvas.drawRect(1, height, width, 0, paint);

/*
* Draws every grid
*/
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j].show(canvas);
}
}
}

和 Cell.class:

public class Cell {
private boolean revealed = true;
private boolean mine = false;
private Context context;

public static int i;
public static int j;
private int x;
private int y;
private int w;

private int neighborCount = 0;

private Bitmap unrev;
private Bitmap rev;
private Bitmap minePic;

private Bitmap[] tiles = new Bitmap[8];

public Cell(int i, int j, int w, Context context) {
this.i = i;
this.j = j;
this.x = i * w;
this.y = j * w;
this.w = w;
this.context = context;

Random rand = new Random();
if(rand.nextInt(2) == 0) {
mine = true;
}

/*
Bitmap coding
*/
unrev = BitmapFactory.decodeResource(context.getResources(), R.drawable.unrevealed_tile);
rev = BitmapFactory.decodeResource(context.getResources(), R.drawable.revealed_tile);
minePic = BitmapFactory.decodeResource(context.getResources(), R.drawable.mine);

tiles[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_0);
tiles[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_1);
tiles[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_2);
tiles[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_3);
tiles[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_4);
tiles[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_5);
tiles[6] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_6);
tiles[7] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_7);
}

public boolean contains(float x, float y) {
if (x > this.x && x < this.x + this.w && y > this.y && y < this.y + this.w) {
return true;
}
return false;
}

public void reveal() {
revealed = true;
}

public void show(Canvas canvas) {
if(!revealed) {
canvas.drawBitmap(unrev, x-1, y, null);
} else {
if (mine) {
canvas.drawBitmap(minePic, x-1, y, null);
} else {
Bitmap draw = tiles[neighborCount];
canvas.drawBitmap(draw, x-1, y, null);
}
}
}

public void countNeighbors() {
if(mine) {

} else {
int total = 0;

for (int xoff = -1 ; xoff <= 1 ; xoff++) {
for (int yoff = -1 ; yoff <= 1; yoff++) {
int row = i + xoff;
int col = j + yoff;

if (row > -1 && row < grid.length && col > -1 && col < grid[row].length) {
//Log.d("SweeperLog", "Found 1 Neighbor");
Cell neighbor = grid[row][col];
if (!neighbor.mine) {
total++;
}
}
}
}
neighborCount = total;
Log.d("SweeperLog", "NeighborCount " + neighborCount);
}
}

最佳答案

我认为问题可能出在这里:

public static int i;
public static int j;

i 和 j 是静态的,这意味着它们在单元类的所有成员之间共享。

换句话说,它们属于类而不是类的实例。

尝试去除静电。

关于java - 扫雷算法卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44592656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com