CSE 240 Homework 2 – Programming with C++ Due: Monday, April 1, 11:59 PM
For each file in your assignment, provide a heading (in comments) which includes:
function comments
and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters.
the array size. Use a for statements to print all the elements in the array. You must use pointers to work with the array. Hint: review pointers as parameters.
The function receives the number (integer number) as parameter
Any changes made to the main method of the file will be penalized unless otherwise instructed
Grading Criteria for the part 1
01 pts: file contains header information 01 pts: adequate comment to explain every function 01 pts: consistent indentation and spacing 08 pts: arrayInsertionSort 08 pts: printArray 08 pts: initializeArray 08 pts: factorial
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.
Step 1.
For the GamePiece class you will use the files gamepiece.h and gamepiece.cpp. The 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:
Function Description void GamePiece () Assign the default string "---" to the game piece’s label.
void GamePiece(char* newLabel) Assign the game piece’s label with the newLabel
provided.
char* getLabel() Returns the piece’s label.
char* toString() Constructs a C string of length 3 from the piece’s label
(Note: this length does not include the null character). If the label is shorter than length 3, then the new string should be the label with spaces appended to make it the correct length. If the label is longer than 3, then use the first 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:
Function Description void GameBoard(int rows, int cols) Instantiates the 2-dimensional array “board” to the size "rows" by "columns" specified by the parameters, then sets the GameBoard’s rows and columns values. Each
GamePiece in the board should be instantiated with the default label using the default constructor.
bool isSpaceValid(int row, int col) The function checks if the parameters row and col are
valid. If at least one of the parameters "row" or "col" is less than 0 or larger than the last index of the array (note that the number of rows and columns can be different), then it return false. Otherwise it returns true.
bool addPiece(GamePiece piece, int row, int col)
This function should validate that the space specified by row and col is valid and that the space is not occupied by a piece. If the GamePiece at the space has the default label, the space should be considered not occupied. If the space is both valid and not already occupied, then the space should be replaced by the parameter “piece” and the method should return true. Otherwise, return false.
int movePiece(int srcRow, int srcCol, int destRow, int destCol)
This method should validate that both the src and dest spaces are valid and that the dest space is not occupied. If both conditions pass, then the piece located at (srcRow, srcCol) should be moved to (destRow, destCol). The space at (srcRow, srcCol) should be replaced by the default GamePiece. If this method moves the piece, return true, otherwise return false. void print() It prints information of the "board". It should show the list
of pieces placed on the board using the GamePiece::toString method (it shows the first 3 characters of each piece). Use the following format:
The GameBoard -------------------- --- Kin --- Paw --- --- --- --- Paw
Please 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. 0 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. 0 New piece "Pawn" added. Please enter a label for a new piece. Enter "Q" when done. Pawn Please enter a row for the piece. 2 Please enter a column for the piece. 2 New piece "Pawn" added. Please enter a label for a new piece. Enter "Q" when done. Queen Please enter a row for the piece. 4 Please enter a column for the piece. 5 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. Please enter a label for a new piece. Enter "Q" when done. Q The GameBoard -------------------- --- Kin --- Paw --- ---
--- --- Paw Would you like to move a piece? Enter "Y" to move a piece. Y Please enter the piece's row. 0 Please enter the piece's column. 1 Please enter the piece's new row. 0 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.
Grading Criteria for the part 2
05 pts: Every file contains header information 05 pts: adequate comment to explain every function 05 pts: consistent indentation and spacing 05 pts: it compiles 05 pts: The GamePiece default constructor is correct 05 pts: The GamePiece(char*) constructor is correct 05 pts: The method GamePiece::getLabel is correct 05 pts: The method GamePiece::toString is correct 05 pts: The GameBoard constructor is correct 05 pts: The method GameBoard::isSpaceValid is correct 05 pts: The method GameBoard::addPiece is correct 05 pts: The method GameBoard::movePiece is correct 05 pts: The method GameBoard::print is correct
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of