Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
File renamed without changes.
56 changes: 32 additions & 24 deletions lib/round.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,57 @@ def initialize(deck)
@deck = deck
@guesses = []
@number_correct = 0
@current_card = 0
@card_position = 0
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This naming better represents how this value is being used, which is as an index position.

end

def current_card
@deck.cards[@current_card]
@deck.cards[@card_position]
end

def delay_output(seconds)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More dynamic, better named, and testable.

sleep seconds
end

def check_guess(guess)
@number_correct += 1 if guess.correct?
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less lines of code, testable, and more readable.

end

def record_guess(response)
@guesses << Guess.new(response.to_s, current_card)
guess = @guesses.last
if guess.correct?
@number_correct += 1
else
@number_correct
end
@current_card += 1
guess = @guesses.last
check_guess(guess)
@card_position += 1
end

def percent_correct
(@number_correct.to_f / deck.cards.length * 100).to_i
(@number_correct.to_f / deck.count * 100).to_i
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing indentation is part of refactoring/cleanup.

end

def start
puts "Welcome! You're playing with #{deck.cards.count} cards"
sleep 1.5
puts "Welcome! You're playing with #{deck.count} cards"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I built a count method on the deck class, I should use it.

delay_output(1)
puts "---------------------------------"
sleep 1.5
game
delay_output(1)
start_game
puts "******* Game over! *******"
sleep 1.5
puts "You had #{@number_correct} correct guesses out of #{deck.cards.count} for a score of #{percent_correct}"
delay_output(1)
puts "You had #{@number_correct} correct guesses out of #{deck.count} for a score of #{percent_correct}"
end

def game
def start_game
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better method naming.

deck.cards.each do |card|
puts "This is card number #{@current_card + 1} out of #{deck.cards.count}"
sleep 1.5
puts "Question: #{card.question}"
input = gets.chomp
record_guess(input)
puts "This is card number #{@card_position + 1} out of #{deck.count}"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation matters for readability.

delay_output(1)
puts "Question: #{card.question}"
record_guess(input)
puts "#{guesses.last.feedback}"
sleep 1.5
end
delay_output(1)
end
end

def input
# More on why I do this with the mocks and stubs lesson
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always want to isolate user input.

gets.chomp
end

end
29 changes: 29 additions & 0 deletions test/round_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require './lib/card'
require './lib/deck'
require "./lib/round"
require "./lib/guess"

class RoundTest < Minitest::Test

Expand Down Expand Up @@ -66,4 +67,32 @@ def test_record_guess_edge_cases
assert_equal 0, round.number_correct
end

def test_dynamic_sleep_ability
card_1 = Card.new("What is the capital of Alaska?", "Juneau")
card_2 = Card.new("Approximately how many miles are in one astronomical unit?", "93,000,000")
card_3 = Card.new("Is Taylor Swift the best artist of our era?", "yes")
deck = Deck.new([card_1, card_2, card_3])
round = Round.new(deck)

assert_equal 1, round.delay_output(1)
assert_equal 2, round.delay_output(2)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discovered quite a bit about the sleep method by writing these tests.

end

def test_check_guess
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever there are multiple scenarios based on a condition, we want to test both of them.

card_1 = Card.new("What is the capital of Alaska?", "Juneau")
card_2 = Card.new("Approximately how many miles are in one astronomical unit?", "93,000,000")
deck = Deck.new([card_1, card_2])
round = Round.new(deck)
guess = Guess.new('Juneau', round.current_card)

assert_equal 1, round.check_guess(guess)
assert_equal 1, round.number_correct

round2 = Round.new(deck)
guess2 = Guess.new('Anchorage', round2.current_card)

assert_nil round2.check_guess(guess2)
assert_equal 0, round2.number_correct
end

end