diff --git a/main.py b/main.py index e40c74c..88ffb68 100644 --- a/main.py +++ b/main.py @@ -1,28 +1,25 @@ from src.deck import Deck from src.io import CLI from src.blackjack import Blackjack +from src.controller import PlayerController def main(): - kill = False - ui = CLI() - print(ui.intro) - num_decks = ui.get_decks() - if num_decks in 'qx': - kill = True - deck = Deck(num_decks) + player = PlayerController() + screen = CLI() + + print(screen.intro) + + deck = Deck(player.get_decks()) - while not kill: + while player.seated: game = Blackjack(deck) - game.deal() + game.check_player_blackjack() + print(screen.update(game)) while game.active: - print(ui.update(game)) - if not game.blackjack(): - game.handle_player_input(ui.player_move(deck.count()), kill) - - print(ui.update(game)) - - if not ui.play_again(): - kill = True -main() + game.update(player.get_input(deck.count())) + print(screen.update(game)) + + player.ask_deal_again() +main() diff --git a/src/blackjack.py b/src/blackjack.py index 4b80983..e103ec6 100644 --- a/src/blackjack.py +++ b/src/blackjack.py @@ -25,39 +25,39 @@ class Blackjack: if player.score(self) > 21: self.active = False status = player.name + ' Bust!' - elif player.blackjack(self): + for player in self.players: + if player.blackjack(self): self.active = False status = player.name + ' has Blackjack!' - return status + def deal(self): for player in self.players: for _ in range(2): player.hand.append(self.deck.draw()) - def blackjack(self): + def check_player_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): - 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 hit(self): + self.player.hand.append(self.deck.draw()) + if self.player.bust(self): + self.active = False + def stand(self): + self.active = False + while self.dealer.score(self) < 17: + self.dealer.hand.append(self.deck.draw()) + def update(self, input): + if not input: + pass + elif input in 'yqn': + self.active = False + elif input == 'h': + self.hit() + elif input == 's': + self.stand() diff --git a/src/controller.py b/src/controller.py new file mode 100644 index 0000000..cebd7c5 --- /dev/null +++ b/src/controller.py @@ -0,0 +1,22 @@ +class PlayerController(): + def __init__(self): + self.seated = True + + def get_decks(self): + user_input = input('How many decks? (1-8): ') + if not user_input: + return '1' + if user_input in 'qx': + self.seated = False + return (int(user_input) if user_input in '12345678' else 1) + + def get_input(self, count): + return input(f'{count} cards left in deck.\n[H]it or [S]tand? ') + + def ask_deal_again(self): + user_input = input('Play again? [Y/n] ').lower() + if not user_input: + return + elif user_input in 'qnx': + self.seated = False + diff --git a/src/io.py b/src/io.py index fc93032..4db2f4e 100644 --- a/src/io.py +++ b/src/io.py @@ -97,21 +97,5 @@ o8YooP' 8 .oPYo. .oPYo. 8 .o 8 .oPYo. .oPYo. 8 .o return buffer - def get_decks(self): - user_input = input('How many decks? (1-8): ') - if not user_input: - return '1' - return user_input - def play_again(self): - match input('Play again? [Y/n] ').lower(): - case 'n': - return False - case 'q': - return False - case _: - return True - - def player_move(self, count): - return input(f'{count} cards left in deck.\n[H]it or [S]tand? ')