Skip to content

Commit

Permalink
Add[ShehrozIrfan#53]: more questions and customization options to tri…
Browse files Browse the repository at this point in the history
…via game

It works by creating an array of questions, each of which has a question and an answer. The game then asks the user each question in the array and checks their answer. If the user answers correctly, their score is incremented. At the end of the game, the user's score is displayed.
  • Loading branch information
vivekvr1 authored Oct 7, 2023
1 parent feae861 commit 952b500
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions ruby-hacktoberfest-2023/trivia.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Question
attr_reader :question, :answer

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

class Game
def initialize
@questions = [
Question.new("What is the capital of France?", "Paris"),
Question.new("Who wrote 'To Kill a Mockingbird'?", "Harper Lee"),
Question.new("What is the chemical symbol for Hydrogen?", "H")
]
@score = 0
end

def start
puts "Welcome to Trivia Game!"

@questions.each do |question|
puts question.question
print "Your answer: "
user_answer = gets.chomp

if user_answer.downcase == question.answer.downcase
puts "Correct!"
@score += 1
else
puts "Sorry, that's incorrect. The correct answer is #{question.answer}."
end
end

puts "Game over! Your score was #{@score}."
end
end

game = Game.new
game.start

0 comments on commit 952b500

Please sign in to comment.