I started making a text-based RPG game and before I spend a lot of time on it, I wanted to know if there is a better way to make it. Currently I'm using switches to flow through the game, I also know some people use a lot of if
statements, but is there a more efficient way?
Main
public class Main { public static GameSave GS = new GameSave(); public static Scanner in = new Scanner(System.in); public static String question; public static String name; public static String user = ""; public static void main(String[] args) { question = GS.LoadGame(); name = GS.LoadName(); //Game Loop while (true) { if (user.equalsIgnoreCase("exit")){ System.out.println("Closing game."); System.exit(0); }else{ switch (question) { case "0": System.out.println("Story...\n\nWhat is your name?"); question = "1"; break; case "1": user = in.next(); name = user; question = "1A"; GS.SaveName(name); GS.SaveGame("1A"); break; case "1A": System.out.println("Are you sure that is your name?\n[1]Yes\n[2]No"); question = "2"; break; case "2": switch (user = in.next()) { case "1": question = "2A"; GS.SaveGame(question); break; case "2": System.out.println("\n\n"); question = "0"; break; default: System.out.println("\nInput not accepted, try again."); question = "1A"; break; } break; case "2A": System.out.println("Hello, " + name + "\n[1]Hello...\n[2]Who the hell are you?"); question = "3"; break; case "3": } } } } }
GameSave
public class GameSave { public static Properties prop = new Properties(); public void SaveGame(String point){ try{ prop.setProperty("Save_Point", point); prop.store(new FileOutputStream("config.prop"), null); }catch (IOException e){ } } public void SaveName(String playerName){ try{ prop.setProperty("Save_Name", playerName); prop.store(new FileOutputStream("config.prop"), null); }catch (IOException e){ } } public String LoadGame(){ String line = ""; try{ prop.load(new FileInputStream("config.prop")); line = prop.getProperty("Save_Point"); }catch (IOException e){ try{ prop.setProperty("Save_Point", "0"); prop.store(new FileOutputStream("config.prop"), null); try{ prop.load(new FileInputStream("config.prop")); line = prop.getProperty("Save_Point"); }catch (IOException exe){ } }catch (IOException ex){ } } return line; } public String LoadName(){ String name = ""; try{ prop.load(new FileInputStream("config.prop")); name = prop.getProperty("Save_Name"); }catch (IOException e){ try{ prop.setProperty("Save_Name", ""); prop.store(new FileOutputStream("config.prop"), null); try{ prop.load(new FileInputStream("config.prop")); name = prop.getProperty("Save_Name"); }catch (IOException exe){ } }catch (IOException ex){ } } return name; } }