Choosing to stick

As you may remember from the previous chapters, when the player chooses to stick, the round is automatically over, and the winner needs to be determined by comparing the player's hand to the dealer's.

The method called by our stick button is that of the GameWindow itself. This method only passes on the stick choice to the GameScreen:

def stick(self):
self.game_screen.stick()

The GameScreen grabs the final state from our GameState, then performs the same steps as it did when we found a winner during our hit logic:

def stick(self):
table_state = self.game_state.calculate_final_state()

self.show_dealers_cards(table_state)
self.show_winner_text(table_state['has_winner'])
self.master.on_winner()

The final state is calculated in the same way as in previous chapters. The code is shortened slightly by our new player properties:

def calculate_final_state(self):
player_hand_value = self.player.score
dealer_hand_value = self.dealer.score

if player_hand_value == dealer_hand_value:
winner = 'dp'
elif player_hand_value > dealer_hand_value:
winner = 'p'
else:
winner = 'd'

self.has_winner = winner

table_state = {
'player_cards': self.player.cards,
'dealer_cards': self.dealer.cards,
'has_winner': winner,
}

return table_state

This dictionary is returned to the GameScreen so that it can flip the dealer's card and display the winner as text. The GameScreen will then pass back to the GameWindow to display the relevant buttons back to the user.

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

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