From 7784bbc1db04c1c8885160c10e8ebbf83ff9613d Mon Sep 17 00:00:00 2001 From: Saurabh Kukreja Date: Tue, 20 Apr 2021 22:52:29 -0400 Subject: [PATCH 1/3] Refactor Print commands --- .../kalebmarc/textfighter/main/Constants.java | 5 + .../kalebmarc/textfighter/main/GameUtils.java | 99 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/com/hotmail/kalebmarc/textfighter/main/Constants.java create mode 100644 src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java diff --git a/src/com/hotmail/kalebmarc/textfighter/main/Constants.java b/src/com/hotmail/kalebmarc/textfighter/main/Constants.java new file mode 100644 index 00000000..e55824f9 --- /dev/null +++ b/src/com/hotmail/kalebmarc/textfighter/main/Constants.java @@ -0,0 +1,5 @@ +package com.hotmail.kalebmarc.textfighter.main; + +public class Constants { + +} diff --git a/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java b/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java new file mode 100644 index 00000000..05513b76 --- /dev/null +++ b/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java @@ -0,0 +1,99 @@ +package com.hotmail.kalebmarc.textfighter.main; + +import java.util.List; + +public class StringUtils { + + /* + * This is a Utility Class for modifying String + */ + public static void print(String input) { + System.out.print(input); + } + + public static void println(String input) { + print(input + "\n"); + } + + public static void println(int input) { + print(input + "\n"); + } + + public static void println(boolean input) { + print(input + "\n"); + } + + public static void println(double input) { + print(input + "\n"); + } + + public static void println() { + print("\n"); + } + + public static void showPopup(String header, String subheader, List message, List inputs) { + Ui.cls(); + println(center(Constants.DASH_DIVIDER)); + println(center(header)); + println(center(subheader)); + println(center(Constants.STAR_DIVIDER)); + println(center(Constants.EMPTY_SPACE_BOX)); + + for (int i = 0; i < message.size(); i++) { + println(center(message.get(i))); + } + + println(center(Constants.EMPTY_SPACE_BOX)); + + for (int i = 0; i < inputs.size(); i++) { + int input_num = i+1; + String input = input_num + "- " + inputs.get(i); + println(leftAlign(input)); + } + println(center(Constants.DASH_DIVIDER)); + println(center(Constants.BRAND_NAME)); + println(center(Constants.DASH_DIVIDER)); + } + + public static String center(String s, int size) { + return center(s, size, ' '); + } + + public static String center(String s) { + return center(s, 45, ' '); + } + public static String leftAlign(String s) { + return leftAlign(s, 45, ' '); + } + + public static String center(String s, int size, char pad) { + if (s == null || size <= s.length()) { + return s; + } + + StringBuilder sb = new StringBuilder(size); + sb.append("|"); + sb.append(String.valueOf(pad).repeat((size - s.length()) / 2)); + sb.append(s); + while (sb.length() < size) { + sb.append(pad); + } + sb.append("|"); + return sb.toString(); + } + + public static String leftAlign(String s, int size, char pad) { + if (s == null || size <= s.length()) { + return s; + } + StringBuilder sb = new StringBuilder(size); + sb.append("|"); + sb.append(String.valueOf(pad).repeat(10)); + sb.append(s); + while (sb.length() < size) { + sb.append(pad); + } + sb.append("|"); + return sb.toString(); + } +} From 34cc565608b643f1fbc10920ef9ef3dc08fef8b9 Mon Sep 17 00:00:00 2001 From: Saurabh Kukreja Date: Tue, 20 Apr 2021 23:00:10 -0400 Subject: [PATCH 2/3] refactor --- .../kalebmarc/textfighter/main/Constants.java | 8 ++ .../kalebmarc/textfighter/main/Game.java | 43 ++++------- .../kalebmarc/textfighter/main/GameUtils.java | 35 +++------ .../kalebmarc/textfighter/main/Menu.java | 73 ++++++++----------- .../kalebmarc/textfighter/main/Start.java | 5 +- 5 files changed, 69 insertions(+), 95 deletions(-) diff --git a/src/com/hotmail/kalebmarc/textfighter/main/Constants.java b/src/com/hotmail/kalebmarc/textfighter/main/Constants.java index e55824f9..723238de 100644 --- a/src/com/hotmail/kalebmarc/textfighter/main/Constants.java +++ b/src/com/hotmail/kalebmarc/textfighter/main/Constants.java @@ -2,4 +2,12 @@ public class Constants { + public static final String WELCOME_HEADER = "WELCOME TO TEXT FIGHTER"; + public static final String HEADER = "TEXT FIGHTER"; + public static final String SUB_HEADER = "A Text-Based Fighting Game"; + public static final String STAR_DIVIDER = "********************************************"; + public static final String DASH_DIVIDER = "__________________________________________"; + public static final String EMPTY_SPACE_BOX = ""; + public static final String BRAND_NAME = "www.TextFighter.tk"; + } diff --git a/src/com/hotmail/kalebmarc/textfighter/main/Game.java b/src/com/hotmail/kalebmarc/textfighter/main/Game.java index 3496eb01..03fe57ae 100644 --- a/src/com/hotmail/kalebmarc/textfighter/main/Game.java +++ b/src/com/hotmail/kalebmarc/textfighter/main/Game.java @@ -10,13 +10,12 @@ import static com.hotmail.kalebmarc.textfighter.player.Health.upgrade; import static com.hotmail.kalebmarc.textfighter.player.Settings.menu; import static com.hotmail.kalebmarc.textfighter.player.Settings.setDif; +import static java.util.Arrays.asList; public class Game { // docschorsch added boolean to indicate if a game had been started private static boolean gameStarted = false; - private Game() { - } // getter to indicate if game had been started for menu.load() public static boolean hadGameStarted() { return gameStarted; @@ -61,29 +60,24 @@ public static boolean hadGameStarted() { private static Scanner scan = new Scanner(System.in); - public static void start() { + public void start() { /* * Asks if the user wants to load from the save file */ // docschorsch inserted new exit option back to Menu.load() - Ui.cls(); - Ui.println("____________________________________________"); - Ui.println("| |"); - Ui.println("| Do you want to load your game |"); - Ui.println("| from save file? |"); - Ui.println("| |"); - Ui.println("| 0) Exit to Main |"); - Ui.println("| 1) Yes |"); - Ui.println("| 2) No, Start a new game |"); - Ui.println("|___________________________________________|"); + GameUtils.showPopup(Constants.HEADER, + Constants.SUB_HEADER, + asList("Do you want to load your game", "from save file?"), + asList("Exit to Main", "Yes", "No") + ); int choice = Ui.getValidInt(); switch(choice){ - case 0: return; - case 1: + case 1: return; + case 2: if(Saves.savesPrompt()) { // docschorsch savesPrompt() true only if not exited --> game started with loaded player gameStarted = true; @@ -364,16 +358,11 @@ private static String getDifficulty() { * they want to play on. Sets variables * according. */ - Ui.cls(); - Ui.println("_____________________________________________"); - Ui.println("| |"); - Ui.println("| What difficulty would you |"); - Ui.println("| like to play on? |"); - Ui.println("| |"); - Ui.println("| 0) Exit |"); - Ui.println("| 1) Easy |"); - Ui.println("| 2) Hard |"); - Ui.println("|___________________________________________|"); + GameUtils.showPopup(Constants.HEADER, + Constants.SUB_HEADER, + asList("What difficulty would you","like to play on?"), + asList("Exit","Easy","Hard") + ); //docschorsch added empty default as new 0) option Exit if (!scan.hasNextInt()) { @@ -381,10 +370,10 @@ private static String getDifficulty() { return "Exit"; } else { int difficultyChoice = scan.nextInt(); - if (difficultyChoice == 1) { + if (difficultyChoice == 2) { Ui.cls(); return "Easy"; - } else if (difficultyChoice == 2) { + } else if (difficultyChoice == 3) { Ui.cls(); return "Hard"; } else { diff --git a/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java b/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java index 05513b76..64e2c93e 100644 --- a/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java +++ b/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java @@ -2,7 +2,7 @@ import java.util.List; -public class StringUtils { +public class GameUtils { /* * This is a Utility Class for modifying String @@ -15,28 +15,17 @@ public static void println(String input) { print(input + "\n"); } - public static void println(int input) { - print(input + "\n"); - } - - public static void println(boolean input) { - print(input + "\n"); - } - - public static void println(double input) { - print(input + "\n"); - } - - public static void println() { - print("\n"); - } - public static void showPopup(String header, String subheader, List message, List inputs) { Ui.cls(); println(center(Constants.DASH_DIVIDER)); - println(center(header)); - println(center(subheader)); - println(center(Constants.STAR_DIVIDER)); + if(!header.isEmpty()) { + println(center(header)); + } + + if(!subheader.isEmpty()) { + println(center(subheader)); + println(center(Constants.STAR_DIVIDER)); + } println(center(Constants.EMPTY_SPACE_BOX)); for (int i = 0; i < message.size(); i++) { @@ -46,7 +35,7 @@ public static void showPopup(String header, String subheader, List messa println(center(Constants.EMPTY_SPACE_BOX)); for (int i = 0; i < inputs.size(); i++) { - int input_num = i+1; + int input_num = i+1; // This addition is because our switch case starts from Case 1 and not Case 0 String input = input_num + "- " + inputs.get(i); println(leftAlign(input)); } @@ -55,10 +44,6 @@ public static void showPopup(String header, String subheader, List messa println(center(Constants.DASH_DIVIDER)); } - public static String center(String s, int size) { - return center(s, size, ' '); - } - public static String center(String s) { return center(s, 45, ' '); } diff --git a/src/com/hotmail/kalebmarc/textfighter/main/Menu.java b/src/com/hotmail/kalebmarc/textfighter/main/Menu.java index 02f720d3..5bfa1e8a 100644 --- a/src/com/hotmail/kalebmarc/textfighter/main/Menu.java +++ b/src/com/hotmail/kalebmarc/textfighter/main/Menu.java @@ -1,47 +1,38 @@ package com.hotmail.kalebmarc.textfighter.main; +import static java.util.Arrays.asList; + class Menu { - - private Menu(){} - public static void load(){ - while(true){ - - Ui.cls(); - //Menu Screen - Ui.println("_____________________________________________"); - Ui.println("| WELCOME TO TEXT FIGHTER |"); - Ui.println("| A Text-Based Fighting Game |"); - Ui.println("|*******************************************|"); - Ui.println("| |"); - Ui.println("| To get started, Type in a number below |"); - Ui.println("| and press enter. |"); - Ui.println("| |"); - Ui.println("| 1) Start Game |"); - Ui.println("| 2) About Game |"); - Ui.println("| 3) Exit |"); - Ui.println("| www.TextFighter.tk |"); - Ui.println("|___________________________________________|"); - switch (Ui.getValidInt()) { - case 1: - Ui.cls(); - Ui.guiEnabled = false; - Game.start(); + public void load() { + while (true) { + + //Menu Screen + GameUtils.showPopup(Constants.WELCOME_HEADER, + Constants.SUB_HEADER, + asList("To get started, Type in a number below", "and press enter."), + asList("Start Game", "About Game") + ); + + switch (Ui.getValidInt()) { + case 1: + Ui.guiEnabled = false; + new Game().start(); - //Saves the game before exiting - // docschorsch: save() only if player is not program default player amd game had started - if(User.getPlayerDefault()>0 && Game.hadGameStarted()) { - Saves.save(); - } - break; - case 2: - About.view(false); - break; - case 3: - return; - default: - break; - } - }//Loop - }//Method + //Saves the game before exiting + // docschorsch: save() only if player is not program default player amd game had started + if (User.getPlayerDefault() > 0 && Game.hadGameStarted()) { + Saves.save(); + } + break; + case 2: + About.view(false); + break; + case 3: + return; + default: + break; + } + }//Loop + }//Method }//Class diff --git a/src/com/hotmail/kalebmarc/textfighter/main/Start.java b/src/com/hotmail/kalebmarc/textfighter/main/Start.java index b687e06b..90f8d29e 100644 --- a/src/com/hotmail/kalebmarc/textfighter/main/Start.java +++ b/src/com/hotmail/kalebmarc/textfighter/main/Start.java @@ -2,7 +2,7 @@ class Start { - public static void main(String args[]) { + public static void main(String[] args) { if (args.length != 0 && args[0].equalsIgnoreCase("nogui")) Ui.guiEnabled = false; Ui.println("Loading.."); @@ -13,7 +13,8 @@ public static void main(String args[]) { } //Runs the game - Menu.load(); + Menu menu = new Menu(); + menu.load(); //Clears Console Ui.cls(); From 94776a339a5a10cc03eb07c9bac8464cce2e38ac Mon Sep 17 00:00:00 2001 From: Saurabh Kukreja Date: Mon, 26 Apr 2021 17:48:33 -0400 Subject: [PATCH 3/3] resolving comments --- README.md | 3 +- .../kalebmarc/textfighter/main/Constants.java | 1 - .../kalebmarc/textfighter/main/GameUtils.java | 54 +++++++++---------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 1c6e831d..26914f3c 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,8 @@ Thanks for all your work! [CLEMENTJOHNSHAJI](https://github.com/CLEMENTJOHNSHAJI) [alfr3dosv](https://github.com/alfr3dosv) [xdvrx1](https://github.com/xdvrx1) -[R1ndT](https://github.com/R1ndT) +[R1ndT](https://github.com/R1ndT) +[SaurabhKukreja](https://github.com/SaurabhKukreja) License -------------- diff --git a/src/com/hotmail/kalebmarc/textfighter/main/Constants.java b/src/com/hotmail/kalebmarc/textfighter/main/Constants.java index 723238de..e7c86c90 100644 --- a/src/com/hotmail/kalebmarc/textfighter/main/Constants.java +++ b/src/com/hotmail/kalebmarc/textfighter/main/Constants.java @@ -8,6 +8,5 @@ public class Constants { public static final String STAR_DIVIDER = "********************************************"; public static final String DASH_DIVIDER = "__________________________________________"; public static final String EMPTY_SPACE_BOX = ""; - public static final String BRAND_NAME = "www.TextFighter.tk"; } diff --git a/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java b/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java index 64e2c93e..95b22490 100644 --- a/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java +++ b/src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java @@ -35,12 +35,11 @@ public static void showPopup(String header, String subheader, List messa println(center(Constants.EMPTY_SPACE_BOX)); for (int i = 0; i < inputs.size(); i++) { - int input_num = i+1; // This addition is because our switch case starts from Case 1 and not Case 0 + int input_num = i + 1; // This addition is because our switch case starts from Case 1 and not Case 0 String input = input_num + "- " + inputs.get(i); println(leftAlign(input)); } - println(center(Constants.DASH_DIVIDER)); - println(center(Constants.BRAND_NAME)); + println(center(Constants.DASH_DIVIDER)); } @@ -51,34 +50,35 @@ public static String leftAlign(String s) { return leftAlign(s, 45, ' '); } - public static String center(String s, int size, char pad) { - if (s == null || size <= s.length()) { - return s; - } + public static String center(String input, int size, char pad) { + if (input == null || size <= input.length()) + return input; + + StringBuilder output = new StringBuilder(size); + output.append("|"); + output.append(String.valueOf(pad).repeat((size - input.length()) / 2)); + output.append(input); - StringBuilder sb = new StringBuilder(size); - sb.append("|"); - sb.append(String.valueOf(pad).repeat((size - s.length()) / 2)); - sb.append(s); - while (sb.length() < size) { - sb.append(pad); + while (output.length() < size) { + output.append(pad); } - sb.append("|"); - return sb.toString(); + output.append("|"); + return output.toString(); } - public static String leftAlign(String s, int size, char pad) { - if (s == null || size <= s.length()) { - return s; - } - StringBuilder sb = new StringBuilder(size); - sb.append("|"); - sb.append(String.valueOf(pad).repeat(10)); - sb.append(s); - while (sb.length() < size) { - sb.append(pad); + public static String leftAlign(String input, int size, char pad) { + if (input == null || size <= input.length()) + return input; + + StringBuilder output = new StringBuilder(size); + output.append("|"); + output.append(String.valueOf(pad).repeat(10)); + output.append(input); + + while (output.length() < size) { + output.append(pad); } - sb.append("|"); - return sb.toString(); + output.append("|"); + return output.toString(); } }