Question: Part C: The AvailableTiles Class [40% = 26% test program + 4% code] In Part C, you will create and document a class named AvailableTiles.

Part C: The AvailableTiles Class [40% = 26% test program + 4% code]

In Part C, you will create and document a class named AvailableTiles. The class will keep track of the five tiles that are available for the players to buy and the cost for each. At the start, five tiles will be chosen randomly. Whenever a tile is removed, the higher-numbered tiles will be moved down and a random new tile will be added at the end. For example, suppose we refer to the available tiles as I, J, K, L, M. Also suppose that tile K was removed and a new tile (referred to as N) was picked randomly to be added. Then the revised list of available tiles would be I, J, L, M, N. When the tiles are moved, their costs move with them.

Note: A tile is actually a three-part object with owner, genus, and species member variableswe are referring to them by single letters for convenience here.

Note: When you remove the tile from the available tiles list, a new tile will be placed in the last position of the list.

By the end of Part C, your AvailableTiles class will have the following public member functions:

AvailableTiles ();

void print () const;

int getCost (unsigned int index) const;

const Tile& getTile (unsigned int index) const;

void replaceAt (unsigned int index);

You will also have the following private member function:

void setRandomTile (unsigned int index);

Perform the following steps:

In AvailableTiles.h, declare an unsigned integer constant named AVAILABLE_TILE_COUNT for the number of available tiles. It should have value 5. Since it is constant, the number of available tiles never changes.

Create the AvailableTiles class. It should have member variables that store the available tiles and tile costs. One way to do this is to have two arrays of the same length with the first array of the Tile type and the second array of the int type.

Copy in the function prototypes.

In AvailableTiles.cpp, write the implementation for the setRandomTile function. You should construct a tile and copy it (with an assignment operator =) into the specified position in the available tiles list. You should also set the corresponding cost. The owner member variable of the tile will be set to NO_OWNER by the tile constructor. In this assignment, all the available tiles will be points tiles. Choose a random value from 1, 2, or 3, with an equal chance of each, and name it P for points. The tile species should be P and the tile cost should be P * P. Give the function a precondition that requires the index parameter to be strictly less than the number of available tiles. Enforce the precondition with an assert.

Hint: You can begin by declaring local unsigned integer variables for the genus and the species. You may also want a variable named P.

o The genus variable should be initialized to a symbolic constant indicating the tile is of the points kind.

o The species variable should be initialized to a random value (named P in the above description) between 1 and 3. You should use the rand function.

Warning: Do not call srand here. The random number generator was already initialized once at the beginning of the program and it does not need to be initialized again.

Hint: One way to construct a tile is to declare a new variable of the Tile type and call the constructor that takes two parameters, one for the genus and one for the species. For example to declare a string and call a constructor with one parameter, you might type: string s1(Hello); Here you need to declare a Tile variable rather than a string variable and you need to pass in two parameters.

Hint: The index parameter tells you which position you should change in the two member arrays in the AvailableTiles class.

Hint: You can assign objects, such as Tiles, with the assignment statement, just like any other variable. Sometimes this is referred to as copying the objects. For example: MyClass c1; MyClass c2; c1 = c2; // this is legal

Write the implementation for the default constructor. It should use a for loop to call setRandomTile for each tile.

Write the implementation for the print function. It should print one line for each tile, showing the tile index, the tile itself, and the tile cost with a dollar sign ('$'). One acceptable format is " N: TTT $C", where N is the index, TTT is the tile, and C is the cost.

Example: If the index is 3, the tile is unowned (" "), the tile has points as its genus ("*"), the tile is worth 2 points ("2"), and the cost is 4, then the output could be (the quotation marks would not appear in the output):

" 3: *2 $4"

Reminder: A function for printing a tile in a three-character format, here shown as TTT, was developed in Assignment 2 and converted to a member function in Part A above.

Hint: You may need a series of statements to print the information, where some statement(s) use cout and other statement(s) call your functions.

Write the implementations for the getCost and getTile functions. Both functions should have a precondition that requires the index parameter to be strictly less than the number of tiles. Enforce the preconditions with asserts.

Write the implementation for the replaceAt function. It should move the tiles that have higher indexes one spot down the array and call a function to set the last tile position in the list to a random tile. Add an assert-enforced precondition that requires the index parameter to be strictly less than the number of tiles.

Reminder: Also move the costs when you move the tiles.

Reminder: You can assign objects, such as Tiles, with the assignment.

Example: Suppose the user selects position 2. Then copy the tile from position 3 into position 2 (using an assignment statement =) and copy the cost from position 3 to position 2. Then copy the tile from position 4 to position 3 and copy the cost from position 4 to position 3. Then set position 4 to be a random tile.

Example: Suppose the user selects position 0. Then copy the tile from position 1 into position 0 (also cost), then copy the tile from position 2 to position 1 (also cost), then copy the tile from position 3 to position 2 (also cost), then copy the tile from position 4 to position 3 (also cost). Then set position 4 to be a random tile.

Example: Suppose the user selects position 4. None of the tiles need to be copied. Set position 4 to be a random tile.

Hint: You should be able to write a for loop that does any of the above examples.

Test your AvailableTiles module using the TestAvailableTiles3.cpp program provided.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!