user2259610
user2259610

Reputation: 29

How to use variable from class inside method in java

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

    }

Upvotes: 0

Views: 3298

Answers (5)

James
James

Reputation: 223

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 {

    }
}

Upvotes: 0

Michael Hall
Michael Hall

Reputation: 185

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

Upvotes: 0

CBass
CBass

Reputation: 993

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.

Upvotes: 0

Laabidi Raissi
Laabidi Raissi

Reputation: 3333

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()

Upvotes: 0

7stud
7stud

Reputation: 48629

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.

Upvotes: 1

Related Questions