Implement the following attribute: protected ChessBoard board; // the board it belongs to, default null ● protected int row; // the index of the horizontal rows 0..7 ● protected int column; // the index of the vertical column 0..7 protected Color color; // the color of the piece Implement the following constructor: public ChessPiece(ChessBoard board, Color color) This constructor sets the board and color attributes. Implement the following methods: a b e h ● public int getRow() 8 This method returns the row. 7 ÅÅÅÅÅÅÅ public int getColumn() 6 This method returns the column. 5| 5 4 public void setRow(int i) 3 | 3 This method sets the row. 2 81 81 81 81 2 8 8 8 8 8 8 8 8 2 |0|0| ● ¹ ABCDEFGH This method sets the column. a b с d e f gh public Color getColor() This method returns the color of the piece. There is no need for a setColor method because a piece cannot change color. public String getPosition() This method returns the position of the piece in the format single letter (a..h) followed by a single digit (1..8). public void setPosition (Position position) This method sets the position of the piece to the appropriate row and column based on the argument, which in the format single letter (a..h) followed by a single digit (1..8). virtual public string toString(); This method will be implemented in the concrete subclasses corresponding to each chess piece. This method returns a string composed of a single character that corresponds to which public void setColumn(int i),
Game of Chess in C++
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.
About:
Chess is played on an 8 X 8 board where the initial placement of pieces. The white king is on e1, and the black king is on e8. Each chess piece can move in a specific way. Follow a simplified version. Most importantly, 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).
- The king does not move at all. Nor can it castle 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 rule and will also not allow promotion 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.
Implement the eight classes described below.
3.1 Class ChessBoard
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 move. Legality is defined based on the rules described above. If the move is legal, it executes the move, changing the value of the board as needed. The method returns true if the move was executed, and false otherwise.
- public string toString()
You must implement the toString method to help debug your program. We assume that ChessPiece has an appropriately.
3.2 Abstract class ChessPiece: This class keeps a reference to the board the piece is on (if any), stores the position where the piece is located, and the color.
Include the following enum type it as shown below: enum Color {WHITE, BLACK}
Preliminary and final testing
A barebones main method is provided for the ChessBoard class.
int main()
{
ChessBoard board = new ChessBoard();
board.initialize();
System.out.println(board);
board.move("c2", "c4");
return 0;
}(Please write description of code functions)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps