-
-
Save jmscholen/9906725 to your computer and use it in GitHub Desktop.
#Guessing Game for user to guess the items in the array by Jeff Scholen with user feedback. Iron Yard Rails Engineering 3/31/2014 | |
@n = 0 | |
@atv_values = ["Be Nice.", "Dream Big.", "Pay It Forward.", "Work Hard. Play Hard."] | |
@atv_guess = ["Be ----.", "----- Big.", "Pay It -------.", "Work-------Play Hard."] | |
@atv_hints = ["---- guys finish last.", "I have a -----.", "Kevin Spacey, Haley Joel Osment Movie", | |
"A serious and fun work culture motto." ] | |
@atv_missing_values = ["nice", "dream", "forward", "hard"] | |
def match_answer(user_input) | |
case user_input | |
when "phrase" | |
puts "#{@atv_guess}" | |
when "nice" | |
@atv_guess[0]= ("Be Nice.") | |
puts "#{@atv_guess}" | |
@atv_hints.delete("---- guys finish last.") | |
@atv_missing_values.delete("nice") | |
when "dream" | |
@atv_guess[1]=("Dream Big.") | |
puts "#{@atv_guess}" | |
@atv_hints.delete("I have a -----.") | |
@atv_missing_values.delete("dream") | |
when "forward" | |
@atv_guess[2]=("Pay It Forward.") | |
puts "#{@atv_guess}" | |
@atv_hints.delete("Kevin Spacey, Haley Joel Osment Movie") | |
@atv_missing_values.delete("forward") | |
when "hard" | |
@atv_guess[3]=("Work Hard. Play Hard.") | |
puts "#{@atv_guess}" | |
@atv_hints.delete("A serious and fun work culture motto.") | |
@atv_missing_values.delete("hard") | |
when "hint" | |
give_a_hint(user_input) | |
else | |
puts "I think you need a hint." | |
puts "Please type in hint:" | |
answer = gets.chomp.downcase | |
if @answer == "hint" | |
give_a_hint(@answer) | |
else | |
puts "I REALLY think you need a hint." | |
puts "Please type in hint:(Last Chance!)" | |
@answer = gets.chomp.downcase | |
if @answer == "hint" | |
give_a_hint(@answer) | |
else | |
puts "Bye-Bye." | |
exit | |
end | |
end | |
end | |
end | |
def give_a_hint(answer) | |
# @n = 0 if @n > 3 | |
current_value_of_missing_value = @atv_missing_values[0] | |
while @answer != current_value_of_missing_value | |
puts "#{@atv_hints[0]}" | |
puts "Guess?" | |
#puts "#{@atv_missing_values[0]}" for debugging | |
@answer = gets.chomp.downcase | |
if @answer != current_value_of_missing_value | |
puts "Try again!" | |
length_of_answer = @answer.length | |
missing_value = current_value_of_missing_value | |
length_of_missing_value = missing_value.length | |
if length_of_answer == length_of_missing_value | |
puts "You have the right number of letters!" | |
else | |
puts "You dont have the right number of letters!" | |
end | |
end | |
end | |
match_answer(@answer) | |
# @n += 1 | |
end | |
puts "It's time to start the Phrase GUESSING GAME!" | |
sleep 1 | |
puts "Go ahead and guess a word to see if it works in the phrase." | |
while @atv_guess != @atv_values | |
print "Word?, type 'hint' if you want a hint, type 'phrase' to see where you are:" | |
@answer = gets.chomp.downcase | |
match_answer(@answer) | |
end | |
puts "You guessed the WHOLE phrase correctly!" | |
The key to refactoring this to DRY it up is looking at these lines:
if answer == "hint"
n = 0 if n > 3
next_answer = " "
while next_answer != "#{atv_missing_values[n]}"
puts "#{atv_hints[n]}"
puts "Guess?"
next_answer = gets.chomp.downcase
Here you're just getting a new user answer, so instead of creating a new variable next_answer, reassign the original 'answer' variable. I'll take another look at this later today to suggest a way to refactor with this in mind :)
Hey Jeff -
With your final version, when I try to run the game in my terminal, nothing happens. There's no point where you actually use the methods you've defined. Now that you have these methods defined, you need to incorporate the method names into a loop so I can actually play the game, like so:
until # end goal
match_answer
end
Also, there's a lot of stuff happening in match_answer that could be broken into smaller, cleaner methods. For example, instead of repeatedly replacing the atv_guess array item explicitly for each case, extract that into a method. Also, deleting the items from your two arrays can be extracted out. Take the following example:
def replace_guess_with_word(num)
@atv_guess[num] = @atv_values[num]
end
def remove_guessed_items(num)
@atv_hints.delete_at(num)
@atv_missing_values.delete_at(num)
end
case user_input
when "nice"
replace_guess_with_word(0)
puts "#{@atv_guess}"
removed_guessed_items(0)
when ...
WIth num in both cases being the position of the element you're replacing or removing.
I left off the good stuff! Sorry about that. I'll look at refactoring it with regard to your comments.
You got feedback in class :)
+1