Select Git revision
App.java
App.java 3.91 KiB
package ch.hepia;
import java.util.Scanner;
public class App {
// Scanner will be used all along to take user inputs in the terminal
public static final Scanner in = new Scanner(System.in);
// All Color choices to be used in terminal prints
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
public static void BlackJack() {
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("*~-~* " + App.ANSI_RED + "BlackJack" + App.ANSI_RESET + " *~-~*\n");
int numPlayers;
int startMoney;
// Ask the number of players until a valide answer is given
while (true) {
System.out.print("How many players ? ");
// Check that the input is an valid int
while (!App.in.hasNextInt()) {
System.out.println(
App.ANSI_YELLOW + "Please enter a valid number for the number of players." + App.ANSI_RESET);
System.out.print("How many players ? ");
App.in.next();
}
numPlayers = App.in.nextInt();
// Player count must be between 1 and 7
if (numPlayers >= 1 && numPlayers <= 7) {
System.out.print("");
break;
} else {
System.out.println(
App.ANSI_YELLOW + "Please enter a number of players between 1 and 7." + App.ANSI_RESET);
}
}
// Ask the start money count for each players until a valide answer is given
while (true) {
System.out.print("How much money do you start with ? ");
// Check that the input is an valid int
while (!App.in.hasNextInt()) {
System.out.println(App.ANSI_YELLOW + "Please enter a valid number for your Money." + App.ANSI_RESET);
System.out.print("How much money do you start with ? ");
App.in.next();
}
startMoney = App.in.nextInt();
// Starting money must be more or equal to minimum bid amount
if (startMoney >= 10) {
System.out.print("");
break;
} else {
System.out.println(App.ANSI_YELLOW + "Please enter an amount above 10." + App.ANSI_RESET);
}
}
// Create a new game
GameManager BlackJack = new GameManager(numPlayers, startMoney);
boolean GameOver = false;
// Game run's in 3 Phases :
// Phase 1 (StartTurn):
// Ask the player how much he wan't to bid
// Phase 2 (PlayTurn):
// Ask the player for all his interactions with this cards
// Phase 3 (ResolveTurn):
// Make the Dealer pick cards
// Do all the math for the player gains and losses
// Check if the player can continue et prepare for the next round
while (!GameOver) {
BlackJack.StartTurn();
BlackJack.PlayTurn();
GameOver = BlackJack.ResolveTurn();
}
// The player has less than the minimum bid amount allowed = Game Over
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("*~-~* " + App.ANSI_RED + "BlackJack" + App.ANSI_RESET + " *~-~*\n");
System.out.println(App.ANSI_PURPLE + "Game Over !" + App.ANSI_RESET);
App.in.close();
}
public static void main(String[] args) {
// Play 1 BlackJack Game
BlackJack();
}
}