基于文本的 RPG 掉落和保留项目功能

Text Based RPG Drop and Keep Item Functions

所以在我的编程中 class 我必须制作一个基于文本的角色扮演游戏。我遇到的问题是弄清楚要在 Player_dropItem() 函数和 Player_keepItem() 函数中放入什么。我知道他们应该做什么,并且我对如何去做有一些想法,但是对于实际将什么放入函数中我完全不知所措。

这是我的一些代码:

#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;
};

应该是这样,但如果有帮助,我可以提供更多代码。任何帮助表示赞赏。谢谢!

根据您的要求,这里有一些修复:

首先,inventory 数组初始化为 null 很重要,因为 null 将标记一个空槽:

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

接下来,通过计算非空项目来计算项目:

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

要删除一个项目,将其设置为空:

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;
}