In C++ Write a program that is capable of replaying the moves in a chess match.
ID: 3742800 • Letter: I
Question
In C++
Write a program that is capable of replaying the moves in a chess match. The user will first enter the number of moves, then they will type in moves of the form: [a] [#]-[a][ where [a] can be any letter a-h (representing a column on the board. can be 1-8 (representing a row on the board. and the move takes the piece at the first position and moves it to the second For example, the common opening move c2-c4 would take the chess piece at c2 (initially a pawn) and move it to space c4. Note that every move will leave its initial space (c2) empty afterwards, and when a piece moves to a space occupied by another piece, the existing piece is taken away and the capturing" piece occupies that space. The game of chess is played between two opposing players, white and black, where each player starts 16 pieces of 6 different types: pawn, rook, knight, bishop, queen, and king. Initially, white's pieces occupy rows 1 and 2, while black's pieces occupy rows 7 and 8. All of the pieces on rows 2 and 7 are pawns, though the pieces on rows 1 and 8 differ. Columns a and h on these rows have rooks, b and g have knights, c and f have bishops, d has the two queens, and e has the two kings. Using P, R, N (knight), B, Q, and K to represent the chess pieces (capital for white and lowercase for black) and. to represent an empty space, we can draw this initial board as RNBQKBNR rnbgkbnr Once the user has entered all of the moves, the program should print the final state of the board, in this format. As usual, this should be the only output for the program. Note that your program does NOT need to check the validity of the moves according to the rules of chess,just to replay them. Your program will not be tested on whether it can recognize improperly formatted moves (e.g., moves that don't start with a-h or don't have two spaces separated by a hyphen), but you may wish to do some input checking to help when testing your code. Also, you may treat a move that starts with an empty space as valid-let the empty space "capture" wherever it moves (i.e., make the destination empty). Lastly, the number of moves is guaranteed to be a nonnegative integer Hint: if you add 32 (0x20) to the ASCII code for an uppercase letter, you can get the ASClIl code for the lowercase version of that letter.Explanation / Answer
void printBoard(Game& game)
{
cout << " A B C D E F G H ";
for (int iLine = 7; iLine >= 0; iLine--)
{
if ( iLine%2 == 0)
{
// Line starting with BLACK
printLine(iLine, BLACK_SQUARE, WHITE_SQUARE, game);
}
else
{
// Line starting with WHITE
printLine(iLine, WHITE_SQUARE, BLACK_SQUARE, game);
}
}
}
void printLine(int iLine, int iColor1, int iColor2, Game& game)
{
// Define the CELL variable here.
// It represents how many horizontal characters will form one square
// The number of vertical characters will be CELL/2
// You can change it to alter the size of the board
// (an odd number will make the squares look rectangular)
int CELL = 6;
// Since the width of the characters BLACK and WHITE is half of the height,
// we need to use two characters in a row.
// So if we have CELL characters, we must have CELL/2 sublines
for (int subLine = 0; subLine < CELL/2; subLine++)
{
// A sub-line is consisted of 8 cells, but we can group it
// in 4 iPairs of black&white
for (int iPair = 0; iPair < 4; iPair++)
{
// First cell of the pair
for (int subColumn = 0; subColumn < CELL; subColumn++)
{
// The piece should be in the "middle" of the cell
// For 3 sub-lines, in sub-line 1
// For 6 sub-columns, sub-column 3
if ( subLine == 1 && subColumn == 3)
{
cout << char(game.getPieceAtPosition(iLine, iPair*2) != 0x20 ?
game.getPieceAtPosition(iLine, iPair*2) : iColor1);
}
else
{
cout << char(iColor1);
}
}
// Second cell of the pair
for (int subColumn = 0; subColumn < CELL; subColumn++)
{
// The piece should be in the "middle" of the cell
// For 3 sub-lines, in sub-line 1
// For 6 sub-columns, sub-column 3
if ( subLine == 1 && subColumn == 3)
{
cout << char(game.getPieceAtPosition(iLine,iPair*2+1) != 0x20 ?
game.getPieceAtPosition(iLine,iPair*2+1) : iColor2);
}
else
{
cout << char(iColor2);
}
}
}
}
}
bishop movement logic :