M blackjack/exception.py => blackjack/exception.py +16 -6
@@ 1,11 1,21 @@
"""This module contains the custom Exception used to manage game flow"""
-
+from player import Player
class BlackJackException(Exception):
- """The exception BlackJackException used to manage game flow. Contains a
- field `message` with the message to be displayed.
- """
+ """The base BlackJackException used to manage game flow."""
+
+
+class ExitException(BlackJackException):
+ """The ExitException used to exit the game."""
+
+class BustedException(BlackJackException):
+ """The BustedException used to signify busting."""
+
+class FoldException(BlackJackException):
+ """The FoldException used to signify folding."""
- def __init__(self, message: str):
+class WinException(BlackJackException):
+ """The exception used to signify winning."""
+ def __init__(self, player: Player):
super().__init__()
- self.messsage = message
+ self.winning_player = player
M blackjack/main.py => blackjack/main.py +59 -20
@@ 3,30 3,69 @@ import os
import sys
from deck import Deck
from player import Dealer, Human
+from exception import BustedException, WinException
+from exception import ExitException, FoldException
+
+
+def print_header():
+ """Prints the persistant header message from global `PLAYER` and `DEALER`
+ variables.
+ """
+ global PLAYER
+ global DEALER
+ os.system('cls' if os.name == 'nt' else 'clear')
+ print('--------------- BLACKJACK ---------------', end='\n\n')
+ print(DEALER, end='\n\n')
+ print(PLAYER, end='\n\n')
+ print('-----------------------------------------')
+
+
+def bet_or_exit():
+ """Asks the user whether they want to `bet` or exit."""
+ choice = input('Input your bet (0 to fold, q to exit): ')
+ if choice == '0':
+ raise FoldException()
+ elif choice == 'q':
+ raise ExitException()
+ else:
+ PLAYER.bet(int(choice))
+
+
+def hit_or_stand():
+ """Asks the user whether they want to `hit` or `stand`."""
+ print('Choose your action:')
+ print('[1] Hit [2] Stand [q] Quit ')
+ while True:
+ try:
+ choice = int(input('Choose an option: '))
+ break
+ except ValueError:
+ pass
+ if choice == 0:
+ print('Exiting...')
+ sys.exit()
+ elif choice == 1:
+ PLAYER.hit(DECK, 1)
+
if __name__ == '__main__':
- deck = Deck()
- dealer = Dealer(deck)
- player = Human(deck, 1000)
+ DECK = Deck()
+ DEALER = Dealer(DECK)
+ PLAYER = Human(DECK, 1000)
while True:
- os.system('cls' if os.name == 'nt' else 'clear')
- print('------------------------ BLACKJACK ------------------------', end='\n\n')
- print(dealer, end='\n\n')
- print(player, end='\n\n')
- print('------------')
- if player.calculate_points()[0] > 21:
- print('Busted!')
- sys.exit()
- print('[1] Hit [2] Stand [0] Quit ')
+ print_header()
+ try:
+ bet_or_exit()
+ except FoldException as ex:
+ print(ex.messsage)
+ continue
+ except ValueError:
+ continue
while True:
+ print_header()
try:
- choice = int(input('Choose an option: '))
+ hit_or_stand()
+ except BustedException as ex:
+ print('Busted!')
break
- except ValueError:
- pass
- if choice == 0:
- print('Exiting...')
- sys.exit()
- elif choice == 1:
- player.hit(deck, 1)
M blackjack/player.py => blackjack/player.py +5 -3
@@ 2,7 2,7 @@
`Dealer` and `Human`.
"""
from deck import Card, Deck
-from exception import BlackJackException
+from exception import BustedException, WinException
class Player:
@@ 56,6 56,8 @@ class Player:
except ValueError:
# TODO: Handle error when the deck is empty
pass
+ if self.calculate_points() > 21:
+ raise BustedException
class Dealer(Player):
@@ 74,8 76,8 @@ class Dealer(Player):
points = self.calculate_points()
oponent_points = oponent.calculate_points()
if oponent_points < points <= 21:
- raise BlackJackException(f'The {self.name} wins!')
- raise BlackJackException(f'The {oponent.name} wins!')
+ raise WinException(self)
+ raise WinException(oponent)
class Human(Player):