why many word when few do trick
This commit is contained in:
parent
2875f08ed7
commit
b8bb4a338b
4 changed files with 58 additions and 55 deletions
29
main.py
29
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()
|
||||
|
||||
while not kill:
|
||||
print(screen.intro)
|
||||
|
||||
deck = Deck(player.get_decks())
|
||||
|
||||
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)
|
||||
game.update(player.get_input(deck.count()))
|
||||
print(screen.update(game))
|
||||
|
||||
print(ui.update(game))
|
||||
|
||||
if not ui.play_again():
|
||||
kill = True
|
||||
player.ask_deal_again()
|
||||
main()
|
||||
|
|
|
@ -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
22
src/controller.py
Normal 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
|
||||
|
16
src/io.py
16
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? ')
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue