0

I have finally got my save function working for my text adventure. When i try to load the game back in it says

Loading. . .

Like i made it do, then the game just ends. I'm not sure how to get it to load the room back in now. Here is the code.

save function

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());
            }
        }
    }

load function

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 method which is in my World class

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);
    }

Room class

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) + ".");
            }
        }
    }
}

I believe that is all the relevant code. I am sorry if i put irrelevant code i will remove whatever is not needed. I found out how to code to save and load from this site but I cannot figure out how to make it actually load my game. Any help is appreciated.

  • 2
    It looks like you don't close your file after writing. Try to add `oos.close()` after or instead of `oos.flush()` – ponomandr Sep 05 '14 at 20:31
  • I'm not sure you can save multiple objects to a file. Either way it might be a better design to have one serializable object SavedGame that is written/read from a file. http://stackoverflow.com/questions/7831759/how-to-write-multiple-objects-to-file – catalyst294 Sep 05 '14 at 20:35
  • Random note: save calls Player.setUserName statically but load calls p.getUserName(). Should probably use one or the other. – spudone Sep 05 '14 at 21:20
  • Thank you for the comments. After playing around with it I got it to work correctly. – Master Zee Sep 05 '14 at 21:21

0 Answers0