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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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)

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),
Transcribed Image Text: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),
piece it is. In the unicode character encoding scheme there are characters that represent each
chess piece. Use one of the following characters:
character piece
"\u2654" white king
"\u2655" white queen
"\u2656" white rook
"\u2657" white bishop
"\u2658" white knight
"\u2659" white pawn
"\u265A" black king
"\u265B" black queen
"\u265C" black rook
Page #2
"\u265D" black bishop
"\u265E" black knight
"\u265F" black pawn
It is important that your toString method return the right character string, as we will use it to
display the board position when testing your code.
Code Ex
●
virtual public vector<string>* legalMoves();
This method will be implemented in the concrete subclasses corresponding to each chess
piece. This method returns all the legal moves that piece can make based on the rules
described above in the assignment. Each string in the ArrayList should be the position of a
possible destination for the piece (in the same format described above)
3.3 Concrete classes Rook, Knight, Bishop, Queen, King, and Pawn
Implement each class to extend ChessPiece and override the methods toString and
legalMoves. Note that the legalMoves methods of King, Knight, and Queen return an empty
Transcribed Image Text:piece it is. In the unicode character encoding scheme there are characters that represent each chess piece. Use one of the following characters: character piece "\u2654" white king "\u2655" white queen "\u2656" white rook "\u2657" white bishop "\u2658" white knight "\u2659" white pawn "\u265A" black king "\u265B" black queen "\u265C" black rook Page #2 "\u265D" black bishop "\u265E" black knight "\u265F" black pawn It is important that your toString method return the right character string, as we will use it to display the board position when testing your code. Code Ex ● virtual public vector<string>* legalMoves(); This method will be implemented in the concrete subclasses corresponding to each chess piece. This method returns all the legal moves that piece can make based on the rules described above in the assignment. Each string in the ArrayList should be the position of a possible destination for the piece (in the same format described above) 3.3 Concrete classes Rook, Knight, Bishop, Queen, King, and Pawn Implement each class to extend ChessPiece and override the methods toString and legalMoves. Note that the legalMoves methods of King, Knight, and Queen return an empty
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY