Created
February 20, 2014 20:05
-
-
Save townie/9122085 to your computer and use it in GitHub Desktop.
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
|---- | |
| | |
| | |
| | |
|------\ | |
|---- | |
| 0 | |
| | |
| | |
|------\ | |
|---- | |
| 0 | |
| | | |
| | |
|------\ | |
|---- | |
| 0 | |
| -| | |
| | |
|------\ | |
|---- | |
| 0 | |
| -|- | |
| | |
|------\ | |
|---- | |
| 0 | |
| -|- | |
| / | |
|------\ | |
|---- | |
| 0 | |
| -|- | |
| / \ | |
|------\ | |
|---- | |
| 0 | |
| *-|- | |
| / \ | |
|------\ | |
|---- | |
| 0 | |
| *-|-* | |
| / \ | |
|------\ | |
|---- | |
| X.X | |
| *-|-* | |
| / \ | |
|------\ | |
|---- | |
| W | |
| I | |
| N | |
|------\ | |
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 | |
def make_guess | |
#returns a guess that is one charater long OR compares against the whole word | |
print "Guess a single letter (a-z) or the entire word: " | |
guess = gets.chomp | |
if !(/[a-zA-z]/.match(guess)) | |
puts "Please a enter a letter or word" | |
guess = make_guess | |
elsif guess.length > 1 | |
puts "Are you sure you want to guess #{guess} as the whole word? (y/n)" | |
yesorno = gets.chomp | |
if yesorno.include?('y') | |
return guess | |
elsif yesorno.include?('n') | |
puts "Ok thats what I thought, Please enter your guess" | |
guess=make_guess | |
else | |
puts "Well clearly you didnt follow instructions you guess will count as #{guess}" | |
end | |
end | |
guess.downcase | |
end | |
def rand_word | |
#returns a word a random | |
words = %w(time person year way day thing man world life hand part child eye woman place work week case point government company number group problem fact) | |
words.shuffle.pop | |
end | |
def is_in?(word_to_guess, guess) | |
word_to_guess.include?(guess) | |
end | |
def show_count_of_guess( guess, word_to_guess) | |
count = 0 | |
word_to_guess.each do |current| | |
count += 1 if current == guess | |
end | |
puts "\nFound #{count} occurrence(s) of the character #{guess}.\n" | |
end | |
def print_correct_letters(correct_guess) | |
print "Word: " + correct_guess.join + " \n " | |
end | |
def update_correct_guess(correct_guess, guess, word_to_guess) | |
word_to_guess.each_with_index do |letter, position| | |
if letter == guess | |
correct_guess[position] = guess | |
end | |
end | |
correct_guess | |
end | |
def win_game | |
puts "Congratulations, you've guessed the word!" | |
false | |
end | |
def animate(init_chances, number_of_chances) | |
anime = File.open('animation.txt').to_a | |
location = init_chances - number_of_chances | |
range_min = 0 + (location * 5) | |
range_max = 4 + (location * 5) | |
(range_min..range_max).each do |position| | |
puts anime[position] | |
end | |
end | |
def hangman(number_of_chances = 8) | |
init_chances = number_of_chances | |
puts "Welcome to hangman!" | |
word_to_guess = rand_word.split(//) | |
unsolved = true | |
correct_guess = Array.new(word_to_guess.length) { |i| i= ' _ ' } | |
while number_of_chances > 0 && unsolved | |
animate(init_chances, number_of_chances) | |
print_correct_letters( correct_guess) | |
puts "Chances remaining: #{number_of_chances}" | |
guess = make_guess | |
if is_in?(word_to_guess, guess) | |
show_count_of_guess( guess, word_to_guess) | |
correct_guess = update_correct_guess(correct_guess, guess, word_to_guess) | |
if correct_guess == word_to_guess | |
animate(10,0) | |
unsolved = win_game | |
end | |
elsif guess.length > 1 | |
if guess == word_to_guess.join | |
animate(10,0) | |
unsolved = win_game | |
else | |
animate(8,0) | |
puts "You Lose! #{guess} was not the correct word" | |
break | |
end | |
else | |
puts "\nSorry, no #{guess}'s found \n" | |
number_of_chances -= 1 | |
end | |
if number_of_chances <= 0 | |
animate(8,0) | |
puts "Sorry you lose the game" | |
end | |
end | |
end | |
hangman |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment