Skip to content

Instantly share code, notes, and snippets.

@MelanieS
Created December 5, 2010 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelanieS/728851 to your computer and use it in GitHub Desktop.
Save MelanieS/728851 to your computer and use it in GitHub Desktop.
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