Question: 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
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 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 CYour program from level has been converted to a C program we just renamed all the C files to cppUse cdhw to change to your work area if accessing via remote terminalcreate a shop.h and shop.cppIn the Makefile
Change all the c extensions to cppAdd shop.o to OBJSChange the main.bin target's compiler from CC to CXX which will use the g compiler instead of gccTo 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 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 mallocsizeofRoomroomMaxId; to the following, which now has a Room just after equals sign Room rooms Room mallocsizeofRoomroomMaxId;
It should now be compiling.
Create an enum in data.h
Add the following enum to data.h
typedef enum ItemType ITEMTYPENONE ITEMTYPEGENERAL, ITEMTYPEQUEST, ITEMTYPEPOTION, ITEMTYPEWEAPON, ITEMTYPEARMOR 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 loadjsonitems in data.cpp to load the new fields
value and damage will use extractinttype will need a new function that equates general, quest, potion, weapon, armor to their enum counterparts.last is used to identify the last record in the array, so it will be false for all records except the last with an id One approach for setting last would be to track a lastValidItemId
If you are not a student in the class, then don't use last here, use a hardcoded macro variable with the value called MAXJSON
Each time the code finds an Id then set the lastValidItemIdAfter the loop, set the last value for the item at the index of lastValidItemId to true
Update main in main.cpp to print out the new fields
Update the printf for the items
Add the new data for value, damage, type, last
This should result in item looking like this:
Crimson Sword
Get the flag by running tester and passing all the tests.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
