Files
blackjack/src/pyjack/game.py

134 lines
4.0 KiB
Python
Raw Normal View History

2025-10-26 22:00:11 +01:00
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."""
2025-10-26 22:03:39 +01:00
# TODO: - play_round() function
# - show_cards function
2025-10-26 22:00:11 +01:00
def __init__(self) -> None:
"""Creates instances of 'Deck', 'Player', and 'Dealer'."""
self.deck = Deck()
self.player = Player(0)
self.dealer = Dealer(1000)
2025-10-27 18:28:54 +01:00
def compare_hands(self, bet: float) -> str:
2025-10-26 22:00:11 +01:00
"""*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!")."""
2025-10-27 17:39:16 +01:00
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
2025-10-26 22:00:11 +01:00
return "Player Lost"
2025-10-27 17:39:16 +01:00
# Check for normal values
if player_value > dealer_value:
2025-10-26 22:00:11 +01:00
self.player.chips += bet
return "Player Wins"
2025-10-27 17:39:16 +01:00
elif player_value < dealer_value:
self.player.chips -= bet
return "Player Lost"
2025-10-26 22:00:11 +01:00
else:
2025-10-27 17:39:16 +01:00
return "Tie"
2025-10-27 18:28:54 +01:00
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
2025-10-29 21:01:03 +01:00
player_hand = self.player.hand
2025-10-27 18:28:54 +01:00
dealer_cards = self.dealer.hand.cards
2025-10-29 21:01:03 +01:00
dealer_hand = self.dealer.hand
2025-10-27 18:28:54 +01:00
print("--Player Cards:--")
for card in player_cards:
print(str(card))
2025-10-29 21:01:03 +01:00
print(f"Hand value: {player_hand.value}")
2025-10-27 18:28:54 +01:00
if hide_dealer:
card = dealer_cards[0]
print("--Dealer Cards:--")
print(str(card))
2025-10-29 21:01:03 +01:00
print("XXXX\n")
2025-10-27 18:28:54 +01:00
else:
print("--Dealer Cards:--")
for card in dealer_cards:
print(str(card))
2025-10-29 21:01:03 +01:00
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
# 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))
player.hand.clear_hand()
dealer.hand.clear_hand()