基于文本的角色扮演游戏拖放和保留物品功能



所以在我的编程课上,我必须制作一个基于文本的RPG。我遇到的问题是弄清楚在我的 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;
}

若要删除项目,请将其设置为 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;
}

最新更新