📄 room.java
字号:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
/**
* 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 1.0 (February 2002)
*/
public class Room
{
private NPC npc; //
//private Item npc;
private String description;
private HashMap<String, Room> exits; // stores exits of this room.
private HashMap<String, Item> items; //set items
//private HashMap<String, String> door;
private Door door;
private String roomname;
/**
* Create a room described "description". Initially, it has no exits.
* "description" is something like "in a kitchen" or "in an open court
* yard".
*/
public Room(String description, String roomName)
{
this.description = description;
this.roomname = roomName;
exits = new HashMap<String, Room>();
items = new HashMap<String, Item>();
// door = new HashMap<String, String>();
}
/**
* Create a NPC which get a Item which gets itemName and itemDescription
* add name and description to HashMap items, show to player.
* @param name npc's name as an item save in HashMap item
* @param description npc's description as an item save in HashMap item
* @param itemName name of Item in npc
* @param itemDescription name of Item in npc
*/
public void addNpc(String name, String description, String itemName, String itemDescription)
{
npc= new NPC(itemName, itemDescription);//
items.put(name, new Item(name,description));
// npc = new Item(itemName, itemDescription);
}
/**
*
* @return the name of item is in current npc
*/
public String getNPCItem()
{
//return npc.getItemName();
return npc.getItemName();
}
/**
* Trade item to npc. Npc obtain new item and give up an item which he held
* @param item the Item will trade to npc
* @return an item which npc held
*/
public Item tradeNPC(Item item)
{
return npc.tradeNPCItem(item);
}
/**
* Add an item in a room itemlist after ensure there
* is not the item with same item name
* @param name Item name
* @param description Item description
*/
public void addItem(String name, String description)
{
if(!sameName(name))
{
items.put(name, new Item(name,description));
}
else
{
System.out.println("Exist the same name Item.");
}
}
/**
* Look for item in a room, if it exists, return true;
* if it does not exist, return false.
*/
public boolean sameName(String name)
{
Set<String> keys = items.keySet();
for(Iterator<String> i=keys.iterator();i.hasNext();)
{
if(i.next().equals(name))
{
return true;
}
}
return false;
}
/**
* Search HashMap items to find out a key is "name", and return its attribute
* @param name key for searching
* @return an item attribute
*/
public Item getItemValue(String name)
{
return (Item)items.get(name);
}
/**
* remove the item which has a key is "name" from HashMap items
* @param name key for searching
*/
public void giveUpItem(String name)
{
items.remove(name);
}
/**
* Define an exit from this room.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
/**
* Return the description of the room (the one that was defined in the
* constructor).
*/
public String getShortDescription()
{
return description;
}
/**
* Return a long description of this room, in the form:
* You are in the kitchen.
* Exits: north west
*/
public String getLongDescription()
{
return "You are " + description + ".\n" + getExitString();
}
/**
* Return a string describing the room's exits, for example
* "Exits: north west".
*/
private String getExitString()
{
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys)
returnString += " " + exit;
return returnString;
}
/**
* Display the item name and description from HashMap.
*/
public void printItemList()
{
System.out.println("The item name and description:");
Set<String> keys = items.keySet();
for(Iterator<String> i=keys.iterator();i.hasNext();)
{
String name = i.next();
Item it = getItemValue(name);
System.out.println(it.getItemName() +" " + it.getItemDescription());
}
}
/**
* 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.
*/
public Room getExit(String direction)
{
return (Room)exits.get(direction);
}
public void setDoor(String doorName, String doorStatus)
{
door = new Door(doorName, doorStatus);
}
public void lockDoor(String doorName)
{
door.lockDoor();
}
public void unlockDoor()
{
door.unlockDoor();
}
public String getDoorStatus()
{
return door.getDoorStatus();
}
/* public Room existsDoor(String exit)
{
Room nextRoom = exits.get(exit);
return nextRoom;
/*
Set<String> name = door.keySet();
Iterator<String> it = name.iterator();
while(it.hasNext())
{
if(it.next().equals(doorName))
return true;
}
return false;
}*/
public String getRoomName()
{
return roomname;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -