Skip to content

Instantly share code, notes, and snippets.

@danigro77
Forked from dbc-challenges/card.rb
Created October 23, 2012 22:23
Show Gist options
  • Save danigro77/3942078 to your computer and use it in GitHub Desktop.
Save danigro77/3942078 to your computer and use it in GitHub Desktop.
FlashCardinator
class Card
attr_reader :term, :definition
def initialize(term, definition)
@term = term
@definition = definition
end
def match?(guess)
if term == guess
true
elsif guess == "exit"
Process.exit
else
false
end
end
def info
"#{self.term}: #{self.definition}"
end
end
require_relative 'card'
require_relative 'cards_loader'
class CardGame
attr_accessor :all_cards, :correct_answers
attr_reader :current_card
def initialize
@all_cards = CardsLoader.all_cards.shuffle
@current_card = ""
@correct_answers = []
@exit_status = false
end
def welcome
puts "Welcome! Type 'view' to see the cards. Type 'game' to begin the game. Type 'exit' to exit at any time."
case gets.chomp
when "view"
view_cards
when "game"
game_start
when "exit"
puts "Good-bye!"
else
puts "I'm sorry. I didn't recognize that selection."
end
end
def view_cards
puts "For a new item hit enter. Write 'exit' if you want to quit the view."
all_cards.each do |card|
case gets.chomp
when ""
puts card.info
next
when 'exit'
puts "Good-bye!"
break
end
end
end
def game_start
until all_cards.empty?
current_card = all_cards.shift
test_for_feedback(current_card)
end
end
def test_for_feedback(current_card)
test_one_card(current_card) ? (puts right_result(current_card)) : (puts wrong_result(current_card))
end
def right_result(current_card)
correct_answers << current_card
"Good job!"
end
def wrong_result(current_card)
counter = 0
until counter == 2
counter += 1
"Try again!"
return right_result(current_card) if test_one_card(current_card)
end
all_cards << current_card
"The correct answer is #{current_card.term}."
end
def test_one_card(card)
puts "#{card.definition}"
card.match?(gets.chomp)
end
end
my_game = CardGame.new.welcome
require_relative 'card_game'
require_relative 'cards_loader'
describe CardGame do
let(:my_game) { CardGame.new }
context "#initialize" do
it "creates a game object" do
my_game.should be_a(CardGame)
end
it "contains an array of card objects" do
my_game.all_cards.should be_a(Array)
end
it "has the expected amount of cards in the starting_array" do
my_game.all_cards.length.should eq(38)
end
end
context "#welcome" do
before (:each) do
my_game.should_receive(:puts).with("Welcome! Type 'view' to see the cards. Type 'game' to begin the game. Type 'exit' to exit at any time.")
end
it "prints a message" do
my_game.stub(:gets).and_return("")
my_game.should_receive(:puts).with("I'm sorry. I didn't recognize that selection.")
my_game.welcome
end
it "returns the view" do
my_game.stub(:gets).and_return("view")
my_game.should_receive(:view_cards)
my_game.welcome
end
it "starts the game" do
my_game.stub(:gets).and_return("game")
my_game.should_receive(:game_start)
my_game.welcome
end
it "quits the game" do
my_game.stub(:gets).and_return("exit")
my_game.should_receive(:puts).with("Good-bye!")
my_game.welcome
end
end
context "#view_cards" do
it "shows what the user has to press" do
my_game.should_receive(:puts).with("For a new item hit enter. Write 'exit' if you want to quit the view.")
my_game.all_cards.each do |card|
my_game.stub(:gets).and_return("")
my_game.should_receive(:puts).with(card.info)
end
my_game.view_cards
end
end
context "#game_start" do
end
context '#test_for_feedback' do
it "determines that the guess was the correct answer" do
my_game.stub(:test_one_card).and_return(true)
my_game.should_receive(:right_result)
my_game.test_for_feedback(Card.new("blah", "blah"))
end
it "determines that the guess was the incorrect answer" do
my_game.stub(:test_one_card).and_return(false)
my_game.should_receive(:wrong_result)
my_game.test_for_feedback(Card.new("blah", "blah"))
end
end
context '#right_result' do
it "adds the current card to the correct_answers array" do
expect {my_game.right_result(Card.new("blah", "blah"))}.to change(my_game.correct_answers, :count).by(1)
end
it "returns 'Good job!'" do
my_game.right_result(Card.new("blah", "blah")).should eq('Good job!')
end
end
context '#wrong_result' do
it "returns the current card to the all_cards array" do
expect {my_game.wrong_result(Card.new("blah", "blah"))}.to change(my_game.all_cards, :count).by(1)
end
it "returns the definition if the limit is reached" do
my_game.wrong_result(Card.new("blah", "blah")).should match(/The correct answer is/)
end
end
context "#test_one_method" do
let(:first_card) {my_game.all_cards[0]}
let(:random_card) {my_game.all_cards[rand(1...my_game.all_cards.length)]}
before (:each) { my_game.should_receive(:puts).with(first_card.definition) }
it "returns positive message for a correct input to the first definition" do
my_game.stub(:gets).and_return(first_card.term)
my_game.test_one_card(first_card).should be_true
end
it "returns negative message for an incorrect input to the first definition" do
my_game.stub(:gets).and_return(random_card.term)
my_game.test_one_card(first_card).should be_false
end
end
end
require_relative "card"
class CardsLoader
TEXTFILE = "flashcards_data.txt"
def self.all_cards
File.readlines(TEXTFILE).collect do |line|
line_array = line.split(' ')
Card.new(line_array.shift, line_array.join(' '))
end
end
end
require_relative "cards_loader"
require_relative "card_game"
describe CardsLoader do
let(:my_loader) { CardsLoader.new }
it "creates a card loader object" do
my_loader.should be_a(CardsLoader)
end
context '#all_cards' do
it "creates an array of card objects" do
CardsLoader.all_cards.should be_a(Array)
end
it "creates card objects based on data from the textfile" do
CardsLoader.all_cards.length.should eq(38) # 38 are the numbers of lines in the file
end
end
end
equire './card.rb'
require_relative 'cards_loader'
# describe "card.rb" do
describe Card do
let(:my_card) { Cards_Loader.new.all_cards[0] }
context '#initialize' do
it "creates a card object" do
my_card.should be_a(Card)
end
it "contains a term" do
my_card.term.should eq("alias")
end
it "contains a definition" do
my_card.definition.should eq("To create a second name for the variable or method.")
end
end
context '#match?' do
it "determines whether a false guess returns false" do
my_card.match?("variable").should be_false
end
it "determines whether a true guess returns true" do
my_card.match?("alias").should be_true
end
end
context '#info' do
it "returns the term and definition of the card" do
my_card.info.should eq("alias: To create a second name for the variable or method.")
end
end
end
alias To create a second name for the variable or method.
and A command that appends two or more objects together.
BEGIN Designates code that must be run unconditionally at the beginning of the program before any other.
begin Delimits a "begin" block of code, which can allow the use of while and until in modifier position with multi-line statements.
break Gives an unconditional termination to a code block, and is usually placed with an argument.
case starts a case statement; this block of code will output a result and end when it's terms are fulfilled, which are defined with when or else.
class Opens a class definition block, which can later be reopened and added to with variables and even functions.
def Used to define a function.
defined? A boolean logic function that asks whether or not a targeted expression refers to anything recognizable in Ruby; i.e. literal object, local variable that has been initialized, method name visible from the current scope, etc.
do Paired with end, this can delimit a code block, much like curly braces; however, curly braces retain higher precedence.
else Gives an "otherwise" within a function, if-statement, or for-loop, i.e. if cats = cute, puts "Yay!" else puts "Oh, a cat."
elsif Much like else, but has a higher precedence, and is usually paired with terms.
END Designates, via code block, code to be executed just prior to program termination.
end Marks the end of a while, until, begin, if, def, class, or other keyword-based, block-based construct.
ensure Marks the final, optional clause of a begin/end block, generally in cases where the block also contains a rescue clause. The code in this term's clause is guaranteed to be executed, whether control flows to a rescue block or not.
false denotes a special object, the sole instance of FalseClass. false and nil are the only objects that evaluate to Boolean falsehood in Ruby (informally, that cause an if condition to fail.)
for A loop constructor; used in for-loops.
if Ruby's basic conditional statement constructor.
in Used with for, helps define a for-loop.
module Opens a library, or module, within a Ruby Stream.
next Bumps an iterator, or a while or until block, to the next iteration, unconditionally and without executing whatever may remain of the block.
nil A special "non-object"; it is, in fact, an object (the sole instance of NilClass), but connotes absence and indeterminacy. nil and false are the only two objects in Ruby that have Boolean falsehood (informally, that cause an if condition to fail).
not Boolean negation. i.e. not true # false, not 10 # false, not false # true.
or Boolean or. Differs from || in that or has lower precedence.
redo Causes unconditional re-execution of a code block, with the same parameter bindings as the current execution.
rescue Designates an exception-handling clause that can occur either inside a begin<code>/<code>end block, inside a method definition (which implies begin), or in modifier position (at the end of a statement).
retry Inside a rescue clause, causes Ruby to return to the top of the enclosing code (the begin keyword, or top of method or block) and try executing the code again.
return Inside a method definition, executes the ensure clause, if present, and then returns control to the context of the method call. Takes an optional argument (defaulting to nil), which serves as the return value of the method. Multiple values in argument position will be returned in an array.
self The "current object" and the default receiver of messages (method calls) for which no explicit receiver is specified. Which object plays the role of self depends on the context.
super Called from a method, searches along the method lookup path (the classes and modules available to the current object) for the next method of the same name as the one being executed. Such method, if present, may be defined in the superclass of the object's class, but may also be defined in the superclass's superclass or any class on the upward path, as well as any module mixed in to any of those classes.
then Optional component of conditional statements (if, unless, when). Never mandatory, but allows for one-line conditionals without semi-colons.
true The sole instance of the special class TrueClass. true encapsulates Boolean truth; however, <emph>all</emph> objects in Ruby are true in the Boolean sense (informally, they cause an if test to succeed), with the exceptions of false and nil.
undef Undefines a given method, for the class or module in which it's called. If the method is defined higher up in the lookup path (such as by a superclass), it can still be called by instances classes higher up.
unless The negative equivalent of if. i.e. unless y.score > 10 puts "Sorry; you needed 10 points to win." end.
until The inverse of while: executes code until a given condition is true, i.e., while it is not true. The semantics are the same as those of while.
when Same as case.
while Takes a condition argument, and executes the code that follows (up to a matching end delimiter) while the condition is true.
yield Called from inside a method body, yields control to the code block (if any) supplied as part of the method call. If no code block has been supplied, calling yield raises an exception.
# Your code here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment