Created
December 16, 2015 01:09
-
-
Save kingcons/e8afe62ab65f9841d9a3 to your computer and use it in GitHub Desktop.
Thanks for Coming!
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
# require 'pry' | |
## Hangman | |
## Data | |
# collection of random words | |
# word, 4 to 12 letters | |
# limited number of turns | |
# collection of guesses | |
## Actions | |
# DONE pick a random word to be the answer | |
# the following four things happen repeatedly: | |
# DONE display information about guesses | |
# DONE guess a letter | |
# DONE add the letter to the guesses | |
# DONE if the guess isn't in the answer, lose a turn | |
# the above stops when the game ends: | |
# DONE either by guessing all the letters | |
# DONE running out of turns | |
# DONE tell the player what happened | |
dictionary = ["bourbon", "banish", "chocolate", "bacon", | |
"milkshake", "falter", "ignite", "programming", "ruby", | |
"computer", "data", "meander", "rambling", "instructor"] | |
answer = dictionary.sample | |
turn_count = 6 | |
guesses = [] | |
puts "Welcome to Hangman! I'm too lazy to explain the rules. Let's just play." | |
def lose?(turn_count) | |
turn_count.zero? | |
end | |
# def win?(answer, guesses) | |
# winner = true | |
# answer.chars.each do |letter| | |
# winner = false unless guesses.include?(letter) | |
# end | |
# winner | |
# end | |
def win?(answer, guesses) | |
answer.chars.all? do |letter| | |
guesses.include?(letter) | |
end | |
end | |
def game_over?(answer, guesses, turns) | |
lose?(turns) || win?(answer, guesses) | |
end | |
def intermediate_word(answer, guesses) | |
characters = answer.chars.map do |letter| | |
guesses.include?(letter) ? letter : "-" | |
end | |
characters.join | |
end | |
until game_over?(answer, guesses, turn_count) | |
puts "The word is: #{intermediate_word(answer, guesses)}." | |
puts "You have #{turn_count} turns left!" | |
puts "Please guess a letter: " | |
guess = gets.chomp | |
guesses.push(guess) | |
turn_count -= 1 unless answer.include?(guess) | |
end | |
if win?(answer, guesses) | |
puts "Congratulations! You didn't die." | |
else | |
puts "Sorry, better luck next time. The word was: #{answer}" | |
end | |
# binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment