Handling the Player’s Turn
The class definitions are now complete. We can begin to focus on the main game flow. First, we’ll tackle the player’s turn. They should have the ability to hit or stay. If they stay, their turn is over. If they hit, then we need to pull a card from the deck and add it to their hand. After the card is added, we’ll have to check if the player went over 21. If they do, they lose, and we’ll need to keep track of that to determine an output later:
83| dealer.showHand( ) ◽◽◽
85| player_bust = False # variable to keep track of player going over 21 87| while input("Would you like to stay or hit?").lower( ) != "stay":
88| clear_output( )
90| # pull card and put into player's hand
91| player.addCard( game.pullCard( ) )
93| # show both hands using method
94| player.showHand( )
95| dealer.showHand( )
97| # check if over 21 98| if player.calcHand( ) > 21:
99| player_bust = True # player busted, keep track for later
100| print("You lose!") # remove after running correctly
101| break # break out of the player's loop
Go ahead and run the cell. For now, try hitting until you go over 21. This will cause an output of “You lose!”. Nothing happens if you don’t go over 21, as we haven’t handled that yet, but we’ll get there. On line 85, we declare a variable to keep track of the player going over 21. We then begin our while loop by asking the user if they’d like to hit or stay. If they choose anything but stay, then the loop will run. Within the loop, we’ll clear the output, add a card to the player’s hand, show the hand, and then check if they busted. There are two ways for the loop to end, they bust, or they choose to stay.
Handling the Dealer’s Turn
The dealer’s turn will be very similar to that of the player’s, but we won’t need to ask if the dealer would like to hit. The dealer automatically hits while under 17. We’ll need to track if the dealer busts as well though:
100| break # break out of the player's loop ◽◽◽
102| # handling the dealer's turn, only run if player didn't bust
103| dealer_bust = False 105| if not player_bust:
106| while dealer.calcHand(False) < 17: # pass False to calculate all cards
107| # pull card and put into player's hand
108| dealer.addCard( game.pullCard( ) )
110| # check if over 21
111| if dealer.calcHand(False) > 21: # pass False to calculate all cards
112| dealer_bust = True
113| print("You win!") # remove after running correctly
114| break # break out of the dealer's loop
Go ahead and run the cell. Try running the cell until you get the dealer to go over 21, resulting in the print statement running. We begin by declaring a variable on line 103 to track the dealer going bust. On line 105, we check to see if the player already busted, as the round would already be over and the dealer doesn’t need to draw any cards. Line 106 is where our loop begins, which will add a card to the dealer’s hand and check if he busted. The loop will continue until the dealer has more than 16 points, or he goes over 21.
When we call the calcHand method for the dealer this time, we pass the argument of False. This is so that the method will calculate the complete total of the hand and not just the second card, as we’ve been doing previously.
Do'stlaringiz bilan baham: |