Created
November 18, 2015 00:54
-
-
Save kingcons/19d851f0afead0da49af to your computer and use it in GitHub Desktop.
November Crash Course
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' | |
## How does Hangman work? | |
## Data | |
# * guessed letters | |
# * answer - randomly selected word | |
# * turn count - 6 | |
## Behaviors | |
# * guess letter, losing a turn if they get it wrong | |
# * continue taking turns, until the word is complete or they are out of turns | |
turn_count = 6 | |
words = [ | |
"popsicle", "pogostick", "bourbon", "grilling", | |
"gorilla", "watermelon", "corkscrew", "ruby", | |
"interpreter", "programming", "cookies", "cocktails" | |
] | |
guesses = [] | |
answer = words.sample | |
def lose?(turns) | |
turns.zero? | |
end | |
def win?(answer, guesses) | |
answer.chars.all? { |letter| guesses.include?(letter) } | |
# winner = true | |
# answer.chars.each do |letter| | |
# if !guesses.include?(letter) | |
# winner = false | |
# end | |
# end | |
# winner | |
end | |
def game_over?(turns, answer, guesses) | |
win?(answer, guesses) || lose?(turns) | |
end | |
until game_over?(turn_count, answer, guesses) | |
puts "There are #{turn_count} turns left." | |
puts "Please make a guess: " | |
guess = gets.chomp | |
guesses.push(guess) | |
if !answer.include?(guess) | |
# turn_count = turn_count - 1 | |
turn_count -= 1 | |
end | |
end | |
puts "Game Over. The word was: #{answer}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment