Files
blackjack/src/pyjack/game.py
2025-10-29 21:13:30 +01:00

134 lines
4.0 KiB
Python

from pyjack.dealer import Dealer
from pyjack.deck import Deck
from pyjack.player import Player
class Game:
"""The Game class is the central coordinator, managing the flow of
the entire round, from setup to payout."""
# TODO: - play_round() function
# - show_cards function
def __init__(self) -> None:
"""Creates instances of 'Deck', 'Player', and 'Dealer'."""
self.deck = Deck()
self.player = Player(0)
self.dealer = Dealer(1000)
def compare_hands(self, bet: float) -> str:
"""*Resolution/Payout*: Compares final hand values, checks for
Bust, Blackjack, and highest value.
*Updates* player.chips based on the outcome and the bet.
*Returns* the result string (e.g., "Player Wins!")."""
player_value = self.player.hand.value
dealer_value = self.dealer.hand.value
player_cards = len(self.player.hand.cards)
dealer_cards = len(self.dealer.hand.cards)
# Check player bust:
if player_value > 21:
self.player.chips -= bet
return "Player Lost"
# Check dealer bust:
if dealer_value > 21:
self.player.chips += bet
return "Player Wins"
# Check for Blackjack (21 with 2 cards)
is_player_bj = player_value == 21 and player_cards == 2
is_dealer_bj = dealer_value == 21 and dealer_cards == 2
if is_player_bj and not is_dealer_bj:
self.player.chips += bet * 1.5
return "Player Wins 3:2"
elif is_dealer_bj:
self.player.chips -= bet
return "Player Lost"
# Check for normal values
if player_value > dealer_value:
self.player.chips += bet
return "Player Wins"
elif player_value < dealer_value:
self.player.chips -= bet
return "Player Lost"
else:
return "Tie"
def show_cards(self, hide_dealer: bool = False) -> None:
"""Displays the cards. The boolean determines if the dealer's
second card is shown or concealed."""
player_cards = self.player.hand.cards
player_hand = self.player.hand
dealer_cards = self.dealer.hand.cards
dealer_hand = self.dealer.hand
print("--Player Cards:--")
for card in player_cards:
print(str(card))
print(f"Hand value: {player_hand.value}")
if hide_dealer:
card = dealer_cards[0]
print("--Dealer Cards:--")
print(str(card))
print("XXXX\n")
else:
print("--Dealer Cards:--")
for card in dealer_cards:
print(str(card))
print(f"Hand value: {dealer_hand.value}\n")
def show_player_cards(self) -> None:
"""Displays the players hand"""
player_cards = self.player.hand.cards
player_hand = self.player.hand
print("--Player Cards:--")
for card in player_cards:
print(str(card))
print(f"Hand value: {player_hand.value}")
def play_round(self) -> None:
"""Logic of a game round"""
# Simplify value calls:
deck = self.deck
player = self.player
dealer = self.dealer
player.hand.__init__()
dealer.hand.__init__()
# beginning of a round, shuffle deck and deal cards
deck.shuffle()
player.hand.add_card(deck.deal_card())
player.hand.add_card(deck.deal_card())
dealer.hand.add_card(deck.deal_card())
dealer.hand.add_card(deck.deal_card())
self.show_cards(hide_dealer=True)
player_turn = True
while player_turn:
if input("Do you want another card? [y] ") == "y":
player.hit(deck)
self.show_player_cards()
else:
print("\n")
player.stand()
player_turn = False
# self.show_cards()
dealer.play(deck)
self.show_cards()
print(self.compare_hands(0))