# Arup Guha
# 7/16/2013
# Solution to SI@UCF Homework: Random Class Practice - Blackjack

import random

# Look up tables for cards.
CARDS = ["Ace","2","3","4","5","6","7","8","9","10", "Jack", "Queen", "King"]
VALUES = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

# Codes for game outcome
PLAYER1 = 1
PLAYER2 = 2
TIE = 3
BOTHBUST = 4
MAX = 21

def main():

    # Play the game
    print("Welcome to Blackjack!")
    p1Score = play(1)
    print()
    p2Score = play(2)

    # See who won.
    outcome = getOutcome(p1Score, p2Score)
    printResults(outcome)

def getOneCard():
    return random.randint(0, 12)

def play(playerNo):

    # Get initial cards.
    c1 = getOneCard()
    c2 = getOneCard()
    score = VALUES[c1] + VALUES[c2]
    
    print("Player",playerNo,"Hand: ",score)

    # Ask player to draw.
    ans = int(input("Do you want to hit (1) or stay (2)? "))
    while ans == 1:

        # Choose the next card.
        nextc = getOneCard()
        print("Player ", playerNo," Drew a ", CARDS[nextc], ".", sep="")

        # Update score and print.
        score += VALUES[nextc]
        print("Player", playerNo,"Hand: ", score)

        # Catch a bust.
        if score > MAX:
            print("You have BUSTED!")
            ans = 0

        # Get next card.
        else:
            ans = int(input("Do you want to hit (1) or stay (2)? "))

    return score

# Returns the outcome of a game with the respective scores, score1 and score2
def getOutcome(score1, score2):

    # Both bust
    if score1 > MAX and score2 > MAX:
        return BOTHBUST

    # One busts
    if score1 > MAX:
        return PLAYER2
    if score2 > MAX:
        return PLAYER1

    # Regular game, three possible outcomes
    if score1 > score2:
        return PLAYER1
    elif score2 > score1:
        return PLAYER2
    else:
        return TIE
    

def printResults(outcome):
    
    # Print the results.
    if outcome == PLAYER1:
        print("Player 1 Wins!")
    elif outcome == PLAYER2:
        print("Player 2 Wins!")
    elif outcome == TIE:
        print("The game ended in a tie!")
    else:
        print("Both players busted. So both lose :(")

main()
