diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..9df847d6b Binary files /dev/null and b/.DS_Store differ diff --git a/.rspec b/.rspec new file mode 100644 index 000000000..3d036d93d --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--format documentation \ No newline at end of file diff --git a/README.md b/README.md index 90e4d38da..b46077b5d 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file diff --git a/flashcard_runner.rb b/flashcard_runner.rb new file mode 100644 index 000000000..a7dddfd1e --- /dev/null +++ b/flashcard_runner.rb @@ -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 \ No newline at end of file diff --git a/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 000000000..f4a4a60d7 Binary files /dev/null and b/lib/.DS_Store differ diff --git a/lib/card.rb b/lib/card.rb index e69de29bb..2c507dd07 100644 --- a/lib/card.rb +++ b/lib/card.rb @@ -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 \ No newline at end of file diff --git a/lib/deck.rb b/lib/deck.rb new file mode 100644 index 000000000..c5c54b532 --- /dev/null +++ b/lib/deck.rb @@ -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 diff --git a/lib/round.rb b/lib/round.rb new file mode 100644 index 000000000..c10d23f58 --- /dev/null +++ b/lib/round.rb @@ -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 \ No newline at end of file diff --git a/lib/turn.rb b/lib/turn.rb new file mode 100644 index 000000000..ac18443fa --- /dev/null +++ b/lib/turn.rb @@ -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 diff --git a/spec/card_spec.rb b/spec/card_spec.rb index 84a45a7a6..1b1333e51 100644 --- a/spec/card_spec.rb +++ b/spec/card_spec.rb @@ -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 diff --git a/spec/deck_spec.rb b/spec/deck_spec.rb new file mode 100644 index 000000000..1e97302ba --- /dev/null +++ b/spec/deck_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/round_spec.rb b/spec/round_spec.rb new file mode 100644 index 000000000..ba43226ba --- /dev/null +++ b/spec/round_spec.rb @@ -0,0 +1,235 @@ +require './lib/round.rb' +require './lib/card' +require './lib/deck.rb' +require './lib/turn' +#require 'pry'; binding.pry + +RSpec.describe Round 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) + + round = Round.new(deck) + + expect(round).to be_instance_of(Round) + end + + it 'has a deck' 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) + + round = Round.new(deck) + + expect(round.round_deck).to eq deck + end + + it 'can display current card' 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) + + round = Round.new(deck) + + + expect(round.current_card).to eq (card_1) + + end + + it 'can do a turn' 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) + + round = Round.new(deck) + + round.take_turn('answer_1') + + expect(round.number_correct).to eq 1 #new_turn = round.take_turn(player guess) + end + + it 'can display a new current card after a turn' 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) + + round = Round.new(deck) + + round.take_turn('answer_1') + + expect(round.current_card).to eq (card_2) + + end + + it 'stores the turn taken in the turns object' 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) + + round = Round.new(deck) + + round.take_turn('answer_1') + + expect(round.turns.count()).to eq 1 + end + + it 'can do a turn and be wrong' 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) + + round = Round.new(deck) + + round.take_turn('an1') + + expect(round.number_correct).to eq 0 #new_turn = round.take_turn(player guess) + + end + + it 'keeps track of turns taken' 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) + + round = Round.new(deck) + + round.take_turn('answer_1') + + expect(round.turns.count).to eq 1 + + round.take_turn('snawer_1') + + expect(round.turns.count).to eq 2 + + end + + it 'tracks correct and incorrect' 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) + + round = Round.new(deck) + + #correct + round.take_turn('answer_1') + # require 'pry'; binding.pry + expect(round.number_correct).to eq 1 + #incorrect + round.take_turn('answer_1') + expect(round.number_correct).to eq 1 + #correct again + round.take_turn('answer_1') + expect(round.number_correct).to eq 2 + + end + + it 'tracks correct by category' 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) + card_4 = Card.new('question_4', 'answer_4', :STEM) + + cards = [card_1, card_2, card_3, card_4] + + deck = Deck.new(cards) + + round = Round.new(deck) + + #correct + round.take_turn('answer_1') + #correct + round.take_turn('snawer_1') + #correct stem + round.take_turn('answer_1') + #incorrect stem + round.take_turn('answer_2') + + expect(round.number_correct_by_category(:Geography)).to eq 1 + expect(round.number_correct_by_category(:STEM)).to eq 2 + + end + + it 'can give percentage correct' 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) + card_4 = Card.new('question_4', 'answer_1', :STEM) + + cards = [card_1, card_2, card_3, card_4] + + deck = Deck.new(cards) + + round = Round.new(deck) + + #correct + round.take_turn('answer_1') + #correct stem + round.take_turn('snawer_1') + #correct stem + round.take_turn('answer_1') + #incorrect stem + round.take_turn('answer_2') + # require 'pry'; binding.pry + round.number_correct + + expect(round.percent_correct).to eq 75.0 + + end + + it 'can give percentage correct by category' 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) + card_4 = Card.new('question_4', 'answer_1', :STEM) + + cards = [card_1, card_2, card_3, card_4] + + deck = Deck.new(cards) + round = Round.new(deck) + + #correct + round.take_turn('answer_1') + #correct stem + + round.take_turn('snawer_1') + #correct stem + round.take_turn('answer_1') + #incorrect stem + round.take_turn('answer_2') + + expect(round.percent_correct_by_category(:Geography)).to eq 100.0 + expect(round.percent_correct_by_category(:STEM)).to eq 66.7 + + + end + + +end \ No newline at end of file diff --git a/spec/turn_spec.rb b/spec/turn_spec.rb new file mode 100644 index 000000000..fac01f15a --- /dev/null +++ b/spec/turn_spec.rb @@ -0,0 +1,51 @@ +require './lib/turn' +require './lib/card' + +RSpec.describe Turn do + it 'creates a turn' do + card = Card.new('What is the capital of Alaska?', 'Juneau', :Geography) + turn = Turn.new("Juneau", card) + + expect(turn).to be_instance_of(Turn) + end + + it 'contains a guess' do + card = Card.new('What is the capital of Alaska?', 'Juneau', :Geography) + turn = Turn.new("Juneau", card) + + expect(turn.player_guess).to eq("Juneau") + end + + it 'has correct guess' do + card = Card.new('What is the capital of Alaska?', 'Juneau', :Geography) + turn = Turn.new("Juneau", card) + + expect(turn.correct?).to eq true + end + + it 'has incorrect guess' do + card = Card.new('What is the capital of Alaska?', 'Juneau', :Geography) + turn = Turn.new("Junu", card) + + expect(turn.correct?).to eq false + end + + it 'tell player they are correct' do + card = Card.new('What is the capital of Alaska?', 'Juneau', :Geography) + turn = Turn.new("Juneau", card) + + turn.correct? + + expect(turn.feedback).to eq 'You are correct!' + end + + it 'tell player they are incorrect' do + card = Card.new('What is the capital of Alaska?', 'Juneau', :Geography) + turn = Turn.new("Junu", card) + + turn.correct? + + expect(turn.feedback).to eq 'You are incorrect!' + end + +end \ No newline at end of file diff --git a/trivia.txt b/trivia.txt new file mode 100644 index 000000000..e036779e5 --- /dev/null +++ b/trivia.txt @@ -0,0 +1,20 @@ +Who is the author of 'Dune'?, Frank Herbert, :Sci-Fi +In 'Star Wars' what is the name of Han Solo's ship?, Millennium Falcon, :Sci-Fi +Which actor portrayed the character of Neo in 'The Matrix'?, Keanu Reeves, :Sci-Fi +What is the name of the artificial intelligence in '2001: A Space Odyssey'?, HAL, :Sci-Fi +In which film series do the characters fight against Skynet?, Terminator, :Sci-Fi +What is the maximum level a character can reach in Dungeons & Dragons 5th Edition?, 20, :TTRPG +Which TTRPG uses the slogan 'In the grim darkness of the far future there is only war'?(be specific), Warhammer 40k, :TTRPG +In D&D what is the alignment of a character who follows their own rules but cares for the greater good?, Chaotic Good, :TTRPG +Which role-playing game is the world of Baldur's Gate built in?(full name not abbreviated), Dungeons and Dragons, :TTRPG +In Pathfinder what is the name of the world where most of the adventures take place?, Golarion, :TTRPG +In Greek mythology what creature is part lion part goat and part serpent?, Chimera, :Mythical_creatures +What is the name of the one-eyed giant in Greek mythology?, Cyclops, :Mythical_creatures +In Norse mythology what is the name of the world serpent?(do not use any special characters), Jormungandr, :Mythical_creatures +What type of mythical creature explodes in flames when it dies but is reborn from its own ashes?, Phoenix, :Mythical_creatures +Which mythical creature is said to disguise itself as chests to eat adventurers?, Mimic, :Mythical_creatures +In D&D what is the cat like creature that can project an image of itself to confuse it's foes?, Displacer beast, :TTRPG +What does the T in TTRPG stand for?, Table, :TTRPG +What year was the first 'Star Wars' movie released?, 1977, :Sci-Fi +In Egyptian mythology what is the name of the god with the head of a jackal?, Anubis, :Mythical_creatures +Who wrote the orignal Star Wars series?, George Lucas, :Sci-Fi \ No newline at end of file