Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

GAMEBOARD.h #ifndef GAMEBOARD_H #define GAMEBOARD_H #include gamepiece.h class GameBoard { public : GameBoard( int rows, int cols); bool isSpaceValid( int row, int col); bool addPiece(GamePiece

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

GAMEBOARD.h

#ifndef GAMEBOARD_H

#define GAMEBOARD_H

#include "gamepiece.h"

class GameBoard

{

public:

GameBoard(int rows, int cols);

bool isSpaceValid(int row, int col);

bool addPiece(GamePiece piece, int row, int col);

bool movePiece(int srcRow, int srcCol, int destRow, int destCol);

void print();

};

#endif

GAMEPIECE.h

#ifndef GAMEPIECE_H

#define GAMEPIECE_H

class GamePiece

{

public:

GamePiece();

GamePiece(char* newLabel);

char* getLabel();

char* toString();

};

#endif

GAMEPIECE.CPP

#include "gamepiece.h"

GamePiece::GamePiece()

{

}

GamePiece::GamePiece(char* newLabel)

{

}

char* GamePiece::getLabel()

{

return "";

}

char* GamePiece::toString()

{

return "";

}

GAMEBOARD.CPP

#include "gameboard.h"

GameBoard::GameBoard(int rows, int cols)

{

}

bool GameBoard::isSpaceValid(int row, int col)

{

return false;

}

bool GameBoard::addPiece(GamePiece piece, int row, int col)

{

return false;

}

bool GameBoard::movePiece(int srcRow, int srcCol, int destRow, int destCol)

{

return false;

}

void GameBoard::print()

{

}

MAIN CODE:

#include "gameboard.h"

#include "gamepiece.h"

#include

#include

using namespace std;

int main()

{

/* declare local variables */

int row;

int col;

int destRow;

int destCol;

int rowNum;

int colNum;

char inputString[30];

/* get the size of the game board */

cout

cin >> rowNum;

cout

cin >> colNum;

GameBoard board(rowNum, colNum);

/* get the first piece's label */

cout

cin >> inputString;

while (strcmp(inputString, "Q") != 0 && strcmp(inputString, "q") != 0)

{

GamePiece piece(inputString);

/* get the location to place the piece */

cout

cin >> row;

cout

cin >> col;

/* verify the space is valid then add the piece to the board */

if (board.isSpaceValid(row, col))

{

if (board.addPiece(piece, row, col))

{

cout

}

else

{

cout

}

}

else

{

cout

}

/* get the label for the next piece */

cout

cin >> inputString;

}

/* print the board and check if user wants to move a piece */

board.print();

cout

cin >> inputString;

while (strcmp(inputString, "Y") == 0 || strcmp(inputString, "y") == 0)

{

/* get the location of the piece */

cout

cin >> row;

cout

cin >> col;

/* get the destination for the piece */

cout

cin >> destRow;

cout

cin >> destCol;

/* verify both spaces are valid then move the piece */

if (board.isSpaceValid(row, col) &&

board.isSpaceValid(destRow, destCol))

{

if (board.movePiece(row, col, destRow, destCol))

{

cout

}

else

{

cout

}

}

else

{

cout

}

/* print the board and check if the user wants move another piece */

board.print();

cout

cin >> inputString;

}

return 0;

}

