基于文本的RPG丢弃和保留项目功能

时间:2016-05-25 18:00:12

标签: c++ text-based

所以在我的编程课中我必须制作一个基于文本的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;
};

应该是它,但如果它有帮助我可以放更多的代码。任何帮助表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

根据您的要求,以下是一些修复:

首先,将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;
}