Skip to content

Commit

Permalink
Merge pull request hhaslam11#123 from SaurabhKukreja/sk-refactor-TF
Browse files Browse the repository at this point in the history
Refactor Print commands
  • Loading branch information
hhaslam11 authored Apr 26, 2021
2 parents 1be9da1 + 94776a3 commit 627cfd6
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 71 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------
Expand Down
12 changes: 12 additions & 0 deletions src/com/hotmail/kalebmarc/textfighter/main/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.hotmail.kalebmarc.textfighter.main;

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 = "";

}
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
84 changes: 84 additions & 0 deletions src/com/hotmail/kalebmarc/textfighter/main/GameUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.hotmail.kalebmarc.textfighter.main;

import java.util.List;

public class GameUtils {

/*
* 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 showPopup(String header, String subheader, List<String> message, List<String> inputs) {
Ui.cls();
println(center(Constants.DASH_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++) {
println(center(message.get(i)));
}

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
String input = input_num + "- " + inputs.get(i);
println(leftAlign(input));
}

println(center(Constants.DASH_DIVIDER));
}

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 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);

while (output.length() < size) {
output.append(pad);
}
output.append("|");
return output.toString();
}

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);
}
output.append("|");
return output.toString();
}
}
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 627cfd6

Please sign in to comment.