I need it in c++. Mostly everything needs to be in functions not main, please us
ID: 3705382 • Letter: I
Question
I need it in c++.
Mostly everything needs to be in functions not main, please use basic c++ syntax.
Overview You will create a problem that takes in a board with chess pieces rom file. For this exercise, the board will only contain pawns and rooks. The dimensions ofthe board will b some M N dimensions where both M and N are greater han 1 Given a board, you will mark all pawns that can be attacked by a rook. Recall, a rook can move horizontally or vertically through empty spaces until it reaches an end of the board or another piece. You job is to read in the file, find all the pawns that can be attacked, and print an updated board to the screen. Piece and Board Representations Rooks: R .Pawns: P Empty spaces:" . When you find a pawn that can be attacked, change it from a 'P' to a T Sample file The files will contain the dimensions on the first line of the file then contain a board with a mix of empty spaces, pawns, and rooks. Command-line argument: s>./labe8 sample.txt Contents of sample.tt input file 4 5 P*P Output to terminal Rx Notes .The pawn at position (3,0) cannot be attacked because there is no rook in its row or column . The pawn at position (3,4) cannot be attacked because there is a piece between it and the rook in its columnExplanation / Answer
// File Name: RockPanChessBoard.cpp
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
/*
Function to display the board.
char **board - Chess board matrix data
int row - number of rows
int col - number of columns
*/
void displayBoard(char **board, int row, int col)
{
// Loops till number of rows
for(int r = 0; r < row; r++)
{
// Loops till number of columns
for(int c = 0; c < col; c++)
// Displays the row r and column c index position character
cout<<board[r][c];
// New line after each end of column
cout<<endl;
}// End of for loop
}// End of function
/*
Function to row attack by the Rock in the board.
char **board - Chess board matrix data
int row - number of rows
int col - number of columns
*/
void rowAttack(char **board, int row, int col)
{
// To keep track of the Pawns and Rock found status
int foundP, foundR;
// To store in which column the Pawn found
int pCol;
// Loops till number of rows
for(int r = 0; r < row; r++)
{
// Initialized all the variables to zero for each row
foundP = 0; foundR = 0; pCol = 0;
// Loops till number of columns
for(int c = 0; c < col; c++)
{
// Checks if 'P' for Pawn character found at r for row and c for column index of the board
if(board[r][c] == 'P')
{
// Set the Pawn found status to one
foundP = 1;
// Assigns the column index c as the found column position value in pCol
pCol = c;
}// End of if condition
// Checks if 'R' character for Rock found at r for row and c for column index of the board
if(board[r][c] == 'R')
// Set the Rock found status to one
foundR = 1;
}// End of inner for loop
// Checks if the found status for both Pawn and Rock is one then both found in same row
if(foundP == 1 && foundR == 1)
// Replace 'P' character by '!' at r for row and pCol for column index position of the board
board[r][pCol] = '!';
}// End of outer for loop
}// End of function
/*
Function to column attack by the Rock in the board.
char **board - Chess board matrix data
int row - number of rows
int col - number of columns
*/
void columnAttack(char **board, int row, int col)
{
// To keep track of the Pawns and Rock found status
int foundP, foundR;
// To store in which row the Pawn found
int pRow;
// Loops till number of columns
for(int r = 0; r < col; r++)
{
// Initialized all the variables to zero for each row
foundP = 0; foundR = 0; pRow = 0;
// Loops till number of rows
for(int c = 0; c < row; c++)
{
// Checks if 'P' for Pawn character found at c for row and r for column index of the board
if(board[c][r] == 'P')
{
// Set the Pawn found status to one
foundP = 1;
// Assigns the row index c as the found row position value in pRow
pRow = c;
// Come out of the inner loop
break;
}// End of if condition
// Checks if 'R' character for Rock found at c for row and r for column index of the board
if(board[c][r] == 'R')
// Set the Rock found status to one
foundR = 1;
}// End of inner for loop
// Checks if the found status for both Pawn and Rock is one then both found in same column
if(foundP == 1 && foundR == 1)
// Replace 'P' character by '!' at pRow for row and r for column index position of the board
board[pRow][r] = '!';
}// End of outer for loop
}// End of function
/*
Function to check attack by the Rock in the board.
char **board - Chess board matrix data
int row - number of rows
int col - number of columns
*/
void checkAttach(char **board, int row, int col)
{
cout<<" After Row Attack ";
// Calls the function for row attack
rowAttack(board, row, col);
// Calls the function for column attack
columnAttack(board, row, col);
// Calls the function to display the board after attack
displayBoard(board, row, col);
}// End of function
/*
Function to read the contents of file and stores in character type matrix
string fileName - name of the file
*/
void readFile(string fileName)
{
char **board;
int row, col;
// ifstream object declared
ifstream rFile;
//int row, col;
// Opens the file for reading
rFile.open(fileName.c_str());
// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!rFile.is_open())
{
// Displays error message
cout<<" Error: Unable to open the file! "<<fileName;
return;
}// End of if condition
// Reads number of rows and columns from the file
rFile>>row;
rFile>>col;
// Dynamically allocates memory for number of rows of size row
board = new char*[row];
// Loops till number of rows
for(int r = 0; r < row; ++r)
// Dynamically allocates memory for number of columns of size col for each row
board[r] = new char[col];
// Loops till number of rows
for(int r = 0; r < row; r++)
{
// Loops till number of columns
for(int c = 0; c < col; c++)
// Reads a character and stores in r as row index and c as column index position of matrix board
rFile>>board[r][c];
}// End of for loop
// Calls the function to display original board
cout<<" Original Board "<<endl;
displayBoard(board, row, col);
// Calls the function to check attack
checkAttach(board, row, col);
// Close the file
rFile.close();
}// End of function
// main function definition
int main()
{
// Function to read the file
readFile("chessBoard.txt");
}// End of function
Sample Output:
Original Board
*P**R
*R***
****P
P***P
After Row Attack
*!**R
*R***
****!
P***P
chessBoard.txt file contents
4 5
*P**R
*R***
****P
P***P