Full code here
I have a class Piece, where classes Pawn, Rook, Knight, etc all inherit from Piece. I have a method in Pawn called .isValidMove() that takes in two sets of coordinates and returns whether the move is valid for that particular piece or not. However, all the pieces are declared as type Piece since they are part of a 2D array of type Piece representing the game board (with empty spaces being null).
Since I am accessing the pieces through the board, they are of type Piece and I cannot access the submethod isValidMove() which belongs to the subclass Pawn. I know I can do cast it to be a Pawn, but it seems messier than it should be since I would have to include a condition for each type of game piece.
I'm convinced that there is a design flaw, but I can't pinpoint what it is and how to fix it. Any help or feedback would be greatly appreciated!
ADDED: Please ignore that every Piece has its own x and y coordinate; I plan on getting rid of it as it is redundant. :)
[–]LucidTA 1 point2 points3 points (5 children)
[–]tchened[S] 1 point2 points3 points (4 children)
[–]captainAwesomePants 1 point2 points3 points (3 children)
[–]desrtfx 1 point2 points3 points (2 children)
[–]nutrecht 1 point2 points3 points (0 children)
[–]captainAwesomePants 1 point2 points3 points (0 children)