Created
March 23, 2012 14:38
-
-
Save apirak/2171255 to your computer and use it in GitHub Desktop.
Hangman games with out class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@words = %w{spiderman superman powerpuf hangman} | |
@finish = false | |
@limit = 8 | |
@win = false | |
@word = @words[rand(@words.length-1)] | |
@answer = "_" * @word.length | |
def guest(answer) | |
@limit -= 1 | |
answer = answer[0..-2] | |
if answer == 'exit' || @limit == 0 | |
@finish = true | |
return | |
end | |
if answer.length == 1 | |
index = 0 | |
@word.each_char do |w| | |
if w == answer | |
@answer[index] = answer | |
end | |
index += 1 | |
end | |
end | |
if answer == @word || @answer == @word | |
@finish = true | |
@win = true | |
end | |
end | |
until @finish | |
puts @answer | |
print "#{@limit} Enter your best guest: " | |
guest(gets) | |
end | |
if @win | |
puts "You are the winner" | |
else | |
puts "Bye loser, the answer is #{word}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment