-1

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!

Dheraxys
  • 17
  • 5
  • 3
    *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* Do you or do you not have an idea what to put in there? What did you try to put in there? – Borgleader May 25 '16 at 18:02
  • Instead of calculating `inventoryCount`, `Player` should probably have an `int inventoryCount` member since the way you are calculating it now always returns 10. – 001 May 25 '16 at 18:17
  • Why are you using an array? Is that a requirement for the assignment? `std::vector` seems like a better solution. – 001 May 25 '16 at 18:31
  • @JohnnyMopp We haven't gotten to vectors. But in the research I've done to help me with this assignment that seems to be a much more efficient way to do things. So yes, I suppose it is a requirement that we use an array – Dheraxys May 25 '16 at 18:32

1 Answers1

0

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;
}
001
  • 13,291
  • 5
  • 35
  • 66