Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python blackjack

import random as rd

cards = {'1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8',
         '9': '9', '10': 'X', '11': 'J', '12': 'Q', '13': 'K', '11': 'A'}
cards_keys = list(cards.keys())
game_status = True
winner = None

def print_card_user(display_card_arr):
    total_cards = len(display_card_arr)
    for idx in range(total_cards):
        print('______________', end = '   ')
    print('
')
    for idx in range(total_cards):
        print('|%s           |' % cards[display_card_arr[idx]], end = '   ') 
    print('
')
    for idx in range(total_cards):
        print('|            |', end = '   ')
    print('
')
    for idx in range(total_cards):
        print('|            |', end = '   ')
    print('
')
    for idx in range(total_cards):
        print('|           %s|' % cards[display_card_arr[idx]], end = '   ')
    print('
')
    for idx in range(total_cards):
        print('--------------', end = '   ')
    print('
')
    
def win_determine(player_cards_list, computer_cards_list):
    global winner
    player_cards_list = [int(i) for i in player_cards_list]
    computer_cards_list = [int(i) for i in computer_cards_list]
    if sum(player_cards_list) == 21:
        winner = 'Player'
    if sum(computer_cards_list) == 21:
        winner = 'Computer'
    if sum(player_cards_list) > 21:
        winner = 'Computer'
    if sum(computer_cards_list) > 21:
        winner = 'Player'
    if sum(player_cards_list) + sum(computer_cards_list) < 42:
        if sum(player_cards_list) > sum(computer_cards_list):
            winner = 'Player'
        if sum(player_cards_list) < sum(computer_cards_list):
            winner = 'Computer'
    if sum(player_cards_list) == sum(computer_cards_list):
        winner = 'Drew'

def reshuffle(player_cards, computer_cards):
    player_cards = [int(i) for i in player_cards]
    computer_cards = [int(i) for i in computer_cards]
    if sum(player_cards) > 21:
        play()
    if sum(computer_cards) > 21:
        play()
    if sum(player_cards) == 21:
        print('Player Wins - Blackjack')
    if sum(computer_cards) == 21:
        print('Computer Wins - Blackjack')

def computer_move(computer_cards):
    cards = [int(i) for i in computer_cards]
    if 21 - sum(cards) <= 4:
        print('
Computer Chose to Stand')
    else:
        computer_cards.append(cards_keys[rd.randint(0, 12)])

def play():
    global game_status, winner
    player_cards = []
    computer_cards = []
    for _ in range(2):
        player_cards.append(cards_keys[rd.randint(0, 12)])
        computer_cards.append(cards_keys[rd.randint(0, 12)])
    reshuffle(player_cards, computer_cards)
    print("Player Current Cards:
")
    print_card_user(player_cards)
    while game_status == True:
        if winner == None:
            computer_move(computer_cards)
            player_decision = input("""Hit or Stand?""")
            if player_decision.lower() == 'hit':
                player_cards.append(cards_keys[rd.randint(0, 12)])
                print(player_cards)
                print_card_user(player_cards)
            if player_decision.lower() == 'stand':
                win_determine(player_cards, computer_cards)
                print("
The Winner is: %s" % winner)
                print("Computer's Cards:")
                print_card_user(computer_cards)
                print(sum([int(i) for i in player_cards]), '-', sum([int(i) for i in computer_cards]))
                quit()
play()
Comment

blackjack in python

# GO TO https://www.askpython.com/python/examples/blackjack-game-using-python
# THERES A TUTORIAL THERE WITH ALL THE NEEDED CODE
Comment

PREVIOUS NEXT
Code Example
Python :: accuracy score 
Python :: python get username windows 
Python :: show battery of my laptop python 
Python :: how to make random colors in python turtle 
Python :: boxplot label python 
Python :: split string by length python 
Python :: pip fuzzywuzzy 
Python :: column contains substring python 
Python :: install qt designer python ubuntu 
Python :: python read and delete line from file 
Python :: pandas datetime.time 
Python :: pandas convert date to quarter 
Python :: instagram private account hacking code python 
Python :: popup window python tkinter 
Python :: flask_mail 
Python :: export a dataframe to excel pandas 
Python :: how shorten with enter long script python 
Python :: is vowel python 
Python :: plt imshow python 
Python :: request.body django 
Python :: python typeddict 
Python :: ipython play sound 
Python :: np shuffle 
Python :: import pyplot python 
Python :: implicit conversion in python example 
Python :: how to do an if input = python 
Python :: numpy array equal 
Python :: python count total no of word in a text 
Python :: generate sha1 python 
Python :: python remove form list 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =