Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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...

1 answer below »
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
Answered Same Day Mar 17, 2021

Solution

Sonu answered on Mar 17 2021
146 Votes
solution4/AvailableTiles.cpp
solution4/AvailableTiles.cpp
  AvailableTiles.cpp
#include #include #include #include #include "Tile.h"
#include "AvailableTiles.h"
using namespace std;
AvailableTiles :: AvailableTiles ()
{
    for(unsigned int i = 0; i < AVAILABLE_TILE_COUNT; i++)
    {
        setRandomTile(i);
    }
}
void AvailableTiles :: print () const
{
    cout 
 "Available tiles:" 
 endl;
    for(unsigned int i = 0; i < AVAILABLE_TILE_COUNT; i++)
    {
        cout 
 "  " 
 i 
 ": ";
        tiles[i].print();
        cout 
 "   $" 
 costs[i] 
 endl;
    }
}
int AvailableTiles :: getCost (unsigned int index) const
{
    assert(index < AVAILABLE_TILE_COUNT);
    return costs[index];
}
const Tile& AvailableTiles :: getTile (unsigned int index) const
{
    assert(index < AVAILABLE_TILE_COUNT);
    return tiles[index];
}
void AvailableTiles :: replaceAt (unsigned int index)
{
    assert(index < AVAILABLE_TILE_COUNT);
    
 shift tiles in a
ay
    for(unsigned int i = index + 1; i < AVAILABLE_TILE_COUNT; i++)
    {
        assert(i >= 1);
        tiles[i - 1] = tiles[i];
        costs[i - 1] = costs[i];
    }
    setRandomTile(AVAILABLE_TILE_COUNT - 1);
}
void AvailableTiles :: setRandomTile (unsigned int index)
{
    assert(index < AVAILABLE_TILE_COUNT);
    unsigned int points = 1 + rand() % 3;
    tiles[index] = Tile(GENUS_POINTS, points);
    costs[index] = points * points;
}
solution4/AvailableTiles.h
AvailableTiles.h
A module to keep track of the tiles are available to buy.
#pragma once
#include "Tile.h"
const unsigned int AVAILABLE_TILE_COUNT = 5;
AvailableTiles
A class to keep track of which tiles are available to buy
and their costs.
class AvailableTiles
{
public:
Default Constructo
Purpose: To create an AvailableTiles containing random
tiles.
Parameter(s): N/A
Precondition(s): N/A
Returns: N/A
Side Effect: A new AvailableTiles is created. Each tile is
chosen randomly.
    AvailableTiles ();
print
Purpose: To list the tiles for this AvailableTiles.
Parameter(s): N/A
Precondition(s): N/A
Returns: N/A
Side Effect: The tiles are printed, along with their indexes
and costs.
    void print () const;
getCost
Purpose: To determine the cost of the specified tile.
Parameter(s):
<1> index: Which tile
Precondition(s):
<1> index < AVAILABLE_TILE_COUNT
Returns: The cost of the tile with index index.
Side Effect: N/A
    int getCost (unsigned int index) const;
getTile
Purpose: To retreive the specified tile.
Parameter(s):
<1> index: Which tile
Precondition(s):
<1> index < AVAILABLE_TILE_COUNT
Returns: The tile with index index.
Side Effect: N/A
    const Tile& getTile (unsigned int index) const;
replaceAt
Purpose: To replace the specified tile with a random new
tile.
Parameter(s):
<1> index: Which tile
Precondition(s):
<1> index < AVAILABLE_TILE_COUNT
Returns: N/A
Side Effect: The tile with index index is removed from this
AvailableTiles. The tiles with higher indexes
are moved down to fill the gap. Then a random
new tile is added at the end.
    void replaceAt (unsigned int index);
private:
    void setRandomTile (unsigned int index);
private:
    Tile tiles[AVAILABLE_TILE_COUNT];
    int costs[AVAILABLE_TILE_COUNT];
};
solution4/Board.cpp
solution4/Board.cpp
  Board.cpp
#include #include #include #include "BoardSize.h"
#include "CellId.h"
#include "Tile.h"
#include "Board.h"
using namespace std;
Board :: Board ()
{
    for(int r = 0; r < BOARD_SIZE; r++)
    {
        for(int c = 0; c < BOARD_SIZE; c++)
        {
            int r2 = abs(r - BOARD_SIZE / 2);
            int c2 = abs(c - BOARD_SIZE / 2);
            int larger = r2;
            if(c2 > r2)
                larger = c2;
            int money = 4 - larger;
            board[r][c] = Tile(GENUS_MONEY, money);
        }
    }
}
void Board :: print () const
{
    printColumnNameRow();
    printBorderRow();
    for(int r = 0; r < BOARD_SIZE; r++)
    {
        printEmptyRow();
        printDataRow(r);
    }
    printEmptyRow();
    printBorderRow();
    printColumnNameRow();
}
const Tile& Board :: getAt (const CellId& cell_id) const
{
    assert(isOnBoard(cell_id));
    return board[cell_id.row][cell_id.column];
}
void Board :: setAt (const CellId& cell_id,
                     const Tile& value,
                     unsigned int owner)
{
    assert(isOnBoard(cell_id));
    assert(!value.isOwner());
    board[cell_id.row][cell_id.column] = value;
    board[cell_id.row][cell_id.column].setOwner(owner);
}
void Board :: printColumnNameRow () const
{
    cout 
 "    ";
    for(int c = 0; c < BOARD_SIZE; c++)
    {
        char label = getColumnName(c);
        cout 
 "   " 
 label 
 " ";
    }
    cout 
 endl;
}
void Board :: printBorderRow () const
{
    cout 
 "   +";
    for(int c = 0; c < BOARD_SIZE; c++)
    {
        cout 
 "-----";
    }
    cout 
 "--+" 
 endl;
}
void Board :: printEmptyRow () const
{
    cout 
 "   |";
    for(int c = 0; c < BOARD_SIZE; c++)
    {
        cout 
 "     ";
    }
    cout 
 "  |" 
 endl;
}
void Board :: printDataRow (int row) const
{
    assert(row >= 0);
    assert(row <  BOARD_SIZE);
    char label = getRowName(row);
    cout 
 " " 
 label 
 " |";
    for(int c = 0; c < BOARD_SIZE; c++)
    {
        cout 
 "  ";
        board[row][c].print();
    }
    cout 
 "  | " 
 label 
 endl;
}
solution4/Board.h
Board.h
A module to represent the game board.
#pragma once
#include "BoardSize.h"
#include "CellId.h"
#include "Tile.h"
Board
A class to represent the game board. A Board behaves as a
2D a
ay of Tiles. When created, the board is filled with
unowned money tiles.
class Board
{
public:
Default Constructo
Purpose: To create a Board containing the starting tiles.
Parameter(s): N/A
Precondition(s): N/A
Returns: N/A
Side Effect: The new Board is initialized with money tiles.
The tiles closer to the center have large
money values.
    Board ();
print
Purpose: To display a Board.
Parameter(s): N/A
Precondition(s): N/A
Returns: N/A
Side Effect: This Board is printed to standard output
(cout). The tile owners, genuses, and species
are displayed as are the row and column names.
    void print () const;
getAt
Purpose: To retreive the Tile in the specified cell.
Parameter(s):
<1> cell_id: Which cell
Precondition(s):
<1> isOnBoard(cell_id)
Returns: The Tile in cell cell_id.
Side Effect: N/A
    const Tile& getAt (const CellId& cell_id) const;
setAt
Purpose: To change the Tile in the specified cell.
Parameter(s):
<1> cell_id: Which cell
<2> value: The new tile
<3> owner: The owner for the new tile
Precondition(s):
<1> isOnBoard(cell_id)
<2> !value.isOwner()
Returns: N/A
Side Effect: Cell cell_id is set to have a tile with the
same genus and species as value, and with owne
owner.
    void setAt (const CellId& cell_id,
     const Tile& value,
     unsigned int owner);
private:
    void printColumnNameRow () const;
    void printBorderRow () const;
    void printEmptyRow () const;
    void printDataRow (int row) const;
private:
    Tile board[BOARD_SIZE][BOARD_SIZE];
};
solution4/BoardSize.cpp
solution4/BoardSize.cpp
  BoardSize.cpp
