Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

WRITE IT IN C + + The Game of Chess Objective The objective of this assignment is to practice concepts related to inheritance, overriding, polymorphism,

WRITE IT IN C ++
The Game of Chess
Objective
The objective of this assignment is to practice concepts related to inheritance, overriding, polymorphism, and abstract classes, to program the basics of a chess playing program. No, we are not implementing Deep Blue (Links to an external site.). We are just creating a toy version that lets you set up pieces on the board, move them, and find possible moves given a certain position.
Because we are simplifying so many rules, we'll call this game, Modified chESS or MESS!
Description
Chess is played on an 8 X 8 board where the initial placement of pieces is as shown in the following figure, taken from Wikipedia (Links to an external site.). Note the indexing scheme. The white king is on e1, and the black king is on e8. Familiarize yourself with the names of the pieces.
Each chess piece can move in a specific way. Details of the original rules are provided here (Links to an external site.). However, we will follow a simplified version in this assignment. Most importantly, we will ignore an important rule in chess: Moving any piece in a way that puts your own king in check is illegal. Since we don't know what check means, for us a move is legal if the piece we are moving has an empty square to move to or can capture (replace) an opponent's piece (including their king, although that's also illegal in real chess).
The king does not move at all. Nor can it castle (Links to an external site.). The queen and knight can't move either.
A pawn in the initial position may move one or two squares vertically forward to an empty square but cannot leap over any piece. Subsequently it can move only one square vertically forward to an empty square. A pawn may also capture (replace) an opponent's piece diagonally one square in front of it. Pawns can never move backwards. These are the only moves; we will not implement the En passant (Links to an external site.) rule and will also not allow promotion (Links to an external site.) to another piece if the pawn reaches the end of the column.
A rook can move any number of squares horizontally or vertically, forward or backward, as long as it does not have to leap over other pieces. At the end of the move, it can occupy a previously empty square or capture (replace) an opponent's piece but it cannot replace another piece of the same player.
A bishop can move any number of squares diagonally in any direction as long as it does not have to leap over other pieces. At the end of the move, it can occupy a previously empty square or capture (replace) an opponent's piece but it cannot replace another piece of the same player.
Tasks
You will implement the eight classes described below.
3.1 Class ChessBoard
This class only stores the state of the board and its pieces for this assignment. In a real program, it would need to store more information (e.g., whoseTurn, etc). The board is represented by a 2-dimensional array of size 8X8. In other words, each dimension has indices 0..7.
Since the positions on a chess board are represented using a letter followed by a number, our array needs to represent the directions accordingly. We will make the following association: a=0, b=1, c=2, d=3, e=4, f=5, g=6, and h=7. In the initial position, the white king at e1 is at index [0][4]. The black queen at d8 is at index [7][3]. Note that initially we had rows and columns the other way around!
Implement the following attribute:
private *ChessPiece[][] board;
Implement the following constructor:
The no-arg constructor ChessBoard() initializes the board to an 8X8 array with all empty squares. An empty square is null.
Implement the following methods:
public void initialize()
This method initializes the board to the standard chess opening state with indexing as shown in the figure. This method should use the constructors of the appropriate pieces, and call placePiece below to place the newly constructed pieces in the right position.
ChessPiece* getPiece(Position position)
This method returns the chess piece at a given position.
public boolean placePiece(ChessPiece* piece, Position position)
This method tries to place the given piece at a given position, and returns true if successful, and false if there is already a piece of the same player in the given position. If an opponent's piece exists, that piece is captured. The position is represented as a class containing chess board position information (e.g., e8) as described above. Validate positions. The Position class contains 2 variables that represent a position on the chess board. The first is a character in lowercase (a..h) and the second is a digit (1..8). If successful, this method should call an appropriate method in the ChessPiece class (i.e., setPosition) to set the piece's position.
public boolean move(Position fromPosition, Position toPosition)
This method checks if moving the piece from the fromPosition to toPosition is a legal

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions