Last active
April 24, 2022 16:36
-
-
Save kingcons/f4f4bff67d37004d1d1f to your computer and use it in GitHub Desktop.
Crash Course 2.0
This file contains hidden or 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' | |
WORDS = ["cookies", "bourbon", "dogs", "sunshine", "vacations", | |
"dictionaries", "georgia"] | |
## Pieces of data in Hangman: | |
# Guesses | |
# Answer | |
# Turn Count | |
## Notes | |
# We can save multiple object in arrays | |
# We can show to the user with puts | |
# We can retrieve input from the user with gets | |
def show_game(turns, guessed) | |
puts "You have #{turns} turns left. Guessed: #{guessed}" | |
# TODO: show them the semi-completed word | |
end | |
def prompt_player | |
puts "Make a guess!" | |
gets.chomp | |
end | |
def win?(guesses, answer) | |
answer.chars.all? { |x| guesses.include?(x) } | |
end | |
def lost?(turns) | |
turns.zero? | |
end | |
def game_over?(turns, guesses, answer) | |
lost?(turns) || win?(guesses, answer) | |
end | |
def hangman(answer) | |
turn_count = 5 | |
guesses = [] | |
puts "Welcome to Hangman!" | |
until game_over?(turn_count, guesses, answer) | |
show_game(turn_count, guesses) | |
guess = prompt_player | |
guesses.push(guess) | |
unless answer.include?(guess) | |
turn_count = turn_count - 1 | |
end | |
end | |
if win?(guesses, answer) | |
puts "The word was #{answer}. You win!" | |
else | |
puts "The word was #{answer}. Better luck next time." | |
end | |
end | |
hangman(WORDS.sample) | |
# binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment