Last active
December 19, 2015 18:48
-
-
Save jsteiner/6001207 to your computer and use it in GitHub Desktop.
Flashcards
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
module Flashcards | |
class Card | |
attr_accessor :front, :back | |
def initialize(front, back) | |
@front = front | |
@back = back | |
end | |
def play | |
print "Guess the answer to #{front}: " | |
guess = gets.chomp | |
if guess == back | |
puts "You were right!" | |
else | |
puts "You were wrong! The correct answer was #{back}." | |
end | |
end | |
end | |
class Deck | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
@cards = [] | |
end | |
def <<(card) | |
@cards << card | |
end | |
def each(&block) | |
@cards.each(&block) | |
end | |
def shuffle | |
@cards.shuffle! | |
end | |
def play | |
shuffle | |
@cards.each do |card| | |
card.play | |
end | |
end | |
end | |
class Application | |
def initialize | |
@decks = [] | |
end | |
def <<(deck) | |
@decks << deck | |
end | |
def play | |
display_decks | |
deck = choose_deck | |
deck.play | |
end | |
def display_decks | |
@decks.each do |deck| | |
puts deck.name | |
end | |
end | |
def choose_deck | |
print "Which deck do you want to play? > " | |
chosen_deck = gets.chomp | |
@decks.detect do |deck| | |
deck.name == chosen_deck | |
end | |
end | |
end | |
end | |
app = Flashcards::Application.new | |
deck = Flashcards::Deck.new('Spanish') | |
deck << Flashcards::Card.new('Hola', 'Hello') | |
deck << Flashcards::Card.new('Adios', 'Goodbye') | |
app << deck | |
app.play |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment