2023-04-25 11:23:25 +01:00
|
|
|
# Python OOP implementation of blackjack.
|
|
|
|
|
|
|
|
from random import choice
|
2023-04-25 12:29:23 +01:00
|
|
|
from os import system, name as os_name
|
2023-04-25 11:23:25 +01:00
|
|
|
|
|
|
|
class Card:
|
|
|
|
def __init__(self):
|
|
|
|
# Set colour as random between red and black
|
|
|
|
self.colour = choice(["red", "black"])
|
|
|
|
# Set suit depending on colour since a suit has an allocated colour
|
2023-04-25 11:52:54 +01:00
|
|
|
self.suit = choice(["hearts", "diamonds"]) if self.colour == "red" else choice(["clubs", "spades"])
|
2023-04-25 11:23:25 +01:00
|
|
|
# Set value
|
2023-04-25 11:52:54 +01:00
|
|
|
self.value = choice(["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"])
|
2023-04-25 11:23:25 +01:00
|
|
|
# Set whether the value is a face card or not
|
2023-04-25 11:52:54 +01:00
|
|
|
self.face = True if self.value in ["Jack", "Queen", "King"] else False
|
2023-04-25 11:23:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Player:
|
|
|
|
def __init__(self, number=None):
|
2023-04-25 11:52:54 +01:00
|
|
|
self.name = f"player-{number}"
|
2023-04-25 11:23:25 +01:00
|
|
|
self.cards = []
|
2023-04-25 11:52:54 +01:00
|
|
|
# This is so each player can take cards until they are "done"
|
|
|
|
# Then the dealer can take his cards
|
|
|
|
self.done = False
|
2023-04-27 11:59:14 +01:00
|
|
|
self.win = False
|
|
|
|
self.bust = False
|
2023-04-25 11:23:25 +01:00
|
|
|
|
2023-04-25 14:10:18 +01:00
|
|
|
def show_cards(self):
|
2023-04-25 11:52:54 +01:00
|
|
|
print(f"{self.name}'s cards:")
|
|
|
|
for card in self.cards:
|
|
|
|
print(f"> {card.value} of {card.suit}")
|
2023-04-27 11:59:14 +01:00
|
|
|
print(f"Total:", end=" ")
|
|
|
|
for value in self.total():
|
|
|
|
print(f"{value}", end=" ")
|
|
|
|
print("\n")
|
2023-04-25 11:23:25 +01:00
|
|
|
|
2023-04-25 12:29:23 +01:00
|
|
|
def total(self):
|
|
|
|
values = {"Ace": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7":7, "8": 8, "9": 9, "10": 10, "Jack": 10, "Queen": 10, "King": 10}
|
|
|
|
aces_in_cards = 0
|
2023-04-27 11:59:14 +01:00
|
|
|
total = [0 for _ in range(len(self.cards)+1)]
|
2023-04-25 12:29:23 +01:00
|
|
|
for card in self.cards:
|
2023-04-27 11:59:14 +01:00
|
|
|
total[0] += values[card.value]
|
2023-04-25 12:29:23 +01:00
|
|
|
if card.value == "Ace":
|
|
|
|
aces_in_cards += 1
|
2023-04-27 11:59:14 +01:00
|
|
|
for i in range(aces_in_cards):
|
|
|
|
total[i+1] = total[i] + 10
|
|
|
|
return [value for value in total if value != 0]
|
2023-04-25 12:29:23 +01:00
|
|
|
|
2023-04-25 11:23:25 +01:00
|
|
|
|
|
|
|
class Dealer(Player):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(self)
|
|
|
|
self.name = "dealer"
|
|
|
|
|
2023-04-25 14:10:18 +01:00
|
|
|
def show_cards(self, game):
|
2023-04-25 11:52:54 +01:00
|
|
|
print(f"{self.name}'s cards:")
|
2023-04-25 14:10:18 +01:00
|
|
|
if not game.players_are_done():
|
|
|
|
print(f"> {self.cards[0].value} of {self.cards[0].suit}")
|
|
|
|
for card in self.cards[1:]:
|
|
|
|
print(f"> HIDDEN")
|
|
|
|
print(f"Total: UNKNOWN")
|
|
|
|
print()
|
|
|
|
return
|
2023-04-25 11:52:54 +01:00
|
|
|
for card in self.cards:
|
|
|
|
print(f"> {card.value} of {card.suit}")
|
2023-04-27 11:59:14 +01:00
|
|
|
print(f"Total:", end=" ")
|
|
|
|
for value in super().total():
|
|
|
|
print(f"{value}", end=" ")
|
2023-04-25 11:52:54 +01:00
|
|
|
print()
|
2023-04-25 11:23:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Game:
|
|
|
|
def __init__(self):
|
|
|
|
self.players = [Dealer()]
|
2023-04-27 11:59:14 +01:00
|
|
|
for player_num in range(100):
|
2023-04-25 11:52:54 +01:00
|
|
|
self.players.append(Player(player_num+1))
|
2023-04-25 11:23:25 +01:00
|
|
|
self.deal_cards()
|
|
|
|
|
2023-04-25 12:29:23 +01:00
|
|
|
def main_loop(self):
|
2023-04-27 11:59:14 +01:00
|
|
|
while True:
|
|
|
|
for player in self.players[1:]:
|
|
|
|
system("cls" if os_name == "nt" else "clear")
|
|
|
|
|
|
|
|
self.overview()
|
|
|
|
|
|
|
|
self.check_for_win()
|
2023-04-25 12:29:23 +01:00
|
|
|
|
2023-04-27 11:59:14 +01:00
|
|
|
if not player.done:
|
|
|
|
self.player_choice(player)
|
|
|
|
|
|
|
|
self.check_for_loss()
|
|
|
|
|
|
|
|
if self.players_are_done():
|
|
|
|
break
|
2023-04-25 11:23:25 +01:00
|
|
|
|
|
|
|
def deal_cards(self):
|
|
|
|
for player in self.players:
|
|
|
|
for _ in range(2):
|
|
|
|
player.cards.append(Card())
|
|
|
|
|
2023-04-25 14:10:18 +01:00
|
|
|
def overview(self):
|
|
|
|
self.players[0].show_cards(self)
|
|
|
|
for player in self.players[1:]:
|
|
|
|
player.show_cards()
|
|
|
|
|
2023-04-27 11:59:14 +01:00
|
|
|
def check_for_win(self):
|
|
|
|
for player in self.players[1:]:
|
|
|
|
for value in player.total():
|
|
|
|
if value == 21:
|
|
|
|
player.win = True
|
|
|
|
player.done = True
|
|
|
|
|
|
|
|
def check_for_loss(self):
|
|
|
|
for player in self.players[1:]:
|
|
|
|
bust_count = 0
|
|
|
|
for value in player.total():
|
|
|
|
bust_count = (bust_count + 1) if value > 21 else bust_count
|
|
|
|
if bust_count == len(player.total()):
|
|
|
|
player.bust = True
|
|
|
|
player.done = True
|
|
|
|
|
|
|
|
def player_choice(self, player):
|
|
|
|
match (input(f"{player.name} Hit or Stand? (h/s)")).lower():
|
|
|
|
case "hit":
|
|
|
|
player.cards.append(Card())
|
|
|
|
case "h":
|
|
|
|
player.cards.append(Card())
|
|
|
|
case "stand":
|
|
|
|
player.done = True
|
|
|
|
case "s":
|
|
|
|
player.done = True
|
|
|
|
case _:
|
|
|
|
print("Please Provide A Valid Choice")
|
|
|
|
system("cls" if os_name == "nt" else "clear")
|
|
|
|
self.overview()
|
|
|
|
self.player_choice(player)
|
|
|
|
|
|
|
|
|
2023-04-25 14:10:18 +01:00
|
|
|
def players_are_done(self):
|
|
|
|
for player in self.players[1:]:
|
|
|
|
if not player.done:
|
|
|
|
return False
|
|
|
|
return True
|
2023-04-25 11:23:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
game = Game()
|
2023-04-25 12:29:23 +01:00
|
|
|
game.main_loop()
|
|
|
|
|
|
|
|
# TODO:
|
2023-04-27 11:59:14 +01:00
|
|
|
# - [X] Win checking
|
|
|
|
# - [X] Lose checking
|
|
|
|
# - [ ] Player choice (hit, stand)
|
|
|
|
# - [ ] Dealers turn
|