Answered step by step
Verified Expert Solution
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 X 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 e and the black king is on e 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. 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 eg whoseTurn, etc The board is represented by a dimensional array of size X In other words, each dimension has indices 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 b c d e f g and h In the initial position, the white king at e is at index The black queen at d is at index Note that initially we had rows and columns the other way around! Implement the following attribute: private ChessPiece board; Implement the following constructor: The noarg constructor ChessBoard initializes the board to an X 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 getPiecePosition position This method returns the chess piece at a given position. public boolean placePieceChessPiece 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 eg e as described above. Validate positions. The Position class contains variables that represent a position on the chess board. The first is a character in lowercase ah and the second is a digit If successful, this method should call an appropriate method in the ChessPiece class ie setPosition to set the piece's position. public boolean movePosition fromPosition, Position toPosition This method checks if moving the piece from the fromPosition to toPosition is a legal
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 X 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 e and the black king is on e 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.
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 eg whoseTurn, etc The board is represented by a dimensional array of size X In other words, each dimension has indices
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 b c d e f g and h In the initial position, the white king at e is at index The black queen at d is at index Note that initially we had rows and columns the other way around!
Implement the following attribute:
private ChessPiece board;
Implement the following constructor:
The noarg constructor ChessBoard initializes the board to an X 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 getPiecePosition position
This method returns the chess piece at a given position.
public boolean placePieceChessPiece 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 eg e as described above. Validate positions. The Position class contains variables that represent a position on the chess board. The first is a character in lowercase ah and the second is a digit If successful, this method should call an appropriate method in the ChessPiece class ie setPosition to set the piece's position.
public boolean movePosition 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started