OOP - Chess

Chess is a game played on an 8 × 8 game board whereby two players have a number of pieces, including one King. Players take turns to move one of their pieces. Each piece has different rules for how it can move around the board, and when one piece is moved onto a tile containing one of the opponent’s pieces, the opponent’s piece is taken out of the game. The winner is the player who manages to take their opponent’s King.

Using the following UML class diagram and class descriptions to help you, create a version of Chess. When it is a player’s turn, they must select the tile with the piece that they would like to move (if this tile doesn’t contain one of their pieces, a message should be displayed to tell the player that they don’t have a piece on that tile and they should be asked for their selection again) and then select the tile that they would like to move this piece to (if this is not a valid move, a message should be displayed to tell the player that this move is invalid and they should be asked for their move again). Player input should be given as a number, to indicate the column of their chosen location, and a letter, to indicate the row (first row is A, second row is B, etc.). You may use the Chess skeleton code to help you.

Board

Attribute/Method

Description

columns

Specifies the number of columns on the game board.

rows

Specifies the number of rows on the game board.

board

Keeps track of the piece locations on the game board.

gameOver

Specifies whether or not the game has been won.

Board()

Creates a new Board object.

display()

Displays the current state of the board.

getWidth()

Accessor for columns.

getHeight()

Accessor for rows.

gameOver()

Accessor for gameOver.

pieceAt()

Returns the piece at a given location, or none if that location has no piece.

movePiece()

Tries to make a given move, displaying a message saying what move has been made or a message saying that the move is invalid.

takePiece()

Displays a message to say which piece has been taken by which piece and sets gameOver = True if the piece which has been taken is a king.

upgradePiece()

Replaces a pawn with a queen if it reaches the end of the board.

setUp()

Sets up the board with all pieces in their starting positions.

Player

Attribute/Method

Description

colour

Specifies this player’s colour.

Player()

Creates a new Player object.

getColour()

Accessor for colour.

HumanPlayer

Attribute/Method

Description

movePiece()

Asks a player for the start and end locations of their move until they provide two valid locations, then makes the move and returns whether or not the game has been won.

getPos()

Checks that the player has given a valid location, returning an array of the row and column numbers if one location has been passed to getPos or making the move on the board if two locations have been passed.

Piece

Attribute/Method

Description

colour

Specifies the colour of this piece.

type

Specifies the name of this type of piece.

range

Specifies the number of tiles this piece can move in a turn.

Piece()

Creates a new Piece object.

getType()

Accessor for type.

getColour()

Accessor for colour.

validMove()

Gets a valid row on a board.

Pawn

Attribute/Method

Description

Pawn()

Creates a new Pawn object, with type = "pawn".

validMove()

A pawn can move one tile straight forward if there is no piece in front of it, or one tile diagonally forward if there is an enemy piece in that location. A pawn can move two tiles straight forward if it is in its starting position and there are no pieces on either of the two tiles in front of it.

Knight

Attribute/Method

Description

Knight()

Creates a new Knight object, with type = "knight".

validMove()

A knight can move two tiles vertically and one tile horizontally, or two tiles horizontally and one tile vertically. A knight can jump over pieces.

Rook

Attribute/Method

Description

Rook()

Creates a new Rook object, with type = "rook".

validMove()

A rook can move any number of tiles in a straight line as long as there are no pieces between it and the end tile.

Bishop

Attribute/Method

Description

Bishop()

Creates a new Bishop object, with type = "bishop".

validMove()

A bishop can move any number of tiles in a diagonal line as long as there are no pieces between it and the end tile.

Queen

Attribute/Method

Description

Queen()

Creates a new Queen object, with type = "queen".

validMove()

A Queen can make any move that a rook or bishop can make.

King

Attribute/Method

Description

King()

Creates a new King object, with type = "king".

validMove()

A King can move one tile in any direction.

BasicMovement

Attribute/Method

Description

validMove()

Makes sure that the given move meets the basic criteria for a valid move for a piece.

StraightMovements

Attribute/Method

Description

validMove()

Makes sure that the given move meets the criteria for a valid move when moving in a straight line.

DiagonalMovement

Attribute/Method

Description

validMove()

Makes sure that the given move meets the criteria for a valid move when moving in a diagonal line.