Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
words = ['banana', 'computer', 'rocks', 'giraffe', 'science']
answer = ''
while answer != 'no'
#init try_count to 6 == number of body parts for hangman
try_count = 6
random = rand(words.count)
game_word = words[random]
game_word = game_word.split(//)
guessed_word = ['-'] * game_word.count
puts 'This is HangMan: Limited Edition'
while guessed_word != game_word && try_count !=0
puts guessed_word.to_s
puts 'Guess a letter: '
letter = gets.chomp
matches = []
counter = 0
game_word.each_index do |i|
if letter == game_word[i]
matches << i
end
end
matches.each do |e|
guessed_word[e] = letter
counter = counter + 1
end
if counter >= 1
puts("You guessed correctly! ")
else
puts("You guessed incorrectly! ")
try_count = try_count - 1
if try_count > 1
puts( "You have #{try_count} tries left")
elsif try_count == 1
puts( "You have #{try_count} try left")
else
puts( "You have no tries left")
end
end
puts try_count.to_s + 'try count'
if try_count == 0
puts 'You lose the game.'
end
end
if try_count > 0
puts guessed_word.to_s
puts( "You completed the word, congratulations!")
end
begin
puts 'Would you like to play again? Yes or No?'
answer = gets.chomp.downcase
end while answer != 'yes' && answer != 'no'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment