Skip to content

Instantly share code, notes, and snippets.

@kingcons
Created September 16, 2015 00:05
Show Gist options
  • Save kingcons/2873ed5e341ff940def3 to your computer and use it in GitHub Desktop.
Save kingcons/2873ed5e341ff940def3 to your computer and use it in GitHub Desktop.
Crash Course for September
# Gets for input, puts for output
# data to track:
# letters guessed, strings
# turn count, number
# answer, string
# actions in hangman:
# inform user of word length
# take turns until the game ends
# user guesses a letter
# lose a turn for a wrong guess
# tell the user the word, and maybe if they won
# how does the game end?
# lose by running out of guesses
# win by solving the word
puts "Welcome to Hangman!"
puts
puts "I won't explain the rules because I'm lazy and it is Tuesday at 7pm."
words = ["cookies", "hamburger", "traffic", "weekend",
"classroom", "football", "computer", "programming",
"vacation", "beaches", "umbrella", "rain"]
answer = words.sample
guesses = []
turn_count = 5
puts "The word is #{answer.length} letters long."
def lose?(turns)
turns == 0
end
def win?(answer, guesses)
answer.chars.all? do |letter|
guesses.include?(letter)
end
end
until lose?(turn_count) || win?(answer, guesses)
puts "Please guess a letter: "
guess = gets.chomp
guesses.push(guess)
if !answer.include?(guess)
# turn_count = turn_count - 1
turn_count -= 1
end
end
if win?(answer, guesses)
puts "You're awesome. Congratulations. Being good at Hangman must be real hard."
else
puts "Sorry, better luck next time."
end
puts "The word was: #{answer}"
# turn_count -= 1 unless answer.include?(guess)
# def win?
# # use include? to check that all the answer letters are in the guesses
# won = true
# answer.chars.each do |letter|
# won = false unless guesses.include?(letter)
# end
# won
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment