From d015932f42d34676cbb7c6678fd712adc736848a Mon Sep 17 00:00:00 2001 From: Adam <24621027+WhiteDopeOnPunk@users.noreply.github.com> Date: Thu, 22 Dec 2022 18:30:42 -0500 Subject: [PATCH] blacker jacks --- main.py => blackjack.py | 25 ++++++----- src/cardprinter.py | 72 ------------------------------ src/deck.py | 14 +++--- src/gamemaster.py | 8 ++-- src/player.py | 22 +++++---- src/screen.py | 98 +++++++++++++++++++++++++++++++++++------ 6 files changed, 121 insertions(+), 118 deletions(-) rename main.py => blackjack.py (74%) delete mode 100644 src/cardprinter.py diff --git a/main.py b/blackjack.py similarity index 74% rename from main.py rename to blackjack.py index fe94594..ddb3a13 100644 --- a/main.py +++ b/blackjack.py @@ -6,15 +6,9 @@ def main(): global kill global screen global num_decks - deck = Deck() + global deck game = GameMaster() - if num_decks == 'q': - game.active = False - - if deck.count_below(4): - deck.shuffle(num_decks) - for player in game.players: for _ in range(2): player.hand.append(deck.draw()) @@ -23,7 +17,10 @@ def main(): player = game.player while game.active: - screen.update(game.players) + screen.update(game) + if player.blackjack(): + game.active = False + print('Blackjack!') if game.active: user_input = input(str(deck.count()) + ' cards left in deck.\n[H]it or [S]tand? ') @@ -41,19 +38,23 @@ def main(): game.active = False case 'h': player.hand.append(deck.draw()) + if player.bust(game): + game.active = False case 's': - while dealer.score() < 17: + game.active = False + while dealer.score(game) < 17: dealer.hand.append(deck.draw()) - screen.update(game.players) - game.score() + screen.update(game) if not game.active: + game.score() user_input = input('Play again? [Y/n] ') kill = False screen = Screen() -print(screen.show_intro()) +print(screen.intro) num_decks = input('How many decks? (1-8): ') +deck = Deck(num_decks) while not kill: main() diff --git a/src/cardprinter.py b/src/cardprinter.py deleted file mode 100644 index 051a0b5..0000000 --- a/src/cardprinter.py +++ /dev/null @@ -1,72 +0,0 @@ -class CardPrinter: - def make_card(self, vs, style): - deck = '''\ -┌────────┐┐┐┐┐┐ -│░░░░░░░░││││││ -│░░░░░░░░││││││ -│░░░░░░░░││││││ -│░░░░░░░░││││││ -│░░░░░░░░││││││ -└────────┘┘┘┘┘┘''' - card_rest = f'''\ -──────┐ - │ - │ - │ - │ - │ -──────┘''' - hidden_part = f'''\ -┌── -│░░ -│░░ -│░░ -│░░ -│░░ -└──''' - hidden_rest = f'''\ -──────┐ -░░░░░░│ -░░░░░░│ -░░░░░░│ -░░░░░░│ -░░░░░░│ -──────┘''' - value = vs[0] - value = '10' if value == '0' else value + ' ' - suit = vs[1] + ' ' - # request text (not emoji) render of preceeding glyph but can break some fonts - # suit += '\uFE0E' - card_part = f'''\ -┌── -│{value} -│{suit} -│ -│ -│ -└──''' - match style: - case 'deck': - return deck - case 'card_rest': - return card_rest - case 'hidden_part': - return hidden_part - case 'hidden_rest': - return hidden_rest - case 'card_part': - return card_part - - def print_hand(self, player): - player_cards = [self.make_card(card,'card_part') for card in player.hand] - if player == 'Dealer' and game_active: - player_cards[0] = self.make_card('na','hidden_part') - player_cards.append(self.make_card('na','card_rest')) - card_slices = [str(card).splitlines() for card in player_cards] - for i in range(7): - clist = [card_slice[i] for card_slice in card_slices] - carriage = '' - for chunk in clist: - carriage += chunk - print(carriage) - diff --git a/src/deck.py b/src/deck.py index f5ee350..99b2876 100644 --- a/src/deck.py +++ b/src/deck.py @@ -1,7 +1,9 @@ import random class Deck: - cards = [] + def __init__(self, n_decks): + self.cards = [] + self.n_decks = n_decks def count(self): return len(self.cards) @@ -16,10 +18,12 @@ class Deck: while n_decks > 0: self.cards += [card + suit for card in cards for suit in suits] n_decks -= 1 - - def count_below(self, n): - if self.count() < n: - return True + + def check(self): + if self.count() < 4: + self.shuffle(self.n_decks) def draw(self): + self.check() return self.cards.pop(random.choice(range(self.count()))) + diff --git a/src/gamemaster.py b/src/gamemaster.py index c0697da..c3d1748 100644 --- a/src/gamemaster.py +++ b/src/gamemaster.py @@ -10,18 +10,18 @@ class GameMaster: def score(self): status = 'Error' - if self.dealer.score() == self.player.score(): + if self.dealer.score(self) == self.player.score(self): self.active = False status = 'Push.' - elif self.dealer.score() > self.player.score(): + elif self.dealer.score(self) > self.player.score(self): self.active = False status = 'House wins.' - elif self.dealer.score() < self.player.score(): + elif self.dealer.score(self) < self.player.score(self): self.active = False status = 'You win!' for player in self.players: - if player.score() > 21: + if player.score(self) > 21: self.active = False status = player.name + ' Bust!' print(status) diff --git a/src/player.py b/src/player.py index db5ebf5..9583d12 100644 --- a/src/player.py +++ b/src/player.py @@ -2,8 +2,6 @@ class Player: def __init__(self, name): self.name = name self.hand = [] - self.blackjack = False - self.bust = False def tally_hand(self): cards = [card[0] for card in self.hand] @@ -11,21 +9,21 @@ class Player: scores = [10 if card in 'JQK0' else int(card) for card in cards] return scores - def score(self): - score = sum(self.tally_hand()) + def score(self, game): + if self.name == 'Dealer' and game.active: + score = sum(self.tally_hand()[1:]) + else: + score = sum(self.tally_hand()) + if score > 21 and 11 in self.tally_hand(): return score - 10 else: return score - def bust(self): - if self.score() > 21: - self.bust = True + def bust(self, game): + if self.score(game) > 21: + return True def blackjack(self): if self.tally_hand() in [[10,11],[11,10]]: - self.blackjack = True - - def check(self): - self.bust() - self.blackjack() + return True diff --git a/src/screen.py b/src/screen.py index a4eddcf..1f0a2b4 100644 --- a/src/screen.py +++ b/src/screen.py @@ -1,8 +1,6 @@ -from src.cardprinter import CardPrinter -printer = CardPrinter() - class Screen: - title = '''\ + def __init__(self): + self.title = '''\ .oPYo. 8 8 o 8 8 `8 8 8 8 8 @@ -13,15 +11,89 @@ o8YooP' 8 .oPYo. .oPYo. 8 .o 8 .oPYo. .oPYo. 8 .o :......:..:.....::.....:..::......::.....::.....:..::... :::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::''' + self.clear = '\033c' + self.intro = self.clear + self.title + self.deck = '''\ +┌────────┐┐┐┐┐┐ +│░░░░░░░░││││││ +│░░░░░░░░││││││ +│░░░░░░░░││││││ +│░░░░░░░░││││││ +│░░░░░░░░││││││ +└────────┘┘┘┘┘┘''' + self.hidden_part = f'''\ +┌── +│░░ +│░░ +│░░ +│░░ +│░░ +└──''' + self.hidden_rest = f'''\ +──────┐ +░░░░░░│ +░░░░░░│ +░░░░░░│ +░░░░░░│ +░░░░░░│ +──────┘''' + self.card_rest = f'''\ +──────┐ + │ + │ + │ + │ + │ +──────┘''' - def update(self, player_list): - print('\033c', end='') + def make_card(self, vs, style): + value = vs[0] + value = '10' if value == '0' else value + ' ' + suit = vs[1] + ' ' + # request text (not emoji) render of preceeding glyph but can break some fonts + # suit += '\uFE0E' + + card_part = f'''\ +┌── +│{value} +│{suit} +│ +│ +│ +└──''' + + match style: + case 'deck': + return self.deck + case 'card_rest': + return self.card_rest + case 'hidden_part': + return self.hidden_part + case 'hidden_rest': + return self.hidden_rest + case 'card_part': + return card_part + + def print_hand(self, player, game): + player_cards = [self.make_card(card,'card_part') for card in player.hand] + + if player.name == 'Dealer' and game.active: + player_cards[0] = self.make_card('na','hidden_part') + + player_cards.append(self.make_card('na','card_rest')) + card_slices = [str(card).splitlines() for card in player_cards] + + for i in range(7): + clist = [card_slice[i] for card_slice in card_slices] + carriage = '' + for chunk in clist: + carriage += chunk + print(carriage) + + def update(self, game): + print(self.clear) print(self.title) - for player in player_list: + for player in game.players: print(' '+player.name, - '\t\t\t\t Score:',player.score()) - printer.print_hand(player) - - def show_intro(self): - print('\033c', end='') - print(self.title) + '\t\t\t\t Score:',player.score(game)) + self.print_hand(player, game)