CS 115: Assignment 4
Assignment 4
Due March 17, 2021
Overview
In this assignment, you will restructure your program and allow the players to acquire and use extra
dice. If a player has any extra dice stored, he/she will roll one of them as well as the row and column
dice. That extra die can then be substituted for any one of the other dice, giving the player some control
over which cell to roll. After an extra die is used, the player’s count of extra dice is decreased by one.
The game will sort the possible positions and then use a binary search to determine if the one the player
chose is available.
The purpose of this assignment is to give you more practice working with classes, especially with
composition, and with searching and sorting. For Part A, you will refactor your main function to create a
class to represent the game state. For Part B, you will add some overloaded operators for the CellId
type. For Part C, you will add dice tiles. For Part D, you will add a class to handle the player choosing a
cell. For Part E, you will add the extra dice to the game.
Rolling Extra Dice – this section has been rewritten (original version is given below)
For this assignment, the player will be allowed to roll an extra die. Thus, the program will have another
function to print out the dice when an extra die is rolled. For example:
Row XXXXXXXXXXColumn XXXXXXXXXXExtra
+---+ +---+ +---+ +---+ +---+
| 2 | | 3 | | 1 | | 0 | | 0 |
+---+ +---+ +---+ +---+ +---+
When an extra die is rolled, it can be substituted for the first row die, the second row die, the first
column die, or the second column die, or it can be not substituted for any die. Each combination yields
a cell on the board. Continuing the above example:
Here, row 2 + 3 = 5 and column 1 + 0 = 1 were rolled, so the cell that would be chosen would
normally be F1. However, using the extra die (which is 0), some other cells can be specified. By
eplacing the first row die with the extra die, the row could be changed to 0 + 3 = 3 (which gives cell
D1). By replacing the second row die with the extra die, the row could be changed to 2 + 0 = 2 (cell
C1). By replacing the first column die with the extra die, the column could be changed to 0 + 0 = 0
(cell F0). However, if the second column die (which was already 0) was replaced with the extra die
(which is 0), the column would again be 1 + 0 = 1 (cell F0). If no replacement is made, the cell
would still be F1. Thus, the available cells are C1, D1, F0, and F1.
In theory, it seems there would be five cells the player can choose from because the four original dice
specify a cell and each substitution of the extra die for one of the four original dice yields a cell. As
shown by the above example, however, some of these cells may be duplicates, and we want to avoid
showing them to the player twice. So there will be at least one and at most five available cells.
The game should construct a list of available cells, print this list, and allow the player to choose which of
the available cells to use as the roll for this round. When the player enters a cell, the game will use
inary search to determine if it is in the list of available cells. If the cell entered by the player is on the
list, the game will activate that cell. Otherwise, the player will have to choose a cell again.
Here are some more details on constructing a list of available cells. The first cell to insert in the list of
available cells is the cell co
esponding to the original rolls of the row and column dice. Then if an extra
die was rolled, the game attempts to insert four more cells into the list of available cells. Whenever an
attempt is made to insert a cell, the game will first perform a linear search on the cu
ent list to see if
that cell is already present. If it is not present, the cell will be added, and if it is present, it will not be
added because it is a duplicate. Then the game will use either insertion or selection sort (programmer's
choice) to sort the available cells.
Rolling Extra Dice – original wording
When an extra die is rolled, it can be substituted for either of the row dice, either of the column dice, or
not substituted. Each combination yields a cell on the board. In theory, it seems there would be five
cells the player can choose from. In practice, some of these may be duplicates, and we want to avoid
showing them to the player twice. So there will be at least one and at most five available cells. The
game should thus print the available cells and allow the player to choose which one to select. If the cell
is on the list, the game will activate that cell. Otherwise, the player will have to choose a cell again.
The game will construct a list of available cells. Whenever a cell is to be inserted, the game will first
perform a linear search on the list to see if that cell is already there. If it is not, the cell will be added,
and if it is, it will not be added. Then the game will use either insertion or selection sort (programmer's
choice) to sort the available cells. After the list of available cells has been prepared, the game will print
it and ask the player to enter which cell to roll. When the player enters a cell, the game will use binary
search to determine if it is in the available cells list.
The program will have another function to print out the dice when an extra die is rolled. For example:
Row XXXXXXXXXXColumn XXXXXXXXXXExtra
+---+ +---+ +---+ +---+ +---+
| 2 | | 3 | | 1 | | 0 | | 0 |
+---+ +---+ +---+ +---+ +---+
Here, row 2 + 3 = 5 and column 1 + 0 = 1 were rolled, so the cell that would be chosen would
normally be F1. However, using the extra die (which rolled 0), three other cells are possible. The row
could be changed to 0 + 3 = 3 (cell D1) or to 2 + 0 = 2 (cell C1). Alternately, the column could
e changed to 0 + 0 = 0 (cell F0). If the extra die replaced the second column die (which was
already 0), the column would still be 1 + 0 = 1 (cell F0). If no replacement is made, the cell would
still be F1. Thus, the available cells are C1, D1, F0, and F1.
Requirements
Start by creating a new project (or new folder, etc.) for Assignment 4. Do not just modify Assignment 3.
If you are using Visual Studio, create a new project and copy in the code files (.h and .cpp) from
Assignment 3. Do not ever copy a Visual Studio project. If you do, it will get all mixed up between the
two of them and Bad Things will happen.
Part A: The Game Class [25% = 20% output + 5% code]
In Part A, you will refactor the main function to move most of the functionality into a new Game class.
A Game will have two member variables, one of the Board type and one of the AvailableTiles
type.
Warning: Your main function must use the Game class. If it does not, you will receive 0/25% for Part A.
By the end of Part A, your Game class will have the following public member functions:
ï‚· Game ();
ï‚· void printState (unsigned int whose_turn) const;
ï‚· void handleDiceRoll (unsigned int whose_turn);
ï‚· bool puchaseTile (unsigned int whose_turn);
Perform the following steps:
1. Start by making a copy of your Main.cpp file and name it Game.cpp. In Game.cpp,
comment out the main function using /* */. Throughout Part A, as you are writing new
functions for the Game class, gradually move parts of the code from the commented-out main
function into the new functions in the Game class. As you implement the Game functions, you
can simultaneously replace the co
esponding code in Main.cpp with a call to the new
function (or you can perform all the replacements in step 7).
ï‚· Note: The program likely will not compile co
ectly until you finish step 7.
2. Add the Game.h file. Declare the Game class with appropriate member variables and copy in
the function prototypes.
ï‚· Suggestion: You may want to declare a variable of the Game type in the main function
now, rather than waiting for step 7.
3. In Game.cpp, add an implementation for the default constructor. The Game constructor
should explicitly call the default constructors for the Board and the AvailableTiles
member variables using initializer lists (":" notation).
ï‚· Reminder: This assignment uses composition rather than inheritance. Thus, the items
listed after the colon (":") are the names of member variables of the Game