Search code examples
javaandroidalgorithmminesweeper

Minesweeper Algorithm is stuck


I'm programming Minesweeper in Android with Android Studio and i'm trying to count the neighbors of an cell for calculating the numbers that are shown.

But the counting doesn't work because all cells have the same wrong number of neighbors:

Here is an example picture as hyperlink (showing the picture in post didn't work)

The search Method:

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);
    }
}

And the SweeperView.class in which everything gets drawed and created:

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);
        }
    }
}

And the 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);
    }
}

Solution

  • I think the problem might be here:

    public static int i;
    public static int j;
    

    i and j are static meaning they are shared amongst all members of the cell class.

    In other words, they belong to the class rather than the instance of a class.

    Try removing the static.