Calculating a Winner
The final piece of this game is to calculate who the winner is. Thus far, we’ve put a couple checks in place to see if either the player has already lost by going over 21. We’ll first check to see if the player busted, then the dealer. If neither player busts, then we’ll need to see who has the higher point total. If they tie, then it’s known as a push, and no one wins:
113| break # break out of the dealer's loop ◽◽◽
115| clear_output( )
117| # show both hands using method
118| player.showHand( )
119| dealer.showHand(False) # pass False to calculate and show all cards, even when there are 2
121| # calculate a winner 122| if player_bust:
123| print("You busted, better luck next time!") 124| elif dealer_bust:
125| print("The dealer busted, you win!") 126| elif dealer.calcHand(False) > player.calcHand( ):
127| print("Dealer has higher cards, you lose!") 128| elif dealer.calcHand(False) < player.calcHand( ):
129| print("You beat the dealer! Congrats!") 130| else:
131| print("You pushed, no one wins!")
Go ahead and run the cell. We now have a fully functioning game of Blackjack! To start, we clear the output and show both player’s hands. The main difference, though, is on line 119. We pass the argument False into the showHand method for the dealer. This is so that all the dealer’s cards show, along with the complete total. Remember that we were calling the calcHand method within showHand and passing the value of dealer_ start, which we set to False with this method call. After that we set up a few conditions which will output the proper result based on the given condition.
Final Output
Congratulations on completing this project! Due to the size of the project, you may find the completed version of the code on Github. To find the specific code for this project, simply open or download the “Week_07.ipynb” file. If you ran into errors along the way, be sure to cross-reference your code with the code in this file and see where you may have gone wrong.
even though today’s project was long, we were able to see some great examples of object-oriented programming. Using classes gives us the ability to reuse several lines of code like we did for the player and dealer objects. this program could certainly be refactored to have more methods within the blackjack class; however, i wanted you to be able to read the code a little easier. For this reason, i kept the classes shorter and the main game functionality separate. be sure to test the game and add your own features to it if you’d like.
Weekly Summary
Throughout this week, we covered the concepts of object-oriented programming and why they are important in the programming world. In Python, we know them as classes. They allow us to reuse code and create multiple instances from one object. When storing variables or creating functions inside of classes, they’re known as attributes and methods. We’re able to reference these using dot syntax and the self parameter. Without classes, we would need to hard-code every line for all objects within our programs. This becomes especially apparent within larger-scale programs. To increase the reusability of the code, we’re able to use inheritance. This allows subclasses to inherit attributes and methods from superclasses, much like that of a parent and their child. At the end of this week, we were able to create an object-oriented game of Blackjack. This showcased the capabilities of OOP, as we were able to create multiple instances of the player object. Going forward, be sure to think of the world around you as objects. It will help you adjust to the world of OOP and understanding what an objects’ attributes and methods are.
Do'stlaringiz bilan baham: |