-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Word Guess - Octos - Jamila & Angelica #10
base: master
Are you sure you want to change the base?
Conversation
Word-Guess GameWhat We're Looking For
Great work overall! I've left some nitpicks down below for you to review, but in general this code is clean and easy to read, and I am quite happy with this submission. Keep up the hard work! |
attr_writer :num_attempts | ||
def initialize | ||
@num_attempts = 5 | ||
@heart_parts = [" ,d88b.d88b, ", " 88888888888 ", " `Y8888888Y' ", " `Y888Y' ", " `Y' "] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is a little hard to understand as is - you could make it more readable by moving things around:
@heart_parts = [
" ,d88b.d88b, ",
" 88888888888 ",
" `Y8888888Y' ",
" `Y888Y' ",
" `Y' "
]
|
||
case user_choice | ||
when "yes","y" | ||
play_game |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here play_game
calls play_again
which in turn calls play_game
, creating a kind of loop. This is an example of a programming technique called recursion, which you'll learn about in CS Fun in a few months.
Recursion is a powerful tool that can elegantly solve many problems. However, in this case I think a while
loop might be a better choice, if only because it makes it immediately obvious to the reader that this code might execute many times. Re-writing this code with a while
loop might look like:
def play_game
user_choice = 'y'
while ['yes', 'y'].include?(user_choice)
game = Game.new
game.round
puts "Would you like to play again?"
user_choice = gets.chomp.downcase
end
puts "Goodbye!"
end
def round | ||
# get word length | ||
# loop through word length and puts "_" that many times | ||
while @attempts != 0 && @word_split != @word_in_progress |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method feels a little long to me. Could you break it up into several smaller methods? For example, you might make one that gets a letter from the user and validates it, and another to update the game state based on the user's guess.
# allows user to play multiple times | ||
def play_game | ||
game = Game.new | ||
puts game.round |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding your question, I don't think this code does need to be inside of a class. To me it feels well-organized as is.
Word Guess
Congratulations! You're submitting your assignment.
Comprehension Questions