Created
January 19, 2015 02:10
-
-
Save troyleach/a70f377bd4157d85a2a1 to your computer and use it in GitHub Desktop.
Screen cast 6 - ask 3 questions!
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
| # => Ask the user a total of three ruby questions. | |
| # => the user will have 3 try's at the question before it moves onto the next question, OR the short game ends. | |
| # => might have gone a little over board with this... oops | |
| class Questions | |
| attr_reader :question, :answer | |
| def initialize(args = {}) | |
| @question = args[:question] | |
| @answer = args[:answer] | |
| end | |
| end | |
| class MakeDeck | |
| attr_accessor :deck | |
| def initialize | |
| @deck = [] | |
| end | |
| def making_cards | |
| @deck << Questions.new({question: "Used to define a function", answer: "def"}) | |
| @deck << Questions.new({question: 'Gives an "otherwise" within a function, if-statement, or for-loop, i.e. if cats = cute, puts "Yay!" else puts "Oh, a cat.', answer: "else"}) | |
| @deck << Questions.new({question: "Much like else, but has a higher precedence, and is usually paired with terms.", answer: "elsif"}) | |
| end | |
| end | |
| class View | |
| def user_input | |
| gets.chomp | |
| end | |
| def welcome | |
| puts <<-STRING | |
| There are 3 questions dealing with Ruby that I'm going to ask you | |
| You will have 3 chances to guess the correct answer before we move onto the next question! | |
| At any time you can type exit to end the game! | |
| Good Luck! | |
| STRING | |
| end | |
| def show_question(card) | |
| puts card.question | |
| end | |
| def show_correct_answer(card) | |
| puts "The correct answer is '#{card.answer}'" | |
| end | |
| def display_next_question | |
| puts "Your next question:" | |
| end | |
| def display_wrong | |
| puts "Wrong, try again!" | |
| end | |
| def number_of_guesses(try) | |
| puts "That was try number #{try}" | |
| end | |
| def game_end | |
| puts "thanks for playing" | |
| end | |
| end | |
| class Game | |
| attr_reader :playing, :deck, :view | |
| MAX_GUESSES = 3 | |
| def initialize | |
| @playing = MakeDeck.new | |
| @view = View.new | |
| @guesses = 0 | |
| end | |
| def run | |
| view.welcome | |
| playing.making_cards | |
| next_question | |
| end | |
| def handle_user_input(guess) | |
| @guesses += 1 | |
| return nil if guess == 'exit' | |
| if guess == @card.answer | |
| correct_guess | |
| else | |
| wrong_guess(@card) | |
| end | |
| end | |
| def next_question | |
| if playing.deck.length > 0 | |
| @card = playing.deck.delete(playing.deck.sample) | |
| view.display_next_question | |
| view.show_question(@card) | |
| handle_user_input(view.user_input) | |
| else | |
| view.game_end | |
| exit | |
| end | |
| end | |
| def wrong_guess(card) | |
| while @guesses < MAX_GUESSES | |
| view.number_of_guesses(@guesses) | |
| handle_user_input(view.user_input) | |
| view.display_wrong | |
| end | |
| if @guesses == MAX_GUESSES | |
| view.show_correct_answer(card) | |
| end | |
| get_next_question | |
| end | |
| def reset_guess! | |
| @guesses = 0 | |
| end | |
| def correct_guess | |
| puts | |
| puts "#{@card.answer} is correct" | |
| get_next_question | |
| end | |
| def get_next_question | |
| reset_guess! | |
| next_question | |
| end | |
| end | |
| puts | |
| puts "Here is your first question:" | |
| Game.new.run | |
| # - - - - -BONE YARD - - - - - - - | |
| =begin | |
| class Questions | |
| attr_reader :cards, :answer, :question | |
| def initialize | |
| @cards = [{question: "Used to define a function", answer: "def"}, | |
| {question: 'Gives an "otherwise" within a function, if-statement, or for-loop, i.e. if cats = cute, puts "Yay!" else puts "Oh, a cat.', answer: "else"}, | |
| {question: "Much like else, but has a higher precedence, and is usually paired with terms.", answer: "elsif"} | |
| ] | |
| @answer | |
| @question | |
| @guess = 0 | |
| end | |
| def ask_question | |
| puts cards[0][:question] | |
| end | |
| def get_input | |
| answer = gets.chomp | |
| end | |
| def check_input | |
| puts answer | |
| @guess += 1 | |
| while true | |
| "What is your quess" | |
| if answer == cards[0][:answer] | |
| puts "Hey.. that's right good job" | |
| exit | |
| end | |
| if answer != cards[0][:answer] | |
| puts "try again" | |
| end | |
| break if @guess == 3 | |
| end | |
| end | |
| def is_correct_guess(get_input) | |
| @guess == get_input | |
| end | |
| end | |
| test = Questions.new | |
| puts "Ok here we go, here is your first question" | |
| print test.ask_question | |
| test.get_input | |
| test.check_input | |
| =end | |
| =begin | |
| def check_input | |
| until answer == cards[0][:answer] || @counter == 3 | |
| "What is your quess" | |
| if answer == cards[0][:answer] | |
| puts "Hey.. that's right good job" | |
| else | |
| @counter += 1 | |
| puts "Sorry, that's not the right answer ..'how many more tries'" | |
| end | |
| end | |
| end | |
| =end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I LOVE THIS. Of course I had to play the game. Luckily I got 3 out of 3. Cheers!