Project 1: Operashun - C++ Program to Implement Classic Kids' Game
Grid Class
Project 1: Operashun
You will complete a C++ program that implements a simplified version of a classic kids’ game in which each player tries to remove body parts from a patient.
Grid Class
Body parts within the operating field in the game are represented using a template class named Grid that stores a particular type of value (determine by its template parameter) in each element. Its member functions accept row/col indices starting at zero (0-based indices). The grid may have a different number of rows than columns. Currently, values are stored internally using a statically-allocated (fixed-size) 2D array.
Your task is to change the class to use a dynamically-allocated 2D array…
First, modify the data type of member data to be an appropriate pointer type. Also change ROWS and COLS from constants to variables. Additionally, you must write the following:
- Add parameters to the existing constructor for the grid row and column sizes of the 2D array. Allocate a dynamic 2D array of those sizes and set all elements to the initial value passed (existing parameter). Pass the sizes chosen by the user from the main program.
- Add a destructor to deallocate the dynamic 2D array and reset variables.
- Add a copy constructor and assignment operator to copy the object since it contains a dynamic 2D array.
- replace(): Make a copy of the implicit object (Hint: the this pointer is relevant), replace a certain value with another value (both values are passed) in the copy, and return the copy. Either the copy constructor or assignment operator can be used to make a copy of a Grid. Note that the copy is of an entire Grid object, not just the internal array. Uncomment the call to replace() in the main program.
Main Program
To make the game functional (see Sample Run), write the following in the main program:
- openBodyPart(): Open a body part for surgery within the operating field. I.e., place either a horizontal part (-) or vertical part (|) at positions specified by the parameters. Assume it is a horizontal part if the start/end rows match (return true),a vertical part if the start/end columns match (return true), or invalid otherwise (return false). E.g., if part is from 1,2 to 1,4, then a horizontal part (-) should be placed at 1,2, 1,3, and 1,4. You may assume the positions passed are within the grid and that the start position is to the left/above the end position.
- removeBodyPart(): Attempt to remove a body part from the operating field. Given the passed row/column, remove any body part that runs through that position. E.g., if that position contains a horizontal part (-), then that position and any connected positions (-) to the left or right should be cut out (x).
- noParts(): Return true/false as to whether there are no parts within the operating field, either because no parts were added or because all were cut out. Add a call to noParts() to stop the game.
- Ensure that the row/column entered by the user for removing a part is within the grid. If not, the user should lose their turn. Reorganize the if/else so that redundant code is not needed to handle both when the row/column is out of range and when there simply is no body part at that position.
- Display the highest score and player # with that score before the program ends. Do so by calling function(s) from the C++ Standard Template Library (look up how to use a chosen library function in cplusplus.com). Recall the scores are being stored in a vector. You don’t need to handle 2 players with the same high score specially— just output 1 of them.
Call Grid’s member functions, such as getLoc() and numCols() as needed.
Standards
Adhere to our standards for constants, variables, functions, classes, placement/alignment of code, and comments. In particular, add comments to explain code that is not obvious. Fill in your name and date at the top of source code files you edit.
Your design choices may impact your grade. Avoid unnecessarily complex solutions. You may not change the interface of existing functions provided unless instructed so.
Test your program thoroughly by playing multiple games with various operating fields.
Submission
Complete the C++ program in Visual Studio 2019 (available from microsoft.com). Rename the top-level folder to Project 1 (Operashun) Your Name. Compress that folder in ZIP format and upload it to Canvas. Delete both “x64” or “Debug” folders as well as any hidden “.vs” folder within the project before compressing (keep the .sln file, source code, and other necessary files).
Bonus
You may attempt this bonus once you complete the core program above. First, create a separate project by copying your entire project folder and renaming the folder to Project 1 Bonus (Operashun) Your Name. This will ensure you do not ruin a valid solution to the required parts of the project when you tackle the bonus.
This bonus is to use a map container to keep track of names of body parts. Start by adding the following variable to the main program:
map<pair<int, int>, string> partNames;
It uses a key consisting of a pair of 2 ints (location in grid) and a value of type string.
- When the user enters the location of a body part, also have them enter a name for the part.
- When laying out the part in openBodyPart() [you may alter its parameters], also store the name (value) under each location (key) making up the part. E.g., a part from 1,2 to 1,4 must have its name stored under:
partNames[pair<int, int>(1,2)]
partNames[pair<int, int>(1,3)]
partNames[pair<int, int>(1,4)]
Of course, accessing the map will instead be done with locations in variables.
- If the user correctly removes a body part, output its name by looking it up using its location in the map.