Skip to main content
Add tags; Make title describe what code does per site convention - see https://codereview.stackexchange.com/help/how-to-ask
Source Link

Advice and assessment Simple Blackjack game with card art

I have reached a point in my course that I feel I could use some feedback as to how my code looks and hopefully some direction on some best practice methodology going forward. I know I have a long way to go and there are problem a million better ways of doing what I did but I did use every concept I was taught up to this point. Its

It’s a simple blackjack game but iI learned a lot making it

Advice and assessment

I have reached a point in my course that I feel I could use some feedback as to how my code looks and hopefully some direction on some best practice methodology going forward. I know I have a long way to go and there are problem a million better ways of doing what I did but I did use every concept I was taught up to this point. Its a simple blackjack game but i learned a lot making it

Simple Blackjack game with card art

I have reached a point in my course that I feel I could use some feedback as to how my code looks and hopefully some direction on some best practice methodology going forward. I know I have a long way to go and there are problem a million better ways of doing what I did but I did use every concept I was taught up to this point.

It’s a simple blackjack game but I learned a lot making it

Source Link

Advice and assessment

I have reached a point in my course that I feel I could use some feedback as to how my code looks and hopefully some direction on some best practice methodology going forward. I know I have a long way to go and there are problem a million better ways of doing what I did but I did use every concept I was taught up to this point. Its a simple blackjack game but i learned a lot making it

art.py

logo = """
.------.            _     _            _    _            _    
|A_  _ |.          | |   | |          | |  (_)          | |   
|( \/ ).-----.     | |__ | | __ _  ___| | ___  __ _  ___| | __
| \  /|K /\  |     | '_ \| |/ _` |/ __| |/ / |/ _` |/ __| |/ /
|  \/ | /  \ |     | |_) | | (_| | (__|   <| | (_| | (__|   < 
`-----| \  / |     |_.__/|_|\__,_|\___|_|\_\ |\__,_|\___|_|\_\\
      |  \/ K|                            _/ |                
      `------'                           |__/           
"""

Blackjack.py

############### Our Blackjack House Rules #####################

## The deck is unlimited in size. 
## There are no jokers. 
## The Jack/Queen/King all count as 10.
## The the Ace can count as 11 or 1.
## Use the following list as the deck of cards:
## cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
## The cards in the list have equal probability of being drawn.
## Cards are not removed from the deck as they are drawn.
## The computer is the dealer.

# required imports
import random
from art import logo
import os

#global Variables#################################################
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10,"J","Q","K"]
exit = False

##################################################################
#Clear terminal screen
def clear():
    os.system('cls' if os.name == 'nt' else 'clear')

#####################################################################
#deal cards##########################################################
def deal_cards():
  hands = {
    "player" : [],
    "computer" : []
  }
  counter = 0
  while counter <= 1:
    hands["player"].append(cards[random.randint(0,12)])
    hands["computer"].append(cards[random.randint(0,12)])
    counter += 1 
  return hands # returns the hands as dealt by the computer

######################################################################
#get card total ######################################################
def card_total(player,hands):

  ace_list=[]
  low_cards=[]
  total = 0
  # process hand to remove aces and creat two lists
  for card in hands[player]:
        if card == 11:
            ace_list.append(card) 
        else:
            low_cards.append(card)
    
  # sum cards for subtotal
  for card in low_cards: 
        if card == "J" or card == "Q" or card == "K":
            card = 10
        total += card

  # process aces
  for ace in ace_list:  #decides which value to use for each ace
    if total + ace + len(ace_list) -1 > 21:
        total += 1 
    else:
        total += ace
  return total

########################################################################
#deal another card #####################################################
def get_card(player,hands):
  hands[player].append(cards[random.randint(0,12)])
  
####################################################
#player Processing################################
def player_turn(hands): 
  choice = ""
  while choice == "":
    print_cards(hands,False)
    choice = input("\n\nHit 'ENTER' to accept another card -- Type 'HOLD' to keep hand\n").lower()
    if choice == "":
      get_card("player",hands)
      if card_total("player",hands) > 21:
        return hands
    elif choice == "hold":
      return hands
    else:
        choice = ""

  return hands


