Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

must be in c++ Part A: The Location Type [5 / 60 marks] Create a class named Location to represent a location in the world.

must be in c++

Part A: The Location Type [5 / 60 marks]

Create a class named Location to represent a location in the world. The Location type should have its own interface (.h) and implementation (.cpp) files. Only the World class will look at the fields of the Location type.

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

Location ();

Location (unsigned int row1, unsigned int column1);

bool operator== (const Location& other) const;

It will also have an associated non-member function with the following prototype:

ostream& operator<< (ostream& out, const Location& location);

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

Define Location as a class with public row and column fields. These are both unsigned ints. Since the data fields are not private, Location is not an encapsulated class.

Add a default constructor to the Location type that sets the row to 0 and the column to 0.

Add an initializing constructor to the Location type that takes a row and a column as parameters. The constructor should initialize the corresponding member fields in the Location to these values.

Add a constant equality test operator (operator==) for Locations. It should take a constant reference to another Location as a parameter and return true of the row and the column are both the same. If you are starting the assignment before being taught about operators, it is recommended that you skip this step and the next step for now (or read Section 7 of the online notes on Operator Overloading.)

Add a stream output operator (operator<<) for Locations as a non-member function in the same file. It should take a non-constant reference to an ostream (the type of cout) and a constant reference to a Location as parameters. Then it should print the row and column of the Location to the ostream (using << syntax) and return a non-constant reference to the ostream. Print the row and column in some easy-to-understand format, such as: (row = 2, column = 4). Do not print a newline.

Part B: Convert World to a Class [25 / 60 marks]

Convert the World type to be an encapsulated class, and to use Locations instead of row and column pairs. The 2D node array and the 1D description array will become member variables. The functions associated with the World type will become member functions.

By the end of Part B, your World type will have public member functions with the following prototypes:

World (const string& game_name);

void debugPrint () const;

Location getStart () const;

bool isValid (const Location& location) const;

void printDescription (const Location& location) const;

bool canGoNorth (const Location& location) const;

bool canGoSouth (const Location& location) const;

bool canGoEast (const Location& location) const;

bool canGoWest (const Location& location) const;

Location getNorth (const Location& location) const;

Location getSouth (const Location& location) const;

Location getEast (const Location& location) const;

Location getWest (const Location& location) const;

bool isDeath (const Location& location) const;

bool isVictory (const Location& location) const;

Note that there is no default constructor. The World class will also have private member functions with the following prototypes:

void loadNodes (const string& filename);

void loadDescriptions (const string& filename);

bool invariant () const;

Perform the following steps to create implementations for the functions listed above. Add beautiful comments to World.h for the functions.

Define World as a class with private fields for nodes, descriptions, and description_count. The nodes field is a 2D array of ROW_COUNT by COLUMN_COUNT unsigned ints, just like the old World type. The descriptions field is a 1D array of strings, and description_count is an unsigned int. Add a constant to the World.h file (just before the World class) named MAX_DESCRIPTION_COUNT with a value of 1000 to use as the size of the descriptions array.

The ROW_COUNT and COLUMN_COUNT constants should be placed before the World class. With older compilers, placing all constants outside the class works better than putting them inside the class.

Convert the worldLoadNodes function to a private helper function named loadNodes. It should load the nodes values into the nodes array.

Convert the worldLoadDescriptions function to a private helper function named loadDescriptions. It should store the number of descriptions in the description_count variable. Then it should load that many descriptions into the descriptions array. Assume that the description count in the file will never be larger than MAX_DESCRIPTION_COUNT.

Convert the worldLoadAll function into an initializing constructor.

Convert the worldDebugPrint function to a constant member function named debugPrint. First it should print the values in the nodes array in a grid. Then it should print the description count and the descriptions themselves, separated by blank lines.

Convert the worldFindStart function to a member function named getStart. It should return the player start location using the Location type instead of modifying row and column values passed by reference.

Add a function named isValid that takes a constant reference to a Location as a parameter and returns whether it is a valid location in this World. A location is valid of its row value is less than ROW_COUNT and its column is less than COLUMN_COUNT.

Convert the worldPrintDescription function to a member function named printDescription. It should take a constant reference to a Location as a parameter instead of row and column values. Use an assert and the isValid function to ensure that the location is valid.

Convert the worldCanGoNorth function to a constant member function named canGoNorth. Change it to take a row and column specified as a Location instead of as two unsigned ints. Convert the worldCanGoSouth, worldCanGoEast, and worldCanGoWest functions in an analogous manner. Also convert the isDeath and isVictory functions. Each function should use an assert to make sure that the location is valid.

Add a constant member function named getNorth that takes a constant reference to a Location as a parameter. It should return the Location to the north of the specified location. It should use asserts to ensure that the location parameter is a valid location and that the player can go north from that location. Also add getSouth, getEast, and getWest functions.

Add a class invariant enforced by a private helper function named invariant. The invariant function should take no parameters and return a bool indicating whether the class invariant is true. The class invariant requires three conditions:

description_count <= MAX_DESCRIPTION_COUNT

nodes[r][c] < description_count (for all rows 0 <= r < ROW_COUNT for all columns 0 <= c < COLUMN_COUNT)

descriptions[d] != "" (for all 0 <= d < description_count)

If any condition is false, the invariant function should return false. If all the conditions are true, the function should return true.

Use asserts to check that the invariant function returns true in every appropriate place:

at the end of the constructor ;

