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 :: python has duplicates 
Python :: min max scaler on one column 
Python :: sum of a column in pandas 
Python :: ignore bad lines pandas 
Python :: Make tkinter window look less blury 
Python :: message box for python 
Python :: convert c_ubyte_Array_ to opencv 
Python :: dopleganger 
Python :: python Split a file path into root and extension 
Python :: Set up and run a two-sample independent t-test 
Python :: cool advances python ptoject ideas 
Python :: remove special characters from dictionary python 
Python :: concat tensors pytorch 
Python :: plotly express lineplot 
Python :: grid search python 
Python :: python init matrix 
Python :: FizzBuzz FizzBuzz is a well known programming assignment, asked during interviews. 
Python :: how to ask python function to return something 
Python :: quamtum criciut python 
Python :: how to get absolute path in python 
Python :: printing hollow triangle in python 
Python :: scikit normalize 
Python :: koncemzem 
Python :: Source Code: Matrix Multiplication Using Nested List Comprehension 
Python :: change size of yticks python 
Python :: importing tkinter in python 
Python :: access element of dataframe python 
Python :: how to get index of week in list in python 
Python :: install python 3.6 ubuntu 16.04 
Python :: how to get started with python 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =