我想在进入房间时检查我的玩家是否持有特定物品

时间:2017-11-23 12:41:29

标签: java

我正在制作" Zuul世界"现在基于文本的冒险游戏,我想这样做,以便进入房间后,它将检查我的玩家是否持有库存中的特定项目。这是我到目前为止的库存代码(播放器类):

/**
 * Gets an item from the inventory
 */
public Item getInventoryItem(String itemName)
{
    Item item = inventory.get(itemName);
    currentWeight -= item.itemWeight();
    inventory.remove(itemName);
    return item;
}

/**
 * Lists all the items currently in the inventory
 */
public String listInventory()
{
    String itemList = "";
    for(String key : inventory.keySet())
    {
        String item = inventory.get(key).showName();
        itemList += " -" + item;
    }
    return "Your inventory contains \n" + itemList;
}

/**
 * Adds an item to the inventory
 */
public void addItemToInventory(String itemName, Item item)
{
    if(item.itemWeight() + currentWeight <maxWeightInv)
    {
        inventory.put(itemName, item);
        currentWeight += item.itemWeight();
        System.out.println(itemName + " was added to your inventory.");
    }
    else
    {
        System.out.println("You can't carry this");
    }
}

所以我真的想知道我是如何做到的,以便在进入房间之前首先检查玩家是否在库存中有特定物品(在这种情况下它会是一把剑)。我还没有尝试过一些东西,因为我不知道如何开始(游戏课):

/**
 * Exceptions upon entering Room15
 */
private void Room15()
{
    player.getInventoryItem("Sword");
    if(inventory.contains("Sword"))
    {
        System.out.println("Congratulations! You won the game and your freedom!");
    }
    else {
        System.out.println("Hurry back and find a sword!!!");
    }  
}

我希望我已经提供了足够的信息,说明我想要完成什么。我只需要一种检查玩家是否持有物品的方法。我可以自己完成其余的工作。

1 个答案:

答案 0 :(得分:0)

您可以在Player类中添加这样的方法:

redis-cli --scan --pattern "*sandeep-pant*" | sed -e 's/^/"/g' -e 's/$/"/g' | xargs -i redis-cli del {}

然后你可以修改你的Game类方法:

public boolean hasItemInInventory(String itemName)
{
   return inventory.containsKey(itemName);
}