diff --git a/lib/flashcard_runner.rb b/flashcard_runner.rb similarity index 100% rename from lib/flashcard_runner.rb rename to flashcard_runner.rb diff --git a/lib/round.rb b/lib/round.rb index 0f82e71..af4e6ca 100644 --- a/lib/round.rb +++ b/lib/round.rb @@ -9,49 +9,57 @@ def initialize(deck) @deck = deck @guesses = [] @number_correct = 0 - @current_card = 0 + @card_position = 0 end def current_card - @deck.cards[@current_card] + @deck.cards[@card_position] + end + + def delay_output(seconds) + sleep seconds + end + + def check_guess(guess) + @number_correct += 1 if guess.correct? 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 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" + 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 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}" + 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 + gets.chomp end end diff --git a/test/round_test.rb b/test/round_test.rb index 2fc44f4..191a092 100644 --- a/test/round_test.rb +++ b/test/round_test.rb @@ -4,6 +4,7 @@ require './lib/card' require './lib/deck' require "./lib/round" +require "./lib/guess" class RoundTest < Minitest::Test @@ -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) + end + + def test_check_guess + 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