文本冒险游戏保存到文件,但当我尝试加载它时停止程序

时间:2014-09-05 20:29:27

标签: java

我终于让我的保存功能为我的文字冒险工作了。当我尝试将游戏加载回来时说

Loading. . .

就像我做的那样,游戏结束了。我现在不知道如何让它重新加载房间。这是代码。

保存功能

public void save(Player p, Room r){

        String username = p.getUserName();
        int currentRoomNumber = r.getCurrentRoomNumber();
        String currentRoomDescription = r.getCurrentRoomDescription();
        ArrayList<String> currentRoomItems = new ArrayList<>();
        ArrayList<String> currentRoomExits = new ArrayList<>();
        currentRoomItems = r.getCurrentRoomItems();
        currentRoomExits = r.getCurrentRoomExits();

        File f = new File(username + ".txt");

        if(f.exists()){

            try{
                FileOutputStream fout = new FileOutputStream(f);
                ObjectOutputStream oos = new ObjectOutputStream(fout);

                oos.writeObject(username);
                oos.writeObject(currentRoomNumber);
                oos.writeObject(currentRoomDescription);
                oos.writeObject(currentRoomItems);
                oos.writeObject(currentRoomExits);
                oos.flush();

            }catch(FileNotFoundException fnfe){

                System.out.println("File not found: " + fnfe.getLocalizedMessage());

            }catch(IOException e){

                System.out.println("IOException: " + e.getLocalizedMessage());
            }
        }
    }

加载功能

public void load(Player p, Room r, World w){

        Sleep s = new Sleep();
        long time = 3000;

        Scanner i = new Scanner(System.in);
        System.out.print("What is your username?: ");
        String username = i.next();
        String name;
        int currentRoomNumber;
        String currentRoomDescription;
        ArrayList<String> currentRoomItems = new ArrayList<>();
        ArrayList<String> currentRoomExits = new ArrayList<>();
        File f = new File(username + ".txt");
        if(f.exists()){

            System.out.println("Loading game . . .");

            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();
                ois.close();

                Player.setUserName(name);
                w.CurrentRoom(currentRoomNumber, currentRoomDescription, currentRoomItems, currentRoomExits);
            }catch(Exception e){

                e.printStackTrace();
            }
        }
    }
}

My CurrentRoom方法,在我的世界级

void CurrentRoom(int roomNumber, String roomDescription, ArrayList<String> roomItems, ArrayList<String> roomExits){

        Room CurrentRoom = new Room();
        CurrentRoom.setCurrentRoomNumber(roomNumber);
        CurrentRoom.setCurrentRoomDescription(roomDescription);
        ArrayList<String> CurrentRoomItems = new ArrayList<String>();
        CurrentRoom.setCurrentRoomItems(roomItems);
        ArrayList<String> CurrentRoomExits = new ArrayList<String>();
        CurrentRoom.setCurrentRoomExits(roomExits);
    }

房间等级

public static class Room{
    public static int currentRoomNumber;
    public static int roomNumber;
    public static String currentRoomDescription;
    public static String roomDescription;
    public static ArrayList<String> currentRoomItems;
    public static ArrayList<String> items;
    public static ArrayList<String> currentRoomExits;
    public static ArrayList<String> exits;
    public static ArrayList<String> commands;

    public int getRoomNumber(){

        return roomNumber;
    }
    public static void setRoomNumber(int roomNumber){

        Room.roomNumber = roomNumber;
    }
    public int getCurrentRoomNumber(){

        return roomNumber;
    }
    public static void setCurrentRoomNumber(int currentRoomNumber){

        Room.currentRoomNumber = roomNumber;
    }
    public String getRoomDescription(){

        return roomDescription;
    }
    public static void setRoomDescription(String roomDescription){

        Room.roomDescription = roomDescription;
    }
    public String getCurrentRoomDescription(){

        return roomDescription;
    }
    public static void setCurrentRoomDescription(String currentRoomDescription){

        Room.currentRoomDescription = roomDescription;
    }
    public ArrayList<String> getItems(){

        return items;
    }
    public static void setItems(ArrayList<String> items){

        Room.items = items;
    }
    public ArrayList<String> getCurrentRoomItems(){

        return items;
    }
    public static void setCurrentRoomItems(ArrayList<String> currentRoomItems){

        Room.currentRoomItems = items;
    }
    public ArrayList<String> getExits(){

        return exits;
    }
    public static void setExits(ArrayList<String> exits){

        Room.exits = exits;
    }
    public ArrayList<String> getCurrentRoomExits(){

        return exits;
    }
    public static void setCurrentRoomExits(ArrayList<String> currentRoomExits){

        Room.currentRoomExits = exits;
    }
    public ArrayList<String> getCOmmands(){

        return commands;
    }
    public static void setCOmmands(ArrayList<String> commands){

        Room.commands = commands;
    }
    void printItems(ArrayList<String> roomItems) {

       if (roomItems.size() > 0) {

            System.out.print("You see a ");
            if (roomItems.size() == 1) {

                System.out.println(roomItems.get(0) + ".");
            }
            if (roomItems.size() == 2) {

                System.out.println(roomItems.get(0) + " and a " + roomItems.get(1) + ".");
            }
            if (roomItems.size() == 3) {

                System.out.println(roomItems.get(0) + ", " + roomItems.get(1) + " and a " + roomItems.get(2) + ".");
            }
        }
    }
    void printExits(ArrayList<String> roomExits){

        if(roomExits.size() > 0){

            System.out.print("It looks like you can go ");
            if(roomExits.size() == 1){

                System.out.println(roomExits.get(0) + ".");
            }
             if (roomExits.size() == 2) {

                System.out.println(roomExits.get(0) + " or " + roomExits.get(1) + ".");
            }
            if (roomExits.size() == 3) {

                System.out.println(roomExits.get(0) + ", " + roomExits.get(1) + ", or " + roomExits.get(2) + ".");
            }
            if(roomExits.size() == 4){

                System.out.println(roomExits.get(0) + ", " + roomExits.get(1) + ", " + roomExits.get(2) + ", or " + roomExits.get(3) + ".");
            }
        }
    }
}

我相信这就是所有相关的代码。我很抱歉,如果我把不相关的代码我将删除任何不需要的。我发现如何编码来保存和加载这个网站,但我无法弄清楚如何让它实际加载我的游戏。任何帮助表示赞赏。

0 个答案:

没有答案