# Arup Guha
# 10/15/2020
# Casino Program

import random

# Constant for quitting on the menu.
QUIT = 4

# Returns the value of a single standard die roll.
def rollOneDie():
    return random.randint(1,6)

# Returns the sum of n standard dice rolls.
def rollDice(n):
    total = 0
    for i in range(n):
        total += random.randint(1,6)
    return total

# Plays a game of craps and returns True if the user won, False otherwise.
def craps():

    # Do the first roll.
    firstRoll = rollDice(2)
    print("Your first roll is", firstRoll)

    # In craps, we win with a 7 or 11...and you get a slurpee!
    if firstRoll == 7 or firstRoll == 11:
        print("Congrats, you win!")
        return True

    # In craps, 2,3, and 12 are losing first rolls.
    if firstRoll == 2 or firstRoll == 3 or firstRoll == 12:
        print("Sorry, you lose!")
        return False

    # Game continues.
    print("The game will continue. Your point is", firstRoll)
    print("If you can roll that again before getting 7, you win!")

    # Roll again, show user what they got.
    nextRoll = rollDice(2)
    print("This time around you rolled", nextRoll)

    # Keep on going until you get 7 or the point.
    while nextRoll != 7 and nextRoll != firstRoll:
        print("The game will continue as that is not an ending roll.")
        nextRoll = rollDice(2)
        print("This time around you rolled", nextRoll)

    # In this case the user loses.
    if nextRoll == 7:
        print("Sorry you lose, you rolled 7 first.")
        return False

    # User wins
    else:
        print("Great, you win! You rolled",firstRoll,"again before 7.")
        return True

def arupsdice():

    # Do the first roll.
    firstRoll = rollDice(2)
    print("Your first roll is", firstRoll)

    # 1 roll win.
    if firstRoll == 11 or firstRoll == 12:
        print("Congrats you win!")
        return True

    # 1 roll loss.
    if firstRoll == 2:
        print("Sorry, this means you lose.")
        return False

    # Get a second roll.
    secondRoll = rollDice(2)
    print("You got", secondRoll,"for your second roll.")

    # Winning case.
    if secondRoll > firstRoll:
        print("You beat your first roll so you win!")
        return True

    # If we get here, we lose.
    print("Sorry, you didn't beat your first roll, so you lose.")
    return False

# Returns a character representing a randomly selected playing card.
def getRandCard():

    # Cards frequencies of the 13 kinds are equally distributed.
    num = random.randint(1,13)

    # Value is digit value.
    if num >= 2 and num <= 9:
        return chr(ord('0') + num)

    # Rest are special cases.
    elif num == 1:
        return 'A'
    elif num == 10:
        return 'T'
    elif num == 11:
        return 'J'
    elif num == 12:
        return 'Q'
    else:
        return 'K'

def getCardValue(card):

    # These we can handle with a formula.
    if card >= '2' and card <= '9':
        return ord(card) - ord('0')

    # Aces are 11 for this version.
    elif card == 'A':
        return 11

    # All other cards are worth 10.
    return 10

def blackjack():

    # User always gets two cards first.
    card1 = getRandCard()
    card2 = getRandCard()

    # Calculate current score.
    curScore = getCardValue(card1) + getCardValue(card2)

    # Show current cards, print score. See if they want to go again.
    print("You received",card1,"and",card2)
    print("Your score is", curScore)
    ans = input("Do you want another card?(y/n)")

    # Keep getting cards until the user doesn't want any more.
    while ans == 'y' and curScore <= 21:

        curCard = getRandCard()

        # Update the status.
        print("You received the following card:", curCard)
        curScore += getCardValue(curCard)
        print("Now, your score is", curScore)

        # See if they want another card.
        if curScore <= 21:
            ans = input("Do you want another card?(y/n)")

    # Lose by going too high.
    if curScore > 21:
        print("Sorry, you busted by going over 21.")
        return False

    # Lose by not getting enough points.
    elif curScore < 17:
        print("You didn't score enough to win.")
        return False

    # If we make it here, we won!
    print("Great, that is a winning score!")
    return True

# Returns the user's menu choice, no error checking done.
def menu():

    # Print the menu.
    print("Welcome to our casino. What would you like to do?")
    print("1. Play Arup's Game of Dice.")
    print("2. Play Craps.")
    print("3. Play Blackjack.")
    print("4. Quit.")

    # Get answer and return it.
    ans = int(input(""))
    return ans

#
def main():

    # Get how much money the user is bringing to the casino.
    money = int(input("How much money do you have?\n"))
    savemoney = money

    # Get the user's choice.
    ans = menu()

    # Keep playing!
    while money > 0 and ans != QUIT:

        # Get wager.
        wager = int(input("How much is your wager?\n"))
        print("just entered ",wager,"for wager")

        # Do the appropriate game.
        result = -1
        if ans == 1:
            result = arupsdice()
        elif ans == 2:
            result = craps()
        elif ans == 3:
            result = blackjack()
        else:
            print("That was not a valid menu option.")

        # Only update money if a game was played.
        if ans != QUIT:

            # Update money.
            if result:
                money += wager
            else:
                money -= wager

        # Update user about their money and see if they want to play again.
        print("Now you have", money,"left after playing.")
        ans = menu()

    # Ending message.
    print("Thanks for playing, you now have", money,"dollars.")

    # Print their relative status.
    if money > savemoney:
        print("Great job, you earned", money-savemoney," dollars.")
    elif savemoney == money:
        print("You are lucky you did not lose any money!")
    else:
        print("You lost",savemoney-money, "dollars.")

# Run it.
main()

'''
My test of my game
def testarupsgame():
    for i in range(10):
        arupsdice()
        print("-------------------")

testarupsgame()
'''

'''
My test of rollDice
def testRollDice():
    for i in range(50):
        print(rollDice(2))

testRollDice()
'''


