Advertisement
LA77

Untitled

May 9th, 2025
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. package nl.rug.oop.rpg;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * Utility class for saving and loading.
  8.  */
  9. public class SaveManager {
  10.  
  11.     /**
  12.      * The directory where the saves are stored.
  13.      */
  14.     public static final String SAVED_GAMES_DIRECTORY = "savedgames";
  15.     /**
  16.      * The file extension for saves.
  17.      */
  18.     public static final String SAVE_FILE_EXTENSION = ".ser";
  19.  
  20.     /**
  21.      * Saves the current state of the game.
  22.      *
  23.      * @param game     The game object to be saved.
  24.      * @param filename The name of the file.
  25.      */
  26.     public static void saveGame(Game game, String filename) {
  27.         ensureDirectory();
  28.  
  29.         try (ObjectOutputStream out = new ObjectOutputStream(
  30.                 new FileOutputStream(getFullPath(filename)))) {
  31.             out.writeObject(game);
  32.             System.out.println("Quicksave successful!");
  33.         } catch (IOException e) {
  34.             System.err.println("Failed to save game: " + e.getMessage());
  35.         }
  36.     }
  37.  
  38.     /**
  39.      * Loading a file.
  40.      *
  41.      * @param filename The name of the file.
  42.      * @return A game object representing the loaded state.
  43.      */
  44.     public static Game loadGame(String filename) {
  45.         try (ObjectInputStream in = new ObjectInputStream(
  46.                 new FileInputStream(getFullPath(filename)))) {
  47.             System.out.println("Quickload successful!");
  48.             return (Game) in.readObject();
  49.         } catch (IOException | ClassNotFoundException e) {
  50.             System.err.println("Failed to load game: " + e.getMessage());
  51.             return null;
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Displays a list of available saves for the user to pick.
  57.      *
  58.      * @param scanner scanner object to read user input.
  59.      * @return the loaded game object.
  60.      */
  61.     public static Game promptAndLoadSave(Scanner scanner) {
  62.         File folder = new File(SAVED_GAMES_DIRECTORY);
  63.         File[] saves = folder.listFiles((dir, name) -> name.endsWith(SAVE_FILE_EXTENSION));
  64.  
  65.         if (saves == null || saves.length == 0) {
  66.             System.out.println("No save files found.");
  67.             return null;
  68.         }
  69.  
  70.         System.out.println("Which file? (-1 : none)");
  71.         for (int i = 0; i < saves.length; i++) {
  72.             System.out.println("(" + i + ") " + saves[i].getName());
  73.         }
  74.  
  75.         int choice = scanner.nextInt();
  76.         if (choice >= 0 && choice < saves.length) {
  77.             return loadGame(saves[choice].getName());
  78.         }
  79.         return null;
  80.     }
  81.  
  82.     /**
  83.      * Returns the full path of the file.
  84.      *
  85.      * @param filename The name of the file.
  86.      * @return The full path of the file.
  87.      */
  88.     private static String getFullPath(String filename) {
  89.         return SAVED_GAMES_DIRECTORY +
  90.                 File.separator +
  91.                 filename +
  92.                 (filename.endsWith(SAVE_FILE_EXTENSION) ? "" : SAVE_FILE_EXTENSION);
  93.     }
  94.  
  95.     /**
  96.      * Ensures that the directory exists.
  97.      */
  98.     private static void ensureDirectory() {
  99.         File saveDir = new File(SAVED_GAMES_DIRECTORY);
  100.         if (!saveDir.exists()) {
  101.             saveDir.mkdir();
  102.         }
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement