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

Completion of Iteration 2 #2

Merged
merged 23 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sorbet
Gemfile.lock
.rubocop.yml
15 changes: 15 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# gem "rails"

gem 'sorbet', group: :development
gem 'sorbet-runtime'
gem 'tapioca', require: false, group: %i[development test]
group :development do
gem 'rubocop'
gem 'rubocop-discourse'
gem 'syntax_tree'
gem 'syntax_tree-disable_ternary'
end
16 changes: 8 additions & 8 deletions lib/card.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Documentation for class Card
class Card
attr_reader :question, :answer, :category
attr_reader :question, :answer, :category

def initialize(question, answer, category)
@question = question
@answer = answer
@category = category
end

end
def initialize(question, answer, category)
@question = question
@answer = answer
@category = category
end
end
23 changes: 23 additions & 0 deletions lib/deck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require './lib/card'
require './lib/deck'

# Documentation for class Deck
class Deck
attr_reader :cards

def initialize(cards)
@cards = cards
end

def count
@cards.length
end

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

# Documentation for class Round
class Round
attr_reader :deck, :turns, :turn, :number_correct

def initialize(deck)
@deck = deck
@turns = []
@turn = 0
@guess = nil
@number_correct = 0
end

def current_card
@deck.cards[@turn]
end

def take_turn(guess)
turn = Turn.new(guess, current_card)
turns.push(turn)
@guess = guess
@turn += 1
@number_correct += 1 if guess == turn.answer
turn
end

def correct?
if @guess == current_card.answer
true
elsif @guess.nil?
nil
else
false
end
end

def number_correct_by_category(category)
count_correct = 0
@turns.each do |turn|
count_correct += 1 if turn.category == category && turn.guess == turn.answer
end
count_correct
end

def percent_correct_by_category(category)
count_category = 0
@turns.each do |turn|
count_category += 1 if turn.category == category
end
fraction_correct = number_correct_by_category(category).to_f / count_category
(fraction_correct * 100.0).round(1)
end
end
37 changes: 19 additions & 18 deletions lib/turn.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# Documentation for class Turn
class Turn
attr_reader :card, :guess
attr_reader :card, :guess, :question, :answer, :category

def initialize(guess, card)
@guess = guess
@card = card
@question = @card.question
@answer = @card.answer
@category = @card.category
end
def initialize(guess, card)
@guess = guess
@card = card
@question = @card.question
@answer = @card.answer
@category = @card.category
end

def correct?
@guess == @answer
end
def correct?
@guess == @answer
end

def feedback
if @guess == @answer
"Correct!"
else
"Incorrect."
end
def feedback
if @guess == @answer
'Correct!'
else
'Incorrect.'
end
end
end
end
18 changes: 8 additions & 10 deletions spec/card_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
# typed: ignore

require './lib/card'

RSpec.describe Card do
it 'exists' do
card = Card.new("What is the capital of Alaska?", "Juneau", :Geography)
before(:each) do
@card = Card.new('What is the capital of Alaska?', 'Juneau', :Geography)
end

it 'exists' do
expect(card).to be_instance_of(Card)
end

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?")
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)
end
end
38 changes: 38 additions & 0 deletions spec/deck_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# typed: ignore

require './lib/card'
require './lib/turn'
require './lib/deck'

RSpec.describe Deck do
before(:each) do
@cards = [
Card.new('Question 1', 'Answer 1', :category1),
Card.new('Question 2', 'Answer 2', :category1),
Card.new('Question 3', 'Answer 3', :category2)
]
@deck = Deck.new(cards)
end

it 'exists' do
expect(@deck).to be_an_instance_of(Deck)
end

it 'cards property is an array' do
expect(@deck.cards).to be_an_instance_of(Array)
end

it 'cards array items are Cards' do
@deck.cards.each do |card|
expect(card).to be_an_instance_of(Card)
end
end

it 'counts number of cards in deck' do
expect(@deck.count).to eq(3)
end

it 'counts number of cards in category' do
expect(@deck.cards_in_category(:category1)).to eq(2)
end
end
89 changes: 89 additions & 0 deletions spec/round_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# typed: ignore

require './lib/card'
require './lib/turn'
require './lib/deck'
require './lib/round'

RSpec.describe Round do # rubocop:disable Metrics/BlockLength
before(:each) do
@card1 = Card.new('What is the capital of Alaska?', 'Juneau', :Geography)
@card2 = Card.new('The Viking spacecraft sent back to Earth photographs and reports about the surface of which planet?', 'Mars', :STEM)
@card3 = Card.new('Describe in words the exact direction that is 697.5° clockwise from due north?', 'North north west', :STEM)
@cards = [@card1, @card2, @card3]
@deck = Deck.new(@cards)
@round = Round.new(@deck)
end

it 'has a deck method to see the deck' do
expect(@round.deck).to be_an_instance_of(Deck)
end

it 'has an array of turns' do
expect(@round.turns).to eq([])
end

it 'has a current card on each turn, starting with the first card' do
expect(@round.current_card).to eq(@card1)
end

it 'adds a new Turn object when take_turn is called' do
new_turn = @round.take_turn('Juneau')
expect(new_turn).to be_an_instance_of(Turn)
end

it 'shows whether the answer is correct' do
new_turn = @round.take_turn('Juneau')
expect(new_turn.correct?).to eq(true)
end

it 'shows whether the answer is incorrect' do
new_turn = @round.take_turn('Topeka')
expect(new_turn.correct?).to eq(false)
end

it 'appends each turn to the round.turns array' do
@round.take_turn('Juneau')
@round.take_turn('Mars')
expect(@round.turns.count).to eq(2)
end

it 'displays the number of correct answers' do
@round.take_turn('Juneau')
@round.take_turn('Mars')
expect(@round.number_correct).to eq(2)
end

it 'gives feedback on the last answer' do
@round.take_turn('Juneau')
@round.take_turn('Mars')
expect(@round.turns.last.feedback).to eq('Correct!')
end

it 'doesn\'t count every answer as correct' do
@round.take_turn('Juneau')
@round.take_turn('Mars')
@round.take_turn('East')
expect(@round.number_correct).to eq(2)
end

it 'shows number correct for a given category' do
@round.take_turn('Juneau')
@round.take_turn('Mars')
@round.take_turn('East')
expect(@round.number_correct_by_category(:STEM)).to eq(1)
end

it 'shows percent correct for a given category' do
@round.take_turn('Juneau')
@round.take_turn('Mars')
@round.take_turn('East')
expect(@round.percent_correct_by_category(:STEM)).to eq(50.0)
end

it 'shows the current turn card' do
@round.take_turn('Juneau')
@round.take_turn('Mars')
expect(@round.current_card).to eq(@card3)
end
end
36 changes: 35 additions & 1 deletion spec/turn_spec.rb
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
require './lib/turn.rb'
# typed: ignore

require './lib/turn'
require './lib/card'

RSpec.describe Turn do
before(:each) do
@card = Card.new('question', 'answer', :category)
@turn = Turn.new('guess', card)
end

it 'exists' do
expect(@turn).to be_an_instance_of(Turn)
end

it 'has a card' do
expect(@turn.card).to be_an_instance_of(Card)
end

it 'has a question' do
expect(@turn.question).to be_an_instance_of(String)
end

it 'has an answer' do
expect(@turn.answer).to be_an_instance_of(String)
end

it 'has a guess' do
expect(@turn.guess).to be_an_instance_of(String)
end

it 'has a category' do
expect(@turn.category).to be_an_instance_of(Symbol)
end
end