Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SaurabhKukreja committed Apr 21, 2021
1 parent 7784bbc commit 34cc565
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 95 deletions.
8 changes: 8 additions & 0 deletions src/com/hotmail/kalebmarc/textfighter/main/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
43 changes: 16 additions & 27 deletions src/com/hotmail/kalebmarc/textfighter/main/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -364,27 +358,22 @@ 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()) {
Ui.cls();
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 {
Expand Down
35 changes: 10 additions & 25 deletions src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

public class StringUtils {
public class GameUtils {

/*
* This is a Utility Class for modifying String
Expand All @@ -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<String> message, List<String> 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++) {
Expand All @@ -46,7 +35,7 @@ public static void showPopup(String header, String subheader, List<String> 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));
}
Expand All @@ -55,10 +44,6 @@ public static void showPopup(String header, String subheader, List<String> 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, ' ');
}
Expand Down
73 changes: 32 additions & 41 deletions src/com/hotmail/kalebmarc/textfighter/main/Menu.java
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions src/com/hotmail/kalebmarc/textfighter/main/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -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..");
Expand All @@ -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();
Expand Down

0 comments on commit 34cc565

Please sign in to comment.