Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions word_guess.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require "colorize"

# class Image generates beating heart
class Image
attr_writer :num_attempts
def initialize
@num_attempts = 5
@heart_parts = [" ,d88b.d88b, ", " 88888888888 ", " `Y8888888Y' ", " `Y888Y' ", " `Y' "]

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'      "
]


end

def print_heart
loser_img = ""
@num_attempts.times do |attempt|
loser_img += "#{@heart_parts[attempt]}".blue.on_red.blink
loser_img += "\n"
end
(5 - @num_attempts).times do
loser_img += " ".blue.on_red.blink
loser_img += "\n"
end
return loser_img

end
end

# class Game logic runs game
class Game

attr_accessor :word, :word_split

def initialize
@words = ["apple", "coconut", "carrot", "skein", "metronome"]
@attempts = 5
@image = Image.new
@word = @words.sample
@word_split = @word.split('')
@word_in_progress = Array.new(@word.length, "_ ")
end

def round
# get word length
# loop through word length and puts "_" that many times
while @attempts != 0 && @word_split != @word_in_progress

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.

puts "\n"
print "Guess a letter: "
puts @word_in_progress.join
user_input = gets.chomp
until /[a-z]+/.match(user_input)
puts "Guess a letter: "
user_input = gets.chomp

end

# updates word if user guesses letter correctly
if @word_split.include?(user_input)
# does something
user_input_indices = @word_split.each_index.select {|i| @word_split[i] == user_input}

user_input_indices.each do |index|
@word_in_progress[index] = user_input
end
print "#{@image.print_heart}"
# look at how to find indices for multiple occurences of a letter
else
# update attempts count
@attempts -= 1
# update image
@image.num_attempts = @attempts
puts "#{@image.print_heart}"
end

if @word_split == @word_in_progress
puts "Congratulations, you won!"
elsif @attempts == 0
puts "You lost."
end


end
puts @word

end

end

# allows user to play multiple times
def play_game
game = Game.new
puts game.round

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.

play_again
end

# controls logic for multiple games
def play_again
puts "Would you like to play again?"
user_choice = gets.chomp.downcase

case user_choice
when "yes","y"
play_game

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

when "n","no"
puts "Goodbye!"
else
puts "Didn't catch that."
play_again
end
end

play_game