From 2875f08ed79ab8fbae797bdf1309f13004218463 Mon Sep 17 00:00:00 2001 From: Adam <24621027+WhiteDopeOnPunk@users.noreply.github.com> Date: Sat, 31 Dec 2022 21:32:54 -0500 Subject: [PATCH] just like tiger woods --- blackjack.py | 28 ------------ main.py | 28 ++++++++++++ src/{gamemaster.py => blackjack.py} | 71 ++++++++++++++--------------- src/deck.py | 12 ++--- src/io.py | 5 +- 5 files changed, 73 insertions(+), 71 deletions(-) delete mode 100644 blackjack.py create mode 100644 main.py rename src/{gamemaster.py => blackjack.py} (65%) diff --git a/blackjack.py b/blackjack.py deleted file mode 100644 index b1f1359..0000000 --- a/blackjack.py +++ /dev/null @@ -1,28 +0,0 @@ -from src.deck import Deck -from src.io import CLI -from src.gamemaster import GameMaster - -kill = False -ui = CLI() -print(ui.intro) -num_decks = ui.get_decks() -if num_decks == 'q': - kill = True -deck = Deck(num_decks) - -while not kill: - game = GameMaster(deck) - game.active = True - - game.deal() - - 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 - diff --git a/main.py b/main.py new file mode 100644 index 0000000..e40c74c --- /dev/null +++ b/main.py @@ -0,0 +1,28 @@ +from src.deck import Deck +from src.io import CLI +from src.blackjack import Blackjack + +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) + + while not kill: + game = Blackjack(deck) + + game.deal() + + 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() diff --git a/src/gamemaster.py b/src/blackjack.py similarity index 65% rename from src/gamemaster.py rename to src/blackjack.py index 2a72eef..4b80983 100644 --- a/src/gamemaster.py +++ b/src/blackjack.py @@ -1,44 +1,12 @@ from src.player import Player -class GameMaster: +class Blackjack: def __init__(self, deck): + self.deck = 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()) + self.dealer = self.players[0] # Mickey + self.player = self.players[1] # Mouse def score(self): status = 'Error' @@ -62,3 +30,34 @@ class GameMaster: 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): + 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()) + diff --git a/src/deck.py b/src/deck.py index e5d2488..df647f1 100644 --- a/src/deck.py +++ b/src/deck.py @@ -8,19 +8,19 @@ class Deck: def count(self): return len(self.cards) - def shuffle(self, n_decks): - self.cards = [] - n_decks = 1 if not n_decks or n_decks not in '12345678' else int(n_decks) + def shuffle(self): suits = '♠♥♦♣' cards = 'A234567890JQK' - while n_decks > 0: + decks_to_shuffle = int(self.n_decks) + + while decks_to_shuffle > 0: self.cards += [card + suit for card in cards for suit in suits] - n_decks -= 1 + decks_to_shuffle -= 1 def check(self): if self.count() < 1: - self.shuffle(self.n_decks) + self.shuffle() def draw(self): self.check() diff --git a/src/io.py b/src/io.py index 9b27d44..fc93032 100644 --- a/src/io.py +++ b/src/io.py @@ -98,7 +98,10 @@ o8YooP' 8 .oPYo. .oPYo. 8 .o 8 .oPYo. .oPYo. 8 .o return buffer def get_decks(self): - return input('How many decks? (1-8): ') + 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():