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

Will Fox: Game On! #633

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
418a09b
test: added require pry to card_spec file for inspection
willfox0409 Dec 3, 2024
5b77410
feat: add card class with attrs
willfox0409 Dec 3, 2024
632ae7a
feat: create runner files for inspection and eventual gameplay
willfox0409 Dec 4, 2024
0f14e66
feat: add turn class with attrs
willfox0409 Dec 4, 2024
d21b32b
test: write initial tests for turn class, all tests passing
willfox0409 Dec 4, 2024
4c66b40
feat: refactored feedback method, updated turn class
willfox0409 Dec 4, 2024
bd8b6c9
test: updated tests, all tests passing, iteration 1 complete
willfox0409 Dec 4, 2024
3eaa08c
feat: add deck class with methods
willfox0409 Dec 4, 2024
33a640d
test: add tests for deck class, all tests passing
willfox0409 Dec 4, 2024
08257b9
feat: add the round class with attrs, no methods yet
willfox0409 Dec 4, 2024
bd6f790
test: add initialization set-up and tests for round class
willfox0409 Dec 4, 2024
b16cd2d
feat: add current_card method
willfox0409 Dec 4, 2024
e3cd5c4
test: add current_card test
willfox0409 Dec 4, 2024
a3b1a99
feat: add take_turn method to round class
willfox0409 Dec 4, 2024
52cc2c3
test: add test for take_turn, test passing
willfox0409 Dec 4, 2024
35ccb96
fix: updated title for take_turn method test
willfox0409 Dec 4, 2024
839fadc
feat: add correct_count method
willfox0409 Dec 4, 2024
ce9751f
test: add correct_count test
willfox0409 Dec 4, 2024
13f4d7f
fix: refactored method names for clarity and consistency
willfox0409 Dec 4, 2024
28ee518
fix: updated test description for clarity
willfox0409 Dec 4, 2024
f7e0721
fix: refactored the set-up for last_turn method to isolate a turn obj…
willfox0409 Dec 4, 2024
291e5bb
feat: add a few annotations for my presentation
willfox0409 Dec 4, 2024
df1fff6
fix: refactored tests, added # to instance methods, updated descriptions
willfox0409 Dec 4, 2024
ded87f7
feat: add percentage methods, complete iteration 2, may go back to re…
willfox0409 Dec 4, 2024
bd95401
test: add percentage tests, all tests passing, may need to test for m…
willfox0409 Dec 4, 2024
a735bf6
fix: changed name of runner file
willfox0409 Dec 6, 2024
8f6f8e6
feat: write set-up for runner file
willfox0409 Dec 6, 2024
343785b
feat: implement game-play, not yet tested
willfox0409 Dec 6, 2024
f77741a
feat: game running, start_game method not functioning correctly, need…
willfox0409 Dec 6, 2024
801aee0
fix: changed the .each method to a 'while' loop to fix the card order…
willfox0409 Dec 6, 2024
5e376fe
fix: customized feedback messages to be specific to each category, no…
willfox0409 Dec 6, 2024
a9266ea
feat: fix the percent_correct_by_category method to output the results
willfox0409 Dec 7, 2024
f147158
fix: refactored the correct? method to make user-input case-insensiti…
willfox0409 Dec 7, 2024
fd98b97
fix: updated card object spelling
willfox0409 Dec 7, 2024
09cb79d
feat: install colorize gem and colorize implementation code in set-up…
willfox0409 Dec 7, 2024
2cf3362
fix: deleted space character after the very last cp method that was c…
willfox0409 Dec 7, 2024
7dcf5af
fix: colorizer is working on the questions and throughout the game, m…
willfox0409 Dec 7, 2024
7151967
fix: deleted dev_runner file, never ended up using it as a sandbox, j…
willfox0409 Dec 9, 2024
c9091cd
fix: updated correct? method with .strip to account for any trailing …
willfox0409 Dec 9, 2024
875aaa7
fix: edited some annotations
willfox0409 Dec 9, 2024
68aa425
fix: added annotation explaining why I don't use the feedback method
willfox0409 Dec 10, 2024
41206a2
fix: updated title of number_correct method to be more precise
willfox0409 Dec 10, 2024
51b0cd0
test: added tests for case insensitivity and trailing whitespace in t…
willfox0409 Dec 10, 2024
407ed9c
fix: updated edge case tests for round class, all tests passing
willfox0409 Dec 10, 2024
13796e3
fix: added annotation for case and whitespace insensitivity
willfox0409 Dec 10, 2024
feaa1df
feat: added 2 more card objects to my runner file, one for Megan spec…
willfox0409 Dec 10, 2024
b47031b
fix: updated a couple of the questions to be a tad easier!
willfox0409 Dec 10, 2024
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
92 changes: 92 additions & 0 deletions flashcard_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require 'pry'
require './lib/card'
require './lib/turn'
require './lib/deck'
require './lib/round'
require 'colorize'

