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

Jeremiah Ross #612

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0759701
created card class and tested via card_spec
Crosswolfv1 Aug 21, 2024
8f7ac72
created initial files turn & turn_spec
Crosswolfv1 Aug 21, 2024
ebb8131
created initial testing parameters for turn class
Crosswolfv1 Aug 21, 2024
9c12b09
created basis of turn class and correct? method
Crosswolfv1 Aug 21, 2024
9fbdcd6
corrected typos and incorrect parameters for testing in turn_spec
Crosswolfv1 Aug 21, 2024
14fa5cd
created deck and round class files as well as related specs to prepar…
Crosswolfv1 Aug 21, 2024
cd1d002
removed extraneous test from turn and turn_spec
Crosswolfv1 Aug 22, 2024
6f69f42
created initial tests for deck.rb
Crosswolfv1 Aug 22, 2024
e8d0333
working on deck rb cards_in_category method to return the cards with …
Crosswolfv1 Aug 22, 2024
25b601f
finalized the methods in deck class I learnedyou can use the .compact…
Crosswolfv1 Aug 22, 2024
cf65f6e
removed pry bindings
Crosswolfv1 Aug 23, 2024
3c4614b
added commenst explaining each class's purpose
Crosswolfv1 Aug 23, 2024
609230d
built parameters for found.rb
Crosswolfv1 Aug 23, 2024
c3be50e
test: modified tests to perform desired functions
Crosswolfv1 Aug 26, 2024
b1ad18b
feat: Added full round class
Crosswolfv1 Aug 26, 2024
8db87f0
docs: updated README
Crosswolfv1 Aug 26, 2024
9d47c73
feat: created game class and game
Crosswolfv1 Aug 27, 2024
c2995c7
feat: created trivia.txt to allow custom questions and categories to …
Crosswolfv1 Aug 27, 2024
9d6b07f
docs: added some instructions to teh readme on how to run and how to …
Crosswolfv1 Aug 27, 2024
fb0e815
rsc: fixed bug preventing the game from providing feedback while doin…
Crosswolfv1 Aug 27, 2024
147784c
feat: removed built-in questions and created ability to read from an …
Crosswolfv1 Aug 27, 2024
927721a
Del: removed game_spec unneccessary file
Crosswolfv1 Aug 27, 2024
2d11fd0
fix: tests were failing due to calling incorrect variable
Crosswolfv1 Aug 27, 2024
a593808
doc: modified answers in trivia.txt
Crosswolfv1 Aug 27, 2024
11d3707
bug: fixed another wrong answer
Crosswolfv1 Aug 27, 2024
9f5499e
bug: fix issues with special character in trivia and modified answers…
Crosswolfv1 Aug 27, 2024
fd24b14
bug: fixed typos in trivia
Crosswolfv1 Aug 28, 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
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--format documentation
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## Flash Cards

This is the starter repository for the [Flash Cards](http://backend.turing.io/module1/projects/flashcards) project.
# This is Jeremiah's Flash card project

You play the game by running 'flashcard_runner.rb'

The answers are not case sensitive.

custom questions can be added to the trivia.txt file in the format of 'Question, answer, :category'

The game will end telling you how many you got right and how many in each category you got right in percentages.
76 changes: 76 additions & 0 deletions flashcard_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require './lib/round.rb'
require './lib/card.rb'
require './lib/deck.rb'
require './lib/turn.rb'

cards = []

File.open('./trivia.txt', 'r') do |file|
file.each_line do |line|
question, answer, category = line.chomp.split(', ', 3)
card = Card.new(question, answer, category)
cards << card
end
end

game_deck = Deck.new(cards)


class Game
attr_reader :card_number,
:round,
:total,
:categories

def initialize(deck)
@deck = deck
@card_number = 1
@total = @deck.deck.count
@categories = @deck.all_categories

end

def start
puts "Welcome, You are playing with #{@total} cards."
@round = Round.new(@deck)
gameplay
end

def gameplay
while @card_number < @total + 1
player_guessing
end

game_over
end

def player_guessing
puts "This is card number #{@card_number} of #{@total} "
puts "#{@round.current_card.question}"
player_guess = gets.chomp
@round.take_turn(player_guess)
@card_number += 1
end

def game_over
puts 'GAME OVER!!!'
puts "You had #{@round.number_correct} out of #{@total} for a total score of #{@round.percent_correct.round(1)}"
# require 'pry'; binding.pry
categories.each do |category|
puts "#{category} - #{@round.percent_correct_by_category(category)}"
end

end

end

# card_1 = Card.new('What is the capital of Texas?', 'Austin', :Geography)
# card_2 = Card.new('The reason why drones have their engines go in alternating directions is because of?', 'Centripetal Force', :STEM)
# card_3 = Card.new('Cats are in what animal family (reminder Kingdom > Phylum > class > order > family)?', 'Felidae', :STEM)
# cards = [card_1, card_2, card_3]
# game_deck = Deck.new(cards)


game = Game.new(game_deck)

game.start
Binary file added lib/.DS_Store
Binary file not shown.
15 changes: 15 additions & 0 deletions lib/card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Card class for flash_card project
#all flashcard templates must follow 'question','answer', :category

class Card
attr_reader :question,
:answer,
:category

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

end
36 changes: 36 additions & 0 deletions lib/deck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#Deck class stores all cards for current round

class Deck
attr_reader :deck,
:categories

def initialize(cards)
@deck = cards
@categories = []
end

def cards_in_category(category_request)

category = []
category = deck.map do |card|

in_category = []
card.category == category_request ? in_category << card : nil

end.compact


end

def all_categories
categories = @deck.map do |card|
card.category
end
categories = categories.uniq()
end

def guessed
@deck.shift
end

end
73 changes: 73 additions & 0 deletions lib/round.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require './lib/turn'
require './lib/deck.rb'


class Round
attr_reader :round_deck,
:turns,
:score



def initialize(deck)
@round_deck = deck
@turns = []
@score = 0
end

def current_card
round_deck.deck[0]
end

def take_turn(guess)
deck_turn = @round_deck
turn = Turn.new(guess, deck_turn.deck[0])
@turns << turn
deck_turn.deck.shift(1)
turn.correct?
puts turn.feedback
end

def number_correct
turns = @turns
correct = turns.map do |turn|
turn.correct?
end.compact

@score = correct.count(true)

end

def number_correct_by_category(requested_category)
turns = @turns
correct = []
correct = turns.map do |turn|

(turn.correct ==true and turn.card.category == requested_category) ? correct << turn : nil

end
@score = correct.compact.count

end

def percent_correct
(@score.to_f / @turns.count().to_f) * 100
end

def percent_correct_by_category(requested_category)
number_correct_by_category(requested_category)

cards = []
cards = turns.map do |turn|

(turn.card.category == requested_category) ? cards << turn : nil

end
cards = cards.compact

((@score.to_f / cards.count().to_f) * 100).round(1)

end


end
32 changes: 32 additions & 0 deletions lib/turn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#Turn class to compare player input to card object
#returns if correct or incorrect

class Turn
attr_reader :player_guess,
:card,
:correct

def initialize(guess, card)
@player_guess = guess
@card = card
@correct = nil
end

def correct?
@player_guess = player_guess.downcase
card_answer = @card.answer.downcase

if @player_guess == card_answer
@correct = true
elsif @player_guess != card_answer
@correct = false
end

end

def feedback
# correct?
@correct ? 'You are correct!' : 'You are incorrect!'
end

end
8 changes: 4 additions & 4 deletions spec/card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

RSpec.describe Card do
it 'exists' do
card = Card.new("What is the capital of Alaska?", "Juneau", :Geography)
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)
card = Card.new('What is the capital of Alaska?', 'Juneau', :Geography)

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)
card = Card.new('What is the capital of Alaska?', 'Juneau', :Geography)

expect(card.answer).to eq("Juneau")
end

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

expect(card.category).to eq(:Geography)
end
Expand Down
74 changes: 74 additions & 0 deletions spec/deck_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require './lib/deck.rb'
require './lib/card.rb'
#require 'pry'; binding.pry
RSpec.describe Deck do

it 'exists' do
card_1 = Card.new('question_1?', 'answer_1', :Geography)
card_2 = Card.new('question_2', 'snawer_1', :STEM)
card_3 = Card.new('question_3', 'answer_1', :STEM)
cards = [card_1, card_2, card_3]

deck = Deck.new(cards)

expect(deck).to be_instance_of(Deck)
end

it 'has cards' do
card_1 = Card.new('question_1?', 'answer_1', :Geography)
card_2 = Card.new('question_2', 'snawer_1', :STEM)
card_3 = Card.new('question_3', 'answer_1', :STEM)
cards = [card_1, card_2, card_3]
deck = Deck.new(cards)

expect(deck.deck.count()).to eq 3
end

it 'has different amount of cards' do
card_1 = Card.new('question_1?', 'answer_1', :Geography)
card_2 = Card.new('question_2', 'snawer_1', :STEM)
cards = [card_1, card_2]
deck = Deck.new(cards)

expect(deck.deck.count()).to eq 2
end

it 'is able to read categories of cards' do
card_1 = Card.new('question_1?', 'answer_1', :Geography)
card_2 = Card.new('question_2', 'snawer_2', :STEM)
card_3 = Card.new('question_3', 'answer_3', :STEM)
cards = [card_1, card_2, card_3]

deck = Deck.new(cards)

expect(deck.cards_in_category(:Geography).count).to eq(1)
expect(deck.cards_in_category(:STEM).count).to eq(2)
expect(deck.cards_in_category('pop culture').count).to eq(0)
end

it 'makes a list of categories of the availible cards' do
card_1 = Card.new('question_1?', 'answer_1', :Geography)
card_2 = Card.new('question_2', 'snawer_2', :STEM)
card_3 = Card.new('question_3', 'answer_3', :STEM)
cards = [card_1, card_2, card_3]

deck = Deck.new(cards)

expect(deck.all_categories.count).to eq 2


end

it 'is able to remove cards' do
card_1 = Card.new('question_1?', 'answer_1', :Geography)
card_2 = Card.new('question_2', 'snawer_2', :STEM)
card_3 = Card.new('question_3', 'answer_3', :STEM)
cards = [card_1, card_2, card_3]

deck = Deck.new(cards)
# require 'pry'; binding.pry
deck.guessed

expect(deck.deck.count).to eq 2
end
end
Loading