2

编辑:如果我不清楚(对不起,我是 Java 的初学者),我只是想增加将项目添加到房间的可能性,但是,我希望项目方面像现在一样属于自己的类。尽管在目前的状态下,它不起作用。

我试图让项目出现在我的游戏的房间中,所以我制作了一个单独的项目类,其中相关代码是

public class Items 
{

private String itemDescription;
private int itemWeight;
/**
 * Add an item to the Room
 * @param description The description of the item
 * @param weight The item's weight
 */
public void addItem(String description, int weight) 
{
    itemDescription = description;
    itemWeight = weight;               
}

/**
 * Does the room contain an item
 * @param description the item
 * @ return the item's weight or 0 if none
 */
public int containsItem(String description) 
{
    if (itemDescription.equals(description)) 
    {
        return itemWeight;
    }
    else
    {
        return 0;
    }
}

/**
 * Remove an item from the Room
 */
public String removeItem(String description) 
{
    if (itemDescription.equals(description)) 
    {
        String tmp = itemDescription;
        itemDescription = null;
        return tmp;
    }
    else 
    {
        System.out.println("This room does not contain" + description);
        return null;
    }
}



public String getItemDescription() 
{
    return this.itemDescription;
}

public void setItemDescription(String itemDescription) 
{
    this.itemDescription = itemDescription;
}

public int getItemWeight() 
{
    return this.itemWeight;
}

public void setItemWeight(int itemWeight) 
{
    this.itemWeight = itemWeight;
}

public String getCharacter() 
{
    return this.character;
}

public void setCharacter(String character) 
{
    this.character = character;
}
}

在我单独的游戏类中,我尝试将它链接到我的游戏类中,就像这样

     // initialise room exits
        outside.setExit("north", lab);
        outside.addItem("notebook", 2); 

就像我对游戏的出口所做的那样。但是,我收到一条错误消息

The method addItem(String, int) is undefined for the type 
Room

这是房间类

import java.util.HashMap;
import java.util.Set;
import java.util.ArrayList;

public class Room 
{
private String description;
private HashMap<String, Room> exits;


/*
private ArrayList<Item> items;
*/
//private HashMap<String, Item> items;
public Items item;

// Characters in the room
private String character; 

/**
 * 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>();
}

/**
 * Define the exits of this room.  Every direction either leads
 * to another room or is null (no exit there).
 * @param north The north exit.
 * @param east The east east.
 * @param south The south exit.
 * @param west The west exit.
 * @param up The upwards exit.
 * @param down The downwards exit.
 * 
 */
 /**
 * Define an exit from this room.
 * @param direction The direction of the exit.
 * @param neighbor The room in the given direction.
 */
 public void setExit(String direction, Room neighbor)
{
        exits.put(direction, neighbor);
}

public Room getExit(String direction)
{
    return exits.get(direction);

}

/**
 * Return a description of the room’s exits,
 * for example "Exits: north west".
 * @return A description of the available exits.
 */
public String getExitString()
{
    String returnString = "Exits:";
    Set<String> keys = exits.keySet();
        for(String exit : keys) 
        {
            returnString += " " + exit;
        }
    return returnString;
 }

/**
 * @return The description of the room.
 */
public String getDescription()
{
    return description;
}

/**
* Return a long description of this room, of the form:
* You are in the kitchen.
* Exits: north west
* @return A description of the room, including exits.
*/
public String getLongDescription()
{
    return "You are " + description + ".\n" + getExitString();
}

}

游戏类,房间本身就是制作的

 import java.util.ArrayList;


public class Game 
{

private Parser parser;
private Room currentRoom;
private ArrayList items;
private ArrayList weights;
private int totalWeight;
private final int MAX_WEIGHT = 10;

/**
 * Create the game and initialise its internal map.
 */
public Game() 
{
    createRooms();
    parser = new Parser();
    items = new ArrayList();
    weights = new ArrayList();
    totalWeight = 0;
}

/**
 * Create all the rooms and link their exits together.
 */
private void createRooms() 
{
    Room outside, theatre, pub, lab, office, up, down;

    // create the rooms
    outside = new Room("outside the main entrance of the university");
    theatre = new Room("in a lecture theatre");
    pub = new Room("in the campus pub");
    lab = new Room("in a computing lab");
    office = new Room("in the computing admin office");
    up = new Room("creepy upstairs");
    down = new Room("spooky downstairs");


    // initialise room exits
    outside.setExit("north", lab);
    outside.item.addItem("notebok", 4); 
    outside.setExit("down", down);
    outside.setExit("up", up);
    outside.setExit("west", pub);
    outside.setExit("east", office);

    down.setExit("north", outside);
    up.setExit("south", outside);

    lab.setExit("east", office);
    lab.setExit("south", outside);
    lab.setExit("north", theatre);

    office.setExit("south", lab);

    pub.setExit("east", theatre);
    pub.setExit("south", outside);

    theatre.setExit("south", outside);
    currentRoom = outside;  // start game outside
}

/**
 * Main play routine. Loops until end of play.
 */
public void play() 
{
    printWelcome();

    // Enter the main command loop.  Here we repeatedly read commands and
    // execute them until the game is over.

    boolean finished = false;
    while (!finished) 
    {
        Command command = parser.getCommand();
        finished = processCommand(command);
    }
    System.out.println("Thank you for playing.  Good bye.");
}

/**
 * Print out the opening message for the player.
 */
private void printWelcome() 
{
    System.out.println();
    System.out.println("Welcome to the World of Zuul!");
    System.out.println("World of Zuul is a new, incredibly boring adventure game.");
    System.out.println("Type 'help' if you need help.");
    System.out.println();
    System.out.println(currentRoom.getLongDescription());

}

我认为这是因为,名为“房间”的类没有对“项目”的任何引用,但是我不确定如何实现这一目标?任何指导将不胜感激。

4

2 回答 2

1

你在这里混合了 Item 逻辑和 Room 逻辑。

  1. 你的addItem函数应该是 Item 的构造函数
  2. 您应该在 Room 类中具有 addItem 函数,该函数实例化一个新项目(如果您想要一个房间中的多个项目,可能将其存储在列表/哈希中)
  3. containsItem方法也应该位于 Room 类中
于 2019-10-29T06:40:42.400 回答
1
The method addItem(String, int) is undefined for the type 
Room

因为在您的房间类中没有方法定义为 addItem(String, Item) 。在尝试调用它之前将此函数添加到 Room 类。或者如下图修复

没有它你会得到编译错误。

看到你的评论,让我给你一些建议,但要小心,因为我不知道你的确切需求。

import java.util.HashMap;
import java.util.Set;
import java.util.ArrayList;

public class Room 
{

        public Items item = new Items();
       ...........

在调用类中使用它就像

         outside.item.addIem(...)
于 2019-10-29T07:00:45.297 回答