The GameState class

As in the previous chapter, the GameState class is responsible for all of the game logic, including handling the deck, determining each player's score, and who has won.

This time around, it will also determine how much money is needed to play each round:

class GameState:
def __init__(self):
self.BASE_BET = 5
self.minimum_bet = self.BASE_BET
self.current_round = 1
self.pot = 0

self.deck = Deck()
self.deck.shuffle()

self.player = Player()
self.dealer = Dealer()

self.begin_round()

This class contains one constant, BASE_BET, which defines both the original bet and how much the bet will increase per round. We have set this to 5, meaning the  minimum_bet will be 5, 10, 15, and so on.

As we increase the bet based on the current round, we need to keep track of it. We use a current_round attribute to accomplish this.

The money in the pot will also be stored as an attribute of our GameState.

As with every iteration of our game, we begin by creating a deck and shuffling it. Instead of creating Hand instances to represent our player and dealer, we can now just create Player and Dealer instances.

With the variables initialized, it's time to begin the first round:

def begin_round(self):
self.has_winner = ''

for i in range(2):
self.player.receive_card(self.deck.deal())
self.dealer.receive_card(self.deck.deal())

self.player.place_bet(self.minimum_bet)
self.add_bet(self.minimum_bet * 2)

At the beginning of each round, we need to ensure that we do not still have a winner set, so we update our has_winner attribute to an empty string.

We then deal both players two cards as usual, this time using the Player class' new receive_card method.

The player then needs to place their bet, removing the minimum bet from their total money. The pot will increase by twice the minimum bet, so we use the add_bet method to add this amount to the game's pot:

def add_bet(self, amount):
self.pot += amount

Now that our GameState has begun the round, we wait for the player to choose an action by clicking the hit or stick button.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset