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


Adding Cards to the Player’s Hand



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

Adding Cards to the Player’s Hand


Once we have the player objects being instantiated properly, we can begin to work on the methods needed for the Player class. When looking at which method to program first, you always need to think about what methods rely on other methods. For this class, the calcHand and showHand methods rely on having cards in the hand. For this reason, we’ll work on the addCard method and then focus on the other two:

26| self.hand = [ ] ◽◽◽
28| # take in a tuple and append it to the hand 29| def addCard(self, card):
30| self.hand.append(card)
32| game = Blackjack( ) ◽◽◽
37| dealer = Player("Dealer") ◽◽◽
39| # add two cards to the dealer and player hand 40| for i in range(2):
41| player.addCard( game.pullCard( ) )
42| dealer.addCard( game.pullCard( ) )
44| print( "Player Hand: { } \nDealer Hand: { }".format(player.hand, dealer.hand) ) # remove after

Go ahead and run the cell. We’ll get an output of two random cards within each of the player’s hands. The addCard method simply takes in a tuple that represents a card and appends it to the player’s hand. On the 40th line, we begin a for loop that will add two cards to each hand. It does this by pulling a card using the game instance method pullCard. That method returns a tuple, and that tuple is then passed into the addCard method, which is then appended to the respective player’s hand. This loop will suffice as the start of the game in which all players begin with two cards in their hand. Be sure to remove the last line, as it’s used for debugging.

Showing a Player’s Hand


In the previous section, we were printing out the full hand of each player. However, in actual Blackjack, you only show the second card dealt to the dealer. It’s also bad practice to reference the attribute directly, so we’ll need to create the showHand method to take care of both these problems. We’ll use nicely formatted print statements to show the hands, but more importantly, we’ll make sure that if it is still the player’s turn, then you can only see one of the dealer’s cards:

30| self.hand.append(card) ◽◽◽
32| # if not dealer's turn then only show one of his cards, otherwise show all 33| def showHand(self, dealer_start = True):
34| print( "\n{ }".format(self.name) )
35| print("===========") 37| for i in range( len(self.hand) ):
38| if self.name == "Dealer" and i == 0 and dealer_start:
39| print("- of –") # hide first card 40| else:
41| card = self.hand[ i ]
42| print( "{ } of { }".format( card[0], card[1] ) )
44| game = Blackjack( ) ◽◽◽
54| dealer.addCard( game.pullCard( ) ) ◽◽◽
56| # show both hands using method
57| player.showHand( )
58| dealer.showHand( )

Go ahead and run the cell. The output results in the player’s hand showing both cards, while the dealer only shows one. Let’s walk through this step by step. On line 33 we declare the showHand method with the dealer_start parameter. This parameter will be a boolean value which tracks whether we hide the first card the dealer is dealt. We set the default value to True so that the only time we need to pass an argument of False into the method is at the end when we want to show the dealer’s cards. The for loop on line 37 allows us to print out each card in the player object’s hand. Line 38 is where we check two things:

  1. The instance that called this method was the dealer.

  2. It’s not the dealer’s turn yet (dealer_start == True).

If both are true, then we hide the first card; otherwise, we’ll show all the cards for both the player and the dealer. The card variable is declared for ease of use when reading the code, as we set it to one of the items within our hand, which represents a card. We then print a formatted statement with the tuple’s values. This is done by accessing the 0 and 1 index of the tuples that represent each card. At the bottom of the cell, we call these methods for each player object.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   119   120   121   122   123   124   125   126   ...   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