-2
  private Piece[][] board;

 public cboard(){
      this.board = new Piece[8][8];
    }

  public  boolean isEmpty(int x, int y){
      boolean empty= true;
        if (board[x][y] != null){
          empty= false;
        }
      return empty;
 }
 public void placePiece(Piece, int x, int y){
     if(isEmpty(x, y)){
        board[x][y] = piece;
     }
 }
}

Will this create an array for the pieces? The function has to check wether or not the place is empty

user3742283
  • 1
  • 1
  • 1

2 Answers2

0

Yes, in your constructor you create the array correctly. By default, Java will fill the array with null values but it knows that they are supposed to be Piece objects.

So your isEmpty method should work because it tests for null. However you might need a method to remove the Piece again and set the entry in the array back to null – otherwise your isEmpty would only work at the beginning.

just a hint:
you can break down your isEmpty method to only one line. It returns a boolean and the test in your if board[x][y] != null results in a boolean as well.
So all you need is return board[x][y]==null;

One last thing: cboard should begin with a capital letter, like all classes in Java.

GameDroids
  • 5,584
  • 6
  • 40
  • 59
0

The preceding program declares an array (named anArray) with the following line of code:

// declares an array of integers int[] anArray;

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html