package vorbereitungen.MonteCarlo;

import java.util.concurrent.ThreadLocalRandom;

public class Simulation{
    int max_attempts;
    int current_attempts;
    double starting_bankroll;
    double current_bankroll;
    double simulated_betsize;

    public Simulation(int max_atts, int bankroll, double betsize) {
         this.max_attempts = max_atts;
         this.current_attempts = 1;
         this.starting_bankroll = bankroll;
         this.current_bankroll = bankroll;
         this.simulated_betsize = betsize;
    }

    public static int roll() {
        return ThreadLocalRandom.current().nextInt(1, 8);
    }

    public static double calculate(int player_roll, int dealer_roll) {
        int diff = dealer_roll - player_roll;
        if (diff >= 1) {return 0;}
        else if (diff == 0) {return 1;}
        else if (diff == -6) {return 2;}
        else {return 1.5;}
    }

    public void handle() {
        if (this.simulated_betsize > this.current_bankroll) {
            System.out.println("|===Out of money===|");
            System.out.printf("|Attempts: %d      |\n", this.current_attempts);
            System.out.println("===================|");
            System.exit(2137);
        }
        this.current_bankroll -= this.simulated_betsize;
        int player_roll = roll();
        int house_roll = roll();
        double mult = calculate(player_roll, house_roll);
        this.current_bankroll += this.simulated_betsize*mult;
        this.current_attempts++;
    }

    public void print_progress() {
        double bankroll_percentage = (this.current_bankroll/this.starting_bankroll)*100;
        double attempt_percentage = (this.current_attempts/this.max_attempts)*100;
        System.out.printf("\033[2J"); // Clear screen
        System.out.println("|===========|");
        System.out.printf("|Current Attempt: %d/%d|\n", this.current_attempts, this.max_attempts);
        System.out.printf("|Current Bankroll: %f|\n", this.current_bankroll);
        System.out.printf("|%f%% left of starting bankroll\n|", bankroll_percentage);
        System.out.printf("|%f%% done|", attempt_percentage);
        System.out.println("|===========|");
    }

    public void run(){
        for (int i = 0; i < max_attempts; i++) {
            handle();
            if (this.current_attempts % 7500 == 0 || this.current_attempts == 1) {
                print_progress();
            }
        }
    }
}
