Question: 3. Th e game currently displays the instructions once, at the beginning. Modify the game to remove the automatic display of its instructions and instead
3. Th e game currently displays the instructions once, at the beginning. Modify the game to remove the automatic display of its instructions and instead allow players to display the instructions as a hidden cheat, accessible when the h key is pressed. Make it so the existence of this feature is revealed when the player is rst prompted to play the game, as well as when the player is prompted to continue playing after a round of play.
4. Th e game currently prevents the player from guessing the same letter twice, acting on the assumption that the player remembers any previous guesses. To provide better feedback, modify the game so that it displays a list of all the wrong guesses each time the player enters a guess thats already been tried. Hint: Use the inspect method to display the contents of the list array. Also, use a regular expression to remove the double quotation marks that are included when array contents are displayed.
5. Whenever play ends without the player having guessed the secret word, the game displays the secret word in all uppercase characters. Modify the game so that only the rst letter of the word is uppercase. You could implement this change by retyping each of the game words stored in the list array, of course. Instead, convert all the words to lowercase, and then, using a regular expression, change the rst character of the game word to uppercase.
//The Code
The Words are added
class Screen
def cls
puts (" " * 10)
puts "\a"
end
def pause
STDIN.gets
end
end
class Game
def display_greeting
Console_Screen.cls
print "\t\t\tWelcome to the Word Guessing Game!" +
" Press Enter to " +
"continue."
Console_Screen.pause
end
def display_instructions
Console_Screen.cls
puts "INSTRUCTIONS: "
puts "At the start of each round of play, the game randomly selects"
puts "a word that is between five and ten characters long and"
puts "challenges you to guess it. Before submitting your guess, you"
puts "must specify five consonants and one vowel. If any of these"
puts "are used in the word, they will be revealed on the screen,"
puts "thereby making it easier for you to guess the secret word. "
puts "Good luck! "
print "Press Enter to continue."
Console_Screen.pause
end
def select_word
words = ["W I N D O W", "S T A T I O N", "H A M B U R G E R",
"E X P R E S S I O N", "W A L L E T", "C A M E R A",
"A I R P L A N E", "C A N D L E", "C O M P U T E R",
"P I C T U R E", "F R A M E", "S H E L F", "B O W L I N G",
"P O L I T E", "S T A T E M E N T", "N E G A T I V E",
"M E T H O D", "F I S H I N G", "C O M P E N S A T E",
"H A P P Y","G O O D","S A D","S E L F","Y O U","C O M E","T O G E T H E R","L O V E","H U M A N","M A N","W O M A N","B O Y","G I R L","L I K E","A N N O U N C E M E N T","C U P","C O F F E E","C H A I R","S H I R T","T E L E V I S S I O N","T A B L E","S H E E T","B R O T H E R","L O G I N","L E A R N","O N L I N E"]
randomNo = rand(44)
return words[randomNo]
end
def get_consonants
list = Array.new
puts "Before you try to guess the secret word, you must specify " +
"5 consonants. "
print "Press Enter to continue."
Console_Screen.pause
5.times do
Console_Screen.cls
print " Please enter a consonant and press Enter. "
input = STDIN.gets
input.chop!
if input !~ /[bcdfghjklmnpqrstvwxyz]/i then
Console_Screen.cls
print "Error: " + input + " is not a consonant. Press Enter to " +
"continue."
Console_Screen.pause
redo
end
if input.length > 1 then
Console_Screen.cls
print "Error: You may only enter one character at a time. Press " +
"Enter to continue."
Console_Screen.pause
redo
end
if list.include?(input.upcase) == true then
Console_Screen.cls
print "Error: You have already guessed " + input + ". Press " +
"Enter to continue."
Console_Screen.pause
redo
else
list.push(input.upcase)
end
end
return list
end
def get_vowel
puts "Before you try to guess the secret word, you must specify " +
"1 vowel. "
1.times do
Console_Screen.cls
print " Please enter a vowel and press Enter. "
input = STDIN.gets
input.chop!
if input !~ /[aeiou]/i then
Console_Screen.cls
print "Error: " + input + " is not a vowel. Press Enter to " +
"continue."
Console_Screen.pause
redo
end
if input.length > 1 then
Console_Screen.cls
print "Error: You may only enter one character at a time. Press " +
"Enter to continue."
Console_Screen.pause
redo
end
input = input.upcase
return input
end
end
def prompt_for_guess(shortWord, word, consonants, vowel)
Console_Screen.cls
consonants.push(vowel)
wordArray = word.split(" ")
i = 0
wordArray.each do |letter|
match = false
consonants.each do |character|
if character == letter then
match = true
break
end
end
if match == false then
wordArray[i] = "_"
end
i = i + 1
end
word = wordArray.join(" ")
3.times do |i|
Console_Screen.cls
puts "I am thinking of a word. "
print "Here is your clue: " + word + " "
print "What do you think this word is? "
reply = STDIN.gets
reply.chop!
reply = reply.upcase
if reply == shortWord then
Console_Screen.cls
print "Correct! Press Enter to continue."
Console_Screen.pause
break
else
Console_Screen.cls
if i == 1 then
print "Wrong! You have one guess left. Press Enter to " +
"try again."
elsif i == 2
print "Sorry, you lose. "
print "The word was " + shortWord + ". Press Enter to continue."
else
print "Wrong! Press Enter to try again."
end
Console_Screen.pause
end
end
end
def play_game
word = select_word
Console_Screen.cls
consonants = get_consonants
Console_Screen.cls
vowel = get_vowel
shortWord = word.gsub(" ", "")
prompt_for_guess(shortWord, word, consonants, vowel)
Console_Screen.cls
end
def display_credits
Console_Screen.cls
puts "\t\t Thank you for playing the Word Guessing Game. "
puts " \t\t\t Developed by Jerry Lee Ford, Jr. "
puts "\t\t\t\t Copyright 2010 "
puts "\t\t\tURL: http://www.tech-publishing.com "
end
end
Console_Screen = Screen.new
WordGuess = Game.new
WordGuess.display_greeting
answer = ""
loop do
Console_Screen.cls
print "Are you ready to play the Word Guessing Game? (y/n): "
answer = STDIN.gets
answer.chop!
break if answer =~ /y|n/i
end
if answer == "n" or answer == "N"
Console_Screen.cls
puts "Okay, perhaps another time. "
else
WordGuess.display_instructions
loop do
WordGuess.play_game
print "Enter Q to quit or press any key to play again: "
playAgain = STDIN.gets
playAgain.chop!
break if playAgain =~ /Q/i
end
WordGuess.display_credits
end
//The Code
The Words are added
class Screen
def cls
puts (" " * 10)
puts "\a"
end
def pause
STDIN.gets
end
end
class Game
def display_greeting
Console_Screen.cls
print "\t\t\tWelcome to the Word Guessing Game!" +
" Press Enter to " +
"continue."
Console_Screen.pause
end
def display_instructions
Console_Screen.cls
puts "INSTRUCTIONS: "
puts "At the start of each round of play, the game randomly selects"
puts "a word that is between five and ten characters long and"
puts "challenges you to guess it. Before submitting your guess, you"
puts "must specify five consonants and one vowel. If any of these"
puts "are used in the word, they will be revealed on the screen,"
puts "thereby making it easier for you to guess the secret word. "
puts "Good luck! "
print "Press Enter to continue."
Console_Screen.pause
end
def select_word
words = ["W I N D O W", "S T A T I O N", "H A M B U R G E R",
"E X P R E S S I O N", "W A L L E T", "C A M E R A",
"A I R P L A N E", "C A N D L E", "C O M P U T E R",
"P I C T U R E", "F R A M E", "S H E L F", "B O W L I N G",
"P O L I T E", "S T A T E M E N T", "N E G A T I V E",
"M E T H O D", "F I S H I N G", "C O M P E N S A T E",
"H A P P Y","G O O D","S A D","S E L F","Y O U","C O M E","T O G E T H E R","L O V E","H U M A N","M A N","W O M A N","B O Y","G I R L","L I K E","A N N O U N C E M E N T","C U P","C O F F E E","C H A I R","S H I R T","T E L E V I S S I O N","T A B L E","S H E E T","B R O T H E R","L O G I N","L E A R N","O N L I N E"]
randomNo = rand(44)
return words[randomNo]
end
def get_consonants
list = Array.new
puts "Before you try to guess the secret word, you must specify " +
"5 consonants. "
print "Press Enter to continue."
Console_Screen.pause
5.times do
Console_Screen.cls
print " Please enter a consonant and press Enter. "
input = STDIN.gets
input.chop!
if input !~ /[bcdfghjklmnpqrstvwxyz]/i then
Console_Screen.cls
print "Error: " + input + " is not a consonant. Press Enter to " +
"continue."
Console_Screen.pause
redo
end
if input.length > 1 then
Console_Screen.cls
print "Error: You may only enter one character at a time. Press " +
"Enter to continue."
Console_Screen.pause
redo
end
if list.include?(input.upcase) == true then
Console_Screen.cls
print "Error: You have already guessed " + input + ". Press " +
"Enter to continue."
Console_Screen.pause
redo
else
list.push(input.upcase)
end
end
return list
end
def get_vowel
puts "Before you try to guess the secret word, you must specify " +
"1 vowel. "
1.times do
Console_Screen.cls
print " Please enter a vowel and press Enter. "
input = STDIN.gets
input.chop!
if input !~ /[aeiou]/i then
Console_Screen.cls
print "Error: " + input + " is not a vowel. Press Enter to " +
"continue."
Console_Screen.pause
redo
end
if input.length > 1 then
Console_Screen.cls
print "Error: You may only enter one character at a time. Press " +
"Enter to continue."
Console_Screen.pause
redo
end
input = input.upcase
return input
end
end
def prompt_for_guess(shortWord, word, consonants, vowel)
Console_Screen.cls
consonants.push(vowel)
wordArray = word.split(" ")
i = 0
wordArray.each do |letter|
match = false
consonants.each do |character|
if character == letter then
match = true
break
end
end
if match == false then
wordArray[i] = "_"
end
i = i + 1
end
word = wordArray.join(" ")
3.times do |i|
Console_Screen.cls
puts "I am thinking of a word. "
print "Here is your clue: " + word + " "
print "What do you think this word is? "
reply = STDIN.gets
reply.chop!
reply = reply.upcase
if reply == shortWord then
Console_Screen.cls
print "Correct! Press Enter to continue."
Console_Screen.pause
break
else
Console_Screen.cls
if i == 1 then
print "Wrong! You have one guess left. Press Enter to " +
"try again."
elsif i == 2
print "Sorry, you lose. "
print "The word was " + shortWord + ". Press Enter to continue."
else
print "Wrong! Press Enter to try again."
end
Console_Screen.pause
end
end
end
def play_game
word = select_word
Console_Screen.cls
consonants = get_consonants
Console_Screen.cls
vowel = get_vowel
shortWord = word.gsub(" ", "")
prompt_for_guess(shortWord, word, consonants, vowel)
Console_Screen.cls
end
def display_credits
Console_Screen.cls
puts "\t\t Thank you for playing the Word Guessing Game. "
puts " \t\t\t Developed by Jerry Lee Ford, Jr. "
puts "\t\t\t\t Copyright 2010 "
puts "\t\t\tURL: http://www.tech-publishing.com "
end
end
Console_Screen = Screen.new
WordGuess = Game.new
WordGuess.display_greeting
answer = ""
loop do
Console_Screen.cls
print "Are you ready to play the Word Guessing Game? (y/n): "
answer = STDIN.gets
answer.chop!
break if answer =~ /y|n/i
end
if answer == "n" or answer == "N"
Console_Screen.cls
puts "Okay, perhaps another time. "
else
WordGuess.display_instructions
loop do
WordGuess.play_game
print "Enter Q to quit or press any key to play again: "
playAgain = STDIN.gets
playAgain.chop!
break if playAgain =~ /Q/i
end
WordGuess.display_credits
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
