From cbc5bd11582f8b0d3720f0d91d2c7638d98916fa Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Fri, 31 Jul 2020 01:27:10 -0600 Subject: [PATCH] Created the story interpreter and an example story. --- scripts/scripta | 30 ++++++++++++ src/Game.java | 119 ++++++++++++++++++++++++++++++++++++++++++++++-- src/Main.java | 4 +- 3 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 scripts/scripta diff --git a/scripts/scripta b/scripts/scripta new file mode 100644 index 0000000..17f5014 --- /dev/null +++ b/scripts/scripta @@ -0,0 +1,30 @@ +WELCOME: +DLG Hi, welcome to the game. Are you ready to begin? +OPT YES -> BEGIN +OPT NO -> BEGINANYWAY +OPT IDK -> BEGINANYWAY +FIN + + +BEGIN: +DLG Okidokey, lets get started + +BEGINANYWAY: +DLG Well too bad we are starting anyway. +DLG Now pick a class. +OPT ROGUE -> ROGUE +OPT WARRIOR -> WARRIOR +OPT MAGE -> MAGE +FIN + +ROGUE: +DLG AH the rogue. Agile and clever +HALT + +WARRIOR: +DLG Warrior powerful and brave +HALT + +MAGE: +DLG You are a mage, Master of the Arcana. +HALT diff --git a/src/Game.java b/src/Game.java index 763e2eb..2d70b06 100644 --- a/src/Game.java +++ b/src/Game.java @@ -1,4 +1,8 @@ -import java.util.Scanner; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.*; /** * This class creates an object that will contain the core variables and methods of the game. @@ -9,6 +13,7 @@ public class Game { public Game() { this.playerName = "Jeff"; // name defaults to jeff. + } /** @@ -16,11 +21,13 @@ public class Game { */ - public void start() { + public void start() throws IOException { prntDlg("WELCOME TO DECISIONS"); // Welcome message playerName = usrInpt("Please enter your name:"); prntDlg("Well hello, $N"); + + readStory("scripts/scripta"); } /** @@ -29,17 +36,121 @@ public class Game { * * @param text the text you want to display. */ + + // TODO: Make this more readable. public void prntDlg(String text) { text = text.replace("$N", playerName); - System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"); + System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println(text); - System.out.println("\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } + /** + * USER INPUT: it gets user input and returns it for use. + * + * @param text The text you want to prompt them with. + * @return The text the user entered. + */ public String usrInpt(String text) { Scanner myObj = new Scanner(System.in); // Create a Scanner object prntDlg(text); return myObj.nextLine(); } + + /** + * This function will interpret a story file you created. + *

+ * The story file must follow a fairly strict syntax. + * This is documented separately. + * This function determines how our story files will be read. + * It's VERY IMPORTANT to understand this function + * + * @param storyName the name of the story file you have stored. + * @throws IOException if something goes wrong. + */ + public void readStory(String storyName) throws IOException { + // This is the name of the next branch to read. + // It is updated in the FIN command. + String nextBranch; + + + // This is an array of options to be compiled for the player to choose from. + // They are updated using the OPT command. + // They are compiled in the FIN then cleared afterwards for the next branch. + ArrayList options = new ArrayList<>(); + + + // Initialize a buffered reader to read the file line by line without using too much memory. + File file = new File(storyName); + BufferedReader br = new BufferedReader(new FileReader(file)); + + // This is the string that will store the current line for interpretation. + String st; + + // read the first line of the file and make sure it ends with a colon + st = br.readLine(); + if (!st.endsWith(":")) { + return; + } + + // Now let's begin the story reading it line by line. + while ((st = br.readLine()) != null) { + + + // HALT: will end the story immediately + if (st.startsWith("HALT")) { + break; + } + + // DLG: will display some dialogue using the prntDlg function + if (st.startsWith("DLG")) { + prntDlg(st.replace("DLG", "")); + } + + // OPT: Will create an option and the -> will point to the tag it should take you to. + else if (st.startsWith("OPT")) { + st = st.replaceAll("OPT", ""); + st = st.replaceAll(" ", ""); + options.add(st); + } + + + // FIN will compile the options and prompt the user to pick one. + // it will then take the interpreter to that option. + else if (st.startsWith("FIN")) { + if (options.isEmpty()) { + continue; + } + + // compiles the decision into a readable dialogue. + // TODO: make this more readable. + StringBuilder optionList = new StringBuilder("Pick One:\n"); + for (int i = 0; i < options.size(); i++) { + optionList.append(i).append(" : "); + optionList.append(options.get(i).split("->")[0]); + optionList.append("\n"); + } + + // print out the compiled options. + prntDlg(optionList.toString()); + + // get the users decision + String decision = usrInpt("enter the number for your decision"); + + // set the next branch based on the user option. + nextBranch = options.get(Integer.parseInt(decision)).split("->")[1]; + + // take our reader to the next branch. + while ((st = br.readLine()) != null) { + if (st.endsWith(":") && st.replace(":", "").equals(nextBranch)) { + break; + } + } + + options = new ArrayList<>(); // clear the options for next branch. + } + } + + } } diff --git a/src/Main.java b/src/Main.java index d8628a7..95090a6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,7 @@ +import java.io.IOException; + public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { Game NewGame = new Game(); System.out.println("This game was developed by GodEmperorDoge, X, Y, and Z"); NewGame.start();