我正在制作这个基于控制台的游戏。
但我不明白这一点,也不明白为什么这样做是错误的。
所以基本上我有10个房间,所以一个房间有以下东西:
-房间号码
-hashmap,其中包含一个Room作为值和一个String作为键
现在的问题是,一个房间可以从4个方向北、东、南、西退出,所以这是哈希图中的字符串/键。
它也可以有2个出口或1或3个。
所以在那个哈希图中,我将方向保存为键和值另一个房间对象,这样我就可以跟踪哪个出口去哪个房间。
所以我有一个添加出口的方法。看起来是这样的:
public class Room {
private int RoomNo;
private HashMap<String, Room> Exits;
html" target="_blank">public Room(int roomNo){
Exits = new HashMap<String, Room>();
this.RoomNo = roomNo;
}
public void Addexits(Room room, String exitDirection){
Exits.put(exitDirection,room);
}
在游戏类中,我创建了10个房间,并使用call the addexits方法添加如下所示的出口:
Rooms = new ArrayList<Room>();
//initialize all rooms
room1 = new Room(1);
room1.Addexits(room4,"West");
room1.Addexits(room3,"East");
room2 = new Room(2);
room2.Addexits(room2,"North");
room2.Addexits(room2,"South");
room3 = new Room(3);
room3.Addexits(room3,"West");
room3.Addexits(room3,"East");
room4 = new Room(4);
room4.Addexits(room4,"West");
room4.Addexits(room4,"East");
然而,正如您在room1中所看到的,我尝试在room4中添加2个出口,在room4中添加1个出口,在room3中添加1个出口,但是当我运行此操作时,它会在hashmap中显示:west-
但是,当我添加两个出口,都是对象room1时,它应该是这样的:
西-
所以我真的不知道我做错了什么每个房间在创建时都有自己的hashmap,房间已经创建好了,所以这不应该是个问题。
这是因为当您调用Addexits()时,不能保证您添加的房间已经存在。因此,改变你的陈述应该可以解决这个问题。
Rooms = new ArrayList<Room>();
//initialize all rooms
room1 = new Room(1);
room2 = new Room(2);
room3 = new Room(3);
room4 = new Room(4);
// add all exits
room1.Addexits(room4,"West");
room1.Addexits(room3,"East");
room2.Addexits(room2,"North");
room2.Addexits(room2,"South");
room3.Addexits(room3,"West");
room3.Addexits(room3,"East");
room4.Addexits(room4,"West");
room4.Addexits(room4,"East");
还要注意,您正在将room3添加为room3的出口,对于Room2和4也是如此。我不知道这是不是目的。
在以下代码中
Rooms = new ArrayList<Room>();
//initialize all rooms
room1 = new Room(1);
room1.Addexits(room4,"West");
room1.Addexits(room3,"East");
你正在添加还没有初始化的房间3和房间4。你刚刚声明了它。这就是你变得空的原因。初始化后试着添加它。
Rooms = new ArrayList<Room>();
//initialize all rooms
room1 = new Room(1);
room2 = new Room(2);
room2.Addexits(room2,"North");
room2.Addexits(room2,"South");
room3 = new Room(3);
room3.Addexits(room3,"West");
room3.Addexits(room3,"East");
room4 = new Room(4);
room4.Addexits(room4,"West");
room4.Addexits(room4,"East");
//here you add room4 and room3 exits
room1.Addexits(room4,"West");
room1.Addexits(room3,"East");
首先初始化所有房间,然后添加出口;
room1 = new Room(1);
room2 = new Room(2);
room3 = new Room(3);
room4 = new Room(4);
room1.Addexits(room4,"West");
room1.Addexits(room3,"East");
room2.Addexits(room2,"North");
room2.Addexits(room2,"South");
room3.Addexits(room3,"West");
room3.Addexits(room3,"East");
room4.Addexits(room4,"West");
room4.Addexits(room4,"East");
我有一种方法,可以按照班级团队中的国家对数据库中的数据进行排序。Hibernate在列表中返回我的数据 我的错误日志: 我的冠军联赛课程: 我的班级: }
Navicat 还能让你管理其他 SQLite 对象:索引和触发器。在主窗口的主工具栏点击相应的按钮来打开对象列表。
Navicat 还能让你管理其他 SQL Server 对象:索引、同义词、触发器、备份设备、链接服务器、服务器触发器、程序集、数据库触发器、分区函数和分区方案。在主窗口中,点击 “其他”,然后选择一个对象来打开对象列表。
Navicat 还能让你管理其他 PostgreSQL 对象:聚合、转换、域、索引、运算符、运算符类别、序列、触发器、表空间、编制和语言。在主窗口中,点击 “其他”,然后选择一个对象来打开对象列表。
Navicat 还能让你管理其他 Oracle 对象:数据库链接、索引、Java、实体化视图日志、序列、同义词、触发器、类型、XML 架构、目录、公用数据库链接、公用同义词和表空间。在主窗口中,点击 “其他”,然后选择一个对象来打开对象列表。
Navicat 还能让你管理其他 SQLite 对象:索引和触发器。在主窗口中,点击 “其他”,然后选择一个对象来打开对象列表。