Skip to content

Instantly share code, notes, and snippets.

@ajaxray
Created July 13, 2012 17:48
Show Gist options
  • Save ajaxray/3106239 to your computer and use it in GitHub Desktop.
Save ajaxray/3106239 to your computer and use it in GitHub Desktop.
A simple word game (hangman) for learning ruby basics
=begin Hangman
A simple word game
Date: 5th Aug, 2011
Author: Anis uddin Ahmad <anisniit@gmail.com>
=end
#require SecureRandom
words = %w"learning lollipop education image computer mobile january february friday flower beauty light earth machine book
news yahoo google internet bangladesh india america cricket football friday sunday sunny"
total_chances = 5
wrong_try = 0
right_guess = ''
hanged = <<HANG
+---+-
| |
| 0
| |\\
| /\\
-+----------
HANG
survibed = <<WIN
(@)
^\\|
|/^
____|_____
WIN
#puts words.inspect
word = words[rand(words.length) - 1]
#word = words[1]
def get_placeholder sample_word, guessed_words
placeholder = ''
sample_word.chars { |char|
placeholder += (guessed_words.include? char)? char : '#'
}
placeholder
end
puts `clear`
puts 'Guess what is:'+ get_placeholder(word, '')
while true
print "Enter word [#{total_chances - wrong_try} chances left]:"
char = gets.chomp
puts `clear`
if word.include? char
if(right_guess.include? char)
puts char + ' is already given and accepted.'
puts 'Try another: ' + get_placeholder(word, right_guess)
else
right_guess = right_guess + char
placeholder = get_placeholder(word, right_guess)
puts 'Great! ' + placeholder
end
unless placeholder.include? '#'
puts "WELL DONE!! YOU SURVIVED"
puts survibed
break
end
else
puts "Sorry! The word dosen't contains '#{char}'"
wrong_try += 1
if (wrong_try == total_chances)
puts "YOU HANGED!"
puts hanged
break
else
puts 'Try another: ' + get_placeholder(word, right_guess)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment