1.3-Object-Georienteerd-Programmeren

Chess again

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.

We advise the following approach

  1. Create a new project that includes the SaxionApp.
  2. Copy the content of the zip file to your project.
    • It contains a src folder and an images folder. Please note that the code is expecting classes that don’t exist yet!
  3. Create the Move object first.
    • Apply encapsulation and make sure you create a constructor and the appropriate getters. Moves coordinates count from the TOP LEFT corner!
  4. Create the Pawn class as an extension of ChessPiece.
  5. 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.

      • You may use the Board.tryAddMove(yourCollection, fromX, fromY, toX, toY) just to check that the move doesn’t fall off the board. (x==-1, or y==8, stuff like that)
  6. Continue with the other pieces. The Moves of the queen are a combination of the rook and the bishop.

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