We will put the code which needs to run at the beginning of any game into the GameState __init__ method. This code will then run any time we make a new game:
class GameState: def __init__(self): self.deck = Deck() self.deck.shuffle() self.player_hand = Hand() self.dealer_hand = Hand(dealer=True) for i in range(2): self.player_hand.add_card(self.deck.deal()) self.dealer_hand.add_card(self.deck.deal()) self.has_winner = ''
Inside the __init__ method, we create and shuffle our Deck, assign a Hand to our player and dealer, and initialize the winner as nobody (using an empty string).
Our player_is_over method now sits in this class. Copy it over if you are using a new file.
We will slightly modify our old check_for_blackjack ...