SlideShare a Scribd company logo
1 of 15
Download to read offline
Hangman with Inheritance
Hangman Code
0 1 2 3 4 5 6 19 20 21 22 23 24 25
‘a’‘b’‘c’‘d’‘e’‘f’‘g’ ‘t’‘u’‘v’ ‘w’‘x’‘y’‘z’
true or false
letterGuessed[]
n  The letterGuessed[] array is used to keep track of which letters
have been guessed so far.
n  We only consider the 26 lowercase letters of the alphabet. All
other characters are considered invalid – i.e. not part of the
game.
n  So each element of letterGuessed[] contains true or false,
depending on whether the corresponding character has been
guessed by the user or not.
Hangman Code
0 1 2 3 4 5 6 19 20 21 22 23 24 25
‘a’‘b’‘c’‘d’‘e’‘f’‘g’ ‘t’‘u’‘v’ ‘w’‘x’‘y’‘z’
true or false
letterGuessed[]
n  There is a correspondence between a letter and a position in the
array.
0 -> ‘a’
1 -> ‘b’
25 -> ‘z’
Hangman Code
0 1 2 3 4 5 6 19 20 21 22 23 24 25
‘a’‘b’‘c’‘d’‘e’‘f’‘g’ ‘t’‘u’‘v’ ‘w’‘x’‘y’‘z’
true or false
letterGuessed[]
n  There is an easy way to work out the position, given the
character:
given a lowercase character c, its position in letterGuessed[] is
c – ‘a’
so, e.g. if c = ‘e’, then its position is ‘e’ – ‘a’ = 4
Hangman Code
n  Now suppose I wanted to play the Hangman game, but I wanted
my opponent to guess my computer password, which is allowed
to contain any sequence of lowercase or uppercase characters or
any number ‘0’, ‘1’, .., ‘9’
n  The only change I need to make to my Hangman game is to
extend the range of valid characters.
0 1 24 25
‘a’‘b’ ‘y’‘z’
true or false
letterGuessed[]
‘A’‘B’ ‘Y’‘Z’‘0’‘1’ ‘8’‘9’
26 27 50 51 52 53 54 55
n  Now we have a more complicated mapping of character to a
position in the letterGuessed[] array.
n  But, otherwise my Hangman game for passwords is the same as
the simple Hangman game.
Hangman Inheritance
n  Inheritance allows us to re-use code for the two
versions of the game.
n  Create two sub-classes of an abstract class called
Hangman. Hangman
SimpleHangman PassHangman
n  SimpleHangman and PassHangman only differ in the
set of valid characters that they allow
Prepare Hangman code for Extension
n  Unfortunately, I was not careful to abstract the
operation of mapping a character to a particular
position in the letterGuessed[] array.
n  This mapping is carried out explicitly at several points
of the existing code.
n  Here, I explicitly chose to store the secret word as lowercase
n  When I checked if a letter is valid, I checked that the
range of characters only included alphabetic
characters
n  In order to add a new guess into the array, I modified
the character to make it lowercase and find its
position in the array.
n  When checking if a letter was used, I again explicitly
computed the mapping from character to position in
the lettersGuessed[] array.
n  A good abstraction would have identified a function
that given a character, maps it to its position in the
array, and returns that position (or -1 if the character
is invalid)
n  That function could then be used whenever we need
to consult the letterGuessed[] array.
Hangman with Inheritance
n  Now the two classes can be identical except for the
way that they implement the function
letterToStoragePosition()
n  Changes to the code:
n  Change the private fields of Hangman to protected
n  Introduce a new method called letterToStoragePosition
which can also have protected access (since external
code does not directly access it).
n  Specific that this function must be implemented by
derived classes:
virtual int letterToStoragePosition(char c) const = 0;
n  Create two derived classes and implement the function
for each class.
n  Note that each function must have its own constructor
too.

More Related Content

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Hangman with Inheritance

  • 2. Hangman Code 0 1 2 3 4 5 6 19 20 21 22 23 24 25 ‘a’‘b’‘c’‘d’‘e’‘f’‘g’ ‘t’‘u’‘v’ ‘w’‘x’‘y’‘z’ true or false letterGuessed[] n  The letterGuessed[] array is used to keep track of which letters have been guessed so far. n  We only consider the 26 lowercase letters of the alphabet. All other characters are considered invalid – i.e. not part of the game. n  So each element of letterGuessed[] contains true or false, depending on whether the corresponding character has been guessed by the user or not.
  • 3. Hangman Code 0 1 2 3 4 5 6 19 20 21 22 23 24 25 ‘a’‘b’‘c’‘d’‘e’‘f’‘g’ ‘t’‘u’‘v’ ‘w’‘x’‘y’‘z’ true or false letterGuessed[] n  There is a correspondence between a letter and a position in the array. 0 -> ‘a’ 1 -> ‘b’ 25 -> ‘z’
  • 4. Hangman Code 0 1 2 3 4 5 6 19 20 21 22 23 24 25 ‘a’‘b’‘c’‘d’‘e’‘f’‘g’ ‘t’‘u’‘v’ ‘w’‘x’‘y’‘z’ true or false letterGuessed[] n  There is an easy way to work out the position, given the character: given a lowercase character c, its position in letterGuessed[] is c – ‘a’ so, e.g. if c = ‘e’, then its position is ‘e’ – ‘a’ = 4
  • 5. Hangman Code n  Now suppose I wanted to play the Hangman game, but I wanted my opponent to guess my computer password, which is allowed to contain any sequence of lowercase or uppercase characters or any number ‘0’, ‘1’, .., ‘9’ n  The only change I need to make to my Hangman game is to extend the range of valid characters. 0 1 24 25 ‘a’‘b’ ‘y’‘z’ true or false letterGuessed[] ‘A’‘B’ ‘Y’‘Z’‘0’‘1’ ‘8’‘9’ 26 27 50 51 52 53 54 55 n  Now we have a more complicated mapping of character to a position in the letterGuessed[] array. n  But, otherwise my Hangman game for passwords is the same as the simple Hangman game.
  • 6. Hangman Inheritance n  Inheritance allows us to re-use code for the two versions of the game. n  Create two sub-classes of an abstract class called Hangman. Hangman SimpleHangman PassHangman n  SimpleHangman and PassHangman only differ in the set of valid characters that they allow
  • 7. Prepare Hangman code for Extension n  Unfortunately, I was not careful to abstract the operation of mapping a character to a particular position in the letterGuessed[] array. n  This mapping is carried out explicitly at several points of the existing code.
  • 8. n  Here, I explicitly chose to store the secret word as lowercase
  • 9. n  When I checked if a letter is valid, I checked that the range of characters only included alphabetic characters
  • 10. n  In order to add a new guess into the array, I modified the character to make it lowercase and find its position in the array.
  • 11. n  When checking if a letter was used, I again explicitly computed the mapping from character to position in the lettersGuessed[] array.
  • 12. n  A good abstraction would have identified a function that given a character, maps it to its position in the array, and returns that position (or -1 if the character is invalid) n  That function could then be used whenever we need to consult the letterGuessed[] array.
  • 13.
  • 14.
  • 15. Hangman with Inheritance n  Now the two classes can be identical except for the way that they implement the function letterToStoragePosition() n  Changes to the code: n  Change the private fields of Hangman to protected n  Introduce a new method called letterToStoragePosition which can also have protected access (since external code does not directly access it). n  Specific that this function must be implemented by derived classes: virtual int letterToStoragePosition(char c) const = 0; n  Create two derived classes and implement the function for each class. n  Note that each function must have its own constructor too.