Assignment #6: WordGuess Exercise Summary: Create the WordGuess program. The sol
ID: 3849749 • Letter: A
Question
Assignment #6: WordGuess Exercise
Summary: Create the WordGuess program.
The solution file should be named WordGuess_ExerciseE.rb.
Use the instructions on page 254 of the textbook.
Run your program until it works and the output looks nice.
254 Working with Regular Expressions CHAPTER 7 Reinforcement Exercises following exercises are designed to further your understanding of Ruby programming by challenging you to make improvements to the chapters game project, the Word Guessing game. 1. The game currently draws on a pool of 20 words when selecting the secret word. Because of such a small pool, the me word may be used more than once in a single session of play. Make the game more fun by doubling the number of words that are available 2. The text contained in the display instructions method, which instructs players in how to play the game, is a bit dry and cryptic. Revise this text to provide the players with better direc tions. Make sure you include information about the minimum and maximum length of the game words (five to 10 characters) and make sure you explain that underscore characters are used to represent the letters that have not been guessed 3. The 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 instruc- tions as a hidden cheat, accessible when the h key is pressed. Make it so the existence of this feature isrevealed when the player is first prompted to play the game, as well as when the player is prompted to continue playing after a round of play. 4. The game currently prevents the player from guessing the letter twice, acting on the assumption that the same feedback, remembers any previous guesses. To provide better modify the game so that it displays a list of all the wrong guesses each time the player enters a guess that's already been 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 gues the game displays the secret word in all the secret word, uppercase characters. Modify the game so that only the first etter 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 first character of the game word to uppercaseExplanation / Answer
#require SecureRandom
words = %w"hello everybody sunday tuesday friday cactus choclate biscuit company software malware virus buffalo college university country india america speaker smartphone"
game_chances = 5
wrong_guess = 0
right_answer_guesses = ''
hanged_user = <<HANG
+---+-
| |
| 0
| |\
| /\
-+----------
HANG
survived_user = <<WIN
(@)
^\|
|/^
____|_____
WIN
#random picking..
words.gsub(/w+/, &:capitalize)
secretWord = words[rand(words.length) - 1]
def get_placeholder userSampleWord, userGuessedWords
place_holder = ''
userSampleWord.chars { |char|
place_holder += (userGuessedWords.include? char)? char : '#'
}
place_holder
end
puts `clear`
puts 'Guess word.... :'+ get_placeholder(secretWord, '')
while true
print "Enter you secretWord [#{game_chances - wrong_guess} chances left]:"
char = gets.chomp
puts `clear`
if secretWord.include? char
if(right_answer_guesses.include? char)
puts char + ' is already processed...'
puts 'Give another: ' + get_placeholder(secretWord, right_answer_guesses)
else
right_answer_guesses = right_answer_guesses + char
place_holder = get_placeholder(secretWord, right_answer_guesses)
puts 'Super!!! ' + place_holder
end
unless place_holder.include? '#'
puts "GOOD JOB!! YOU SURVIVED"
puts survived_user
break
end
else
puts "Sorry yaar! The secretWord does not have '#{char}'"
wrong_guess += 1
if (wrong_guess == game_chances)
puts "YOU ARE HANGED!"
puts hanged_user
break
else
puts 'Try another one: ' + get_placeholder(secretWord, right_answer_guesses)
end
end
end