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.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()

View file

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

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
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? ')