Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment 2 Due February 6, 2018 A simple exploration game such as the one in Assignment 1 may be fun for a short time, but

Assignment 2

Due February 6, 2018

A simple exploration game such as the one in Assignment 1 may be fun for a short time, but the lack of challenges limits its appeal. The most common solution to this is to include obstacles in the world that can only be overcome in a specific way. Failure to overcome an obstacle can have varying penalties, ranging from inability to continue (the player cant get past the locked gates of the castle) to automatic loss of the game (the player is killed by the castle guards). One of the simplest types of obstacle is one that the player can only pass after acquiring a specific item (such as the key to the castle) earlier in the game. In this assignment, you will make an encapsulated C++ record representing an item for a game similar to the one in Assignment 1. There will be no actual obstacles in this assignment; those are in Assignment 6.

Each item will be identified by a single-character id, such as 'a' for apple and 'b' for boat. Each item can be in the player's inventory or at any location in the world. Whenever the player is at a location, the game will print a list of any items there. The player will have new commands to take an item ('t'), leave an item ('l'), and display the inventory ('i').

The purpose of this assignment is to ensure that you understand how to use C++ records, including passing records as parameters, and how a record can be used to implement an abstract data type. For Part A, you will develop an Item module to represent a single item in the game. For Part B, you will thoroughly test your Item module. For Part C, you will incorporate your tested module into your game program. At that time, we will create an array of items.

(If you are using Visual Studio, you must start by creating a new project for Assignment 2. You must NOT copy the whole folder including the .sln file or massive confusion results!)

Copy the code and data files of your Assignment 1. Do not just modify Assignment 1.

Part A: The Item Module Defining the Item Type [20 / 50 marks]

Create a type named Item to represent an item in the world. Put the Item type and its associated constants and functions in its own module with a header file named Item.h and an implementation file named Item.cpp.

The Item type should be treated as if it is encapsulated, i.e., do not access any of its variables outside the Item.cpp file. Also add comments for each function, in the style shown in the class notes for the Program Organization lecture (hereafter called "beautiful comments").

You may want to test your Item type as you develop it, by doing Part A and Part B simultaneously.

By the end of Part A, your Item type will have functions with the following prototypes:

void itemInit (Item& item, char id1, unsigned int row1, unsigned int column1, int points1, const string& world_description1, const string& inventory_description1);

void itemDebugPrint (const Item& item);

char itemGetId (const Item& item);

bool itemIsInInventory (const Item& item);

bool itemIsAtLocation (const Item& item, unsigned int row, unsigned int column);

int itemGetPlayerPoints (const Item& item);

void itemPrintDescription (const Item& item);

void itemReset (Item& item);

void itemMoveToInventory (Item& item);

void itemMoveToLocation (Item& item, unsigned int row, unsigned int column);

Perform the following steps to create implementations for the functions listed above.

Define Item as a record (struct) with the following fields: id, start_row, start_column, current_row, current_column, is_in_inventory, points, world_description, and inventory_description. The first of these, id, is a char and will be different for each item. The rows and columns are unsigned ints and is_in_inventory is a bool. The descriptions are strings and points is an int (not an unsigned int).

Add a char constant named ID_NOT_INITIALIZED with a value of '\0'.

Add a function named itemInit to initialize an Item. As parameters, it should take a non-constant reference to an Item, a char representing the id, two unsigned ints representing a location (row and column), an int representing the points the Item is worth, and two strings representing descriptions for when in the world and when in the player inventory. Initialize the item fields to these values. Both the starting and current locations should be set to the row and column provided, and the item should be set to not be in the inventory. Use asserts to ensure that the id parameter is not ID_NOT_INITIALIZED and that neither description parameter is an empty string.