4. Part 2 Structs and Arrays (65 points). In this assignment, we will be making a program to populate a game board with pieces and to move those pieces around on the board. Use the files homework part_2.cpp, gamepiece.h, gamepiece.cpp, gameboard.h, and gameboard.cpp (available on Canvas). You will need to add header comments to each file along with comments for all the methods you will be implementing. Any changes made to the main method of the file will be penalized unless otherwise instructed Step1. For the GamePiece class you will use the files gamepiece.h and gamepiece.cpp. Thej gamepiece.cpp file should implement the methods for the GamePiece class declared in gamepiece.h. You will need to add a private member variable, char label[30], to the class declaration in gamepiece.h. Implement the methods in gamepiece.cpp as follows: unction Description oid GamePiece 0 ssign the default string "." to th e game piece's label. oid GamePiece(char* newLabel) sign the game piece's label with the newLabel ovided getLabel0 the piece's label toString0 a C string of length 3 from the piece's label ote: this length does not include the null character). If label is shorter than length 3, then the new string uld be the label with spaces appended to make it the t length. If the label is longer than 3, then use the irst 3 characters of the label Step 2 For the GameBoard class you will use the files gameboard.h and gameboard.cpp. The gameboard.cpp file should implement the methods for the GameBoard class declared in gameboard.h. The class will contain a 2-dimensional array called "board" of GamePiece type and 2 ints, rows and columns. These member variables should be added to the class declaration in gameboard.h as private members. Implement the methods in gameboard.cpp as follows unction escriptioin oid GameBoard(int rows, int cols) ates the 2-dimensional array "board" to the size Tows" by "columns" specified by the parameters, then ets the GameBoard's rows and columns values. Each amePiece in the board should be instantiated with the fault label using the default constructor ol isSpaceValid(int row, int col) e function checks if the parameters row and col are alid. If at least one of the parameters "ro ess than 0 or larger than the last index of the array (note w" or "col" is at the number of rows and columns can be difterent), en it retun false. Otherwise it returns true ol addPiece(GamePiece piece, int s function should validate that the space specified by ow and col is valid and that the space is not occupied by iece. If the GamePiece at the space has the default label, e space should be considered not occupied. If the space s both valid and not already occupied, then the space hould be replaced by the parameter "piece" and the ethod should retum true. Otherwise, return false , int col) 01 sSpaceValid(int row, int col) e function checks if the parameters row and col are alid. If at least one of the parameters ess than 0 or larger than the last index of the array (note "row" or"c ol" is t the number of rows and columns can be different), en it return false. Otherwise it returns true ol addPiece(GamePiece piece, int function should validate that the space specified by int col) ow and col is valid and that the space is not occupied by ece. If the GamePiece at the space has the default label, e space should be considered not occupied. If the space s both valid and not already occupied, then the space hould be replaced by the parameter "piece" and the ethod should retum true. Otherwise, return false t movePiece(int srcRow, int srcCol, t destRow, int destCol) s method should validate that both the src and dest paces are valid and that the dest space is not occupied. If th conditions pass, then the piece located at (srcRow., 1) should be moved to (destRow, destCol). The pace at (srcRow, srcCol) should be replaced by the fault GamePiece. If this method moves the piece, return e, otherwise retun false t prints information of the "board". It should show the list f pieces placed on the board using the amePiece:toString method (it shows the first 3 oid print0 of each piece). Use the following format: he GameBoard --Rn aw lease see the sample output listed below After compiling the homework part 2.cpp, gameboard.cpp, and gamepiece.cpp files, you need to execute it. Sample Output: (the inputs entered by a user are shown in red Please enter the number of rows. 3 Please enter the number of columns. 3 Please enter a label for a new piece. Enter "Q" when done. King Please enter a row for the piece Please enter a column for the piece. 1 New piece "King" added. Please enter a label for a new piece. Enter "Q" when done. Pawn Please enter a row for the piece 1 Please enter a column for the piece. New piece "Pawn" added. Please enter a label for a new piece. Enter "Q" when done. Pawn Please enter a row for the piece Please enter a column for the piece. New piece "Pawn" added Please enter a label for a new piece. Enter "Q" when done. Queer Please enter a row for the piece. Please enter a column for the piece. Invalid row and/or column Please enter a label for a new piece. Enter "Q" when done. King Please enter a row for the piece. 1 Please enter a column for the piece. 2 Invalid row and/or column iece. Enter "on when done Invalid row and/or column Please enter a label for a new piece. Enter "Q" when done. The GameBoard Kn-- Paw ---_- -_ Paw Nould you like to move a piece? Enter "Y" to move a piece. Please enter the piece' s row 0 Please enter the piece' s column. Please enter the piece's new row. Please enter the piece' s new column 2 Piece moved to new space. The GameBoard Kin Paw ---_- Paw Would you like to move a piece? Enter "Y" to move a piece

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions