Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joseph Bloom - Flash Cards #630

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Binary file added .DS_Store
Binary file not shown.
37 changes: 37 additions & 0 deletions flashcard_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require './lib/round'
require './lib/card'
require './lib/turn'
require './lib/deck'


card_1 = Card.new("Question: What is 5 + 5?", "10", :STEM)
card_2 = Card.new("Question: What is Rachel's favorite animal?", "Turkey", :TuringStaff)
card_3 = Card.new("Question: What is Mike's middle name?", "nobody knows", :TuringStaff)
card_4 = Card.new("Question: What cardboard cutout lives at Turing?", "Justin Bieber", :PopCulture)

@cards = [card_1, card_2, card_3, card_4]

@deck = Deck.new(cards)

@round = Round.new(deck)

def start
@round
p "Welcome! You're playing with 4 cards.
-------------------------------------------------
This is card number" @turns_taken "out of "@cards.count"."
cards(card_1(@question))

end

start

def game_over
if current_card == @round.deck.cards.card_1
p "****** Game over! ******"
"You had" @number_correct "correct guesses out of" @turns_taken "for a total score of" @percent_correct.
"STEM - " percent_correct_by_category(:STEM) "correct"
"Turing Staff - " percent_correct_by_category(:TuringStaff) "correct"
"Pop Culture - " percent_correct_by_category(:PopCulture) "correct"
end
end
8 changes: 8 additions & 0 deletions lib/card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Card
attr_reader :question, :answer, :category
def initialize(question, answer, category)
@question = question
@answer = answer
@category = category
end
end
17 changes: 17 additions & 0 deletions lib/deck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Deck
attr_reader :cards
attr_accessor :cards
def initialize(cards)
@cards = cards
end

def count
cards.count
end

def cards_in_category(category)
cards.find_all do |card|
card.category == category
end
end
end
52 changes: 52 additions & 0 deletions lib/round.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require './lib/deck'
require './lib/card'
require './lib/turn'

class Round
attr_reader :deck, :turns, :turns_taken
attr_accessor :number_correct, :correct_count_by_category, :cards_by_category
def initialize(deck)
@deck = deck
@turns = []
@turns_taken = 0
@number_correct = 0
@correct_count_by_category = Hash.new(0)
@cards_by_category = Hash.new(0)
end

def current_card
@deck.cards.first
end

def take_turn(guess)
new_turn = Turn.new(guess, current_card)
@turns << new_turn
@turns_taken += 1
@cards_by_category[current_card.category] += 1
if new_turn.guess == current_card.answer
@number_correct = @number_correct + 1 #(shorthand is +=)
@correct_count_by_category[current_card.category] += 1
end
@deck.cards = @deck.cards.rotate(1)
return new_turn
end

def number_correct
@number_correct
end

def percent_correct
@number_correct.div(@turns_taken)*100
return "#{@number_correct.div(@turns_taken)*100}%"
end

def number_correct_by_category(category)
return @correct_count_by_category[category]
end

def percent_correct_by_category(category)
@correct_count_by_category[category].div(@cards_by_category[category])*100
return "#{@correct_count_by_category[category].div(@cards_by_category[category])*100}%"
end
end

21 changes: 21 additions & 0 deletions lib/turn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Turn
attr_reader :guess, :card, :answer
def initialize(guess, card)
@guess = guess
@card = card
end

def correct?
guess == card.answer
end
def feedback
if correct?
print "Correct!"
else
print "Incorrect."
end
end
end



2 changes: 1 addition & 1 deletion spec/card_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require './lib/card'

require './lib/turn'
RSpec.describe Card do
it 'exists' do
card = Card.new("What is the capital of Alaska?", "Juneau", :Geography)
Expand Down
44 changes: 44 additions & 0 deletions spec/deck_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require './lib/deck'
require './lib/card'
require './lib/turn'
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 'checks card 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 'checks card category 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)

cards = [card_1, card_2, card_3]
deck = Deck.new(cards)
expect(deck.cards_in_category(:Geography)).to eq([card_1])
end
it 'checks card category 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)

cards = [card_1, card_2, card_3]
deck = Deck.new(cards)
expect(deck.cards_in_category('Pop Culture')).to eq([])
end
end
72 changes: 72 additions & 0 deletions spec/round_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require './lib/round'
require './lib/card'
require './lib/turn'
require './lib/deck'
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 'shows round 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)

expect(round).to be_instance_of(Round)
end

it 'shows number 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)
number_correct = @number_correct
expect(number_correct).to eq(@number_correct)
end

it 'shows number 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)

deck = Deck.new([card_1, card_2, card_3])
round = Round.new(deck)
round.take_turn('Juneau')
expect(round.number_correct_by_category(:Geography)).to be(1)
end

it 'shows percent 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)
round.take_turn('Juneau')
expect(round.percent_correct).to eq(100.0)
end
it 'shows percent 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)

deck = Deck.new([card_1, card_2, card_3])
round = Round.new(deck)
round.take_turn('Juneau')
expect(round.percent_correct_by_category(:Geography)).to be(100)
expect(round.percent_correct_by_category(:STEM)).to be(100)
end
end
19 changes: 19 additions & 0 deletions spec/turn_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require './lib/card'
require './lib/turn'
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 'returns 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 'returns false' do
card = Card.new("What is the capital of Alaska?", "Juneau", :Geography)
turn = Turn.new('Judeau', card)
expect(turn.correct?).to eq(false)
end
end