#include <iostream> #include <string> using namespace std; enum piece_t {EMPTY,
ID: 3743159 • Letter: #
Question
#include <iostream>
#include <string>
using namespace std;
enum piece_t {EMPTY, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING};
enum side_t {WHITE, BLACK};
int main()
{
//Set up the board
//Get # of moves
//Read in and perform moves
//Print out the board
return 0;
}
**PLEASE USE THE FORMAT ABOVE**
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: where [a] can be any letter a-h (representing a column on the board), the piece at the first position and moves it to the second can be 1-8 (representing a row on the board), and the move takes 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:Explanation / Answer
#include <iostream>
using namespace std;
int main() {
//Set up the board
//
char board[8][9] = {
"RNBQKBNR",
"PPPPPPPP",
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ''},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ''},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ''},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ''},
"pppppppp",
"rnbqkbnr",
};
/*
Chess board 2D array to make moves
it is 8x9 to accomodate the character aswell
*/
char move[6];
/*
Character arrays to store moves
*/
//Get number of moves
int moves;
cin>>moves;
//Read in and perform moves
while(moves--) {
cin>>move;
if(move[0] >= 'a' && move[0] <= 'h' && move[3] >= 'a' && move[3] <= 'h'
&& move[1] >= '0' && move[1] <= '9' && move[4] >= '0' && move[4] <= '9') {
int source_row = 8 - (move[1] - '0'),
source_column = move[0] - 'a',
destination_row = 8 - (move[4] - '0'),
destination_column = move[3] - 'a';
/*
Because the numbering of a chessboard begins from bottom
we have to subtract the row coordinate from eight to get
the real coordinate that we have to work on
Convert the letter to number indexes
by subtracting the character from the beggining
of the character set
*/
char tmp = board[source_row][source_column];
board[source_row][source_column] = board[destination_row][destination_column];
board[destination_row][destination_column] = tmp;
/*
Swap the two locations
*/
}
}
/*
keep on scanning until EOF is found
*/
//Print board
for(int i = 0; i < 8; i++)
cout<<board[i]<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.