colors = [:light_red, :light_green, :yellow, :light_blue, :light_magenta, :light_cyan]

def cputs(message, colors)
puts message.colorize(colors.sample)
end

def cp(message, colors)
puts message.inspect.colorize(colors.sample)
end

card1 = Card.new("What is the name of the island in the film Jurassic Park?", "Isla Nublar", :Movies)
card2 = Card.new("What is the title of the Beatles' final album release?", "Let it Be", :Music)
card3 = Card.new("What is the capital city of Spain?", "Madrid", :Geography)
card4 = Card.new("Who played the hit-man, Jules in the film Pulp Fiction?", "Samuel L Jackson", :Movies)
card5 = Card.new("What is the smallest US State?", "Rhode Island", :Geography)
card6 = Card.new("Who wrote the 1971 hit song, What's Going On?", "Marvin Gaye", :Music)
card7 = Card.new("Who played Elizabeth, in the film Pirates of the Caribbean?", "Keira Knightley", :Movies)
card8 = Card.new("What is the largest ski area in Colorado?", "Vail", :Geography)

cards = [card1, card2, card3, card4, card5, card6, card7, card8]

deck = Deck.new(cards)

round = Round.new(deck)

def start_game(round, colors)

initial_card_count = round.deck.cards.size # added this variable for correct interpolation in Game Over message

puts ""
cputs("Welcome! You're playing Will's Trivia with #{initial_card_count} cards.", colors)
puts ""
cputs(">>>---- Game On! ----<<<", colors)
puts ""
# round.deck.cards.each do |card|
while !round.deck.cards.empty? do
cputs("This is card number #{round.turns.count + 1} out of #{initial_card_count}... 'cue Jeopardy theme song'...", colors)
puts ""
cp(round.current_card.question, colors)
puts ""
cputs("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", colors)
puts ""
guess = gets.chomp
round.take_turn(guess)
puts ""
# puts round.turns.last.feedback
case [round.last_turn.correct?, round.last_turn.card.category]
when [true, :Movies]
cp("Film buff huh? I bet you have a Criterion Collection subscription don't you?", colors)
when [true, :Music]
cp("Yep, wow, you must have an exquisite vinyl collection...", colors)
when [true, :Geography]
cp("I'm truly jealous of how worldly you are, take me with you next time?", colors)
when [false, :Movies]
cp("Nope. You can use my Netflix password if you want.", colors)
when [false, :Music]
cp("Uh oh, we've got to get you some better headphones!", colors)
when[false, :Geography]
cp("Welp! Time to invest in a globe!", colors)
end
puts ""
end

cputs("****** Game Over! ******", colors)
puts ""
cputs("You had #{round.number_correct} correct guesses out of #{initial_card_count} for a total score of #{round.percent_correct.round(0)}%", colors)
puts ""

cards_played = round.turns.map do |turn| #must use turns to access card objects, deck is depleted
turn.card
end

categories = cards_played.map do |card|
card.category
end