at the beginning of every public member function except the constructor and debugPrint.

Note: We dont check the invariant condition in debugPrint so that this function can be used when something is wrong with a World variable. We also dont check the invariant at the end of constant functions since they cannot change anything (it happens that all functions other than the constructor are constant).

Make sure that you have beautiful function comments for all 18 member functions in the World module.

Part C: Convert Item to a Class [18 / 60 marks]

Convert the Item type to be an encapsulated class, and to use Locations instead of row and column pairs. The functions associated with the Item type will become member functions.

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

Item ();

void debugPrint () const;

bool isInitialized () const;

char getId () const;

bool isInInventory () const;

bool isAtLocation (const Location& location) const;

int getPlayerPoints () const;

void printDescription () const;

bool operator< (const Item& other) const;

void init (char id1, const Location& location, int points1, const string& world_description1, const string& inventory_description1);

void reset ();

void moveToInventory ();

void moveToLocation (const Location& location);

The Item class will also have a private member function with the following prototype:

bool invariant () const;

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

Change the Item record (struct) to be a class. Replace the start_row, start_column, current_row, and current_column fields with start_location and current_location fields of the Location type.

Add a default constructor to the Item class. It should initialize id to ID_NOT_INITIALIZED, the starting and current locations with the default constructor for the Location type, is_in_inventory to false, points to 0, and both descriptions to "[Item not initialized]".

Convert the itemDebugPrint function to be a constant member function named debugPrint.

Hint: You can print Locations using the << operator.

Add a constant member function named isInitialized. It should return false if the id field is ID_NOT_INITIALIZED and true otherwise.

Convert the itemInit function to a member function named init. Replace the row and column parameters with a single constant reference to a Location.

Convert the itemIsInInventory, itemGetPlayerPoints, itemPrintDescription, itemReset, and itemMoveToInventory functions to be constant member functions.

Convert the itemIsAtLocation function to a constant member function named isAtLocation that takes a constant reference to a Location as a parameter. Convert the moveToLocation function in an analogous manner.

Hint: The Location type does not have the inequality operator (!=) defined. You can check if two Locations are not equal with the expression: !(location1 == location2)

Add a constant less than comparison operator (operator<) to the Item class. It should take a constant reference to another Item as a parameter and return whether the id for this Item is strictly less than the id for the other Item. You will use this function to sort the items in Assignment 4.

Hint: The less than operator is defined for chars, so you can write id < other.id.

Add a class invariant enforced by a private helper function named invariant, similar to the class invariant in the World class. The class invariant requires that neither description is an empty string.

Use asserts to check that the invariant function returns true at the end of all member functions not declared as const, and at the beginning of each public member function except the constructor and debugPrint.

Part D: Update the main Function [12 / 60 marks]

Adapt your main function to use the new World and Item classes.

Replace the player row and column location variables with a single variable of the Location type.

Update the World-related functions to use dot notation (e.g. my_world.canGoNorth(...) instead of worldCanGoNorth(myWorld, ...) ). They should also use Locations instead of rows and columns. Use the World constructor instead of declaring the World and initializing it later. Also use the getNorth, etc. functions to determine the location to go to, instead of incrementing or decrementing the row and column values.

Update the Item-related functions to use dot notation and Location variables.

Do not access the fields of the Location type anywhere in the main function.

Load a new game world named ghostwood. The _grid and _text files are on the course website. Change the ROW_COUNT and COLUMN_COUNT constants to 10. Comment out the starting message and replace it with: Welcome to Ghostwood Manor! There are rumors of treasure in the old haunted house but many who venture in never come out again. Similarly, comment out the ending message and replace it with: Thank you for playing Ghostwood Manor!

Replace the current items with a new set of nine different items and update the ITEM_COUNT constant.

A scarab beetle with id 's' at location row 0 and column 3, worth -5 points, with world description "There is a black scarab beetle (s) here." and inventory description "A black scarab beetle (s) is crawling up your arm.".

A candlestick with id 'c' at location row 1 and column 1, worth 9 points, with world description "There is a silver candlestick (c) here." and inventory description "You are carrying a silver candlestick (c).".

A key with id 'k' at location row 2 and column 0, worth 3 points, with world description "There is an old iron key (k) here." and inventory description "You have an old iron key (k) in your pocket.".

A tarantula with id 't' at location row 2 and column 9, worth -8 points, with world description "There is a tarantula (t) here." and inventory description "There is a tarantula (t) hanging on your shirt.".

A book with id 'b' at location row 3 and column 4, worth 4 points, with world description "There is a book (b) here with an eye drawn on the cover." and inventory description "You have a book (b) under your arm with an eye drawn on the cover.".

A moth with id 'm' at location row 5 and column 5, worth -2 points, with world description "There is a giant moth (m) sleeping here." and inventory description "A giant moth (m) is perched on your shoulder.".

An amulet with id 'a' at location row 7 and column 9, worth 7 points, with world description "There is a golden amulet (a) here." and inventory description "You are wearing a golden amulet (a).".

A dagger with id 'd' at location row 8 and column 0, worth 1 points, with world description "There is an rune-carved dagger (d) here." and inventory description "You have an rune-carved dagger (d) stuck in your belt.".

A ring with id 'r' at location row 9 and column 6, worth 10 points, with world description "There is a diamond ring (r) here." and inventory description "You are wearing a diamond ring (r).".

Test your game with the test cases on the course website.

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

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions