Skip to content

Instantly share code, notes, and snippets.

@dhinojosa
Created January 27, 2025 23:07
Show Gist options
  • Select an option

  • Save dhinojosa/5cc3a57a5984869c86bd3f36b107626e to your computer and use it in GitHub Desktop.

Select an option

Save dhinojosa/5cc3a57a5984869c86bd3f36b107626e to your computer and use it in GitHub Desktop.
PythonGame
import random
from colorama import Fore, Style, init
# Initialize colorama
init(autoreset=True)
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def rank_value(self):
if self.rank in "JQK":
return 10
elif self.rank == "A":
return 1
else:
return int(self.rank)
def display(self):
lines = [
"┌───────┐",
f"│{self.rank}{' ' if self.rank != '10' else ''} │",
"│ │",
f"│ {self.suit} │",
"│ │",
f"│ {' ' if self.rank != '10' else ''}{self.rank}│",
"└───────┘",
]
color = Fore.RED if self.suit in "♥♦" else Fore.BLACK
return color + "\n".join(lines)
def __str__(self):
return f"Card(suit={self.suit}, rank={self.rank})"
def __eq__(self, other):
if isinstance(other, Card):
return self.suit == other.suit and self.rank == other.rank
return False
def __hash__(self):
return hash((self.suit, self.rank))
class Deck:
def __init__(self):
card_values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
suits = ["\u2660", "\u2666", "\u2665", "\u2663"] # Spades, Diamonds, Hearts, Clubs
self.cards = [Card(suit, rank) for suit in suits for rank in card_values]
random.shuffle(self.cards)
def size(self):
return len(self.cards)
def draw(self):
return self.cards.pop(0)
class Game:
def __init__(self):
self.deck = Deck()
self.dealer_hand = []
self.player_hand = []
def initial_deal(self):
self.player_hand.append(self.deck.draw())
self.dealer_hand.append(self.deck.draw())
self.player_hand.append(self.deck.draw())
self.dealer_hand.append(self.deck.draw())
def play(self):
player_busted = False
while not player_busted:
self.display_game_state()
player_choice = input("[H]it or [S]tand? ").strip().lower()
if player_choice.startswith("s"):
break
elif player_choice.startswith("h"):
self.player_hand.append(self.deck.draw())
if self.hand_value_of(self.player_hand) > 21:
player_busted = True
else:
print("You need to [H]it or [S]tand")
if not player_busted:
while self.hand_value_of(self.dealer_hand) <= 16:
self.dealer_hand.append(self.deck.draw())
self.display_final_game_state()
self.determine_winner(player_busted)
def hand_value_of(self, hand):
hand_value = sum(card.rank_value() for card in hand)
has_ace = any(card.rank_value() == 1 for card in hand)
if has_ace and hand_value <= 11:
hand_value += 10
return hand_value
def display_game_state(self):
print("\033c", end="")
print("Dealer has:")
print(self.dealer_hand[0].display())
self.display_back_of_card()
print("\nPlayer has:")
self.display_hand(self.player_hand)
print(f" ({self.hand_value_of(self.player_hand)})")
@staticmethod
def display_back_of_card():
lines = [
"\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510",
"\u2502░░░░░░░░░\u2502",
"\u2502░ J I T ░\u2502",
"\u2502░ T E R ░\u2502",
"\u2502░ T E D ░\u2502",
"\u2502░░░░░░░░░\u2502",
"\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518",
]
print("\n".join(lines))
@staticmethod
def display_hand(hand):
print("\n".join(card.display() for card in hand))
def display_final_game_state(self):
print("\033c", end="")
print("Dealer has:")
self.display_hand(self.dealer_hand)
print(f" ({self.hand_value_of(self.dealer_hand)})")
print("\nPlayer has:")
self.display_hand(self.player_hand)
print(f" ({self.hand_value_of(self.player_hand)})")
def determine_winner(self, player_busted):
if player_busted:
print("You Busted, so you lose. \U0001F4B8")
elif self.hand_value_of(self.dealer_hand) > 21:
print("Dealer went BUST, Player wins! Yay for you!! \U0001F4B5")
elif self.hand_value_of(self.dealer_hand) < self.hand_value_of(self.player_hand):
print("You beat the Dealer! \U0001F4B5")
elif self.hand_value_of(self.dealer_hand) == self.hand_value_of(self.player_hand):
print("Push: You tie with the Dealer. \U0001F4B8")
else:
print("You lost to the Dealer. \U0001F4B8")
if __name__ == "__main__":
game = Game()
print(Fore.GREEN + "Welcome to" + Fore.RED + " Jitterted's" + Fore.BLACK + " BlackJack")
game.initial_deal()
game.play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment