Question
In Part C, you will create a structured record (struct) to represent a tile, as well as related constants and functions. You will have an
In Part C, you will create a structured record (struct) to represent a tile, as well as related constants and functions. You will have an interface file named Tile.h and an implementation file named Tile.cpp. Each tile will have unsigned integer member fields for the tile owner, genus, and species. We will use a special value to indicate that a tile has no owner. Tile will be an abstract data type (ADT), so client code will only operate on Tile variables using the functions we provide.
By the end of Part C, you will have functions with the following prototypes:
Tile tileCreate (unsigned int genus1, unsigned int species1);
bool tileIsOwner (const Tile& tile);
unsigned int tileGetAffectedPlayer (const Tile& tile,
unsigned int whose_turn);
unsigned int tileGetGenus (const Tile& tile);
unsigned int tileGetSpecies (const Tile& tile);
void tileSetOwner (Tile& tile, unsigned int owner1);
void tileActivate (const Tile& tile, unsigned int whose_turn);
void tilePrint (const Tile& tile);
void tilePrintOwnerChar (const Tile& tile);
void tilePrintGenusChar (const Tile& tile);
void tilePrintSpeciesChar (const Tile& tile);
Perform the following steps:
- In Tile.h, define a record to represent a tile.
- In Tile.h, define unsigned integer constants for the different kinds of tile genus. We will create all the constants for the whole CS 115 course now so that we never have to change them. The constants are GENUS_COUNT, GENUS_MONEY, GENUS_DICE, GENUS_POINTS, and GENUS_BOMB, and they have the respective values 4, 0, 1, 2, and 3.
- Copy in the function prototypes shown above.
- In Tile.cpp, define unsigned integer constant named NO_OWNER to indicate that a tile has no owner. It should have a very large value so that there is no danger of a player having that number.
- Add an implementation for the tileCreate function. First, it should create a local variable of the Tile type. Next, it should set the genus and species of the tile variable based on the parameters. Then, it should set the tile owner to NO_OWNER. Finally, the function should return the tile.
- Add an implementation for the tileIsOwner function that returns false if the tile owner is NO_OWNER and true otherwise.
- Add implementations for the tileSetOwner function that sets the tile owner to owner1.
- Note: The Tile parameter for this function is not const. If it was, we wouldn't be able to change the owner.
- Add implementations for the tileGetGenus and tileGetSpecies functions. They should return the values of the appropriate fields of the Tile parameter.
- Note: These functions will not be used by the game, but they will be used by the test programs.
- Add implementations for the tileGetAffectedPlayer function. If the tile has an owner, it should return the owner. Otherwise, it should return whose_turn.
- Add an implementation for the tilePrintOwnerChar function. If the tile has no owner, it should print a space (' '). Otherwise, it should call the playerGetTileChar function and print the value that function returns.
- Add an implementation for the tilePrintGenusChar function. If the tile genus is GENUS_MONEY, it should print a dollar sign ('$'). If the tile genus is GENUS_POINTS, it should print a star ('*'). Otherwise, it doesn't need to do anything.
- Note: We will print things for the other kinds of genus in later assignments.
- Add an implementation for the tilePrintSpeciesChar function. It should just print the tile species as a number.
- Add an implementation for the tilePrint function to display three characters representing a tile. It should call the tilePrint*Char functions.
- Hint: Remember * means "anything". In this case, there are three tilePrint*Char functions.
- Add an implementation for the tileActivate function to handle what happens when a tile is selected by a dice roll. It should start by determining which player is affected. Suppose that we name the player q. Then your code should check the tile genus. If the tile is a money tile, increase the affected player's money by an amount equal to the tile's species. In other words, increase qs money. If the tile is a points tile, increase that player's points. In other words, increase qs points.
- Hint: Call a Tile function to determine the affected player.
- Hint: Call a Player function to increase a player's money or points. The function also prints out the players money or points.
- Add a precondition to the tileCreate function. It should require that the genus parameter is strictly less than GENUS_COUNT.
- Test your Tile module using the TestTile2.cpp program provided. You will need the TestHelper.h and TestHelper.cpp files.
Part D: Update the Board [5% test program]
In Part D, you will convert the game board to contain tiles instead of ints.
By the end of Part D, the function prototypes will be:
void boardInit (Board board);
const Tile& boardGetAt (const Board board, int row, int column);
void boardSetAt (Board board, int row, int column, const Tile& value, unsigned int owner);
void boardPrint (const Board board);
void boardPrintColumnNameRow ();
void boardPrintBorderRow ();
void boardPrintEmptyRow ();
void boardPrintDataRow (const Board board, int row);
Perform the following steps:
- Add an #include for "Tile.h" in Board.cpp. The function prototypes will need to refer to tiles, so also add it in Board.h.
- Change the Board type so it corresponds to a 2D array of Tiles instead of integers. Change the boardGetAt function to return a constant reference to a Tile. Change the boardSetAt function to take a constant reference to a Tile as the value parameter.
- Change the function implementations for boardGetAt and boardSetAt to match the changes to the prototypes. The boardGetAt function should return a const reference to a Tile. The boardSetAt function should take a const reference to a Tile as a parameter.
- Hint: In boardSetAt, you should return the value directly from the array. Do not create a temporary Tile variable. If you do, the calling function will get a reference to a variable that doesn't exist anymore and Bad Stuff will happen.
- In boardInit, initialize the tiles by calling the tileCreate function. All of the tiles should have genus GENUS_MONEY. The old money value should become the tile species.
- Example: The command to set the board will look something like the following, although it will not use GENUS_INVALID or 47: my_board[here][there] = tileCreate(GENUS_INVALID, 47);
- Change the boardPrintDataRow function to display tiles instead of printing spaces, dollar signs, and numbers. Call a function from the Tile module to display a tile. Print a suitable number of spaces between the tiles. If your board matches the example exactly, you will need to print two spaces between tiles.
- Change the boardSetAt function to also set the tile owner. Add an additional parameter in the interface (.h) and implementation (.cpp) files (if you haven't already). After setting the element of the array, it should call a function from the Tile module to set that tile's owner.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started