In: Computer Science
c++
(1) Create a class, named Board, containing at least one data member (two-dimensional array) to store game states and at least two member functions for adding players’ moves and printing the game board. Write a driver to test your class.
(2). The data type of the two-dimensional array can be either int or char. As a passlevel program, the size of the board can be hardcoded to 10 or a constant with a pre-set value, anything between 4 and 20
(3). Set the initial state of the board as follows: assuming the size of the board is m*m, the initial position of red player is at (2, 2) and the blue player is at (m-1, m-1) as shown below (where m=5).
| | | | | |
| | R | | | |
| | | | | |
| | | | B | |
| | | | | |
(4). program should be able to accept human players’ moves. The format of input can be a pair of characters from ‘U’, ‘D’, ‘L’ and ‘R’. For instance, (U, L) means that the red player plays “up” and the blue player players “left”.
(5). program should be able to check if a move terminates the game (either a player crashes or two players collide). If a move does not terminate the game, the board state should be updated in accordance with the move.
(6). program should redisplay board state after each legal move. The layout of display must be text-based, similar to the following, where ‘R’ stands for red player, ‘B’ for blue player and ‘O’ for visited cells.
| | O | | | |
| | R | O | | |
| | | | | |
| | | | B | O |
| | | | O | |
7. Take an input from the user to set the size of the board. For
easy display, you can
place a restriction on the board size to 4 - 20. The size should be
passed to the board class
via its constructor.
(8). Since the dynamic array needed is two-dimensional, you
might have to declare a
pointer of pointers of either int or char to point to the
array.
I have implemented till task 6, i need help with 7 & 8.
// C++ program to create and implement a Board class
#include <iostream>
#include <cctype>
using namespace std;
// structure to represent the position of red and blue in the board
struct Position
{
int row,col;
};
class Board
{
private:
int size ;
char **board;
Position red, blue;
void setUpBoard();
public:
Board(int size);
bool addMove(char redMove, char blueMove);
void printBoard();
};
// constructor to set the size of the board and set up the initial setup of the board
Board::Board(int size)
{
this->size =size;
board = new char*[size];
for(int i=0;i<size;i++)
board[i] = new char[size];
setUpBoard(); // set the initial state of the board
}
// function to set the initial configuration of the board
void Board::setUpBoard()
{
// loop to set all the positions of the board to empty space
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
board[i][j] = ' ';
}
// set the red and blue positions
red.row = 1;
red.col = 1;
blue.row = size-2;
blue.col = size-2;
board[red.row][red.col] = 'R';
board[blue.row][blue.col] = 'B';
}
// function to add a move in the board and return if the move was successful or not
// if move was unsuccessful, it means game over
bool Board::addMove(char redMove, char blueMove)
{
Position newRed, newBlue;
// place the red piece
if(toupper(redMove) == 'U')
{
if((red.row-1) >= 0)
{
newRed.row = red.row-1;
newRed.col = red.col;
}else
{
return false;
}
}else if(toupper(redMove) == 'D')
{
if((red.row+1) < size)
{
newRed.row = red.row+1;
newRed.col = red.col;
}else
return false;
}else if(toupper(redMove) == 'L')
{
if((red.col-1) >= 0)
{
newRed.row = red.row;
newRed.col = red.col-1;
}else
return false;
}else if(toupper(redMove) == 'R')
{
if((red.col+1) < size)
{
newRed.row = red.row;
newRed.col = red.col+1;
}else
return false;
}
// place the blue piece
if(toupper(blueMove) == 'U')
{
if((blue.row-1) >= 0)
{
newBlue.row = blue.row-1;
newBlue.col = blue.col;
}else
{
return false;
}
}else if(toupper(blueMove) == 'D')
{
if((blue.row+1) < size)
{
newBlue.row = blue.row+1;
newBlue.col = blue.col;
}else
return false;
}else if(toupper(blueMove) == 'L')
{
if((blue.col-1) >= 0)
{
newBlue.row = blue.row;
newBlue.col = blue.col-1;
}else
return false;
}else if(toupper(blueMove) == 'R')
{
if((blue.col+1) < size)
{
newBlue.row = blue.row;
newBlue.col = blue.col+1;
}else
return false;
}
if((newRed.row == newBlue.row) && (newRed.col == newBlue.col))
return false;
else
{
board[red.row][red.col] = 'O';
board[blue.row][blue.col] = 'O';
board[newRed.row][newRed.col] = 'R';
board[newBlue.row][newBlue.col] = 'B';
red = newRed;
blue = newBlue;
return true;
}
}
// function to print the board
void Board:: printBoard()
{
cout<<"Game Board : "<<endl;
for(int i=0;i<size;i++)
{
cout<<"|";
for(int j=0;j<size;j++)
{
cout<<board[i][j]<<"|";
}
cout<<endl;
}
}
int main() {
char redMove, blueMove;
int size;
// input of size of the board
cout<<"Enter the size of the board(4-20) : ";
cin>>size;
// validate the size
while(size < 4 || size > 20)
{
cout<<"Invalid size. Value of board size can be between 4 and 20"<<endl;
cout<<"Enter the size of the board(4-20) : ";
cin>>size;
}
Board board(size); // create the board object
bool gameOver = false;
// loop that continues until the game is over
do
{
board.printBoard(); // print the board
// input of redMove and blueMove
cout<<"Enter redMove and blueMove with a space(Valid moves are U, D,L and R) : ";
cin>>redMove>>blueMove;
// validate the moves
while(((toupper(redMove) != 'U') && (toupper(redMove) != 'D') && (toupper(redMove) != 'L') && (toupper(redMove) != 'R'))
|| ((toupper(blueMove) != 'U') && (toupper(blueMove) != 'D') && (toupper(blueMove) != 'L') && (toupper(blueMove) != 'R')))
{
cout<<"Invalid red or blue move"<<endl;
cout<<"Enter redMove and blueMove with a space(Valid moves are U, D,L and R) : ";
cin>>redMove>>blueMove;
}
// move the red and blue piece
if(!board.addMove(redMove,blueMove))
gameOver = true;
}while(!gameOver);
cout<<"Game Over"<<endl;
return 0;
}
//end of program
Output: