Project 3.2 MUD Shop


Intro to Programming Languages.

[ ← Module List ]


It's time to bring some merchantilism into the MUD. In this project, you will be building a shopping interface that will list items and allow the player to purchase the items at various locations around the MUD map. The interface will rely on the linked list data structure to hold the items in the shop, and it will allow the player to browse through the items, search for items, and buy items.


Module Content


Challenges

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.

  1. 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.
  2. 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;
          
  3. 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
              
  4. Update load_json_items in data.cpp to load the new fields
    • value and damage will use extract_int
    • type 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 > -1
    • 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 1600 called MAX_JSON

      • Each time the code finds an Id, then set the lastValidItemId
      • After the loop, set the last value for the item at the index of lastValidItemId to true
  5. 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 1697 looking like this:
      1697 Crimson Sword 104097 320 3 1
Get the flag by running tester and passing all the tests.

This challenge is locked

This challenge is locked

This challenge is locked

This challenge is locked

This challenge is locked

This challenge is locked


30-Day Scoreboard:

This scoreboard reflects solves for challenges in this module after the module launched in this dojo.

Rank Hacker Badges Score