diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..e43b0f988 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/flashcard_runner.rb b/flashcard_runner.rb new file mode 100644 index 000000000..53462bc84 --- /dev/null +++ b/flashcard_runner.rb @@ -0,0 +1,25 @@ +require 'pry' +require './lib/card.rb' +require './lib/turn.rb' +require './lib/deck.rb' +require './lib/round.rb' + +card_1 = Card.new("Who is leading the NBA in points per game?", "Luka Doncic", :"Basic Stats") +card_2 = Card.new("What team is leading the NBA in points per game?", "Indiana Pacers", :"Team Stats") +card_3 = Card.new("Who is leading the NBA in Box Plus/Minus?", "Nikola Jokic", :"Advanced Stats") +card_4 = Card.new("Who is leading the NBA in VORP?", "Nikola Jokic", :"Advanced Stats") +card_5 = Card.new("How many assists per game is Tyrese Halliburton averaging?", 12, :"Basic Stats") +card_6 = Card.new("What team has most wins in the NBA?", "Boston Celtics", :"Team Stats") +card_7 = Card.new("Who is the worst team in the NBA?", "Detroit Pistons", :"Team Stats") +card_8 = Card.new("Who is the reigning NBA champion?", "Denver Nuggets", :"Team Stats") +card_9 = Card.new("Who is the all time leader in points scored?", "Lebron James", :"Basic Stats") +card_10 = Card.new("Who has the highest effective field goal percentage?", "Dereck Lively", :"Advanced Stats") + +cards = [card_1, card_2, card_3, card_4, card_5, card_6, card_7, card_8, card_9, card_10] + +deck = Deck.new(cards) + +round = Round.new(deck) + +round.start +round.summary diff --git a/lib/card.rb b/lib/card.rb index e69de29bb..7b8b37c1b 100644 --- a/lib/card.rb +++ b/lib/card.rb @@ -0,0 +1,11 @@ +class Card + attr_reader :question, + :answer, + :category + + def initialize(question, answer, category) + @question = question + @answer = answer + @category = category + end +end diff --git a/lib/deck.rb b/lib/deck.rb new file mode 100644 index 000000000..2b9dc55d2 --- /dev/null +++ b/lib/deck.rb @@ -0,0 +1,15 @@ +class Deck + attr_reader :cards + + def initialize(cards) + @cards = cards + end + + def count + @cards.count + end + + def cards_in_category(category) + cards.select { |card| card.category == category} + end +end \ No newline at end of file diff --git a/lib/round.rb b/lib/round.rb new file mode 100644 index 000000000..f0e4f0b54 --- /dev/null +++ b/lib/round.rb @@ -0,0 +1,66 @@ +class Round + attr_reader :deck, :turns, :number_correct + + def initialize(deck) + @deck = deck + @turns = [] + @number_correct = 0 + end + + def current_card + @deck.cards[0] + end + + def take_turn(guess) + current_turn = Turn.new(guess, current_card) + puts current_turn.feedback + @turns << current_turn + if current_turn.correct? + @number_correct += 1 + else + end + @deck.cards.rotate! + current_turn + end + + def number_correct_by_category(category) + @turns.count { |turn| turn.card.category == category && turn.correct?} + end + + def percent_correct + ((@number_correct.to_f / @turns.count) * 100).round(2) + end + + def percent_correct_by_category(category) + category_correct = @turns.count { |turn| turn.card.category == category && turn.correct?} + category_count = @turns.count { |turn| turn.card.category == category} + ((category_correct.to_f / category_count) * 100).round(2) + end + + def start + puts "Welcome! You're playing with #{deck.count} cards." + puts "-------------------------------------" + + deck.cards.each do + puts "This is card number #{turns.count+1} out of #{deck.count}" + puts "Question: #{current_card.question}" + + guess = gets.chomp + take_turn(guess) + end + end + + def summary + puts "***** Game over! *****" + + puts "You had #{number_correct} correct guesses out of #{deck.count} for a total score of #{percent_correct}%." + category_array = [] + turns.each do |turn| + category = turn.card.category + category_array << category + end + category_array.uniq.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..094f5e3ce --- /dev/null +++ b/lib/turn.rb @@ -0,0 +1,21 @@ +class Turn + attr_reader :guess, + :card + + def initialize(guess, card) + @guess = guess + @card = card + end + + def correct? + @guess == card.answer + end + + def feedback + if correct? == true + "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..0c159e24b 100644 --- a/spec/card_spec.rb +++ b/spec/card_spec.rb @@ -1,27 +1,32 @@ require './lib/card' +require 'rspec' + +RSpec.configure do |config| + config.formatter = :documentation +end RSpec.describe Card do - it 'exists' do - card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + it 'exists' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) - expect(card).to be_instance_of(Card) - end + expect(card).to be_instance_of(Card) + end - it 'has a question' do - card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + it 'has a question' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) - expect(card.question).to eq("What is the capital of Alaska?") - end + 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) + it 'has an answer' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) - expect(card.answer).to eq("Juneau") - end + expect(card.answer).to eq("Juneau") + end - it 'has a category' do - card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + it 'has a category' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) - expect(card.category).to eq(:Geography) - end + 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..9e28d5395 --- /dev/null +++ b/spec/deck_spec.rb @@ -0,0 +1,76 @@ +require './lib/card' +require './lib/deck' +require 'rspec' + +RSpec.configure do |config| + config.formatter = :documentation +end + +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 'is an array' 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 be(cards) + end + + it 'returns how many cards there are' 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(cards.count).to eq(3) + end + + it 'returns cards from a specific 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) + + stem_cards = deck.cards_in_category(:STEM) + geography_cards = deck.cards_in_category(:Geography) + + expect(stem_cards).to eq([card_2, card_3]) + expect(geography_cards).to eq([card_1]) + end + + it 'returns an empty array if an invalid category is entered' 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) + + pop_culture_cards = deck.cards_in_category("Pop Culture") + + expect(pop_culture_cards).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..205f62bee --- /dev/null +++ b/spec/round_spec.rb @@ -0,0 +1,248 @@ +require 'rspec' +require 'pry' +require './lib/card' +require './lib/turn' +require './lib/deck' +require './lib/round' + + +RSpec.configure do |config| + config.formatter = :documentation +end + +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) + + cards = [card_1, card_2, card_3] + + deck = Deck.new(cards) + + round = Round.new(deck) + + expect(round).to be_a(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) + + cards = [card_1, card_2, card_3] + + deck = Deck.new(cards) + + round = Round.new(deck) + + expect(round.deck).to be deck + end + + it 'starts with an empty turns array' 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) + + expect(round.turns).to eq [] + end + + it 'returns the current card when the current_card method is called' 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) + + expect(round.current_card).to eq card_1 + end + + it 'can take a 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) + + cards = [card_1, card_2, card_3] + + deck = Deck.new(cards) + + round = Round.new(deck) + + new_turn = round.take_turn("Juneau") + + expect(new_turn). to be_a Turn + end + + it 'can tell you if the guess 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) + + cards = [card_1, card_2, card_3] + + deck = Deck.new(cards) + + round = Round.new(deck) + + new_turn = round.take_turn("Juneau") + + expect(new_turn.correct?). to be true + + new_turn = round.take_turn("Venus") + + expect(new_turn.correct?). to be false + end + + it 'adds the last turn to the turns array' 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) + + expect(round.turns). to eq [] + + turn_1 = round.take_turn("Juneau") + + expect(round.turns).to eq ([turn_1]) + + turn_2 = round.take_turn("Venus") + + expect(round.turns).to eq ([turn_1, turn_2]) + expect(round.turns.count). to eq 2 + end + + it 'returns how many guesses 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) + + cards = [card_1, card_2, card_3] + + deck = Deck.new(cards) + + round = Round.new(deck) + + expect(round.number_correct).to be 0 + + turn_1 = round.take_turn("Juneau") + + expect(round.number_correct).to be 1 + end + + it 'moves a new card to the front of the deck after taking a 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) + + cards = [card_1, card_2, card_3] + + deck = Deck.new(cards) + + round = Round.new(deck) + + expect(round.current_card).to eq card_1 + + turn_1 = round.take_turn("Juneau") + + expect(round.current_card).to eq card_2 + + turn_2 = round.take_turn("Venus") + + expect(round.current_card).to eq card_3 + end + + it 'returns how many guesses are correct 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) + + round = Round.new(deck) + + turn_1 = round.take_turn("Juneau") + turn_2 = round.take_turn("Venus") + + geography_correct = round.number_correct_by_category(:Geography) + stem_correct = round.number_correct_by_category(:STEM) + + expect(geography_correct).to eq 1 + expect(stem_correct).to eq 0 + end + + it 'returns feedback for last guess' 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) + + turn_1 = round.take_turn("Juneau") + turn_2 = round.take_turn("Venus") + + feedback = round.turns.last.feedback + + expect(feedback).to eq "Incorrect." + end + + it 'returns returns what percent of the round that has been answered correctly' 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) + + turn_1 = round.take_turn("Juneau") + turn_2 = round.take_turn("Venus") + + expect(round.percent_correct).to eq 50.0 + end + + it 'returns returns what percent of answers in a category 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) + + cards = [card_1, card_2, card_3] + + deck = Deck.new(cards) + + round = Round.new(deck) + + turn_1 = round.take_turn("Juneau") + turn_2 = round.take_turn("Venus") + + expect(round.percent_correct_by_category(:Geography)).to eq 100.0 + end +end + + + +# A Round will be the object that processes responses and records guesses. A Round is initialized with a Deck. The idea is that when we start a Round, the current card is the first in the deck (the first in the Deck’s array of Cards). When we make a guess, the guess is recorded, and the next card in the deck becomes the current card. + +# The take_turn method is the crux of this problem. The take_turn method takes a string representing the guess. It should create a new Turn object with the appropriate guess and Card. It should store this new Turn, as well as return it from the take_turn method. Also, when the take_turn method is called, the Round should move on to the next card in the deck. \ No newline at end of file diff --git a/spec/turn_spec.rb b/spec/turn_spec.rb new file mode 100644 index 000000000..0bd4031bc --- /dev/null +++ b/spec/turn_spec.rb @@ -0,0 +1,48 @@ +require './lib/turn' +require './lib/card' +require 'rspec' + +RSpec.configure do |config| + config.formatter = :documentation +end + +RSpec.describe Turn do + it 'exists' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + turn = Turn.new("guess", card) + + expect(turn).to be_instance_of(Turn) + end + + it 'returns a guess' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + turn = Turn.new("Denver", card) + + expect(turn.guess).to eq "Denver" + end + + it 'returns a card' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + turn = Turn.new("Denver", card) + + expect(turn.card).to eq(card) + end + + it 'returns if guess is correct or incorrect' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + incorrect_guess = Turn.new("Denver", card) + correct_guess = Turn.new("Juneau", card) + + expect(incorrect_guess.correct?).to eq false + expect(correct_guess.correct?).to eq true + end + + it 'returns feedback based on guess' do + card = Card.new("What is the capital of Alaska?", "Juneau", :Geography) + incorrect_guess = Turn.new("Denver", card) + correct_guess = Turn.new("Juneau", card) + + expect(incorrect_guess.feedback).to eq "Incorrect." + expect(correct_guess.feedback).to eq "Correct!" + end +end \ No newline at end of file