Created
March 22, 2016 23:55
-
-
Save kingcons/dae96cd1b4a24fa44a48 to your computer and use it in GitHub Desktop.
March 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
## Hangman | |
## Data | |
# * guesses | |
# * answer | |
# * dictionary / word_list | |
# * body parts? turn count? limit on turns? | |
# CamelCaseIsLikeThis | |
# snake_case_is_like_this | |
# common-lisp-does-it-like-this | |
word_list = [ | |
"democracy", "muffin", "laptop", "beer", | |
"coffee", "bourbon", "gin", "mezcal", | |
"hamburger", "marzipan", "pimento", "code", | |
"ruby", "doorhandle", "therapy", "sleep" | |
] | |
# answer = word_list[rand(word_list.length)] | |
def greeting | |
puts "Welcome to Hangman." | |
puts "I'm too lazy to explain the rules. Just guess letters stupid." | |
end | |
# def win?(guesses, answer) | |
# result = false | |
# answer.chars.each do |l| | |
# if guesses.include?(l) | |
# result = true | |
# else | |
# result = false | |
# end | |
# end | |
# result | |
# end | |
# def complicated_win?(guesses, answer) | |
# result = true | |
# answer.chars.each do |l| | |
# result = false unless guesses.include?(l) | |
# # unless guesses.include?(l) | |
# # result = false | |
# # end | |
# # if !guesses.include?(l) | |
# # result = false | |
# # end | |
# end | |
# result | |
# end | |
# def display_game(guesses, turn_count) | |
# puts "You have #{turn_count} turns left." | |
# puts "You have already guessed: #{guesses.join(", ")}" | |
# end | |
def display_game(guesses, turn_count, answer) | |
puts "You have #{turn_count} turns left." | |
answer.chars.each do |l| | |
if guesses.include?(l) | |
print l | |
else | |
print "-" | |
end | |
end | |
puts | |
end | |
def win?(guesses, answer) | |
answer.chars.all? do |letter| | |
guesses.include?(letter) | |
end | |
end | |
def lose?(turn_count) | |
turn_count == 0 | |
end | |
def game_over?(guesses, answer, turn_count) | |
win?(guesses, answer) || lose?(turn_count) | |
end | |
def take_turn | |
puts "Please guess a letter: " | |
gets.chomp | |
end | |
def hangman(words) | |
guesses = [] | |
turn_count = 7 | |
answer = words.sample | |
greeting | |
until game_over?(guesses, answer, turn_count) | |
display_game(guesses, turn_count, answer) | |
guess = take_turn | |
guesses << guess # guesses.push(guess) | |
turn_count -= 1 unless answer.include?(guess) | |
end | |
postmortem | |
end | |
hangman(word_list) | |
# if 2 + 2 == 4 | |
# puts "cool" | |
# else | |
# puts "liar" | |
# end | |
# 2 + 2 == 4 ? puts "cool" : puts "liar" | |
# puts "caz is not a win for 'cat'" | |
# puts win?(["c", "a", "z"], "cat") ? "FAILED" : "SUCCESS" | |
# puts "catz is a win for 'cat'" | |
# puts win?(["c", "a", "t", "z"], "cat") ? "SUCCESS" : "FAILED" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment