Skip to content

Instantly share code, notes, and snippets.

@apirak
Created March 23, 2012 14:38
Show Gist options
  • Save apirak/2171255 to your computer and use it in GitHub Desktop.
Save apirak/2171255 to your computer and use it in GitHub Desktop.
Hangman games with out class
@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