-
Notifications
You must be signed in to change notification settings - Fork 21
Add refactoring #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I built a |
||
| 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 | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}" | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We always want to isolate user input. |
||
| gets.chomp | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discovered quite a bit about the |
||
| end | ||
|
|
||
| def test_check_guess | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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.