java文本冒险,加载方法一直工作,直到用户输入到不同房间的方向?
我最近一直在尝试做一个文本冒险类型的游戏。我已经准备好了大部分框架,我只需要完成调试。现在我的问题是,当你以新用户的身份运行游戏并保存游戏时,它工作得非常好。即使你把它装进去,它也能工作,直到你试图离开房间
Please enter a username: test
Creating game . . .
You are in a dark room.
You see a book and a key.
It looks like you can go forwards.
>get book
You pick up the book.
>save
Saved successfully
那部分没问题,这部分是
What is your username: test
Loading game . . .
You are in a dark room.
You see a key.
It looks like you can go forwards.
>get key
You pick up the key.
>forwards
You can't do that.
>exit
Are you sure? (y/n): y
Thanks for playing.
正如你所看到的,当我加载上一个游戏时,控制台会告诉我有什么出口,我可以看到我的库存中有这本书,我可以得到其他物品。我只是不知道如何让游戏把我送到下一个房间,因为你想从哪个出口出去
这是我的代码,我觉得和这个问题是一致的。我为我的代码的草率和低效提前道歉。我仍在学习并试图找出解决问题的最佳方法。非常感谢你们的任何建议或提示
荷载法
public void load(Player p, Room r, World w, Inventory inven) throws Exception
{
Sleep s = new Sleep();
long time = 3000;
Scanner in = new Scanner(System.in);
System.out.print("What is your username?: ");
String username = in.next();
String name;
int currentRoomNumber;
String currentRoomDescription;
ArrayList<String> currentRoomItems = new ArrayList<String>();
ArrayList<String> currentRoomExits = new ArrayList<String>();
ArrayList<String> inv = new ArrayList<String>();
File f = new File(username + ".txt");
if(f.exists())
{
try
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
name = (String)ois.readObject();
currentRoomNumber = (int)ois.readObject();
currentRoomDescription = (String)ois.readObject();
currentRoomItems = (ArrayList<String>)ois.readObject();
currentRoomExits = (ArrayList<String>)ois.readObject();
inv = (ArrayList<String>)ois.readObject();
ois.close();
p.setUserName(name);
r.setRoomNumber(currentRoomNumber);
r.setRoomDescription(currentRoomDescription);
r.setItems(currentRoomItems);
r.setExits(currentRoomExits);
inven.setInv(inv);
System.out.println();
w.CurrentRoom(currentRoomNumber, currentRoomDescription, currentRoomItems, currentRoomExits);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
这就是我的代码出错的地方。这是我当前房间方法的一部分。它和其他房间都在我的世界级房间里。这是我唯一能想到的挽救比赛进程的方法
电流室
void CurrentRoom(int roomNumber, String roomDescription, ArrayList<String> items, ArrayList<String> exits)
{
Room CurrentRoom = new Room();
String i1 = ""; // I use these to get the Strings out of the items Array.
String i2 = "";
String i3 = "";
String e1 = "";//I was going to use these to get the exit Strings out of the exits Array
String e2 = "";
String e3 = "";
String e4 = "";
CurrentRoom.setRoomNumber(roomNumber);
CurrentRoom.setRoomDescription(roomDescription);
if(items.size() == 1)
{
i1 = items.get(0);
}
//I initialize all of my items like this
if(exits.size() == 1)
{
e1 = exits.get(0);
}
else if(exits.size() == 2)
{
e1 = exits.get(0);
e2 = exits.get(1);
}
else if(exits.size() == 3)
{
e1 = exits.get(0);
e2 = exits.get(1);
e3 = exits.get(2);
}
else if(exits.size() == 4)
{
e1 = exits.get(0);
e2 = exits.get(1);
e3 = exits.get(2);
e4 = exits.get(3);
}
CurrentRoom.setExits(exits);
while (true)
{
if(items.size() > 0)
{
if(items.size() == 1)
{
if (input.equals("get " + i1) && items.contains(i1))
{
items.remove(i1);
this.inventory.add(i1);
}
}//I use this plus other if statements to tell if the player is trying to get the items.
//I tried copying the above code that worked for the items and using it on the exits, but I figured out it does not work the same for the exits.
}
input = getInput();
}
}
问题是我将出口存储在一个数组中。我现在知道这根本不起作用,因为在游戏中加载出口阵列时,无法知道哪个出口真正通向哪个房间。我在这个网站上找到了一些方法,看到了一些关于使用
HashMap<String direction, Room neighbor>
我不知道如何真正使用上面的HashMap。解决此问题的最佳方法是什么,以便正确加载我的出口?我有一个示例室,用于测试我的代码。如果我需要在这里添加,请告诉我,谢谢
共 (0) 个答案