【问题标题】:Hashmap Issue, Add Item into Text Game ZuulHashmap 问题,将项目添加到文本游戏 Zuul
【发布时间】:2014-03-10 22:16:49
【问题描述】:

我正在尝试将物品添加到游戏中,所有房间都有几件物品。一个项目有一个描述。这个描述总是一个词。每当进入房间时,房间描述都会包含房间内物品的清单。

例如,当我进入“入口”时,描述会显示“房间里有 Xitem Yitem Xitem”,但我在描述中不断收到 Null 而不是物品。 p>

下面是教室

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

}

【问题讨论】:

  • 在哪里构造 Room 对象?

标签: java bluej


【解决方案1】:

如果您查看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 返回的字符串中时,它为空。

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

【讨论】:

    猜你喜欢
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 2012-04-12
    相关资源
    最近更新 更多