Objective
Extract fields value, damage, ItemType type, and last from items.json and save in the Item struct.
Prerequisites
This project requires that the MUD program from project 5 is working and passing the tests related to movement and viewing the player's inventory.
- Upgraded to C++, alter Makefile, and verify it compiles
- The Mud Shop functionality of this level will be written in C++
- Your program from level 5 has been converted to a C++ program (we just renamed all the .C files to .cpp)
- Use
cdhw
to change to your work area (if accessing via remote terminal).
- create a
shop.h
and shop.cpp
- In the Makefile
- Change all the
.c
extensions to .cpp
- Add
shop.o
to OBJS
- Change the
main.bin
target's compiler from CC
to CXX
, which will use the g++
compiler instead of gcc
- To the bottom of the dependencies list, add
shop.o: shop.h
- Run
make
to verify the conversion was successful.
- If it did not compile because of an undefined reference to your functions, try executing make clean && make. This is because the project is converted from C to C++.
- If it did not compile because of a malloc statement causing an invalid conversion from
void *
,
this is caused by a slight difference between C and C++. C++ is requiring that malloc specifically cast the result
to match the pointer.
- To fix, use casting to change the return type to match the variable being allocated.
Example
// Add a cast to the malloc changing
Room *rooms = malloc(sizeof(Room)*(roomMaxId+1));
// to the following, which now has a (Room*) just after equals sign
Room *rooms = (Room*) malloc(sizeof(Room)*(roomMaxId+1));
- It should now be compiling.
- Create an
enum
in data.h
- Add the following enum to
data.h
typedef enum ItemType {
ITEM_TYPE_NONE=-1,
ITEM_TYPE_GENERAL,
ITEM_TYPE_QUEST,
ITEM_TYPE_POTION,
ITEM_TYPE_WEAPON,
ITEM_TYPE_ARMOR
} ItemType;
- Upgrade Item struct in
data.h
- The Item struct needs some new fields
int value; // the value of the item
int damage; // damage the item does when used as a weapon
ItemType type; // the type of item, general, quest, potion, weapon, armor
bool last; // marks the last item in the items list
- Update
load_json_items
in data.cpp
to load the new fields
- Update
main()
in main.cpp
to print out the new fields
Get the flag by running
tester
and passing all the tests.