Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

My BlackJack Game - Python3

Hello everyone.

I am learning Python at the moment and I am liking it, so after finishing the collection course and the Dungeon Game I wanted to test my skills with a BlackJack game.

Here is my code so far:

import random

deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
number_of_decks = 4

# define function to define the cards

def cards_define_function():
    cards = deck * number_of_decks
    return cards

# define the cards

cards = cards_define_function()

# define function remove card

def remove_card(string):
    return cards.remove(string)

# define a function for computer cards

def computer_cards():
    computer_card_one = random.choice(cards)
    remove_card(computer_card_one)
    computer_card_two = random.choice(cards)
    remove_card(computer_card_two)
    computer_total = computer_card_one + computer_card_two
    return computer_card_one, computer_card_two, computer_total

# define a function for user's initial cards

def user_cards():
    user_card_one = random.choice(cards)
    remove_card(user_card_one)
    user_card_two = random.choice(cards)
    remove_card(user_card_two)
    user_total = user_card_one + user_card_two
    return user_card_one, user_card_two, user_total

# define a single random card function

def one_more_card():
    extra_card = random.choice(cards)
    remove_card(extra_card)
    return extra_card

# define computer random card function

def computer_new_random_card():
    computer_new_card = random.choice(cards)
    remove_card(computer_new_card)
    return computer_new_card

# define cards lists (user and computer)

computer_cards = list(computer_cards())
user_cards = list(user_cards())

# define computer stands on

computer_stands_on = 17

# define the showing functions

def computer_show():
    print("Computer has {}".format(computer_cards))

def user_show():
    print("You have {}".format(user_cards))

def bust():
    print("BUST! You lost, that was easy money for the Computer!")

def ask_again():
     new_answer = input("Computer stands on {}. Do you want another card? ".format(computer_stands_on))
     return new_answer



# define function to ask user (main function)

def ask_user_initial():
    answer = input("Your first card is {}, computer's first card is {}, type 'SHOW' to show your other card! "
        .format(user_cards[0], computer_cards[0]))
    if answer != '':
        user_show()
    while user_cards[len(user_cards)-1] <= 21:
        new_answer = ask_again()
        if new_answer == 'yes' or new_answer == 'YES':
            extra_card = one_more_card()
            user_cards[len(user_cards)-1] += extra_card
            print("Your new card is a {}!".format(extra_card))
            print("Your new total is {}".format(user_cards[len(user_cards)-1]))
            user_cards.insert(len(user_cards)-1, extra_card)
        else:
            # case 1 while computer total < computer_stands_on -> draw card
                # computer has to draw its new card/cards
            while computer_cards[len(computer_cards)-1] < computer_stands_on:
                computer_new_card = computer_new_random_card()
                print("Computer has drawn {}".format(computer_new_card))
                computer_cards[len(computer_cards)-1] += computer_new_card
                computer_cards.insert(len(computer_cards)-1, computer_new_card)

            # case 2 else show user's and computer's results
            else:
                if computer_cards[len(computer_cards)-1] <= 21:
                    if computer_cards[len(computer_cards)-1] < user_cards[len(user_cards)-1]:
                        print("Your total is {}".format(user_cards[len(user_cards)-1]))
                        user_show()
                        print("Computer has {}".format(computer_cards))
                        print("CONGRATS, it looks like you won this one! Let's play again!")
                        break
                    elif computer_cards[len(computer_cards)-1] > user_cards[len(user_cards)-1]:
                        print("Your total is {}".format(user_cards[len(user_cards)-1]))
                        user_show()
                        print("Computer has {}".format(computer_cards))
                        print("OOOOPS, You should have asked for ANOTHER card, computer wins AGAIN!")
                        break
                    else:
                        print("Your total is {}".format(user_cards[len(user_cards)-1]))
                        print("Computer has {}".format(computer_cards))
                        print("This is a PUSH, it was a nice hand both of you played great!")
                        break
                else:
                    print("Your total is {}".format(user_cards[len(user_cards)-1]))
                    user_show()
                    print("Computer has {}".format(computer_cards))
                    print("CONGRATS, it looks like you won this one! Let's play again!")
                    break

    else:
        bust()
        user_show()
        while computer_cards[len(computer_cards)-1] < computer_stands_on:
            computer_new_card = computer_new_random_card()
            print("Computer has drawn {}".format(computer_new_card))
            computer_cards[len(computer_cards)-1] += computer_new_card
            computer_cards.insert(len(computer_cards)-1, computer_new_card)
        computer_show()


ask_user_initial()


# print remaining cards (just for myself to doublecheck remove card functions)
# print(cards)

# feat to add: color to make the total look better

# problem to solve: ace can be 1 or 11

# problem to solve: initial blackjack check (and insurance?)

# define number of decks and card main variable in relationship to that

# addition to make: virtual money with cards deck updated (still have to see the blackjack rules for that)

# addition to make: seeds

# in blackjack cards are drawn like this: player1, player2, player3, player1, player2, 
# player3, dealer(or computer). Have a look at this issue

# problem to solve: blackjack beats 21

# addition to make: integrate player's decisions. Hit, Stand, Double down, Split, Surrender
# and more (see Wikipedia page)

I have tested it on my terminal in my virtual environment and I would say it is doing fine. I have added what I will need to do next as comments at the bottom.

Please test it and let me know if you find any bugs that I have missed!! THANKS

Also, as you may remember Kenneth Love , I have asked you about global variables in Python yesterday and the question was because of this project:

I have to use a few: 4 if I have counted correctly. I have thought (actually still think) that I need those because I will have to use them a lot in the code.

But if there is a better way to do this (which I am pretty sure there is :) ), feel free to let me know.

Thanks

Vittorio

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Instead of selecting a card and then removing it as its own step, why not just .pop() the first occurrence of the card's value?

computer_card_one = cards.pop(cards.index(random.choice(cards))
computer_card_one = cards.pop(cards.index(random.choice(cards))

Awesome work, though! Great to see people come up with brand new creations!

Right, pop is surely needed here. I was wondering about that also because now I don't want to waste the card value as I need it later to check the stats.

ty Kenneth,

good to see you often around the forum