Skip to content
Snippets Groups Projects
Select Git revision
  • a7ef5800930bb3d46ee19440b7dbc758c9503dfe
  • master default protected
2 results

App.java

Blame
  • App.java 6.28 KiB
    package ch.hepia;
    
    import java.io.File;
    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;
            int computerStrategy = 1;
            boolean loadSave = false;
            String filePath = "save.csv";
    
            File f = new File(filePath);
    
            // Check if a save file exists
            if (f.exists() && !f.isDirectory()) {
    
                char choice = 'x';
    
                System.out.print("Do you want to load last game save (y/n) ? ");
    
                while (true) {
    
                    choice = App.in.next().charAt(0);
    
                    // Choices are checked with available one for confirmation
                    if (choice == 'y' || choice == 'n') {
                        break;
                    } else {
                        System.out.println(App.ANSI_YELLOW + "Please enter a valid choice." + App.ANSI_RESET);
                    }
                }
    
                if (choice == 'y') {
                    loadSave = true;
                }
            }
    
            GameManager BlackJack;
    
            if (loadSave) {
    
                // Load save to create game
                Save Game = new Save();
    
                BlackJack = new GameManager(Game.loadMoney(), Game.loadStrategy());
    
            } else {
                // 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);
                    }
                }
    
                // Ask if the Computer players should be smart of not
                if (numPlayers > 1) {
                    while (true) {
    
                        System.out.print("Should the Compter players be [1] Smart or [2] Stupid ? ");
    
                        // Check that the input is an valid int
                        while (!App.in.hasNextInt()) {
                            System.out
                                    .println(App.ANSI_YELLOW + "Please enter a valid choice." + App.ANSI_RESET);
                            System.out.print("Should the Compter players be [1] Smart or [2] Stupid ? ");
                            App.in.next();
                        }
    
                        computerStrategy = App.in.nextInt();
    
                        // Check if the value i s valide
                        if (computerStrategy == 1 || computerStrategy == 2) {
                            System.out.print("");
                            break;
                        } else {
                            System.out.println(App.ANSI_YELLOW + "Please enter a valid choice." + App.ANSI_RESET);
                        }
                    }
                }
    
                // Create a new game
                BlackJack = new GameManager(numPlayers, startMoney, computerStrategy);
            }
    
            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();
    
        }
    
    }