The black jack game with Pyhton.
Making an update on my progress. This is one of my Python projects that was before the OOP. So, the task was to build the Blackjack Game:
Difficulty Normal ๐: Use all Hints below to complete the project. Difficulty Hard ๐ค: Use only Hints 1, 2, 3 to complete the project. Difficulty Extra Hard ๐ญ: Only use Hints 1 & 2 to complete the project. Difficulty Expert ๐คฏ: Only use Hint 1 to complete the project.
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: In this task I alredy had the list of cards.
So first I thought that I will need to create two lists for user choices and computer choices Also, the choices had to be random numbers. Otherwise there's no fun so at the beginning I should import the random module. Also, at the beginning I imported the clear module since after the game restarts it will start from the new window without prevoius results.
from replit import clear
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
print("Welcome to the Black Jack game.")
user_cards = []
computer_cards = []
So, here I create the function, where we create random choice for user and computer cards. Also, we have booling for the end of the game so we could have some trigger for while loop. Also, here will be two variable that will change in the future.
def deal_card():
user_cards.extend(random.choices(cards))
computer_cards.extend(random.choices(cards))
continue_game = True
sum_user = 0
sum_computer = 0
here we create the while loop where the actions of the game will continue until some trigger will switch continue_game
to False
Basically, all the game will happen inside of the while loop.
while continue_game:
Inside the while loop I created if else statement where I can check if the each player had two cards or more. In that case it will sum the choices for each player.
if len(user_cards) >= 2:
sum_user+= sum(user_cards)
sum_computer += sum(computer_cards)
else:
sum_user = user_cards
sum_computer = computer_cards
than in another if statement I compare the sums for user choices and computer choices and also check if the sum for each player is not bigger than 21 In each case there will be a print out of the result.
if sum_user > sum_computer and sum_user <= 21:
print (f"your cards are {user_cards} sum is {sum_user}and computers cards are {computer_cards}")
print("YOU WON!")
elif sum_user == sum_computer:
print (f"your cards are {user_cards} and computers cards are {computer_cards}")
print("YOU ARE EVEN!")
else:
print (f"your cards are {user_cards} and computers cards are {computer_cards}")
print("YOU LOST!")
Lastly, I ask the user if he wants another card. Depending on a response we add another random card or show the result and
do_you_continue = input("Do you want another one card? Type 'y' or 'n'")
if do_you_continue == "y":
deal_card()
else:
continue_game = False
clear()
deal_card()