Search code examples
c++text-based

Text Based RPG Drop and Keep Item Functions


So in my programming class I have to make a text based RPG. The issue I'm having is figuring out what to put inside of my Player_dropItem() function and my Player_keepItem() function. I know what they're supposed to do and I have some kind of an idea for how to do it, however I'm completely stumped as to what to actually put into the functions.

Here's some of my code:

#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

// Macros ----------------
#define ForAll(m) for(int ndx = 0; ndx < int(m); ndx++)

using std::cout;
using std::cin;
using std::endl;
using std::getline;
using std::string;
using std::stringstream;
using std::ofstream; // Output file stream
using std::ifstream; // Input file stream

typedef char const* Directions;
typedef char const* Walls;
typedef int Coord;
typedef char Key;
typedef char Wall;

enum Facing {
    kNorth = 0,
    kEast,
    kWest,
    kSouth,
    kEnd
}; 

enum ItemKind {
    ItemKind_detail = 0,
    ItemKind_keep,
    ItemKind_use,
    ItemKind_object
};

Directions gDirections[] = {
    "North",
    "East",
    "West",
    "South"
};

// Player ----------------

const int kInventoryItemsMax = 10;

struct Player{
    int facing;
    int row, col;
    Item* inventory[kInventoryItemsMax];
};

// return a count of all items in player's inventory
int Player_inventoryCount(Player& p) 
{
    int inventoryCount = sizeof(p.inventory) / sizeof(p.inventory[0]);

    return inventoryCount;
}

// Find empty slot in player's inventory and keep item
// Return true if kept - false if not (inventory at max)
bool Player_keepItem(Player& p, Item* item)
{
    ForAll(p.inventory) {
        if (p.inventory == 0){
            return true;
        }
        else {
            return false;
        }
    }
}

// Searches inventory for itemName.
// if found removes from inventory
// returns item* if found or nullptr if not.
Item* Player_dropItem(Player& p, string& itemName)
{
    ForAll(p.inventory) {
        if (itemName == ) {
            return p.inventory;
        }
        else if (itemName != ) {
            return nullptr;
        }
    }
}

// Print player inventory to console
void Player_printInventory(Player& p)
{
    int inventoryCount = sizeof(p.inventory) / sizeof(p.inventory[0]);

    ForAll(inventoryCount) {
        cout << ndx;
    }
}

// Items from file
struct Item {
    int     id,
            kind,
            value;

    string  name,
            describe;
};

That should be it but if it helps I can put more code up. Any help is appreciated. Thanks!


Solution

  • Based on your requirements, here are some fixes:

    First, it's important the inventory array gets initialized to null, since null will mark an empty slot:

    struct Player {
        int facing;
        int row, col;
        Item* inventory[kInventoryItemsMax] = {nullptr};
    };
    

    Next, count the items by counting the non-null items:

    int Player_inventoryCount(Player& p) 
    {
        int inventoryCount= 0;
        for (int i = 0; i < kInventoryItemsMax; i++) {
            if (nullptr != p.inventory[i]) {
                inventoryCount += 1;
            }
        }
        return inventoryCount;
    }
    

    To drop an item, set it to null:

    Item* Player_dropItem(Player& p, string& itemName)
    {
        for (int i = 0; i < kInventoryItemsMax; i++) {
            // Check for match - skipping null items
            if (nullptr != p.inventory[i] && itemName == p.inventory[i]->name) {
                // Temporarily store the pointer
                Item *temp = p.inventory[i];
                // Set it to null in the array to mark as dropped
                p.inventory[i] = nullptr;
                // Return the item
                return temp;
            }
        }
        // Not found - return null
        return nullptr;
    }