changes to card game

This commit is contained in:
Evelyne 2023-04-25 12:29:23 +01:00
parent add4b42d12
commit c467e7b98c
2 changed files with 27 additions and 4 deletions

View File

@ -1,6 +1,7 @@
# Python OOP implementation of blackjack. # Python OOP implementation of blackjack.
from random import choice from random import choice
from os import system, name as os_name
class Card: class Card:
def __init__(self): def __init__(self):
@ -26,8 +27,19 @@ class Player:
print(f"{self.name}'s cards:") print(f"{self.name}'s cards:")
for card in self.cards: for card in self.cards:
print(f"> {card.value} of {card.suit}") print(f"> {card.value} of {card.suit}")
print(f"Total: {self.total()}")
print() print()
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
total = 0
for card in self.cards:
total += values[card.value]
if card.value == "Ace":
aces_in_cards += 1
return total if aces_in_cards == 0 else (total, total+(10*aces_in_cards))
class Dealer(Player): class Dealer(Player):
def __init__(self): def __init__(self):
@ -41,10 +53,12 @@ class Dealer(Player):
print(f"> {self.cards[0].value} of {self.cards[0].suit}") print(f"> {self.cards[0].value} of {self.cards[0].suit}")
for card in self.cards[1:]: for card in self.cards[1:]:
print(f"> HIDDEN") print(f"> HIDDEN")
print(f"Total: UNKNOWN")
print() print()
return return
for card in self.cards: for card in self.cards:
print(f"> {card.value} of {card.suit}") print(f"> {card.value} of {card.suit}")
print(f"Total: {super().total(self)}")
print() print()
@ -55,10 +69,12 @@ class Game:
self.players.append(Player(player_num+1)) self.players.append(Player(player_num+1))
self.deal_cards() self.deal_cards()
def main_loop(self):
system("cls" if os_name == "nt" else "clear")
for player in self.players: for player in self.players:
player.show_cards(self.players) player.show_cards(self.players)
def deal_cards(self): def deal_cards(self):
for player in self.players: for player in self.players:
for _ in range(2): for _ in range(2):
@ -68,3 +84,10 @@ class Game:
if __name__ == "__main__": if __name__ == "__main__":
game = Game() game = Game()
game.main_loop()
# TODO:
# - Fix the dealer being passed the players attribute
# - Win checking
# - Player choice (hit, stand)
# - Dealers turn

View File

@ -1,6 +1,6 @@
from random import choice, randint from random import choice, randint
from time import sleep from time import sleep
from os import system from os import system, name as os_name
class Color: class Color:
red = "\033[91m" red = "\033[91m"
@ -45,7 +45,7 @@ class Game:
def gameloop(self): def gameloop(self):
while self.PLAYERS[0].health > 0 and self.PLAYERS[1].health > 0: while self.PLAYERS[0].health > 0 and self.PLAYERS[1].health > 0:
system("cls" if name == "nt" else "clear") system("cls" if os_name == "nt" else "clear")
print(f"{Color.cyan}-----------------------------------------------{Color.norm}") print(f"{Color.cyan}-----------------------------------------------{Color.norm}")
for player in self.PLAYERS: for player in self.PLAYERS:
@ -69,7 +69,7 @@ class Game:
player.update_health(player) player.update_health(player)
sleep(1) sleep(1)
system("cls" if name == "nt" else "clear") system("cls" if os_name == "nt" else "clear")
self.game_end() self.game_end()