// Arup Guha
// 2/4/2026
// Solution to COP 3330 Program 3: Casino

import java.util.*;

public class casino {

	public static Random rndObj;
	public static Scanner stdin;

	public static void main(String[] args) {

		// All games will use these objects.
		rndObj = new Random();
		stdin = new Scanner(System.in);

		// Initial set up and greeting.
		int chips = 100;
		System.out.println("Welcome to Arup's Casino. You begin with 100 chips. Buenos suerte!");
		int choice = getMenuChoice();

		// Process user choice.
		while (choice != 3 && chips > 0) {

			// Craps.
			if (choice == 1) {

				// Get the bet and play.
				System.out.println("Time to play craps!");
				int mybet = getBet(chips);
				boolean result = craps();

				// Adjust chips, print new chip value.
				if (result)
					chips += mybet;
				else
					chips -= mybet;
				System.out.println("Now, after the game, you have "+chips+" number of chips.");

			}

			// Roulette
			else if (choice == 2) {

				// Get the bet and play.
				System.out.println("Time to play roulette!");
				int mybet = getBet(chips);
				int mult = roulette();

				// Adjust chips, print new chip value.
				chips += mult*mybet;
				System.out.println("Now, after the game, you have "+chips+" number of chips.");
			}

			// Get next choice, only if you have chips.
			if (chips > 0)
				choice = getMenuChoice();
		}
		
		// Extra message.
		if (chips == 0) 
			System.out.println("Sorry, since you don't have any chips left, you have to quit.");
		

		// Ending message.
		System.out.println("Thanks for visiting Arup's casino. You ended with "+chips+" number of chips.");
	}

	// Prints the menu and gets the user's choice.
	public static int getMenuChoice() {

		int choice = 0;

		do {

			// Print the choices and get the user's answer.
			System.out.println("What would you like to do?");
			System.out.println("1. Play craps.");
			System.out.println("2. Play roulette.");
			System.out.println("3. Cash out and quit.");
			choice = stdin.nextInt();

			// Error message.
			if (choice < 1 || choice > 3)
				System.out.println("Sorry that is invalid. Please try again.");

		} while (choice < 1 || choice > 3);

		// When we get here, we're good.
		return choice;
	}

	// Let's the user play one game of craps and returns true iff they won, false otherwise.
	public static boolean craps() {

		// Show first roll.
		int die1 = rndObj.nextInt(6) + 1;
		int die2 = rndObj.nextInt(6) + 1;
		int total = die1 + die2;
		System.out.println("You rolled "+die1+" and "+die2+" for a total of "+total);

		// Screen out the win.
		if (total == 7 || total == 11) {
			System.out.println("Great, you win on with the first roll of the dice, congratulations!");
			return true;
		}

		// Now do the loss.
		if (total == 2 || total == 3 || total == 12) {
			System.out.println("Sorry, you lose with the first roll of the dice.");
			return false;
		}

		// Explain to the user what's going on.
		System.out.println("Now continue to roll until you either get "+total+" or a sum of 7.");
		int current = 0;
		while (true) {

			// Do the next roll.
			die1 = rndObj.nextInt(6) + 1;
			die2 = rndObj.nextInt(6) + 1;
			current = die1+die2;
			System.out.println("You rolled "+die1+" and "+die2+" for a total of "+current);

			// Here's when we get out.
			if (current == total || current == 7)
				break;

			// If we're here, the game goes on.
			System.out.println("The game continues, roll again.");
		}

		// Woohoo!
		if (current == total) {
			System.out.println("Great, you hit your original roll before 7 and win!");
			return true;
		}

		// Too bad...
		System.out.println("Sorry, you lose by rolling 7 first before getting your original roll.");
		return false;
	}

	// Plays a game of Arup's Roulette and returns -1 if you lose, 1 if you win a 1:1 payout
	// and 36 if you win an 36:1 payout.
	public static int roulette() {

		// Get the user's answer.
		System.out.println("What would you like to bet on: odds, +evens, 0 or 00?");
		System.out.println("Pay out for odds and evens is 1:1 for 0 and 00 is 36:1.");
		String bet = stdin.next();

		// Make sure bet is one of the four choices.
		while (!bet.equals("odds") && !bet.equals("+evens") && !bet.equals("0") && !bet.equals("00")) {
			System.out.println("Sorry that's not a valid bet, please enter odds, +evens, 0 or 00.");
			bet = stdin.next();
		}

		// Spin the wheel.
		int rndNum = rndObj.nextInt(38);
		String result = getRouletteSpace(rndNum);
		System.out.println("You spun "+result+".");

		// Get these two cases out of the way.
		if (bet.equals(result)) {
			System.out.println("What a great spin! You get an 36 times payout!!!");
			return 36;
		}

		// Win on odds.
		if (bet.equals("odds") && rndNum%2 == 1 && !result.equals("00")) {
			System.out.println("You bet on odds and got an odd number. You get an even payout.");
			return 1;
		}

		// Win on evens.
		if (bet.equals("+evens") && rndNum%2 == 0 && !result.equals("0")) {
			System.out.println("You bet on +evens and got a positive even number. You get an even payout.");
			return 1;
		}

		// Bad news if we get down here.
		System.out.println("Sorry, your bet didn't land. Better luck next time.");
		return -1;
	}

	// Returns a string storing the corresponding roulette wheel spot with numerical id id.
	public static String getRouletteSpace(int id) {
		if (id < 37) return ""+id;
		else return "00";
	}

	// Asks the user how much to bet in between 1 and maxChips number of chips.
	public static int getBet(int maxChips) {

		int bet = 0;
		while (true) {

			// Get the bet.
			System.out.println("How many chips (1-"+maxChips+") would you like to bet?");
			bet = stdin.nextInt();

			// Error message.
			if (bet < 1 || bet > maxChips)
				System.out.println("Sorry that's not a valid number of chips to bet.");

			// We're good get out.
			else
				break;
		}

		// Here's the bet.
		return bet;
	}
}