##################################################### 
#computer play#######################################
def comp_turn(hands):
  while card_total("computer",hands) < card_total("player",hands):
    get_card("computer",hands)
  return hands


#####################################################
#create print pattern################################
def print_cards(hands,reveal):
    #clear()
    print(logo)
    print ("=" * 60)
    card_art = {
        "line1":[],
        "line2":[],
        "line3":[],
        "line4":[],
        "line5":[],
        "line6":[],
    }

##############################################
####player card pattern##########
    for card in hands["player"]:
        card_art["line1"].append(".------.")
        if card == 11:
            card = "A"

        if card != 10:
            id_line = ("| {}/\  |".format(card))

        elif card == 10:
            id_line = ("|10/\  |")

        card_art["line2"].append(id_line)
        card_art["line3"].append("| /  \ |")
        card_art["line4"].append("| \  / |")

        if card != 10:
            id_line = ("|  \/{} |").format(card)
        elif card == 10:
            id_line = ("|  \/10|")

        card_art["line5"].append(id_line)
        card_art["line6"].append("`------'") 

    #Card spacer  
    card_art["line1"].append("     < P | D >     ")
    card_art["line2"].append("     < L | E >     ")
    card_art["line3"].append("     < A | A >     ")
    card_art["line4"].append("     < Y | L >     ")
    card_art["line5"].append("     < E | E >     ")
    card_art["line6"].append("     < R | R >     ")

    #computer card pattern
    for card in hands["computer"]:
        if reveal == True:
            card_art["line1"].append(".------.")
            if card == 11:
                card = "A"

            if card != 10:
                id_line = ("| {}/\  |".format(card))

            elif card == 10:
                id_line = ("|10/\  |")

            card_art["line2"].append(id_line)
            card_art["line3"].append("| /  \ |")
            card_art["line4"].append("| \  / |")

            if card != 10:
                id_line = ("|  \/{} |").format(card)
            elif card == 10:
                id_line = ("|  \/10|")

            card_art["line5"].append(id_line)
            card_art["line6"].append("`------'")
        # Face card pattern        
        else :
            card_art["line1"].append(".------.")
            card_art["line2"].append("|  /\  |")
            card_art["line3"].append("| /  \ |")
            card_art["line4"].append("| \  / |")
            card_art["line5"].append("|  \/  |")
            card_art["line6"].append("`------'")
            reveal = True
    
    #print cards
    for line in card_art["line1"]:
        print (line,end ="") 
    print()                
    for line in card_art["line2"]:
        print (line,end="")
    print()
    for line in card_art["line3"]:
        print (line,end="")
    print()
    for line in card_art["line4"]:
        print (line,end="")
    print()
    for line in card_art["line5"]:
        print (line,end="")
    print()
    for line in card_art["line6"]:
        print (line,end="")
    print()

    
############################################################# 
############### Main Program ################################

while exit == False:
    player_win = "pending"
    cur_hands = deal_cards()
    cur_hands = player_turn(cur_hands)
    if card_total("player",cur_hands) > 21:
        player_win = False
    else:
        cur_hands = comp_turn(cur_hands)
        if card_total("computer",cur_hands) > 21:
            player_win = True

    if player_win == "pending":
        if card_total("player",cur_hands) > card_total("computer",cur_hands):
            player_win = True
        else:
            player_win = False

    if player_win == True:
            if card_total("computer",cur_hands) <= 21:
                print_cards(cur_hands,True)
                print ("You win !!!")
            else:
                print_cards(cur_hands,True)
                print("Computer busted You Win !!")
    

    elif player_win == False:
        if card_total("player",cur_hands) <= 21:
            print_cards(cur_hands,True)
            print("You Lose !!")
        else:
             print_cards(cur_hands,True)
             print("You busted You lose !!!")

    else:
        print("ERROR")
    choice = input("Hit enter to play again or type 'exit' to quit")
    if choice.lower()=="exit":
        exit = True