Skip to main content Accessibility help
×
Hostname: page-component-77c89778f8-7drxs Total loading time: 0 Render date: 2024-07-22T05:17:50.861Z Has data issue: false hasContentIssue false

18 - Inheritance

Published online by Cambridge University Press:  05 June 2012

Allen B. Downey
Affiliation:
Olin College of Engineering, Massachusetts
Get access

Summary

In this chapter we will develop classes to represent playing cards, decks of cards, and poker hands. If you don't play poker, you can read about it at wikipedia.org/wiki/Poker, but you don't have to; I'll tell you what you need to know for the exercises.

If you are not familiar with Anglo-American playing cards, you can read about them at wikipedia.org/wiki/Playing_cards.

CARD OBJECTS

There are fifty-two cards in a deck, each of which belongs to one of four suits and one of thirteen ranks. The suits are Spades, Hearts, Diamonds, and Clubs (in descending order in bridge). The ranks are Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, and King. Depending on the game that you are playing, an Ace may be higher than King or lower than 2.

If we want to define a new object to represent a playing card, it is obvious what the attributes should be: rank and suit. It is not as obvious what type the attributes should be. One possibility is to use strings containing words like ‘Spade’ for suits and ‘Queen’ for ranks. One problem with this implementation is that it would not be easy to compare cards to see which had a higher rank or suit.

An alternative is to use integers to encode the ranks and suits. In this context, “encode” means that we are going to define a mapping between numbers and suits, or between numbers and ranks.

Type
Chapter
Information
Python for Software Design
How to Think Like a Computer Scientist
, pp. 201 - 213
Publisher: Cambridge University Press
Print publication year: 2009

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • Inheritance
  • Allen B. Downey, Olin College of Engineering, Massachusetts
  • Book: Python for Software Design
  • Online publication: 05 June 2012
  • Chapter DOI: https://doi.org/10.1017/CBO9780511813832.020
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • Inheritance
  • Allen B. Downey, Olin College of Engineering, Massachusetts
  • Book: Python for Software Design
  • Online publication: 05 June 2012
  • Chapter DOI: https://doi.org/10.1017/CBO9780511813832.020
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • Inheritance
  • Allen B. Downey, Olin College of Engineering, Massachusetts
  • Book: Python for Software Design
  • Online publication: 05 June 2012
  • Chapter DOI: https://doi.org/10.1017/CBO9780511813832.020
Available formats
×