PROject Python | Blackjack (#extremely.cool.it’s.kinda.unbelieveable,just.saying)

Quote of AWESOMENESS: “Thinking is already a conversation” ~ Paul Pangaro

 

Hey there!

I know it’s been awhile but here I am with another Python project. Today, you will be learning the code for blackjack! Just put it out on your editor, study the code, and explore the game on your terminal. It was quite some challenge getting toward the end of finishing up the game. And, at some point, problems and bugs constantly came up that I didn’t that it would actually get done. BUT, nevertheless, it’s finally hereeee!!

*ahem, drum rollllllllll*

*cricket sounds*

*Umm….. DRUMMM ROLLLL!!!……. please?*

*DRUM ROLLLLLLLLLL (in the background, finally)*

TADA!!!!:

#import the random library from python
from random import *


#Function Definitions 
def deal_from_deck(deck):
    if len(deck)<1:
         create_deck(deck)
         shuffle_deck(deck)
         deal_cards()

def deal_cards(deck):
    return deck.pop(0)

def define_cards(n):
    rank_string = ("ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen","king")
    suit_string = ("clubs", "diamonds", "hearts", "spades")
    cards = []

    for suit in range(4):
        for rank in range(13):
            card_string = rank_string[rank] + " of " + suit_string[suit]
            cards.append(card_string)
    return cards[n]

def create_deck(deck):
    for i in range(52):
        deck.append(i)
    return

def shuffle_deck(deck):
    shuffle(deck)
    return

def card_value(n):
    vals = (11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10)
    card_vals = []

    for s in range(4):
        for r in range(13):
            card_vals.append(vals[r])
    return card_vals[n]

def card_display(n):
    name = define_cards(n)
    val = card_value(n)
    print (name + " : " + str(val))

def show_cards(hand):
    for card in hand:
        card_display(card)


def hand_val(hand):
    list = []
    for card in hand:
        list.append(card_value(card))
    if sum(list) > 21:
        for i in range(len(list)):
            if list[i] == 11:
                list[i] = 1
    return sum(list)





# Initializing
drawn_hands = 0
player_wins = 0
dealer_win = 0
deck = []
player_hand = []
dealer_hand = []
playing = True


#Processing
create_deck(deck)
shuffle_deck(deck)

for i in range(2):
    card = deal_cards(deck)
    card2 = deal_cards(deck)
    player_hand.append(card)
    dealer_hand.append(card2)


# put in a loop for player hand
print()
print("--------------------")
print("Dealer shows: ")
card_display(dealer_hand[0])
print("--------------------")

while playing == True:
    player_val = hand_val(player_hand)
    print()
    print()
    print("* * * * * * * * * * *")
    print("Your current hand is...")
    show_cards(player_hand)
    print(hand_val(player_hand))
    print("* * * * * * * * * * *")
    print()
    print()
    if player_val < 22:
        response = input("Would you like another card?")
        if response == "yes":
            player_hand.append(deal_cards(deck))
        else:
            playing = False
    else:
        playing = False
        print("sorry you busted")
        print("Thanks for playing")
        exit()

# check if it is higher than 21, if yes then end program, dealer hand wins
show_cards(dealer_hand)

playing = True
while playing == True:
    if hand_val(dealer_hand) < 17:
        dealer_hand.append(deal_cards(deck))
    else:
        playing = False

print()
print()
print("****************")
print("Dealer has:")
print(show_cards(dealer_hand))
print("****************")
print()
print()

if hand_val(dealer_hand) > 21:
    print("The dealer busted")
    show_cards(dealer_hand)
    print("You win!")
    exit()
elif hand_val(dealer_hand) > hand_val(player_hand):
    print("Dealer wins")
    show_cards(dealer_hand)
    exit()
elif hand_val(player_hand) > hand_val(dealer_hand):
    print("You win!")
    show_cards(player_hand)
    exit()
else:
    print("It's a draw")
    exit()

 

don’t forget to smile & be awesome 🙂

Yours truly, 

L.O.A.S.H

 


 © Elizabeth Anne Villoria

PROject Python: Hangman Game!

Quote of AWESOMENESS: “First, solve the problem. Then, write the code.” ~ John Johnson

Yup, I know, this is like the fourth blog in a row that relates to Python. BUT, it’s okay! This one is really fun. The program we will be making today is Hangman. The code is shown below:


import random

def print_game_rules(max_incorrect,word_len):
    print("This game we will be playing hangman!")

def display_figure(bad_guesses):
    gallows = (
        """
            +----------+
            |
            |
            |
            |
            |
        ================
        """,
        """
            +----------+
            |          o
            |
            |
            |
            |
        ================
        """,
        """
            +----------+
            |          o
            |          +---
            |
            |
            |
        ================
        """,
        """
            +----------+
            |          o
            |       ---+---
            |
            |
            |
        ================
        """,
        """
            +----------+
            |          o
            |       ---+---
            |          |
            |
            |
        ================
        """,
        """
            +----------+
            |          o
            |       ---+---
            |          |
            |         /
            |        /
        ================
        """,
        """
            +----------+
            |          o
            |       ---+---
            |          |
            |         / \
            |        /   \
        ================
        """
    )
    print(gallows[bad_guesses])
    return()

def prompt_for_letter():
    print("")
    player_guess = str(input("Guess a letter in the mystery word: "))
    player_guess = player_guess.strip()
    player_guess = player_guess.lower()
    print("")
    return(player_guess)

animal_words = ("horse", "mall", "desktop", "apple", "earth", "tree", "sun", "can", "love", "dog", "door", "house", "mansion", "beach", "computer", "window", "keyboard", "airplane", "hotel", "cat")
theword = random.choice(animal_words)
word_len = len(theword)
letterguess = word_len * ["_"]
max_incorrect = 6
alphabet = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
letters_tried = []
number_guesses = 0
letters_correct = 0
incorrect_guesses = 0
###############################
# PROCESSING SECTION
# Branching code: if/else
# Looping code: for-loop, while-loop
###############################

print("This game is hangman.")
while (incorrect_guesses != max_incorrect) and (letters_correct != word_len):
    letter = prompt_for_letter()
    if letter in alphabet:
        if letter in letters_tried:
            print("You already picked", letter)
        else:
            letters_tried.append(letter)
            if theword.find(letter) == -1:
                print("the letter", letter,"is not in the word")
                incorrect_guesses += 1
                print("***********************")
                print(incorrect_guesses)
            else:
                print("the letter", letter,"is in the word")
                for i in range(word_len):
                    if letter == theword[i]:
                        letterguess[i] = letter
                        letters_correct += 1
                    else:
                        pass
    else:
        print("Please guess a single letter in the alphabet.")


    x = ""
    print(x.join(letterguess))
    a = ""
    print("Letters tried so far: ", a.join(letters_tried))
    if incorrect_guesses == max_incorrect:
        print("Sorry, too many incorrect guesses. You are hanged.")
        print("The word was", theword)
    if letters_correct == word_len:
        print("You guessed all the letters in the word!")
        print("The word was", theword)



    display_figure(incorrect_guesses)

 

Honestly, this was a challenging project at the start and I had quite a few obstacles that were highly annoying but eventually here we are!

 

Pssst, don’t forget to be awesome today.

Yours truly, 

L.O.A.S.H

 


 © Elizabeth Anne Villoria