Created
August 20, 2013 17:34
-
-
Save dmehrotra/6284620 to your computer and use it in GitHub Desktop.
Hangman
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
#!/usr/bin/env ruby | |
class Game | |
def init(word) | |
@letter_array=[] | |
@guess_array = [] | |
@guesses = [] | |
@chances = 0 | |
@target_word = word.each_char do |c| | |
@letter_array << c | |
@guess_array << '|-|' | |
@chances = @chances + 1 | |
end | |
self.start | |
end | |
def start | |
puts ' | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
xxxxxxxxxxWELCOMExxxxxxxxxxxxxxxxx | |
xxxxxxxxxxxxxxxxxxxxTOxxxxxxxxxxxx | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxHANGMAN | |
' | |
self.display | |
end | |
def display | |
puts "chances: '#{@chances}' | |
" | |
if @chances == 0 | |
self.loser | |
elsif @guess_array == @letter_array | |
self.winner | |
else | |
@guess_array.each{|c| print ' ', c , ' ' } | |
self.guess | |
end | |
end | |
def guess | |
print " | |
guess a letter or word: " | |
guess = gets.chomp.to_s | |
@guess = guess | |
if @guesses.include?(@guess) | |
print "you already guessed this | |
" | |
else | |
@guesses << @guess | |
end | |
if @guess.length <= 1 | |
if @letter_array.include?(guess) | |
print " | |
Good work, The word contains a #{guess} | |
" | |
letter_index = @letter_array.each_with_index do |value, index| | |
if value == @guess | |
@guess_array[index] = value | |
end | |
end | |
self.display | |
else | |
puts " | |
xxxxxNOxxxxxxxxWAYxxxxxx | |
xxxxxxxxGUESSxxxxxxxxxxx | |
xxxxxxxxAGAINxxxxxxxxxxx | |
" | |
@chances = @chances - 1 | |
self.display | |
end | |
else | |
self.word_guess | |
end | |
end | |
def word_guess | |
if @guess == @target_word | |
self.winner | |
else | |
self.loser | |
end | |
end | |
def loser | |
puts " | |
The word was #{@target_word} | |
xxxxxxxxxxxxxxLOOOOOSEEERRRRxxxxxxxxxxxxxxxxxxx | |
" | |
end | |
def winner | |
print " | |
WINNERWINNERWINNERWINNERWINNERWINNERWINNERWINNERWINNER | |
WINNERWINNERWINNERWINNERWINNERWINNERWINNERWINNERWINNER | |
WINNERWINNERWINNERWINNERWINNERWINNERWINNERWINNERWINNER | |
WINNERWINNERWINNERWINNERWINNERWINNERWINNERWINNERWINNER | |
WINNERWINNERWINNERWINNERWINNERWINNERWINNERWINNERWINNERS | |
" | |
end | |
end | |
article = %w(Expressing frustration at the frequency with which he is required to guide people through their darkest moments of drug-induced psychosis, Six Flags Great America employee Mark Griffin told reporters Monday that he is sick and tired of talking park visitors down from their bad acid trips) | |
game = Game.new | |
game.init(article.sample) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment