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.
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects....
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects. The Byte class will provide the following member functions: Byte - word: Bit[8] static Bit array defaults to all Bits false + BITS_PER_BYTE: Integer16 Size of a byte constant in Bits; 8 + Byte() default constructor + Byte(Byte) copy constructor + set(Integer): void sets Bit to true + clear(): void sets to 0 + load(Byte): void sets Byte to the passed Byte + read():...
C++ Classes & Objects Create a class named Student that has three private member: string firstName...
C++ Classes & Objects Create a class named Student that has three private member: string firstName string lastName int studentID Write the required mutator and accessor methods/functions (get/set methods) to display or modify the objects. In the 'main' function do the following (1) Create a student object "student1". (2) Use set methods to assign StudentID: 6337130 firstName: Sandy lastName: Santos (3) Display the students detail using get functions in standard output using cout: Sandy Santos 6337130
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
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional array will be used to store the rows and columns of a multiplication and division table. Note that the table will start at 1 instead of zero, to prevent causing a divide-by-zero error in the division table! struct mult_div_values { int mult; float div; }; The program needs to read the number of rows and columns from the user as command line arguments. You...
In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT