Question

In: Computer Science

c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to...

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.

Solutions

Expert Solution

// 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:


Related Solutions

C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
C++ please Create a Stats class whose member data includes an array capable of storing 30...
C++ please Create a Stats class whose member data includes an array capable of storing 30 double data values, and whose member functions include total, average, lowest, and highest functions for returning information about the data to the client program. These are general versions of the same functions you created for Programming Challenge 7, but now they belong to the Stats class, not the application program. In addition to these functions, the Stats class should have a Boolean storeValue function...
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
1. Create a PHP program containing an multi dimensional array of all the first and last...
1. Create a PHP program containing an multi dimensional array of all the first and last names of the students in your class. Sort the array by last name in alphabetic order. Also sort the array in reverse order. 2. Split the array from #1 into two arrays; one containing first names, the other containing last names.
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.
In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.
(In C language) Given a two-dimensional char array, how do I encode it into a one...
(In C language) Given a two-dimensional char array, how do I encode it into a one dimensional integer array? For example: char arr [8][8] = {{1,1,1,1,1,1,1,1}, {1,0,0,0,1,0,0,1}, {1,0,1,0,1,1,0,1}, {1,0,1,0,0,0,0,1}, {1,0,1,1,1,1,0,1}, {1,0,0,0,0,0,0,1}, {1,0,1,0,1,0,1,1}, {1,1,1,1,1,1,1,1}} into int arr2 [8] I know this problem requires bit-shifting but I am unsure how. It also needs to be as efficient as possible. Thanks!
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT