OOP desing, Java Swing, chess game, instanceof

Viewed 833

OK, I'm in the process of making a simple java swing chess game. This question is more about OOP design then Java Swing.

I have the following:

  • I have a Panel class that implements JPanel.
  • I then have an abstract class Piece that extends from my Panel class
  • Then I have my classes for the different Pieces: Pawn, King, Bishop etc that extend from my Pieces class

In my main ChessGame Class:

  • I am using an array of Panel to store the layout of my board
  • So the array will store, Panel objects, for board places with no pieces on it.
  • And it will store, the subclasses such as Pawn, Queen, Bishop etc (board places with pieces)

So, the top left square (0,0) maps to myArray[0][0]

My problem is that, to check if the place is empty or has chess pieces in it I have to use:

        if(panels[x][y] instanceof Piece){
            ((Piece)panels[x][y]).makeMove();
        }

What I'm asking is this terrible design? I know I should try and stay away from instanceof. What would be a better approach?

Thanks.

6 Answers
Related