private void createRooms()
{
myNeighbor = new HashMap <String, Room> ();
crumbs = new Item("Crumbs", "small crumbs of some kind of food", 100);
eggs = new Item("Raw Eggs", "a couple of raw eggs still contained within their egg shells", 1100);
cellPhone = new Item("Cell Phone", "Mike's cell phone he must have forgotten here...", 0);
textBooks = new Item("Textbooks", "Jay's textbooks, because he can't use his bedroom to store his stuff", 0);
poptarts = new Item("Pop Tarts", "an un-opened box of chocolate pop tarts that someone must have left behind...", 1500);
pizzaRolls = new Item("Pizza Rolls", "cooked steaming pizza rolls piled high", 2000);
clothes = new Item("Clothes", "clothes, a lot of clothes all over the floor and all over the room, who knows if they're clean or not...", 0);
// miningTools = new Item("Mining Tools", "pickaxes, drills, and everything else you need to extract rocks and minerals from the earth's crust", 100);
chips = new Item("Chips", "chip bag hidden away that is only half full now", 400);
hallway = new Room("in a dark hallway with crumbs scattered over the ground", crumbs);
kitchen = new Room("in a kitchen with raw eggs lying on the counter tops", eggs);
bathroom = new Room("in a bathroom with a stand up shower, a washer, a drier, and Mike's cell phone left behind laying on the counter", cellPhone);
livingRoom = new Room("in a living room with Jay's textbooks all over the room", textBooks);
upstairsLobby = new Room("in a lobby at the top of the stairs with a box of pop tarts on the ground", poptarts);
blakesRoom = new Room("in a dark room with towers of pizza rolls covering the desk and scattered across the bed", pizzaRolls);
jaysRoom = new Room("in a cluttered room with clothes covering every inch of the floor and nothing hanging on the walls", clothes);
mikesRoom = new Room("in a bed room with mining tools and a bag of chips hidden underneath a pillow on the bed", chips);
hallway.addNeighbor("north", kitchen);
hallway.addNeighbor("west", upstairsLobby);
hallway.addNeighbor("east", livingRoom);
kitchen.addNeighbor("west", bathroom);
kitchen.addNeighbor("south", hallway);
bathroom.addNeighbor("east", kitchen);
livingRoom.addNeighbor("west", hallway);
upstairsLobby.addNeighbor("north", jaysRoom);
upstairsLobby.addNeighbor("west", blakesRoom);
upstairsLobby.addNeighbor("east", mikesRoom);
upstairsLobby.addNeighbor("south", hallway);
blakesRoom.addNeighbor("east", upstairsLobby);
jaysRoom.addNeighbor("south", upstairsLobby);
mikesRoom.addNeighbor("west", upstairsLobby);
}
房间类别
import java.util.HashMap;
/**
* Write a description of class Room here.
*
* @author (Christopher a date)
*/
public class Room
{
private String description;
private Item item;
private HashMap <String, Room> myNeighbor;
public Room (String pDescription)
{
description = pDescription;
item = null;
HashMap <String, Room> myNeighbor = new HashMap <String, Room> ();
}
public Room (String pDescription, Item pItem)
{
description = pDescription;
item = pItem;
}
public String getDescription()
{
return description;
}
public Item getItem()
{
return item;
}
public void addItem(Item i)
{
item = i;
}
public boolean hasItem()
{
if (item != null)
return true;
else
return false;
}
public void addNeighbor(String pDirection, Room r)
{
myNeighbor = new HashMap <String, Room> ();
myNeighbor.put(pDirection, r);
}
public Room getNeighbor(String pDirection)
{
Room next = myNeighbor.get(pDirection);
if(next != null){
return next;
}
else{
return null;
}
}
public Item removeItem()
{
Item temp;
temp = item;
item = null;
return temp;
}
public String getLongDescription()
{
String part1 = "You are " + description;
String part2 = "You see ";
if(item != null){
return part1 + "" + part2 + "" + item.getDescription() + "" + item.getCalories();
}
return part1;
}
}
长话短说,这一点是为了添加房间,并能够导航它们,捡起物品,然后放下它们。在我尝试运行程序时,我注意到我不能有多个北/南/东/西键。我怎样才能避开这件事,这样我才能把它做好?
假设您希望能够编写:
Room targetRoom = currentRoom.neighbour("north");
然后你需要改变你的设计。
邻居必须是房间的成员(ivars),例如:
class Room;
typedef HashMap<string, Room*> NeighbouringRooms;
public class Room {
...
public NeighbouringRooms const& neighbour() const {
return _neighbours;
}
private NeighbouringRooms neighbours;
}
(我省略了课堂上的一些细节,比如在房间里添加一个邻居。)
现在,由于只有4个可能的方向(N,S,E,W),每个房间的邻居数组也可以做到这一点。
public class Room {
public Room neighbours[4];
...
}
Room room;
room.neighbour[north] = ... ;
它不允许我发表评论所以...
我不确定你的ROOM类是什么样子的,但我猜它是在构造器中使用hasmap初始化的,并且有一个名为addNeighbor的方法来实际修改这个哈希映射?
----EDIT-----
看到您的AddNeighbor方法表明,每次将邻居添加到hashmap时,您都会创建一个新的hasmap。没有必要,您已经在构造器中渴望MyNeighbor,现在您可以将它们的新键、值组合“放入”哈希图中
每次只需删除该行即可创建新的hasmap。
我有一个Google Cloud函数,它包含多个模块,可以在不同的路径上调用。 我正在使用无服务器框架来部署我的函数,但它有每个函数只有一个路径的限制。 我想在一个函数中使用多个路径,就像我们可以在AWS无服务器框架中一样。 假设云函数有两个路径和;两个路径都应该调用相同的函数。 类似这样的事情:
问题内容: 我们可以在Android中拥有服务的多个实例吗?我想要一种可以在特定时间使手机静音的服务,并且我想重新使用该服务以使手机在特定时间处于振动模式。因此,基本上,如果我可以使该服务多次运行,则无需创建其他服务即可将手机设置为振动模式。另外,如果我们可以运行多个实例,请说明如何停止它。任何代码提示都会有很大帮助。 问题答案: 我们可以在Android中拥有服务的多个实例吗? 不能。您可以具有
问题内容: 我有这个领域: 对于每个我都需要创建一个,其项是的值(恰好是HashMap本身)。 通过(无功能的)演示: 问题答案: 我知道我对那件事有些迟了,但是我也会分享我所做的,以防它对其他人有帮助:
有一个应用程序需要像搜寻列表这样的东西。这个应用程序可以用不同的配置多次启动。有没有办法跨JVM共享数据结构。在JVM中是有效的。有一个数据库可以解决这个问题。但是,有没有更简单、更快速的方法?
我需要三个实体——用户、订单和产品。订单可以有多个产品,产品可以有多个订单。但是在一个订单中,我们可以有几个相似的产品。如何使用Hibernate来组织它? 我认为其中一种方法是创建另一个实体-OrderProducts,但是我需要使用什么映射?或者有另一种简单的方法可以做到这一点吗? Order.java 产品Java语言 我需要order_products表有数量列,但我不知道怎么做。
问题内容: 我想在Java中实现具有多个值的哈希表,即 并且将返回2倍的值。 我怎样才能做到这一点? 问题答案: 您可以改用Multimap。它在列表中为一个键保留多个值。在commons- collection 和Guava中有实现。 这类似于使用值是列表的Hashmap,但是不必显式创建列表。 自己动手做的同一示例如下所示: 请注意,您可以将Multimap用作构建器,并对其调用asMap以返