why many word when few do trick

This commit is contained in:
Adam 2023-01-01 23:37:37 -05:00
parent 2875f08ed7
commit b8bb4a338b
4 changed files with 58 additions and 55 deletions

33
main.py
View file

@ -1,28 +1,25 @@
from src.deck import Deck from src.deck import Deck
from src.io import CLI from src.io import CLI
from src.blackjack import Blackjack from src.blackjack import Blackjack
from src.controller import PlayerController
def main(): def main():
kill = False player = PlayerController()
ui = CLI() screen = CLI()
print(ui.intro)
num_decks = ui.get_decks() print(screen.intro)
if num_decks in 'qx':
kill = True deck = Deck(player.get_decks())
deck = Deck(num_decks)
while not kill: while player.seated:
game = Blackjack(deck) game = Blackjack(deck)
game.deal() game.deal()
game.check_player_blackjack()
print(screen.update(game))
while game.active: while game.active:
print(ui.update(game)) game.update(player.get_input(deck.count()))
if not game.blackjack(): print(screen.update(game))
game.handle_player_input(ui.player_move(deck.count()), kill)
player.ask_deal_again()
print(ui.update(game)) main()
if not ui.play_again():
kill = True
main()

View file

@ -25,39 +25,39 @@ class Blackjack:
if player.score(self) > 21: if player.score(self) > 21:
self.active = False self.active = False
status = player.name + ' Bust!' status = player.name + ' Bust!'
elif player.blackjack(self): for player in self.players:
if player.blackjack(self):
self.active = False self.active = False
status = player.name + ' has Blackjack!' status = player.name + ' has Blackjack!'
return status return status
def deal(self): def deal(self):
for player in self.players: for player in self.players:
for _ in range(2): for _ in range(2):
player.hand.append(self.deck.draw()) player.hand.append(self.deck.draw())
def blackjack(self): def check_player_blackjack(self):
for player in self.players: for player in self.players:
if player.blackjack(self): if player.blackjack(self):
self.active = False self.active = False
return True return True
return False return False
def handle_player_input(self, input, kill): def hit(self):
match input: self.player.hand.append(self.deck.draw())
case 'y': if self.player.bust(self):
self.active = False self.active = False
case 'q': def stand(self):
self.active = False self.active = False
kill = True while self.dealer.score(self) < 17:
case 'n': self.dealer.hand.append(self.deck.draw())
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 update(self, input):
if not input:
pass
elif input in 'yqn':
self.active = False
elif input == 'h':
self.hit()
elif input == 's':
self.stand()

22
src/controller.py Normal file
View file

@ -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

View file

@ -97,21 +97,5 @@ o8YooP' 8 .oPYo. .oPYo. 8 .o 8 .oPYo. .oPYo. 8 .o
return buffer 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? ')