Add a function named itemDebugPrint that takes a constant reference to an Item as a parameter and then prints all fields of the item. Each field should be printed on its own line with the format field_name: value. Print single quotes ('s) around the id and double quotes ("s) around the descriptions. Example output would be:

id: 'a'

start_row: 4

start_column: 2

current_row: 1

current_column: 7

is_in_inventory: 0

points: 5

world_description: "There is an apple (a) here."

inventory_description: "You are carrying an apple (a)."

Add a function named itemGetId that takes a constant reference to an Item as a parameter and returns the item's id value.

Add a function named itemIsInInventory that takes a constant reference to an Item as a parameter and returns whether the item is in the player inventory.

Add a function named itemIsAtLocation that takes a constant reference to an Item and a row and column representing a location as parameters. The function should return whether the item is at that location. An item in the player's inventory is not at any location, no matter what its row and column values are.

Add a function named itemGetPlayerPoints that takes a constant reference to an Item as a parameter and returns how many points the player current has from the item. If the item is in the player's inventory, the function should return the value of points field. If the item is not in the player's inventory, the function should return 0.

Add a function named itemPrintDescription that takes a constant reference to an Item as a parameter. If the item is in the player's inventory, the function should print the inventory description; otherwise it should print the world description.

Add a function named itemReset that takes a non-constant reference to an Item as a parameter. It should move the item to its starting location and set it to not be in the player inventory.

Add a function named itemMoveToInventory that takes a non-constant reference to an Item as a parameter and moves the item to the player inventory.

Add a function named itemMoveToLocation that takes a non-constant reference to an Item and a row and column representing a location as parameters. It should move the item to the specified location and set it to not be in the player inventory.

Part B: Testing the Item Type [15 / 50 marks]

Write a separate main function to test your Item type. It should have its own file named something like TestItem.cpp. Nothing in the TestItem.cpp file should use the fields of the item directly. You should compile TestItem.cpp and Item.cpp together and run the program after every step of Part B.

Write a main function that creates an apple Item with id 'a', starting row 4 and starting column 2, points value 5, world description "There is an apple (a) here.", and inventory description "You are carrying an apple (a).". Print the apple values using the itemDebugPrint function.

Write a test function named printItem that takes a constant reference to an Item as a parameter. This function should call various item functions (e.g., itemGetId) to print labelled information about the item. First print the item id; explicitly show a single quote (') before and after it. Then print whether the item is in the player inventory, and whether it is at each of the following locations: row 0 and column 0, row 4 and column 2, row 1 and column 7, and row 9 and column 0, and row 6 and column 3. Then, print the current number of points that the player gets from the item, followed by the current description. Finally, print a blank line. Example output would be:

Item id: 'a'

Is item in inventory: 0

Is item at r = 0, c = 0: 0

Is item at r = 4, c = 2: 1

Is item at r = 1, c = 7: 0

Is item at r = 9, c = 0: 0

Is item at r = 6, c = 3: 0

Player points: 0

There is an apple (a) here.

Add a command in main to print the apple using printItem after printing it with itemDebugPrint.

Move the apple to the inventory (using a function from Part A) and print it with printItem. Move it to row 1 and column 7 (using another function from Part A), to row 9 and column 0, and then back to the inventory. Print it with printItem after each move. Finally reset the apple and print it again.

Create a boat Item with id 'b', starting row 6 and starting column 3, points value -10, world description "There is a boat (b) here.", and inventory description "You are in a boat (b).". Print out the boat with the itemDebugPrint function and the printItem function. Then print the apple with the printItem function. The apple shouldn't have changed.

Move the boat to location row 0 and column 0 and print out both the boat and the apple again using the printItem function. Then move the boat to the inventory and print the boat and the apple again.

Create a cat Item with id 'c', starting row 8 and starting column 9, points value 0, world description "There is a cat (c) hiding somewhere near here.", and inventory description "". It should cause an assert error. After testing the cat, comment it out.

Part C: Add Items to the Game [15 / 50 marks]

Improve your game to handle items.

Add a constant named ITEM_COUNT to your main function with a value of 5. Add an array of ITEM_COUNT Items and initialize them as follows:

A girl named Alice with id 'a' at location row 0 and column 6, worth 1 point, with world description "You see Alice (a) here, trying to read a very small compass." and inventory description "Somewhere nearby, you hear Alice (a) jabbering about directions.".

A boy named Charlie with id 'c' at location row 0 and column 6, worth 1 point, with world description "You see Charlie (c) lying half-buried in the drifting snow here." and inventory description "Behind you, Charlie (c) is dragging himself through the snow.".

A girl named Emma with id 'e' at location row 3 and column 5, worth 1 point, with world description "Young Emma (e) is crouched down here, out of the wind." and inventory description "Young Emma (e) is trying to use you to block the wind.".

A boy named David with id 'd' at location row 4 and column 1, worth 1 point, with world description "David (d) is using a stick to write in the snow here." and inventory description "David (d) is dashing this way and that, despite the weather.".

Twin boys with id 'b' at location row 5 and column 8, worth 2 points, with world description "The twins, Benny and Bobby (b), are huddled together here." and inventory description "The twins, Benny and Bobby (b), are huddled behind you.".

When you print the node description, also print the description of any items at the player location.

Hint: Write a helper function to find every item at a location and print its description.

Add a command 't' to take an item. When the player enters the take command, the game should print "Take what? " without a newline and read in another line of player input. If the first character of the line is the id of an item at the player location, that item should be moved to the inventory. Otherwise, the game should print "Invalid item".

Hint: Remember that if the player enters a blank line (empty string), it does not have a first character. In this case, your program should print "Invalid item" and not crash.

Hint: You can declare variables inside a switch statement if you put curly braces {} around the part of the code where they are used. Put the opening brace '{' after the case statement and the closing brace '}' before the break statement.

Add a command 'l' to leave an item, similar to the take command. The game should print "Leave what? " before reading a line of player input. If the first character of the line is the id of an item at the inventory, that item should be moved to the player location. Otherwise, the game should print "Invalid item".

Add a command 'i' to display the inventory. The game should print the description for each item in the player inventory.

When the game ends, print the player score. The player score is the sum of the points that the player receives for each item. A suitable message would be "In this game you scored ??? points.", where ??? is the player score.

Submission

Submit a complete copy of your source code. You should have the following files:

o Main.cpp

o World.h

o World.cpp

o Item.h

o Item.cpp

o TestItem.cpp

Don't submit a compiled version

Don't submit intermediate files, such as:

o Debug folder

o Release folder

o ipch folder

o *.ncb, *.sdf, or *.db files

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the purpose of afragment of a face relief By ancient egypt

Answered: 1 week ago

Question

Which form of proof do you find most persuasive? Why?

Answered: 1 week ago