From 7572833e60d7446f3f8c18111a8de58aef9f4ec7 Mon Sep 17 00:00:00 2001 From: Adam <24621027+WhiteDopeOnPunk@users.noreply.github.com> Date: Sat, 31 Dec 2022 00:47:08 -0500 Subject: [PATCH] gamemaster is the boss --- blackjack.py | 32 ++++++-------------------------- src/gamemaster.py | 35 ++++++++++++++++++++++++++++++++++- src/io.py | 15 +++++++-------- 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/blackjack.py b/blackjack.py index 1a57d41..1e03337 100644 --- a/blackjack.py +++ b/blackjack.py @@ -7,36 +7,16 @@ def main(): global ui global num_decks global deck - game = GameMaster() + game = GameMaster(deck) - for player in game.players: - for _ in range(2): - player.hand.append(deck.draw()) - - dealer = game.dealer - player = game.player + game.deal() while game.active: - ui.update(game) + print(ui.update(game)) + if not game.blackjack(): + game.handle_player_input(ui.player_move(deck.count()), kill) - if game.active: - match ui.player_move(deck).lower(): - case 'y': - game.active = False - case 'q': - game.active = False - kill = True - case 'n': - game.active = False - case 'h': - player.hand.append(deck.draw()) - if player.bust(game): - game.active = False - case 's': - game.active = False - while dealer.score(game) < 17: - dealer.hand.append(deck.draw()) - ui.update(game) + print(ui.update(game)) kill = False ui = CLI() diff --git a/src/gamemaster.py b/src/gamemaster.py index f51b9c3..2a72eef 100644 --- a/src/gamemaster.py +++ b/src/gamemaster.py @@ -1,11 +1,44 @@ from src.player import Player class GameMaster: - def __init__(self): + def __init__(self, deck): self.active = True self.players = [Player('Dealer'), Player('Player')] # 5-9 seats self.dealer = self.players[0] self.player = self.players[1] + self.deck = deck + + def deal(self): + for player in self.players: + for _ in range(2): + player.hand.append(self.deck.draw()) + + def blackjack(self): + for player in self.players: + if player.blackjack(self): + self.active = False + return True + + return False + + def handle_player_input(self, input, kill): + if self.active: + match input: + case 'y': + self.active = False + case 'q': + self.active = False + kill = True + case 'n': + self.active = False + case 'h': + self.player.hand.append(self.deck.draw()) + if self.player.bust(self): + self.active = False + case 's': + self.active = False + while self.dealer.score(self) < 17: + self.dealer.hand.append(self.deck.draw()) def score(self): status = 'Error' diff --git a/src/io.py b/src/io.py index 36e1c11..9b27d44 100644 --- a/src/io.py +++ b/src/io.py @@ -91,13 +91,12 @@ o8YooP' 8 .oPYo. .oPYo. 8 .o 8 .oPYo. .oPYo. 8 .o return buffer def update(self, game): - print(self.clear + self.title + - self.show_players(game)) - for player in game.players: - if player.blackjack(game): - game.active = False + buffer = f'{self.clear}{self.title}{self.show_players(game)}' if not game.active: - print(game.score()) + buffer += '\n' + game.score() + + return buffer + def get_decks(self): return input('How many decks? (1-8): ') @@ -110,6 +109,6 @@ o8YooP' 8 .oPYo. .oPYo. 8 .o 8 .oPYo. .oPYo. 8 .o case _: return True - def player_move(self, deck): - return input(str(deck.count()) + ' cards left in deck.\n[H]it or [S]tand? ') + def player_move(self, count): + return input(f'{count} cards left in deck.\n[H]it or [S]tand? ')