Hangman

Hangman

hangman.rb

Hangman is another fun word game! Most people know how to play hangman. If a user doesn't know how to play, the rules are simple to explain. A word is randomly chosen and only the number of characters is known—sort of like Wheel of Fortune. The user has six tries to figure out the word, or else the man is hanged and game lost.

The Code

 unless ARGV[0] and File.exists?(ARGV[0])
     puts "\n\nUsage is hangman.rb <word.file>\n\n"
     exit
 end

 words = File.readlines(ARGV[0])
 mystery_word = words[rand(words.size)].chomp
 solution = Array.new(mystery_word.length, "-")
 guessed = []
 steps = 6

 while steps > 0
 puts <<EOM \n\n\nYou have #{steps} ...

Get Wicked Cool Ruby Scripts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.