OOP Blackjack Improvements
This commit is contained in:
parent
b0882de943
commit
51499a9f79
@ -22,23 +22,29 @@ class Player:
|
||||
# This is so each player can take cards until they are "done"
|
||||
# Then the dealer can take his cards
|
||||
self.done = False
|
||||
self.win = False
|
||||
self.bust = False
|
||||
|
||||
def show_cards(self):
|
||||
print(f"{self.name}'s cards:")
|
||||
for card in self.cards:
|
||||
print(f"> {card.value} of {card.suit}")
|
||||
print(f"Total: {self.total()}")
|
||||
print()
|
||||
print(f"Total:", end=" ")
|
||||
for value in self.total():
|
||||
print(f"{value}", end=" ")
|
||||
print("\n")
|
||||
|
||||
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
|
||||
total = [0 for _ in range(len(self.cards)+1)]
|
||||
for card in self.cards:
|
||||
total += values[card.value]
|
||||
total[0] += 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))
|
||||
for i in range(aces_in_cards):
|
||||
total[i+1] = total[i] + 10
|
||||
return [value for value in total if value != 0]
|
||||
|
||||
|
||||
class Dealer(Player):
|
||||
@ -57,22 +63,36 @@ class Dealer(Player):
|
||||
return
|
||||
for card in self.cards:
|
||||
print(f"> {card.value} of {card.suit}")
|
||||
print(f"Total: {super().total(self)}")
|
||||
print(f"Total:", end=" ")
|
||||
for value in super().total():
|
||||
print(f"{value}", end=" ")
|
||||
print()
|
||||
|
||||
|
||||
class Game:
|
||||
def __init__(self):
|
||||
self.players = [Dealer()]
|
||||
for player_num in range(1):
|
||||
for player_num in range(100):
|
||||
self.players.append(Player(player_num+1))
|
||||
self.deal_cards()
|
||||
|
||||
def main_loop(self):
|
||||
while True:
|
||||
for player in self.players[1:]:
|
||||
system("cls" if os_name == "nt" else "clear")
|
||||
|
||||
self.overview()
|
||||
|
||||
self.check_for_win()
|
||||
|
||||
if not player.done:
|
||||
self.player_choice(player)
|
||||
|
||||
self.check_for_loss()
|
||||
|
||||
if self.players_are_done():
|
||||
break
|
||||
|
||||
def deal_cards(self):
|
||||
for player in self.players:
|
||||
for _ in range(2):
|
||||
@ -83,6 +103,39 @@ class Game:
|
||||
for player in self.players[1:]:
|
||||
player.show_cards()
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def players_are_done(self):
|
||||
for player in self.players[1:]:
|
||||
if not player.done:
|
||||
@ -95,6 +148,7 @@ if __name__ == "__main__":
|
||||
game.main_loop()
|
||||
|
||||
# TODO:
|
||||
# - Win checking
|
||||
# - Player choice (hit, stand)
|
||||
# - Dealers turn
|
||||
# - [X] Win checking
|
||||
# - [X] Lose checking
|
||||
# - [ ] Player choice (hit, stand)
|
||||
# - [ ] Dealers turn
|
||||
|
Loading…
Reference in New Issue
Block a user