Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet124/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   120   121   122   123   124   125   126   127   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Calculating the Hand Total


Now that we’re able to call a method to show each of the player’s hands correctly, we need to calculate the total of the cards within the hand. This method becomes a bit tricky, however, as we need to keep a few checks in mind:

  1. Aces can be worth 11 or 1 point. They are worth 1 point if the total is over 21.

  2. If the dealer is only showing one card, the value of his hand should only represent the value of that one card even though he has two cards in his hand.

  3. All face cards (J, Q, K) are worth 10 points.

There are several ways to handle this method. What we’ll program together is just one of those many ways. When thinking about how to calculate aces, we need to check for their value after we’ve calculated the total of all other cards. We’ll keep track of how many aces we have first and then total them up afterward. To make sure we return the dealer’s total properly, we’ll keep track of whether it’s his turn or not like we did in the showHand method.
Lastly, to calculate the face card values, we’ll create a dictionary of values to pull from:

42| print( "{ } of { }".format( card[0], card[1] ) ) ◽◽◽
43| print( "Total = { }".format( self.calcHand(dealer_start) ) ) 45| # if not dealer's turn then only give back total of second card 46| def calcHand(self, dealer_start = True):
47| total = 0
48| aces = 0 # calculate aces afterwards
49| card_values = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:10, "J":10, "Q":10, "K":10, "A":11} 51| if self.name == "Dealer" and dealer_start:
52| card = self.hand[ 1 ]
53| return card_values[ card[ 0 ] ] 55| for card in self.hand:
56| if card[ 0 ] == "A":
57| aces += 1 58| else:
59| total += card_values[ card[ 0 ] ] 61| for i in range(aces):
62| if total + 11 > 21:
63| total += 1 64| else:
65| total += 11
67| return total
69| game = Blackjack( ) ◽◽◽

Go ahead and run the cell. Starting at line 46, we declare our calcHand method with the parameter dealer_start. We’ll set this parameter to a default of True, so that it defaults to only showing the total of one card for the dealer. Line 47 is where we declare our variable to keep track of the total. Line 48 is where we declare our variable to keep track of how many aces we have in our hand. On line 49, we declare a dictionary of key- value pairs that represent the card’s value. Our conditional statement on line 51 checks to see if the dealer instance is the object calling this method, as well as if the dealer_start parameter is True. If they are both true, then we’ll simply return the value of the second card in the dealer’s hand. It’s the second card because we set the card variable to equal the second item within the hand, which is the second card. Then we reference the card_ values dictionary with the card variables’ item in index 0. This item is going to be one of the keys, and the dictionary will then return the value of that key-value pair. If the item at index 0 is “J”, the dictionary will return a value of 10. The for loop starting on line 55 will loop over each card in the respective player’s hand, reference the dictionary for a card value, and add that card value to the current total. If the card is an ace, it will simply add one to our aces variable and not add anything to the total. The next for loop on line 61 will loop as many times as there are aces in the player’s hand. For each ace, we’ll either add 1 point or 11 points depending on the total. If adding 11 points to the hand makes the total greater than 21, we simply add one point instead. At the end of the method, we return the total. Lastly, line 43 is where we call calcHand within the showHand method. We pass the dealer_start variable in case we’re trying to show the hand during the dealer’s turn. Later, during the dealer’s turn, we’ll pass the argument of False, which will then calculate the total of all the dealer’s cards rather than just one.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   120   121   122   123   124   125   126   127   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish