diff --git a/lib/card.rb b/lib/card.rb index e69de29bb..142b66f70 100644 --- a/lib/card.rb +++ b/lib/card.rb @@ -0,0 +1,10 @@ + +class Card + attr_reader :question, :answer, :category + + def initialize(question, answer, category) + @question = question + @answer = answer + @category = category + end +end \ No newline at end of file diff --git a/lib/deck.rb b/lib/deck.rb new file mode 100644 index 000000000..60d9595c6 --- /dev/null +++ b/lib/deck.rb @@ -0,0 +1,17 @@ +class Deck + attr_reader :card, :cards + + def initialize(cards) + @cards = cards + end + + def count + @cards.length + end + + def cards_in_category(category) + @cards.select do |card| + category == card.category + end + end +end \ No newline at end of file diff --git a/lib/flashcard_runner.rb b/lib/flashcard_runner.rb new file mode 100644 index 000000000..8d3dbd3d9 --- /dev/null +++ b/lib/flashcard_runner.rb @@ -0,0 +1,34 @@ +require_relative './card.rb' +require_relative './turn.rb' +require_relative './round.rb' +require_relative './deck.rb' + + +@card_1 = Card.new("What is the capital of Alaska?", "Juneau", :Geography) +@card_2 = Card.new("The Viking spacecraft sent back to Earth photographs and reports about the surface of which planet?", "Mars", :STEM) +@card_3 = Card.new("Describe in words the exact direction that is 697.5° clockwise from due north?", "North north west", :STEM) +@cards = [@card_1, @card_2, @card_3] +@deck = Deck.new(@cards) +@turn = Turn.new(@guess, @card) +@round = Round.new(@deck) + +def start + @round.deck + @round.turns + puts "Welcome! You're playing with #{@cards.length} cards." + puts "----------------------------------------------------------" + + @round.deck.cards.length.times do + puts "This is card number #{@round.turn_number} out of #{@cards.length}." + puts "Question: #{@round.current_card.question}" + user_input = gets.chomp + new_turn = @round.take_turn(user_input) + puts new_turn.feedback + end + + puts "****** Game over! ******" + puts "You had #{@round.number_correct} correct guesses out of #{@cards.length} for a total score of #{@round.percent_correct}." + @round.return_percent_correct_by_category +end + +start diff --git a/lib/round.rb b/lib/round.rb new file mode 100644 index 000000000..86fcc8fdd --- /dev/null +++ b/lib/round.rb @@ -0,0 +1,68 @@ +require_relative './turn.rb' +require_relative './card.rb' + +class Round + attr_reader :deck, :turns, :current_card, :user_guesses, :number_correct, :card_number, :number_correct_by_category, :percent_correct, :percent_correct_by_category, :turn_number, :new_turn_category, :new_card + + def initialize(deck) + @deck = deck + @turns = [] + @card_number = 0 + @turn_number = 1 + @number_correct = 0 + @number_correct_by_category = 0 + @user_guesses = [] + @current_card = current_card + @percent_correct = 0.0 + @percent_correct_by_category = 0 + @correct_by_category = Hash.new(0) + + end + + def current_card + @current_card = @deck.cards[@card_number] + end + + def take_turn(guess) + new_turn = Turn.new(guess, current_card) + @turns. << new_turn + user_guesses << new_turn.correct? #returns true/false boolean + + if new_turn.correct? + @number_correct += 1 + @correct_by_category[current_card.category] += 1 #@correct_by_category hash tracks the number of correct guesses by category + end + + @card_number += 1 + @turn_number += 1 + new_turn + end + + def percent_correct + @percent_correct = ((@number_correct.to_f / @turns.length) * 100).round(2) + end + + def number_correct_by_category(category) + @correct_by_category[category] #this pulls the category argument into the correct_by_category hash to identify the number of correct answers per category + end + + def percent_correct_by_category(category) + total_in_category = @turns.count { |turn| turn.card.category == category} #use block code here - |turn| is the argument, and the block iterates over each turn in the @turns array + #identifies whether the category of the card associated with each turn in the @turns array matches the cateegory of the argument passed into the method + + return 0 if total_in_category == 0 + ((number_correct_by_category(category).to_f / total_in_category) * 100).round(2) + + end + + def unique_categories + @deck.cards.map(&:category).uniq + end + + def return_percent_correct_by_category + unique_categories.each do |category| + puts "#{category}: #{percent_correct_by_category(category)}% correct" + end + end + +end diff --git a/lib/turn.rb b/lib/turn.rb new file mode 100644 index 000000000..259d36e02 --- /dev/null +++ b/lib/turn.rb @@ -0,0 +1,21 @@ +class Turn + attr_reader :card, :guess + + def initialize(guess, card) + @guess = guess + @card = card + end + + def correct? + return true if card.answer.downcase == guess.downcase + false + end + + def feedback + if correct? + "Correct!" + else + "Incorrect." + end + end +end \ No newline at end of file diff --git a/spec/card_spec.rb b/spec/card_spec.rb index 84a45a7a6..58c054e7e 100644 --- a/spec/card_spec.rb +++ b/spec/card_spec.rb @@ -1,27 +1,24 @@ require './lib/card' RSpec.describe Card do - it 'exists' do - card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) - expect(card).to be_instance_of(Card) + before(:each) do + @card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) end - it 'has a question' do - card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + it 'exists' do + expect(@card).to be_instance_of(Card) + end - expect(card.question).to eq("What is the capital of Alaska?") + it 'has a question' do + expect(@card.question).to eq("What is the capital of Alaska?") end it 'has an answer' do - card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) - - expect(card.answer).to eq("Juneau") + expect(@card.answer).to eq("Juneau") end it 'has a category' do - card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) - - expect(card.category).to eq(:Geography) + expect(@card.category).to eq(:Geography) end end diff --git a/spec/deck_spec.rb b/spec/deck_spec.rb new file mode 100644 index 000000000..7c346f0e7 --- /dev/null +++ b/spec/deck_spec.rb @@ -0,0 +1,30 @@ +require './lib/card' +require './lib/deck' + +RSpec.describe Deck do + + before(:each) do + @card_1 = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + @card_2 = Card.new("The Viking spacecraft sent back to Earth photographs and reports about the surface of which planet?", "Mars", :STEM) + @card_3 = Card.new("Describe in words the exact direction that is 697.5° clockwise from due north?", "North north west", :STEM) + @cards = [@card_1, @card_2, @card_3] + @deck = Deck.new(@cards) + end + + it 'exists' do + expect(@deck).to be_instance_of(Deck) + end + + it 'loads cards into the deck' do + expect(@deck.cards).to eq(@cards) + end + + it 'counts the number of cards in the deck' do + expect(@deck.count).to eq(3) + end + + it 'identifies all of the cards in each category' do + expect(@deck.cards_in_category(:STEM)).to eq([@card_2, @card_3]) + expect(@deck.cards_in_category(:Geography)).to eq([@card_1]) + end +end diff --git a/spec/round_spec.rb b/spec/round_spec.rb new file mode 100644 index 000000000..357e1fe47 --- /dev/null +++ b/spec/round_spec.rb @@ -0,0 +1,70 @@ +require './lib/card' +require './lib/turn' +require './lib/deck' +require './lib/round' + +RSpec.describe Round do + + before(:each) do + + @card_1 = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + @card_2 = Card.new("The Viking spacecraft sent back to Earth photographs and reports about the surface of which planet?", "Mars", :STEM) + @card_3 = Card.new("Describe in words the exact direction that is 697.5° clockwise from due north?", "North north west", :STEM) + @cards = [@card_1, @card_2, @card_3] + @deck = Deck.new(@cards) + @round = Round.new(@deck) + + end + + it 'exists' do + expect(@round).to be_instance_of(Round) + end + + it 'loads the deck into the round' do + expect(@round.deck).to eq(@deck) + end + + it 'starts the turn with an empty turns array' do + expect(@round.turns).to eq([]) + end + + it 'pulls the current card in the deck' do + expect(@round.current_card).to eq(@card_1) + end + + it 'logs each new turn into the turns array' do + new_turn = @round.take_turn("Juneau") + + expect(@round.turns.last).to eq(new_turn) + end + + it 'add correct answers to the number correct counter' do + new_turn = @round.take_turn("Juneau") + + expect(@round.number_correct).to eq(1) + end + + it 'identifies the percent of correct answers per round' do + new_turn = @round.take_turn("Juneau") + + expect(@round.percent_correct).to eq(100) + end + + it 'identifies the number of correct answers in the round by category' do + new_turn = @round.take_turn("Juneau") + + expect(@round.number_correct_by_category(:Geography)).to eq(1) + end + + it 'calculates the percent of correct answers in the round by category' do + new_turn = @round.take_turn("Juneau") + + expect(@round.percent_correct_by_category(:Geography)).to eq(100) + end + + it 'identifies each unique category in the round' do + new_turn = @round.take_turn("Juneau") + + expect(@round.deck.cards.map(&:category).uniq).to eq([:Geography, :STEM]) + end +end \ No newline at end of file diff --git a/spec/turn_spec.rb b/spec/turn_spec.rb new file mode 100644 index 000000000..32d68f563 --- /dev/null +++ b/spec/turn_spec.rb @@ -0,0 +1,46 @@ +require './lib/card' +require './lib/turn' + +RSpec.describe Turn do + + before(:each) do + @card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + end + + it 'exists' do + turn = Turn.new("Juneau", @card) + + expect(turn).to be_instance_of(Turn) + end + + it 'is Juneau by default' do + turn = Turn.new("Juneau", @card) + + expect(turn.guess).to eq("Juneau") + end + + it "does not have to be Juneau" do + turn = Turn.new("Spain", @card) + + expect(turn.guess).to eq("Spain") + end + + it 'returns the Card' do + turn = Turn.new("Juneau", @card) + + expect(turn.card).to eq(@card) + end + + it 'indicates if the guess matched the answer on the card' do + turn = Turn.new("Juneau", @card) + + expect(turn.correct?).to eq(true) + end + + it 'provides feedback' do + turn = Turn.new("Juneau", @card) + + expect(turn.feedback).to eq("Correct!") + end + +end