CollegeWork/OOP/Card Game/main.py

71 lines
2.0 KiB
Python
Raw Normal View History

2023-04-25 11:23:25 +01:00
# Python OOP implementation of blackjack.
from random import choice
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-25 11:23:25 +01:00
2023-04-25 11:52:54 +01:00
def show_cards(self, players):
print(f"{self.name}'s cards:")
for card in self.cards:
print(f"> {card.value} of {card.suit}")
print()
2023-04-25 11:23:25 +01:00
class Dealer(Player):
def __init__(self):
super().__init__(self)
self.name = "dealer"
2023-04-25 11:52:54 +01:00
def show_cards(self, players):
print(f"{self.name}'s cards:")
for player in players[1:]:
if player.done is False:
print(f"> {self.cards[0].value} of {self.cards[0].suit}")
for card in self.cards[1:]:
print(f"> HIDDEN")
print()
return
for card in self.cards:
print(f"> {card.value} of {card.suit}")
print()
2023-04-25 11:23:25 +01:00
class Game:
def __init__(self):
self.players = [Dealer()]
for player_num in range(1):
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()
for player in self.players:
2023-04-25 11:52:54 +01:00
player.show_cards(self.players)
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())
if __name__ == "__main__":
game = Game()