diff --git a/lib/card.rb b/lib/card.rb index e69de29bb..4fbd91f0d 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..cfcd4bfd8 --- /dev/null +++ b/lib/deck.rb @@ -0,0 +1,17 @@ + +class Deck + attr_reader :cards + + def initialize(cards) + @cards = cards + end + + def count + cards.length + end + + def cards_in_category(category) + cards.select {|card| card.category == category} + 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..9367362ff --- /dev/null +++ b/lib/flashcard_runner.rb @@ -0,0 +1,53 @@ +require_relative 'card' +require_relative 'deck' +require_relative 'round' +require_relative 'turn' + + +card_1 = Card.new("What is 5 + 5?", "10", :STEM) +card_2 = Card.new("What is Rachel's favorite animal?", "panda", :Turing_Staff) +card_3 = Card.new("What is Mike's middle name?", "nobody knows", :Turing_Staff) +card_4 = Card.new("What cardboard cutout lives at Turing?", "Justin Bieber", :Pop_Culture) + +card_5 = Card.new("What is the method to return a boolean on whether an object is even", ".even?", :Ruby) +card_6 = Card.new("What is the return of 2.odd?", "false", :Ruby) +card_7 = Card.new("What does a @ represent before a variable?", 'instance variable', :Ruby) +card_8 = Card.new("What does a method ending in a ? return?", "boolean", :Ruby) + +deck = Deck.new([card_1, card_2, card_3, card_4]) +deck_2 = Deck.new([card_5, card_6, card_7, card_8]) + +round = Round.new(deck) +round_2 = Round.new(deck_2) + + +def start(round) + puts "Welcome! You're playing with #{round.deck.cards.count} cards." + puts "-------------------------------------------------" + + round.deck.cards.each_with_index do |card, index| + puts "This is card number #{index + 1} out of #{round.deck.cards.count}." + puts "Question: #{card.question}" + + print "> " + guess = gets.chomp + turn = round.take_turn(guess) + + puts turn.feedback + end + + puts "****** Game over! ******" + puts "You had #{round.number_correct} correct guesses out of #{round.turns.count} for a total score of #{round.percent_correct}%." + + round.deck.cards.map{ |card| card.category }.uniq.each do |category| + puts "#{category} - #{round.percent_correct_by_category(category)}% correct" + end + puts round.percent_correct +end + + +start(round) +start(round_2) + + + diff --git a/lib/round.rb b/lib/round.rb new file mode 100644 index 000000000..91577f381 --- /dev/null +++ b/lib/round.rb @@ -0,0 +1,42 @@ +class Round + attr_accessor :deck, :turns + + def initialize(deck) + @deck = deck + @turns = [] + @current_card_index = 0 + end + + def current_card + deck.cards[@current_card_index] + end + + def take_turn(string) + new_turn = Turn.new(string, current_card) + @turns << new_turn + @current_card_index += 1 + new_turn + end + + def number_correct + @turns.count(&:correct?) + end + + def percent_correct + return 0 if @turns.empty? + + (number_correct.to_f / @turns.size * 100).round(2) + end + + def number_correct_by_category(category) + @turns.count { |turn| turn.card.category == category && turn.correct? } + end + + def percent_correct_by_category(category) + turns_in_category = @turns.select { |turn| turn.card.category == category } + return 0 if turns_in_category.empty? + + correct_in_category = turns_in_category.count(&:correct?) + (correct_in_category.to_f / turns_in_category.size * 100).round(2) + end +end \ No newline at end of file diff --git a/lib/turn.rb b/lib/turn.rb new file mode 100644 index 000000000..cf5b036db --- /dev/null +++ b/lib/turn.rb @@ -0,0 +1,22 @@ +class Turn + attr_reader :string, :card, :guess + + def initialize(string, card) + @string = string + @card = card + end + + def guess + return string + end + + def correct? + guess == card.answer + end + + def feedback + correct? ? 'Correct!' : 'Incorrect.' + end + + +end diff --git a/spec/card_spec.rb b/spec/card_spec.rb index 84a45a7a6..b55348d8c 100644 --- a/spec/card_spec.rb +++ b/spec/card_spec.rb @@ -1,4 +1,9 @@ -require './lib/card' +require_relative '../lib/card' +require_relative '../lib/turn' +require_relative '../lib/deck' +require_relative '../lib/round' +require 'pry' + RSpec.describe Card do it 'exists' do diff --git a/spec/deck_spec.rb b/spec/deck_spec.rb new file mode 100644 index 000000000..823117630 --- /dev/null +++ b/spec/deck_spec.rb @@ -0,0 +1,52 @@ +require_relative '../lib/card' +require_relative '../lib/turn' +require_relative '../lib/deck' +require_relative '../lib/round' +require 'pry' + + +RSpec.describe Deck do + it 'exists' 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) + + expect(deck).to be_instance_of(Deck) + end + + it 'has cards' 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) + + expect(deck.cards).to eq(cards) + end + + it 'has cards in category' 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) + + expect(deck.cards_in_category(:STEM)).to eq([card_2, card_3]) + end + it 'has cards in category' 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) + + expect(deck.cards_in_category("Pop Culture")).to eq([]) + end + end + \ No newline at end of file diff --git a/spec/round_spec.rb b/spec/round_spec.rb new file mode 100644 index 000000000..c1dbb106a --- /dev/null +++ b/spec/round_spec.rb @@ -0,0 +1,192 @@ +require_relative '../lib/card' +require_relative '../lib/turn' +require_relative '../lib/deck' +require_relative '../lib/round' +require 'pry' + + +RSpec.describe Round do + + it 'exists' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + + expect(round).to be_instance_of(Round) + end + + it 'stores deck' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + + expect(round.deck).to eq(deck) + end + + it 'has the current card' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + + expect(round.current_card).to eq(card_1) + end + + + it 'that it is correct' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + new_turn = round.take_turn("Juneau") + + expect(new_turn.correct?).to eq(true) + end + + xit 'test round count' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + round.take_turn("Juneau") + round.take_turn("Venus") + + expect(round.turns).to eq(card_2) + end + + it 'number correct in round' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + + round.take_turn("Juneau") + + expect(round.number_correct).to eq(1) + + round.take_turn("Venus") + + expect(round.number_correct).to eq(1) + end + + it 'shows the current card' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + round.take_turn("Juneau") + + expect(round.current_card).to eq(card_2) + end + + it 'counts the turns' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + round.take_turn("Juneau") + round.take_turn("Venus") + + expect(round.turns.count).to eq(2) + end + + it 'correct number' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + round.take_turn("Juneau") + round.take_turn("Venus") + + expect(round.number_correct).to eq(1) + end + + it 'correct category guesses' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + round.take_turn("Juneau") + round.take_turn("Venus") + + + expect(round.number_correct_by_category(:Geography)).to eq(1) + end + + it 'correct number in category' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + round.take_turn("Juneau") + round.take_turn("Venus") + + expect(round.number_correct_by_category(:STEM)).to eq(0) + end + + it 'percent right' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + round.take_turn("Juneau") + round.take_turn("Venus") + + expect(round.percent_correct).to eq(50.0) + end + + it 'percent by category' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + round.take_turn("Juneau") + round.take_turn("Venus") + + expect(round.percent_correct_by_category(:Geography)).to eq(100.0) + expect(round.percent_correct_by_category(:STEM)).to eq(0.0) + end + + it 'current card' 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) + + deck = Deck.new([card_1, card_2, card_3]) + round = Round.new(deck) + round.take_turn("Juneau") + round.take_turn("Venus") + + expect(round.current_card).to eq(card_3) + end + + +end diff --git a/spec/turn_spec.rb b/spec/turn_spec.rb new file mode 100644 index 000000000..e8507a881 --- /dev/null +++ b/spec/turn_spec.rb @@ -0,0 +1,56 @@ +require './lib/card.rb' +require './lib/turn.rb' +require './lib/deck.rb' +require './lib/round.rb' +require 'pry' + +RSpec.describe Turn do + it "exists" do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + turn = Turn.new("Juneau", card) + + expect(turn).to be_a(Turn) + end + + it "has a card" do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + turn = Turn.new("Juneau", card) + + expect(turn.card).to eq(card) + end + + it "has a guess" do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + turn = Turn.new("Juneau", card) + + expect(turn.guess).to eq("Juneau") + end + + it "can check if a guess is correct" do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + turn = Turn.new("Juneau", card) + + expect(turn.correct?).to eq(true) + end + + it "can check if a guess is incorrect" do + card = Card.new("Which planet is closest to the sun?", "Mercury", :STEM) + turn = Turn.new("Saturn", card) + + expect(turn.correct?).to eq(false) + end + + it "provides feedback for a correct guess" do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + turn = Turn.new("Juneau", card) + + expect(turn.feedback).to eq("Correct!") + end + + it "provides feedback for an incorrect guess" do + card = Card.new("Which planet is closest to the sun?", "Mercury", :STEM) + turn = Turn.new("Saturn", card) + + expect(turn.feedback).to eq("Incorrect.") + end + end \ No newline at end of file