We are going to return to the chess excercise from week 1. Using the new skills that we’ve learned the last few weeks, we have new ways of handling the various pieces. A lot of code is the same for each piece.
Using inheritance, polymorphism and abstraction we only need to change the way specific chess pieces come up with a list containing their legal(!) moves.
You are presented with an abstract ChessPiece class,
your task is to work out the getMoves()
functions for all decendants.
Moves are simple POJO’s (Plain Old Java Object) with five member variables. The interface is shown in the class diagram.
Implement the getMoves(...)
method.
Please note that this time it is your job to generate an ArrayList that contains all possible moves the piece can take from its current position on the board. Instead of answering the question “can you move here?”. You can find a larger example under tips.
The Board class is very powerful. Don’t change its code! All actual movements are handled by it together with the Application.
Additionally when your list contains Moves that aren’t actually legal they will be filtered. For instance a bishop may move anywhere diagonally from its current position.
In this case the bishop piece generated all possible moves from its current position, but while showing all those moves the Board has filtered out the ones that jump over other pieced. (It also detects moves that take another piece and colors those red.)
When you are finished, try to play a game. We think that you will be pleasantly surprised. (Even though there are some important rules missing from the game.)