Created
February 17, 2016 01:13
-
-
Save kingcons/19f77fdbeeac89d45618 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
## Hangman | |
# Data | |
## Letters Guessed | |
## Number of Turns? | |
## Answer | |
## Dictionary | |
# Behaviors | |
## Greeting (and rules?) | |
## Choose an Answer | |
## Until the game is over keep doing ... | |
### Display number of letters and how many turns left | |
### Let the player guess a letter | |
### If the guess is correct, do nothing | |
### If the guess is incorrect, lose a turn | |
## Tell them the word and if they won or lost | |
require "pry" | |
DICTIONARY = [ | |
"puppies", "feed", "cats", "bourbon", | |
"democracy", "automobiles", "election", "year", | |
"vacation", "king", "confidence", "combinator" | |
] | |
# answer = DICTIONARY[rand(DICTIONARY.length)] | |
answer = DICTIONARY.sample | |
turns_left = 7 | |
def greeting | |
puts "Welcome to Hangman. I'm too lazy and tired to explain the rules. Have fun!" | |
end | |
def take_turn(word, turns, guesses) | |
# puts "The word is " + word.length.to_s + " letters long." | |
puts "The word is #{word.length} letters long." | |
puts "You have guessed: #{guesses}" | |
puts "You have #{turns} turns left." | |
puts "Please guess a letter: " | |
guess = gets.chomp | |
guesses.push(guess) | |
guess | |
end | |
def win?(guesses, answer) | |
answer.chars.all? do |letter| | |
guesses.include?(letter) | |
end | |
# winner = true | |
# answer.chars.each do |letter| | |
# if !guesses.include?(letter) | |
# winner = false | |
# end | |
# end | |
# winner | |
end | |
def game_over?(turns, guesses, answer) | |
turns.zero? || win?(guesses, answer) | |
end | |
def postmortem(turns_left, guesses, answer) | |
if win?(guesses, answer) | |
puts "Good job. The word was: #{answer}" | |
else | |
puts "Sorry, better luck next time. The word was: #{answer}" | |
end | |
end | |
def hangman(word, turns_left) | |
guesses = [] | |
greeting | |
until game_over?(turns_left, guesses, word) | |
guess = take_turn(word, turns_left, guesses) | |
if !word.include?(guess) | |
turns_left = turns_left - 1 | |
puts "Wrong guess, you lose a turn!" | |
end | |
end | |
postmortem(turns_left, word, guesses) | |
end | |
hangman(answer, turns_left) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment