我用Android工作室在Android中编程扫雷器,我试图计算一个单元格的邻居来计算显示的数字。
但是计数不起作用,因为所有的细胞都有相同数目的错误邻居:
Here是一个超链接示例图片(在post中显示图片不起作用)
搜索方法:

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是静态的,这意味着它们在cell类的所有成员之间共享。
换句话说,它们属于类,而不是类的实例。
试着去掉静电。

09-07 15:07