1

For practice, I'm following a Minesweeper tutorial but it won't count the bomb to the right when adding up the number of bombs around each square and I'm not completely sure what the issue is. I've tried relaunching, opening it in multiple compilers, moving it around, nothing. I've looked and looked and I cannot find any logic errors.

Below is my counting code. The last if statement is the one counting the square to the right.

btnAmt = 10

background is a 2D array that holds all the mines values.

If the error isn't in here I can post the full code, but I'm frustrated because there is seemingly no logic error, and all other directions work.

//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;
        }
    }
}
k_ssb
  • 6,024
  • 23
  • 47
Jek Jek
  • 17
  • 2
  • We'll not want to see the whole program, but a small subset that compiles and runs and shows your problem, a [mcve] code post in your question would be ideal. – Hovercraft Full Of Eels May 21 '18 at 22:26
  • There is no "down" count? – Loris Securo May 21 '18 at 22:28
  • This probably belongs on [code review](http://codereview.stackexchange.com) – Lino May 21 '18 at 22:33
  • 1
    @Lino: no, I can say without a doubt that this does *not* belong on code review since he's not asking how to make this code better. Rather he's asking about code that is currently not functioning properly, and that makes this site much more appropriate for the question. It can certainly be improved though (as per my first comment). – Hovercraft Full Of Eels May 21 '18 at 22:35

2 Answers2

1

You never checked for background[x][y+1]. This is the "right" direction, and what you have commented as "Right" (background[x+1][y]) is actually down.

Remember that mat[i][j] denotes (by convention) the i-th row and the j-th column of matrix mat. So, going right means adding 1 to the column, hence mat[i][j+1].

k_ssb
  • 6,024
  • 23
  • 47
  • Thank you so much, I knew it was a simple mistake. Sorry for being so inept, I'm pretty new to coding and have VERY little GUI knowledge so I get slightly overwhelmed. I'll put stuff like this in code review next time, I wasn't aware that existed., my fault – Jek Jek May 22 '18 at 04:35
  • @JekJek A question where you aren't getting the result you expect does _not_ belong in Code Review, the person who suggested that was wrong. Also, the standard way to "thank" on StackOverflow is to accept an answer you thought was the most helpful. – k_ssb May 22 '18 at 19:34
0

I would try to make it more logical and easier to follow myself by looping through the surrounding squares and checking mine count, but after deciding what my loop boundaries should be, taking all edges into consideration. For example, something like so:

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

As proof of concept, the following program uses the above code unaltered, and works:

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

Example output:

   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   .
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373