Flylib.com

Books Software

 
 
 

Python Programming for the Absolute Beginner, 3rd Edition - page 98


Summary

This chapter introduced you to a different way of programming by using the software object. You learned that software objects can combine functions and data ( methods and attributes in OOP-speak) and in many ways mimic real-world objects. You saw how to write classes, the blueprints of objects. You learned about a special method called the constructor that is automatically invoked when a new object is instantiated . You saw how to create and initialize object attributes through a constructor. You learned how to create class-wide elements such as class attributes and static methods. Next, you learned about object encapsulation. You saw ways to help ensure encapsulation, including the use of private attributes. But you learned that, more than anything, good object design is the best way to help ensure encapsulation. Finally, you saw all of these ideas put to work to create a demanding virtual pet that requires constant attention.



Challenges

  1. Improve the Critter Caretaker program by allowing the user to specify how much food he or she feeds the critter and how long he or she plays with the critter. Have these values affect how quickly the critter's hunger and boredom levels drop.

  2. Write a program that simulates a television by creating it as an object. The user should be able to enter a channel number and raise or lower the volume. Make sure that the channel number and volume level stay within valid ranges.

  3. Create a "back door" in the Critter Caretaker program that shows the exact values of the object's attributes. Accomplish this by printing the object when a secret selection, not listed in the menu, is entered as the user's choice. (Hint: add the special method __str__() to the Critter class.)

  4. Create a Critter Farm program by instantiating several Critter objects and keeping track of them through a list. Mimic the Critter Caretaker program, but instead of requiring the user to care for a single critter, require them to care for an entire farm. Each menu choice should allow the user to perform some action for all of the critters (feed all of the critters, play with all of the critters, or listen to all of the critters). To make the program interesting, give each critter random starting hunger and boredom levels.



Chapter 9: Object-Oriented Programming: The Blackjack Game

 Download CD Content

Overview

In the last chapter, you learned about the software object. Almost every program you saw involved a single object. That's a great way to begin to understand how objects work, but the true power of OOP can only be appreciated by seeing a group of objects work together. In this chapter, you'll learn to create multiple objects and define relationships among them so that they can interact. Specifically, you'll learn to do the following:

  • Create objects of different classes in the same program

  • Allow objects to communicate with each other

  • Create more complex objects by combining simpler ones

  • Derive new classes from existing ones

  • Extend the definition of existing classes

  • Override method definitions of existing classes



Introducing the Blackjack Game

The final project for this chapter is a simplified version of the card game, black-jack. The game works like this: Players are dealt cards with point values. Each player tries to reach a total of 21 without going over. Numbered cards count as their face value. An ace counts as either 1 or 11 (whichever is best for the player) and any jack, queen , or king counts as 10.

The computer is the dealer and competes against one to seven players. At the opening of the round, the computer deals all participants (including itself) two cards. Players can see all of their cards, and the computer even displays their total. However, one of the dealer's cards is hidden for the time being.

Next, each player gets a chance to take additional cards. Each player can take one card at a time for as long as the player likes. But if the player's total goes over 21 (known as "busting"), the player loses. If all players bust, the computer reveals its first card and the round is over. Otherwise, play continues. The computer must take additional cards as long as its total is less than 17. If the computer busts, all players who have not themselves busted, win. Otherwise, each remaining player's total is compared with the computer's. If the player's total is greater, the player wins. If the player's total is less, the player loses. If the two totals are the same, the player ties the computer (also known as "pushing"). Figure 9.1 shows off the game.

click to expand
Figure 9.1: One player wins, the other is not so lucky.