#include "BoardSize.h"
char getRowName (int row)
{
    return 'A' + row;
}
char getColumnName (int column)
{
    return '0' + column;
}
solution4/BoardSize.h
BoardSize.h
A module to store the size of the game board.
#pragma once
const int BOARD_SIZE = 7;
const int BOARD_CELL_COUNT = BOARD_SIZE * BOARD_SIZE;
char getRowName (int row);
char getColumnName (int column);
solution4/CellChooser.cpp
#include "CellChooser.h"
#include "CellId.h"
#include #include
its/stdc++.h
using namespace std;
const CellId SPECIAL_CELL = toCellId(-1,-1);
const int MAX = 9999999;
CellChooser::CellChooser (int row1_roll, int row2_roll, int column1_roll, int column2_roll, int extra_roll, bool is_extra){
    count = 0;
    calculateAllAvailable(row1_roll, row2_roll, column1_roll, column2_roll,extra_roll, is_extra);
    selectionSort();
    if(count == 1){
        cellId = cellIds[0];
    }
    else{
        cellId = SPECIAL_CELL;
    }
}
ool CellChooser::isChosen(){
    assert(isInvariantTrue ());
    
    if(cellId == SPECIAL_CELL)
        return false;
    else
        return true;
        
    assert(isInvariantTrue ());
}
CellId CellChooser::getChosen(){
    assert(isInvariantTrue ());
    assert(isChosen());
    assert(isInvariantTrue ());
    return cellId;
    
}
void CellChooser::printAvailable() const{
    assert(isInvariantTrue ());
    cout
"Available cells:";
    for(int i = 0;i        cout
" "
cellIds[i];
    }
    cout
endl;
    assert(isInvariantTrue ());
}
unsigned int CellChooser::linerSearch (const CellId& cell_id) const{
    for(unsigned int i = 0;i        if(cell_id == cellIds[i])
            return i;
    }
    return MAX;
}
void CellChooser::addAvailableCell(const CellId& cell_id){
    assert(isOnBoard(cell_id));
    if(linerSearch(cell_id) == MAX){
        cellIds[count++] = cell_id;
    }
}
void CellChooser::calculateAllAvailable (int row1_roll, int row2_roll, int column1_roll, int column2_roll, int extra_roll, bool is_extra){
    addAvailableCell(toCellId(row1_roll + row2_roll, column1_roll + column2_roll));
    if(is_extra){
        addAvailableCell(toCellId(extra_roll + row2_roll, column1_roll + column2_roll));
        addAvailableCell(toCellId(row1_roll + extra_roll, column1_roll + column2_roll));
        addAvailableCell(toCellId(row1_roll + row2_roll, extra_roll + column2_roll));
        addAvailableCell(toCellId(row1_roll + row2_roll, column1_roll + extra_roll));
    }
}
void CellChooser::selectionSort(){
    for(int i = 0;i        CellId cell = cellIds[i];
        int k = i;
        for(int j = i+1;j            if( cellIds[j] < cell){
                cell = cellIds[j];
                k = j;
            }
        }
        cellIds[k] = cellIds[i];
        cellIds[i] = cell;
    }
    printAvailable();
}
void CellChooser::addCellIds(const CellId cells[], unsigned int cell_count){
    assert(isInvariantTrue ());
    for(int i = 0;i        addAvailableCell(cells[i]);
    }
    selectionSort();
}
unsigned int CellChooser:: binarySearch (const CellId& cell_id) const{
    int l = 0;
    int h = count-1;
    while(l <= h){
        int m = (h + l) / 2;
        if(cellIds[m] == cell_id){
            return m;
        }
        else if(cellIds[m] < cell_id){
            l = m+1;
        }
        else{
            h = m-1;
        }
    }
    return MAX;
}
ool CellChooser::isAvailable(const CellId& cell_id) const{
    assert(isInvariantTrue ());
    
    if(binarySearch(cell_id) == MAX){
        return false;
    }
    return true;
    assert(isInvariantTrue ());
}
void CellChooser::chooseAvailable(const CellId& cell_id){
    assert(isAvailable(cell_id));
    assert(isInvariantTrue ());
    cellId = cell_id;
    
}
ool CellChooser:: isInvariantTrue () const{
    if(count == 0)
        return false;
    if(count > BOARD_CELL_COUNT)
        return false;
    for(int i = 0;i        if(!isOnBoard(cellIds[i]))
            return false;
    }
    for(int i = 0;i        if(cellIds[i+1] < cellIds[i])
            return false;
    }
    return true;
}
int CellChooser::getSize(){
    return count;
}
solution4/CellChooser.h
#pragma once
#include "BoardSize.h"
#include "CellId.h"
class CellChooser{
    private:
        int count;
        CellId cellIds[2*BOARD_CELL_COUNT];
        CellId cellId;
        
    public:
        CellChooser (int row1_roll, int row2_roll, int column1_roll, int column2_roll, int extra_roll, bool is_extra);
        bool isChosen();
        CellId getChosen();
        void printAvailable() const;
        void addAvailableCell(const CellId& cell_id);
        void addCellIds (const CellId cells[], unsigned int cell_count);
        bool isAvailable (const CellId& cell_id) const;
        void chooseAvailable (const CellId& cell_id);
        int getSize();
        
    private:
        void selectionSort();
        unsigned int binarySearch (const CellId& cell_id) const;
        unsigned int linerSearch (const CellId& cell_id) const;
        bool isInvariantTrue () const;
        void calculateAllAvailable (int row1_roll, int row2_roll, int column1_roll, int column2_roll, int extra_roll, bool...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here