Demo entry 6336871

21 (uncommented)

   

Submitted by Jamie Land on Dec 08, 2016 at 13:48
Language: Python 3. Code size: 6.5 kB.

import random, sys, time
CARD_LOOKUP = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
def menu():
    runs = 0
    print("What would you like to do?")
    print("1) Play against the computer\n2) Play against a human\n3) Quit")
    answer = input("\nChoose your selection. ")
    try:
        answer = int(answer)
    except(TypeError, ValueError):
        print("Please type in 1, 2 or 3.")
        menu()
    if answer == 1 or answer == 2:
        player1Hand = []
        player2Hand = []
        player1Points = 0
        player2Points = 0
        player1Bust = False
        player2Bust = False
        if answer == 1:
            mode = "computer"
        elif answer == 2:
            mode = "human"
        play_loop(player1Hand, player2Hand, mode, runs, player1Points, player2Points, player1Bust, player2Bust)
    elif answer == 3:
        print("\nThank you for playing my game.")
        sys.exit()
    else:
        print("\nPlease type in 1, 2 or 3.")
        menu()
def adder(hand):
    points = sum(hand)
    aces = hand.count(11)
    if points > 21 and aces >= 1:
        points -= 10
        aces -= 1
    return points
def play_loop(hand, hand2, mode, runs, pPoints, p2Points, pBust, p2Bust):
    runs += 1
    while True:
        if runs == 1 and mode == "human":
            player = "first player"
        elif runs == 2 and mode == "human":
            player = "second player"
        elif runs == 1 and mode == "computer":
            player = "player"
        elif runs == 2 and mode == "computer":
            player = "computer"
        hand.append(random.choice(CARD_LOOKUP))
        hand.append(random.choice(CARD_LOOKUP))
        while True:
            pPoints = adder(hand)
            hand.sort()
            print("\nThe %s has the %s cards, with a total of %d points." % (player, hand, pPoints))
            if pPoints == 21:
                print("%s: Blackjack!" % player)
                if runs == 1:
                    play_loop(hand2, hand, mode, runs, p2Points, pPoints, p2Bust, pBust)
                else:
                    win(hand2, hand, p2Points, pPoints, p2Bust, pBust, "first player", "second player")
            elif pPoints > 21:
                print("%s went bust!\n" % player)
                pBust = True
                if runs == 1:
                    play_loop(hand2, hand, mode, runs, p2Points, pPoints, p2Bust, pBust)
                else:
                    win(hand2, hand, p2Points, pPoints, p2Bust, pBust, "first player", "second player")
            else:
                if mode == "human" or (mode == "computer" and runs == 1):
                    nextPlay = input("1) Hit\n2) Stand\n\nWhat is your choice? ")
                    try:
                        nextPlay = int(nextPlay)
                    except(TypeError, ValueError):
                        print("Please type in 1 or 2.\n")
                    if nextPlay == 1:
                        hand.append(random.choice(CARD_LOOKUP))
                    elif nextPlay == 2:
                        print("%s: Standing at %d" % (player, pPoints))
                        if runs == 1:
                            time.sleep(2)
                            play_loop(hand2, hand, mode, runs, p2Points, pPoints, pBust, p2Bust)
                        else:
                            if mode == "computer":
                                time.sleep(2)
                                win(hand2, hand, p2Points, pPoints, p2Bust, pBust, "player", "computer")
                            else:
                                time.sleep(2)
                                win(hand2, hand,  p2Points, pPoints, p2Bust, pBust, "first player", "second player")
                    else:
                        print("\nPlease type in 1 or 2.\n")
                elif mode == "computer" and runs == 2:
                    if pPoints < 18:
                        hand.append(random.choice(CARD_LOOKUP))
                        time.sleep(2)
                    else:
                        print("%s: Standing at %d" % (player, pPoints))
                        time.sleep(2)
                        win(hand2, hand, p2Points, pPoints, p2Bust, pBust, "player", "computer")
def win(p1Hand, p2Hand, p1Points, p2Points, p1Bust, p2Bust, p1Player, p2Player):
    if p1Points == p2Points:
        if (p1Hand.count(11) == 1 and p1Hand.count(10) == 1) and (p2Hand.count(11) == 1 and p2Hand.count(10) == 1):
            print("It's a draw!\n")
            menu()
        elif p1Hand.count(11) == 1 and p1Hand.count(10) == 1:
            print("%s wins with a Two-Card Blackjack!" % p1Player)
        elif p2Hand.count(11) == 1 and p2Hand.count(10) == 1:
            print("%s wins with a Two-Card Blackjack!" % p2Player)
        else:
            print("It's a draw!\n")
            menu()
    elif p1Points > p2Points:
        if p1Bust:
            print("%s went bust! %s wins with %s in their hand, making %d points." % (p1Player, p2Player, p2Hand,
                                                                                      p2Points))
        elif p2Bust:
            print("Both players went bust!")
        else:
            print("%s wins with %s in their hand, making %d points" % (p1Player, p1Hand, p1Points))
            print("%s loses with %s in their hand, making %d points" % (p2Player, p2Hand, p2Points))
    elif p1Points < p2Points:
        if p2Bust:
            print("%s went bust! %s wins with %s in their hand, making %d points." % (p2Player, p1Player, p1Hand,
                                                                                      p1Points))
        elif p1Bust:
            print("Both players went bust!")
        else:
            print("%s wins with %s in their hand, making %d points" % (p2Player, p2Hand, p2Points))
            print("%s loses with %s in their hand, making %d points" % (p1Player, p1Hand, p1Points))
    fileName = input("\nWhat is the file name that you would like to store the game under? ")
    with open(fileName + ".txt", "w") as file:
        file.write("---- Game: %s ----\n" % fileName)
        file.write("%s - Hand: %s, Points: %d, Bust? %s\n" % (p1Player, p1Hand, p1Points, p1Bust))
        file.write("%s - Hand: %s, Points: %d, Bust? %s" % (p2Player, p2Hand, p2Points, p2Bust))
    time.sleep(4)
    sys.exit()
menu()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).