diff --git a/lib/card.rb b/lib/card.rb index e69de29bb..7af002805 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..dc256d8a7 --- /dev/null +++ b/lib/deck.rb @@ -0,0 +1,18 @@ +class Deck + attr_reader :cards + + def initialize(cards) + @cards=cards + end + + def count + @cards.count + end + + def cards_in_category(cat) + cards.filter_map do |card| + card if card.category == cat + 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..d62b4ba6a --- /dev/null +++ b/lib/flashcard_runner.rb @@ -0,0 +1,13 @@ +require './lib/turn' +require './lib/card' +require './lib/deck' +require './lib/round' + +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) +card_4 = Card.new("Lisa Vanderpump starred on what reality show before getting her own show, Vanderpump Rules?(Acroynm please)", "Rhobh", "Pop Culture") + +deck = Deck.new([card_1, card_2, card_3,card_4]) +round = Round.new(deck) +round.start diff --git a/lib/round.rb b/lib/round.rb new file mode 100644 index 000000000..2eaec7b86 --- /dev/null +++ b/lib/round.rb @@ -0,0 +1,81 @@ +class Round + attr_reader :deck, + :turns, + :number_correct, + :percent_correct, + :unique_categories + + + def initialize(deck) + @deck=deck + @turns=[] + @number_correct=0 + @percent_correct=0 + @unique_categories=[] + + end + + def current_card + @deck.cards[@turns.count] + end + + def take_turn(guess) + new_turn = Turn.new(guess,current_card) + if new_turn.correct? == true + @number_correct +=1 + end + @turns << new_turn + @percent_correct= ((@number_correct).to_f/ @turns.count)*100 + new_turn + end + + def number_correct_by_category(cat) + num_correct_by_cat=0 + @turns.each do |turn| + if turn.card.category == cat + if turn.guess == turn.card.answer + num_correct_by_cat +=1 + end + end + end + num_correct_by_cat + end + + def percent_correct_by_category(cat) + total_by_cat=0 + @turns.each do |turn| + if turn.card.category == cat + total_by_cat +=1 + end + end + (((number_correct_by_category(cat)).to_f)/total_by_cat)*100 + end + + def get_unique_categories + deck.cards.each do |card| + @unique_categories << card.category + end + @unique_categories.uniq! + end + + def start + puts "Welcome! You're playing with #{deck.count} cards" + puts "------------------------------------------------" + + deck.cards.each do |card| + puts "This is card number #{deck.cards.index(card)+1} out of #{deck.count}. " + puts "Question: #{card.question}" + guess = gets.chomp + take_turn(guess) + puts turns.last.feedback + end + + puts "**** Game over! ****" + puts "You had #{number_correct} out of #{turns.count} for a total score of #{percent_correct}%." + + get_unique_categories + @unique_categories.each do |category| + puts "#{category} - #{(percent_correct_by_category(category)).round}% correct." + end + end +end \ No newline at end of file diff --git a/lib/turn.rb b/lib/turn.rb new file mode 100644 index 000000000..568646556 --- /dev/null +++ b/lib/turn.rb @@ -0,0 +1,21 @@ +class Turn + attr_reader :guess, + :card + + def initialize(guess,card) + @guess = guess.capitalize + @card = card + end + + def correct? + @guess == @card.answer + end + + def feedback + if @guess == @card.answer + "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..9ae3fad89 100644 --- a/spec/card_spec.rb +++ b/spec/card_spec.rb @@ -3,25 +3,13 @@ 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) end - it 'has a question' do + it 'has a question, answer, and category' do card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) - 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") - end - - it 'has a category' do - card = Card.new("What is the capital of Alaska?", "Juneau", :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..ed3675aab --- /dev/null +++ b/spec/deck_spec.rb @@ -0,0 +1,34 @@ +require './lib/card' +require './lib/deck' + +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 'can return the count of cards in the 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) + cards = [card_1, card_2, card_3] + deck = Deck.new(cards) + expect(deck.count).to eq(3) + end + + it 'can give card info 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) + cards = [card_1, card_2, card_3] + deck = Deck.new(cards) + expect(deck.cards_in_category(:STEM).count).to eq(2) + expect(deck.cards_in_category(:Geography).count).to eq(1) + expect(deck.cards_in_category("Pop Culture").count).to eq(0) + 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..4985e9f6b --- /dev/null +++ b/spec/round_spec.rb @@ -0,0 +1,120 @@ +require './lib/turn' +require './lib/card' +require './lib/deck' +require './lib/round' + +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 'has a 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 a turn array and can take turn' 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.turns).to eq([]) + + new_turn=round.take_turn("Juneau") + expect(round.turns.count).to eq(1) + end + + it 'can display current card in the 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.current_card).to eq(card_1) + + new_turn=round.take_turn("Juneau") + expect(round.current_card).to eq(card_2) + end + + it 'can make a new Turn instance' 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.class).to eq(Turn) + end + + it 'lets you know how many questions are 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(round.number_correct).to eq(1) + expect(round.current_card).to eq(card_2) + end + + it 'moves on to turn 2'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") + round.take_turn("Venus") + expect(round.turns.count).to eq(2) + end + + it 'can give feedback' 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(round.turns.last.feedback).to eq("Correct!") + + round.take_turn("Venus") + expect(round.turns.last.feedback).to eq("Incorrect.") + end + + it 'provides number correct and 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) + new_turn=round.take_turn("Juneau") + round.take_turn("Venus") + expect(round.number_correct_by_category(:Geography)).to eq(1) + expect(round.number_correct_by_category(:STEM)).to eq(0) + expect(round.percent_correct).to eq(50.0) + expect(round.percent_correct_by_category(:Geography)).to eq(100.0) + expect(round.percent_correct_by_category(:STEM)).to eq(0.0) + + round.take_turn("North north west") + expect(round.percent_correct_by_category(:STEM)).to eq(50.0) + end + + it 'creates array of unique categories' 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.get_unique_categories).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..2cc365407 --- /dev/null +++ b/spec/turn_spec.rb @@ -0,0 +1,40 @@ +require './lib/turn' +require './lib/card' + +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_instance_of(Turn) + end + + it 'has a guess and card' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + turn = Turn.new("Juneau", card) + expect(turn.guess). to eq("Juneau") + expect(turn.card). to eq(card) + end + + it 'checks if the 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 'checks if the 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 on the test' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + turn = Turn.new("Juneau", card) + expect(turn.feedback). to eq("Correct!") + + 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