0

So I have to write a minesweeper game for an assignment. If I made a class for the Board, containing two 2D-arrays, one for the board value and one holding whether the user had already clicked there or not clicked. I wrote the methods with the arguments of the two 2D-arrays. How would I call those arrays in my main class?

public class Board {

    int x;
    int y;
    public char[][] board;
    public char[][] reveal;

    Board(int x, int y){
        board = new char[x][y];
        reveal = new boolean[x][y];
}

    }

public class Mine{

    public static void main(String[] args){

        Board gameboard;
        gameboard = new Board(5, 5);

                   ???

        Board.printBoard(board, reveal);

    }
}


public void printBoard(char[][] board, boolean[][] test){

        for(int i=0; i<=board.length; i+=1){
            for(int j=0; j<board[i].length; j+=1){
                if (test[i][j]==true){
                System.out.print(board[i][j]);
                }
                else {
                    System.out.print('?');
                }
            }
            System.out.println();
        }

    }

5 Answers 5

1

How would I call those arrays in my main class?

You don't 'call' an array. You call methods. If you want to access an instance variable of a class, you need to provide accessor methods, i.e. methods that 'get' and 'set' the instance variable.

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

5 Comments

Accessor methods are recommended, but not required. As his arrays are public (unrecommended) he could simply refer to them via an instance.
I tried this... not sure how to set up the getter here public char[][] getBoard(Board board){ return char[][] board; }
How would I refer to it via an instance?
gameboard is such an instance. An instance is the result of calling new - a 'new' instance has been created of the Object. If you want to encapsulate the arrays, try public char[][] getBoard() { return board; } The idea is to not expose an Object's members to direct editing to keep control of them within the Object, so variables should be private and exposed via public accessors.
When you declare a method you have to specify the types that are used. When you actually call a method with arguments or return a value from a method, you don't specify the types of the arguments or the return value. Java uses your type declarations to make sure the actual arguments match the types you declared and that the return value matches the type you declared.
0

Since you are declaring board and reveal attributes as public you can just access their values using gameboard.board and gameboard.reveal.
However, this is a very very bad approach. Encapsulation.
So you should declare getter and setter for each class attribute x and access its value with: objectName.getX()

Comments

0

After you instantiate an instance of your board class in your main method, you would access them as follows (for example):

Board gameboard = new Board(5,5);
for (int i = 0; i < 5; i++) {
  for (int j = 0; j < 5; j++) {
    System.out.print(gameboard.board[i][j]);
  }
}

etc.

Using private members and accessor methods is a better a approach but certainly not a requirement.

Comments

0

You just have to type gameboard.board to get the "board" that's stored in your Board object.

Comments

0

You can access method easily as other people have suggested by using the following code.

System.out.print(gameboard.board[i][j]);

You could incorporate encapsulation like many have suggested.

However, I'd also suggest implementing it simpler, and within one class if you're allowed to for your assignment.

This works just as effectively with less code and IMO is just as easy to understand.

public class Board {
    private int x;
    private int y;
    private char[][] board;
    private boolean[][] reveal;

    public static void main(String[] args) {
        Board gameboard = new Board(5, 5);
        gameboard.printBoard();
    }

    public Board(int x, int y) {
        this.x = x;
        this.y = y;
        board = new char[x][y];
        reveal = new boolean[x][y];
    }

    public void printBoard() {
        for (int i = 0; i < board.length; i += 1) {
            for (int j = 0; j < board[i].length; j += 1) {
                if (reveal[i][j] == true) {
                    System.out.print(board[i][j]);
                } else {
                    System.out.print('?');
                }
            }
            System.out.println();
        }
    }

    public class Mine {

    }
}

Comments

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.