Overview
This miniature project implements the classic casino card game of blackjack. Players hit, stand, double down, or take insurance against a virtual dealer, with card values and deck shuffling handled automatically. Originally a small college assignment in Python introducing control flow, data structures and random number generation — now also playable in the browser below.
Play the game
Loading game… If this message stays, the script failed to load. Refresh the page or download the original Python source.
Key highlights
Features and learning objectives explored in this project.
- Uses Python lists and dictionaries to model a standard 52‑card deck.
- Plays in the browser with button-driven hit, stand, double-down, and insurance flows; the original Python uses keyboard input at a Colab prompt.
- Calculates hand totals with appropriate handling of Aces as 1 or 11.
- Demonstrates random shuffling, loops, conditional logic and basic game state management.
Source
The original Colab Python script, kept verbatim. The in-browser version above ports its logic to JavaScript without changing rules.
View original Python source (blackjack.py)
# -*- coding: utf-8 -*-
# NOTE: blackjack-game.html inlines this source in its <details> source viewer
# so search engines and noscript visitors see it. Keep the two in sync.
"""Copy of python project 1(Logan and the minions).ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1o6GT6Z78Wdkd4Lz3Kg551xXzHpre9r1p
"""
import random
import math
from IPython.display import clear_output
import time
### Code Should be run in google colabs
### Logan Edwards, MacKenzie Manchester, Shane Doyle
print('Welcome to the Casino!')
print('Would you like to play a game?')
user_ans = input('')
if user_ans == 'Yes' or user_ans == 'yes':
clear_output()
print('Good Choice')
elif user_ans != 'Yes' or user_ans != 'yes':
clear_output()
print('We Dont Need Your Permission')
################################################################################################################################################################
user_cash = 1000
stack_of_cards = [ '2 of Spades', '3 of Spades', '4 of Spades', '5 of Spades', '6 of Spades', '7 of Spades', '8 of Spades', '9 of Spades', '10 of Spades', 'Jack of Spades', 'Queen of Spades', 'King of Spades', 'Ace of Spades','2 of Clubs', '3 of Clubs', '4 of Clubs', '5 of Clubs', '6 of Clubs', '7 of Clubs', '8 of Clubs', '9 of Clubs', '10 of Clubs', 'Jack of Clubs', 'Queen of Clubs', 'King of Clubs', 'Ace of Clubs','2 of Hearts', '3 of Hearts', '4 of Hearts', '5 of Hearts', '6 of Hearts', '7 of Hearts', '8 of Hearts', '9 of Hearts', '10 of Hearts', 'Jack of Hearts', 'Queen of Hearts', 'King of Hearts', 'Ace of Hearts','2 of Diamonds', '3 of Diamonds', '4 of Diamonds', '5 of Diamonds', '6 of Diamonds', '7 of Diamonds', '8 of Diamonds', '9 of Diamonds', '10 of Diamonds', 'Jack of Diamonds', 'Queen of Diamonds', 'King of Diamonds', 'Ace of Diamonds']
card_value ={
'2 of Spades': 2, '3 of Spades': 3, '4 of Spades': 4, '5 of Spades': 5, '6 of Spades': 6, '7 of Spades': 7, '8 of Spades': 8, '9 of Spades': 9, '10 of Spades': 10, 'Jack of Spades': 10, 'Queen of Spades': 10, 'King of Spades': 10, 'Ace of Spades': 11,
'2 of Clubs': 2, '3 of Clubs': 3, '4 of Clubs': 4, '5 of Clubs': 5, '6 of Clubs': 6, '7 of Clubs': 7, '8 of Clubs': 8, '9 of Clubs': 9, '10 of Clubs': 10, 'Jack of Clubs': 10, 'Queen of Clubs': 10, 'King of Clubs': 10, 'Ace of Clubs': 11, #this section of code explains what each card is and defines them as a number using a dictionary
'2 of Hearts': 2, '3 of Hearts': 3, '4 of Hearts': 4, '5 of Hearts': 5, '6 of Hearts': 6, '7 of Hearts': 7, '8 of Hearts': 8, '9 of Hearts': 9, '10 of Hearts': 10, 'Jack of Hearts': 10, 'Queen of Hearts': 10, 'King of Hearts': 10, 'Ace of Hearts': 11,
'2 of Diamonds': 2, '3 of Diamonds': 3, '4 of Diamonds': 4, '5 of Diamonds': 5, '6 of Diamonds': 6, '7 of Diamonds': 7, '8 of Diamonds': 8, '9 of Diamonds': 9, '10 of Diamonds': 10, 'Jack of Diamonds': 10, 'Queen of Diamonds': 10, 'King of Diamonds': 10, 'Ace of Diamonds': 11
}
def shuffle_deck(card_deck):
playing_deck = card_deck[:]
random.shuffle(playing_deck)
return playing_deck
def adjust_for_ace(hand_value, hand):
num_aces = hand.count('Ace of Spades') + hand.count('Ace of Clubs') + hand.count('Ace of Hearts') + hand.count('Ace of Diamonds') #This section shows how Ace can equal 1 or 11 based on the player's and dealer's hand.
while hand_value > 21 and num_aces > 0:
hand_value -= 10
num_aces -= 1
return hand_value
def round_of_game(next_m,dealer_num,player_num,dealer_hand,player_hand,game_state): #This defines how the game is run
clear_output()
next_move = next_m
final_score = 0
if next_move == 'hit':
player_hand.append(playing_deck.pop())
player_num = sum(card_value[card] for card in player_hand)
player_num = adjust_for_ace(player_num, player_hand)
print("Player's hand:",player_hand)
print("Player's total:",player_num)
dealer_num = sum(card_value[card] for card in dealer_hand)
dealer_num = adjust_for_ace(dealer_num, dealer_hand)
final_score = player_num
if player_num > 21:
print('Bust!')
next_m = 'stand'
game_state = 'bust'
final_score = player_num
if dealer_num < 17:
dealer_hand.append(playing_deck.pop())
dealer_num = 0
dealer_num = sum(card_value[card] for card in dealer_hand)
player_num = sum(card_value[card] for card in player_hand)
player_num = adjust_for_ace(player_num, player_hand)
dealer_num = adjust_for_ace(dealer_num, dealer_hand)
final_score = player_num
print('Dealer Hits')
print("Dealer's hand:",dealer_hand[1:],"and Unknown")
else:
print('Dealer stands')
print("Dealer's hand:",dealer_hand[1:],"and Unknown")
dealer_num = sum(card_value[card] for card in dealer_hand)
player_num = sum(card_value[card] for card in player_hand)
player_num = adjust_for_ace(player_num, player_hand)
dealer_num = adjust_for_ace(dealer_num, dealer_hand)
final_score = player_num
return dealer_num,player_num,player_hand,dealer_hand,playing_deck,final_score,game_state,next_m
if dealer_num > 21:
print('Dealer Busts!')
dealer_num = sum(card_value[card] for card in dealer_hand)
player_num = sum(card_value[card] for card in player_hand)
dealer_num = adjust_for_ace(dealer_num, dealer_hand)
player_num = adjust_for_ace(player_num, player_hand)
final_score = player_num
return dealer_num,player_num,player_hand,dealer_hand,playing_deck,final_score,game_state,next_m
final_score = player_num
return dealer_num,player_num,player_hand,dealer_hand,playing_deck,final_score,game_state,next_m
if next_move == 'stand':
dealer_num = sum(card_value[card] for card in dealer_hand)
dealer_num = adjust_for_ace(dealer_num, dealer_hand)
if dealer_num < 17:
dealer_hand.append(playing_deck.pop())
dealer_num = 0
dealer_num = sum(card_value[card] for card in dealer_hand)
player_num = sum(card_value[card] for card in player_hand)
player_num = adjust_for_ace(player_num, player_hand)
dealer_num = adjust_for_ace(dealer_num, dealer_hand)
final_score = player_num
print("Dealer's hand:",dealer_hand[1:],"and Unknown")
if dealer_num > 21:
print('Dealer Busts!')
else:
dealer_num = 0
dealer_num = sum(card_value[card] for card in dealer_hand)
player_num = sum(card_value[card] for card in player_hand)
player_num = adjust_for_ace(player_num, player_hand)
dealer_num = adjust_for_ace(dealer_num, dealer_hand)
final_score = player_num
print('Dealer stands')
print("Dealer's hand:",dealer_hand[1:],"and Unknown")
print("Player's hand:",player_hand)
print("Player's total:",player_num)
return dealer_num,player_num,player_hand,dealer_hand,playing_deck,final_score,game_state,next_m
######################################################################################################################################################################
while user_cash > 0:
print('Our game shall begin!')
print(f'Your current balance is: ${user_cash}')
playing_deck = shuffle_deck(stack_of_cards)
dealer_num = 0
player_num = 0
final_score = 0
initial_bet = 0
next_m = ''
game_state =''
print('How much would you like to bet?') #This section is for the player's bet and rules reguarding what the player can or can not bet.
time.sleep(1)
initial_bet = input()
try:
initial_bet = int(initial_bet)
except:
clear_output()
print("We don't gamble in variables bud")
break
if initial_bet == 0:
clear_output()
print('No Spectators Allowed')
break
if initial_bet > user_cash:
clear_output()
print("Real funny man aren't cha")
break
if initial_bet < 0:
clear_output()
print("We've got a Theif!!!")
true_gamble = random.randint(0,1)
if true_gamble == 0:
user_cash += abs(initial_bet)
print(f"You've Escaped with Your Life and {abs(initial_bet)} bucks in cash.")
elif true_gamble == 1:
user_cash -= abs(initial_bet)
print("you've been shot and forced to pay a fine...")
initial_bet = abs(initial_bet)
print(f'Your current balance is: ${user_cash}')
if user_cash <= 0:
print('You are out of money!')
break
else:
time.sleep(1)
print('would you like to try again?')
risk_it = input('')
if risk_it != 'Yes' and risk_it != 'yes':
clear_output()
print('Then go leach off of someone else you freeloader!')
break
if risk_it == 'Yes' or risk_it == 'yes':
clear_output()
continue
dealer_hand = [playing_deck.pop(), playing_deck.pop()]
player_hand = [playing_deck.pop(), playing_deck.pop()]
dealer_num = sum(card_value[card] for card in dealer_hand) #this section of code defines player/dealer hands along with player/dealer's num as sum and adjusting them for aces
player_num = sum(card_value[card] for card in player_hand)
player_num = adjust_for_ace(player_num, player_hand)
dealer_num = adjust_for_ace(dealer_num, dealer_hand)
clear_output()
print("Dealer's hand:",dealer_hand[1:],"And Unknown")
print("Player's hand:",player_hand)
print("Player's total:",player_num)
insurance = 0 #this section of code offers insurance if the dealers face up card is an ace
if dealer_hand[1] == 'Ace of Spades' or dealer_hand[1] == 'Ace of Clubs' or dealer_hand[1] == 'Ace of Hearts' or dealer_hand[1] == 'Ace of Diamonds':
print('Dealer has an Ace!')
print("Would you like Insurance?")
choice = input('').lower()
if choice == 'yes':
if user_cash >= initial_bet / 2:
user_cash -= initial_bet / 2
print(f"You have paid ${initial_bet / 2} for insurance.")
insurance = 1
else:
print("You don't have enough money for insurance.")
else:
print("You have chosen not to take insurance.")
player_wants_double = ''
if user_cash >= initial_bet*2 and insurance == 0:
time.sleep(.5)
print("Would you like to double down?")
player_wants_double = input('') #this section allows the player to double down
time.sleep(1)
if player_wants_double == "yes":
initial_bet *= 2
print(f"Your new bet is ${initial_bet}")
time.sleep(1)
elif player_wants_double == "no" or player_wants_double != '':
print("Your call")
time.sleep(1)
while player_num <= 21 and next_m != 'stand' and game_state != 'bust' and dealer_num <= 21 and final_score <= 21: #this is where the game loop officially is
print('Would you like to hit or stand?')
time.sleep(1)
next_m = input('')
if next_m == 'stand':
print('Your call')
round_of_game(next_m,dealer_num,player_num,dealer_hand,player_hand,game_state)
elif next_m != 'hit' and next_m != 'stand':
print('Learn to type')
break
elif next_m == 'hit':
round_of_game(next_m,dealer_num,player_num,dealer_hand,player_hand,game_state)
time.sleep(1)
dealer_num = sum(card_value[card] for card in dealer_hand)
player_num = sum(card_value[card] for card in player_hand) #this section of code defines player/dealer_num as sum, adjusts them for aces and equals final_score to player_num
player_num = adjust_for_ace(player_num, player_hand)
dealer_num = adjust_for_ace(dealer_num, dealer_hand)
final_score = player_num
if next_m != 'hit' and next_m != 'stand':
break
time.sleep(1)
dealer_num = sum(card_value[card] for card in dealer_hand)
player_num = sum(card_value[card] for card in player_hand) #this section of code defines player/dealer_num as sum, adjusts them for aces and equals final_score to player_num
player_num = adjust_for_ace(player_num, player_hand)
dealer_num = adjust_for_ace(dealer_num, dealer_hand)
final_score = player_num
clear_output()
print('Your cards:',player_hand)
print("Dealer's cards",dealer_hand)
print('Your Score:',final_score)
print("Dealer's Score",dealer_num)
if (final_score > dealer_num and not final_score > 21) or (dealer_num > 21 and not final_score > 21):
if len(player_hand) == 2 and (player_num == 21 or final_score == 21):
print('natural blackjack!')
user_cash += initial_bet * (3/2) #This section is for the blackjack bonus if the play gets a natural blackjack.
print(f'You Won ${initial_bet * (3/2)}!!')
else:
print('Player Wins!')
user_cash += initial_bet
print(f'You Won ${initial_bet}!!')
elif (dealer_num > final_score and not dealer_num > 21) or (final_score > 21 and not dealer_num > 21):
if insurance == 1:
print('Dealer Wins!')
print('But you have insurace so you dont have to worry about it')
else:
print('Dealer Wins!')
user_cash -= initial_bet
print(f'You Lost ${initial_bet}, Better Luck Next Time Bud')
elif final_score == dealer_num or (final_score > 21 and dealer_num > 21):
print('Push!')
if user_cash <= 0:
print('You are out of money!')
break
else:
print(f'Your current balance is: ${user_cash}')
print('Would you like to play again?')
time.sleep(1)
play_again = input('')
if play_again == 'Yes' or play_again == 'yes': #to continue playing type yes
clear_output()
print('Good Choice')
continue
elif play_again == 'No' or play_again == 'no': #to leave type no
clear_output()
print('Well goodbye for now.')
break
else:
clear_output()
print("We Speak English in This Casino Ma'am")