-
-
Save RyanSnodgrass/9895876 to your computer and use it in GitHub Desktop.
| answer = ["The Iron Yard", "Atlanta Tech Village", "Marietta", "Survivor Bar", "Buckhead"] | |
| puts "Let's play a game" | |
| play_again = "yes" | |
| def parse_letters_and_words | |
| @guess_letters = @guess.split("") | |
| @matched_letters = @current_answer_array & @guess_letters | |
| @guess_words = @guess.split(/ /) | |
| @current_answer_words = @current_answer.downcase.split(/ /) | |
| @matching_words = @guess_words & @current_answer_words | |
| end | |
| while play_again.downcase == "yes" | |
| @current_answer = answer.sample | |
| @current_answer_array = @current_answer.downcase.split("") | |
| puts "Guess a local Atlanta location" | |
| puts "The locale has #{@current_answer.size} characters including spaces" | |
| puts "The answer is #{@current_answer.split.size} words" | |
| puts "What do you think the locale is?" | |
| @guess = gets.chomp.downcase | |
| while @current_answer.downcase != @guess | |
| parse_letters_and_words | |
| puts "Sorry, that was wrong." | |
| puts "The good news is you got #{@matched_letters} letters right" | |
| puts "you guessed these #{@guess_words} words. \nLet's see if they match" | |
| if @matching_words[0] == nil | |
| puts "Bummer, none of your words matched" | |
| else | |
| puts "Almost: you matched #{@matching_words} but- not whole thing" | |
| end | |
| puts "\nPlease try again." | |
| @guess = gets.chomp.downcase | |
| end | |
| puts "You guessed right! \nYou guessed #{@current_answer} correctly!" | |
| puts "Wasn't that such fun!\nI say, wouldn't you like to play again? Say only Yes or No" | |
| play_again = gets.chomp.downcase | |
| end | |
| puts "Have a good day then." |
+1
This is good, but you've only defined one method in your refactoring! Since it's a rather short and simple program, I can see not too much is needed, but you can certainly extract some stuff into methods. Also, I'm not sure what I think of the method parse_letters_and_words, because really it's doing a lot more than just parsing. It's splitting the user input into necessary letters or words and then creating an array of matched things. It's also repetitive within itself, because it's doing the same thing to both words and letters. It's also not consistent. So, here is your code:
def parse_letters_and_words
# handling letters
@guess_letters = @guess.split("")
@matched_letters = @current_answer_array & @guess_letters
# all of this stuff below handles words in almost exactly the same way
@guess_words = @guess.split(/ /)
@current_answer_words = @current_answer.downcase.split(/ /)
@matching_words = @guess_words & @current_answer_words
end
You'll notice that for words, within this method you define @current_answer_words, but you don't define the letter equivalent, @current_answer_array. You define that way down in the middle of your script in line 15, outside of this method. Why is it on it's own if it's actually conceptually part of parsing letters?
Since you're not persisting the matched words and letters and instead are resetting these arrays with every guess (like, in one guess i might get 't', 'r', and 'i' correct, but in the next guess it'll tell me I got 'm', 'o', 's' correct, not a concatenation of both, which'd be 't', 'r', 'i', 'm', 'o', and 's'), then you don't really need to persist those variables as instance variables, and you could refactor this whole method like so:
def match(guess, type)
if type == "letter"
space = ""
answer_array = @current_answer_array
else
space = " "
answer_array = @current_answer_words
end
guess_array = guess.split(space)
matched_array = answer_array & guess_array
matched_array
end
while @guess != @current_answer
matched_words = match(@guess, "word")
matched_letters = match(@guess, "letter")
puts "The good news is you got #{matched_letters} letters right"
...
Also, you have
while @current_answer.downcase != @guess
I would change the order of this to
while @guess != @current_answer.downcase
because it makes more sense semantically and logically - the guess is what's changing, so you're checking the guess against the answer ("while the guess does not match the correct answer"), but with your syntax, it reads like the current answer is changing and you're checking that against the guess ("while the answer doesn't match the user's guess"). It may seem like a minor point, but nuances like this are quite important.
First iteration