unique_categories = categories.uniq

unique_categories.each do |category|
cputs("#{category} - #{round.percent_correct_by_category(category).round(0)}% correct", colors)
end
puts ""
end

start_game(round, colors)
9 changes: 9 additions & 0 deletions lib/card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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

def initialize(cards)
@cards = cards
end

def cards_in_category(category)
@cards.select do |card|
card.category == category
end
end

def card_count
@cards.count
end
end
65 changes: 65 additions & 0 deletions lib/round.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'pry'

class Round
attr_reader :deck, :turns

def initialize(deck)
@deck = deck
@turns = []
end

def current_card
@deck.cards.first
end

def take_turn(guess)
new_turn = Turn.new(guess, current_card)
@turns << new_turn
@deck.cards.shift
new_turn
end

def turn_count #refactored these names for clarity and consistency, I believe this is an example of Law of Demeter
@turns.count # was def count, @rounds.turns.count (in spec file)
end

def last_turn #previously was def last, @round.turns.last (in spec file)
@turns.last
end

def number_correct #need to test for more options, even if it's working in runner
correct_count = 0

@turns.each do |turn|
if turn.correct?
correct_count += 1
end
end
correct_count
end

def correct_by_category(category)
correct_by_cat_count = 0

@turns.each do |turn|
if turn.correct? && turn.card.category == category
correct_by_cat_count += 1
end
end
correct_by_cat_count
end

def percent_correct
(number_correct / turn_count.to_f) * 100
end

def percent_correct_by_category(category)
count_by_cat = 0
@turns.each do |turn|
if turn.card.category == category
count_by_cat += 1
end
end
(correct_by_category(category) / count_by_cat.to_f) * 100
end
end
20 changes: 20 additions & 0 deletions lib/turn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Turn
attr_reader :guess, :card

def initialize(guess, card)
@guess = guess
@card = card
end

def correct?
@guess.strip.upcase == @card.answer.strip.upcase #case and whitespace insensitivity
end

def feedback # technically not using this method in game because I wanted to customize the feedback based on category
if correct?
"Correct!"
else
"Incorrect."
end
end
end
29 changes: 16 additions & 13 deletions spec/card_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
require 'pry'
require './lib/card'

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

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?")
describe '#question' do
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?")
end
end

it 'has an answer' do
card = Card.new("What is the capital of Alaska?", "Juneau", :Geography)

expect(card.answer).to eq("Juneau")
describe '#answer' do
it 'has an answer' do
card = Card.new("What is the capital of Alaska?", "Juneau", :Geography)
expect(card.answer).to eq("Juneau")
end
end

it 'has a category' do
card = Card.new("What is the capital of Alaska?", "Juneau", :Geography)

expect(card.category).to eq(:Geography)
describe '#category' do
it 'has a category' do
card = Card.new("What is the capital of Alaska?", "Juneau", :Geography)
expect(card.category).to eq(:Geography)
end
end
end
33 changes: 33 additions & 0 deletions spec/deck_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'pry'
require './lib/card'
require './lib/deck'

RSpec.describe Deck do
before(:each) do
@card1 = Card.new("What is the capital of Alaska?", "Juneau", :Geography)
@card2 = Card.new("Which planet is closest to the sun?", "Mercury", :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]
@deck1 = Deck.new(@cards)
end

it 'exists' do
expect(@deck1).to be_a(Deck)
end

it '#cards returns the collection of cards' do
expect(@deck1.cards).to eq([@card1, @card2, @card3])
end

describe '#cards_in_category' do
it 'checks what category a card belongs to' do
expect(@deck1.cards_in_category(:STEM)).to eq([@card2, @card3])
expect(@deck1.cards_in_category(:Geography)).to eq([@card1])
expect(@deck1.cards_in_category("Pop Culture")).to eq([])
end
end

it '#card_count returns the number of cards in the deck' do
expect(@deck1.card_count).to eq(3)
end
end
Loading