Skip to content

Commit

Permalink
Various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bkis committed Jan 25, 2022
1 parent 436c090 commit c4e7748
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
20 changes: 14 additions & 6 deletions src/idh/hotseatgames/HotSeatGames.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ private HotSeatGames() {
StringUtils.slideInText(
ResourceReader.readResource("intro_banner.ascii", getClass()),
150, 4);

StringUtils.printLineBreaks(1);

this.input = UserInput.instance(); // create user input wrapper instance
this.players = createPlayers(); // create players
this.games = createGames(); // create list of games to play
this.startGames(); // start games
this.input.close(); // close user input resources
input = UserInput.instance(); // create user input wrapper instance
boolean restart = false;

do {
players = createPlayers(); // create players
games = createGames(); // create list of games to play
startGames(); // start games
// ask if player want to play another round
restart = !input.prompt("> Do you want to play another round? (y/n)", "[yn]")
.toLowerCase()
.equals("n");
} while (restart);

input.close(); // close user input resources
}

private void startGames() {
Expand Down
6 changes: 3 additions & 3 deletions src/idh/hotseatgames/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ public static String layout(String text, int charsPerLine, int pad) {

/**
* Returns a sequence of blank spaces of the given length {@code width}.
* @param width The padding width / length of blank spaces sequence (must be greater than 0)
* @param width The padding width / length of blank spaces sequence (must be at least 0)
* @return The resulting blank spaces sequence
*/
public static String padString(int width) {
if (width <= 0)
throw new IllegalArgumentException("Padding width must be > 0.");
if (width < 0)
throw new IllegalArgumentException("Padding width must be >= 0.");
return width > 0 ? String.format("%" + width + "s", "") : "";
}

Expand Down
10 changes: 6 additions & 4 deletions src/idh/hotseatgames/utils/UserInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class UserInput {
* A private constructor (can only be called from within this class!)
*/
private UserInput() {
this.scanner = new Scanner(System.in);
scanner = new Scanner(System.in);
}

/**
Expand All @@ -41,7 +41,7 @@ public static UserInput instance() {
*/
public String prompt(String prompt) {
System.out.print(prompt.trim() + " ");
return this.scanner.nextLine();
return scanner.nextLine();
}

/**
Expand Down Expand Up @@ -77,10 +77,12 @@ public int prompt(String prompt, int min, int max) {
}

/**
* Cleans up the resources used by this instance
* Cleans up the resources used by this instance.
* This method should NEVER be called from within a game, as the input stream is managed
* by the engine. Once it is closed, the engine loses access to the input stream (bad!).
*/
public void close() {
this.scanner.close();
scanner.close();
}

}

0 comments on commit c4e7748

Please sign in to comment.