Hashmap问题,将项目添加到文本游戏Zuul

时间:2014-03-10 22:16:50

标签: java bluej

我正在尝试为游戏添加项目,所有房间都有几个项目。商品具有描述。这个描述总是一个单词。无论何时进入房间,房间描述都将包括房间内的物品清单。

例如,当我进入“入口”时,描述会说“房间里有一个Xitem Yitem Xitem”但我在描述中不断获得 Null 而不是项目。

以下是课堂

    import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Class Room - a room in an adventure game.
 *
 * This class is part of the "World of Zuul" application. 
 * "World of Zuul" is a very simple, text based adventure game.  
 *
 * A "Room" represents one location in the scenery of the game.  It is 
 * connected to other rooms via exits.  For each existing exit, the room 
 * stores a reference to the neighboring room.
 * 
 * @author  Michael Kolling and David J. Barnes
 * @version 2008.03.30
 */

public class Room 
{
    private String description;
    private HashMap<String, Room> exits;        // stores exits of this room.
    private HashMap<String,Item> itemList;
    private String name;
    private String itemDescription;
    private int weight;

    /**
     * Create a room described "description". Initially, it has
     * no exits. "description" is something like "a kitchen" or
     * "an open court yard".
     * @param description The room's description.
     */
    public Room(String description) 
    {
        this.description = description;
        exits = new HashMap<String, Room>();
        itemList = new HashMap<String,Item>();
        this.name = name;
        this.itemDescription = itemDescription;
        this.weight = weight;
        itemList = new HashMap<String,Item>();
    }

    /**
     * Define an exit from this room.
     * @param direction The direction of the exit.
     * @param neighbor  The room to which the exit leads.
     */
    public void setExit(String direction, Room neighbor) 
    {
        exits.put(direction, neighbor);
    }

    /**
     * @return The short description of the room
     * (the one that was defined in the constructor).
     */
    public String getShortDescription()
    {
        return description;
    }

    /**
     * Return a description of the room in the form:
     *     You are in the kitchen.
     *     Exits: north west
     * @return A long description of this room
     */

    public String getLongDescription()
    {
        return "You are " + description + ".\n" + getExitString() + " There is a " + getItemDescription() + " in the room";
    }

    /**
     * Return a string describing the room's exits, for example
     * "Exits: north west".
     * @return Details of the room's exits.
     */
    private String getExitString()
    {
        String returnString = "Exits:";
        Set<String> keys = exits.keySet();
        for(String exit : keys) {
            returnString += " " + exit;
        }
        return returnString;
    }

    /**
     * Return the room that is reached if we go from this room in direction
     * "direction". If there is no room in that direction, return null.
     * @param direction The exit's direction.
     * @return The room in the given direction.
     */
    public Room getExit(String direction) 
    {
        return exits.get(direction);
    }

    public void addItem(String name, String description, int weight)   
    {
        itemList.put(name, new Item(name, description, weight));
    }

    /**
     * 
     */
    public String getItemDescription()
    {
        return itemDescription;
    }


}

以下是课程项目(项目所在地)

    import java.util.HashMap;
/**
 * Write a description of class Item here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Item
{
    // instance variables 
    private HashMap<String,Item> itemList;
    private String name;
    private String itemDescription;
    private int weight;

    /**
     * 
     */
    public Item(String name, String itemDescription, int weight)
    {
        this.name = name;
        this.itemDescription = itemDescription;
        this.weight = weight;
        itemList = new HashMap<String,Item>();
    }

    /**
     * 
     */
    private void addItems()
    {
        itemList.put("golden_key", new Item("golden_key", "a_golden_key_is_the_key_for_the_golden_door", 1));
        itemList.put("silver_key", new Item("silver_key", "a_silver_key_is_the_key_for_the_silver_door", 1));
        itemList.put("bronze_key",new Item("bronze_key", "a_bronze_key_is_the_key_for_the_bronze_door", 1));
        itemList.put("machete",new Item("manchete", "a_melee_weapon", 5));
        itemList.put("golden_apple", new Item("golden_apple", "a_golden_apple_is_an_apple_for_health_regen", 2));
        itemList.put("backpack", new Item("backpack", "a_backpack_increases_your_inventory_capacity", 2));
    }

    /**
     * 
     */

    public String getItemDescription()
    {
        return itemDescription;
    }

}

1 个答案:

答案 0 :(得分:0)

如果你看一下Room构造函数......

public Room(String description) 
{
    this.description = description;
    exits = new HashMap<String, Room>();
    itemList = new HashMap<String,Item>();
    this.name = name;
    this.itemDescription = itemDescription;
    this.weight = weight;
    itemList = new HashMap<String,Item>();
}

这些是自我指定:

    this.name = name;
    this.itemDescription = itemDescription;
    this.weight = weight;

因此,当您从itemDescription返回的字符串中添加getLongDescription时,{{1}}为空。

我不确定如何在这里建议一个解决方案,除了你需要为这些创建构造函数参数或以其他方式制定它们。