0

This code is the bare-bones of a Minesweeper game board class I'm making. However, this only displays the first row of the array when I display the board. I have looked at this code for so long, and can't see where I'm going wrong with the two-dimensional array. Any help is appreciated

public class Board {

 private Cell[][] cells;

    public Board(PApplet p, int rows, int columns, double bombChance) {

        cells = new Cell[rows][columns];

        for (int r = 0; r < cells.length; r++) {

            for (int c = 0; c < cells[r].length; c++) {

                double randomSeed = Math.random();

                if (randomSeed < bombChance) {
                    cells[r][c] = new BombCell(p);
                } else {
                    cells[r][c] = new SafeCell(p);
                }


            }
        }

    }

    public void display() {

        double tempX = 0;
        double tempY = 0;
        double size = 50;

        for (int r = 0; r < cells.length; r++) {
            for (int c = 0; c < cells[r].length; c++) {

                cells[r][c].display(tempX, tempY, size);

                tempX += 50;
            }

            tempY += 50;

        }
    }
}
5
  • Try to separate game logic from presentation logic. Things will get a lot easier Commented Mar 6, 2017 at 14:33
  • @SeanPatrickFloyd I have a separate Minesweeper class to deal with game logic and modify the board. However, the array of cells itself is dealt with here, so this is where the problem would be found. Commented Mar 6, 2017 at 14:35
  • How does yor Cell.display Method looks like? Commented Mar 6, 2017 at 14:36
  • 1
    Step through the code. And what is the value of rows and columns? Commented Mar 6, 2017 at 14:36
  • Breakpoints go a long way Commented Mar 6, 2017 at 14:37

1 Answer 1

2

It appears that you are failing to reset tempX when you start to render the next row.

This may fix your issue:

public void display() {

    double tempX = 0;
    double tempY = 0;
    double size = 50;

    for (int r = 0; r < cells.length; r++) {
        for (int c = 0; c < cells[r].length; c++) {

            cells[r][c].display(tempX, tempY, size);

            tempX += 50;
        }

        tempY += 50;
        tempX = 0;           // <-------------- look here

    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Glad to help @JoshKatofsky
@jlordo You are correct. However once a row is rendered, tempX needs to be reset to 0. The original code did not reset tempX to 0 at the end of the row.
you can even improve by getting rid of tempX and tempY by using (r * size) and (c * size)
@ThomasPawlitzki thank you for this as well, another set of eyes can really streamline your code!
@mangotang gave you one, Stackoverflow said it's not going to be displayed because I'm a newbie to their site though :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.