diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9a01640 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +TriviaGame \ No newline at end of file diff --git a/QuestionClasses/MultipleChoiceQuestion.cpp b/QuestionClasses/MultipleChoiceQuestion.cpp new file mode 100644 index 0000000..0b684d4 --- /dev/null +++ b/QuestionClasses/MultipleChoiceQuestion.cpp @@ -0,0 +1,29 @@ +#include "MultipleChoiceQuestion.h" +#include + +MultipleChoiceQuestion::MultipleChoiceQuestion(const string& prompt, const vector& choices, int correctChoice) + : Question(prompt), correctChoice(correctChoice) { + if (choices.size() > 4 || choices.size() < 2) { + throw invalid_argument("MultipleChoiceQuestion must have between 2 and 4 choices."); + } + this->choices = choices; + if (correctChoice < 1 || correctChoice > choices.size()) { + throw out_of_range("Correct choice must be a valid option index (1-based)."); + } +} + +void MultipleChoiceQuestion::displayQuestion() const { + cout << prompt << endl; + for (size_t i = 0; i < choices.size(); ++i) { + cout << i + 1 << ". " << choices[i] << endl; + } +} + +bool MultipleChoiceQuestion::checkAnswer(const string& answer) const { + try { + int choice = stoi(answer); + return choice == correctChoice; + } catch (...) { + return false; + } +} diff --git a/QuestionClasses/MultipleChoiceQuestion.h b/QuestionClasses/MultipleChoiceQuestion.h new file mode 100644 index 0000000..b5edd79 --- /dev/null +++ b/QuestionClasses/MultipleChoiceQuestion.h @@ -0,0 +1,21 @@ +#ifndef MULTIPLE_CHOICE_QUESTION_H +#define MULTIPLE_CHOICE_QUESTION_H + +#include "Question.h" +#include +#include + +class MultipleChoiceQuestion : public Question { +private: + vector choices; + int correctChoice; + +public: + MultipleChoiceQuestion(const string& prompt, const vector& choices, int correctChoice); + + void displayQuestion() const override; + + bool checkAnswer(const string& answer) const override; +}; + +#endif diff --git a/QuestionClasses/OneWordResponseQuestion.cpp b/QuestionClasses/OneWordResponseQuestion.cpp new file mode 100644 index 0000000..5e43190 --- /dev/null +++ b/QuestionClasses/OneWordResponseQuestion.cpp @@ -0,0 +1,21 @@ +#include "OneWordResponseQuestion.h" +#include +#include + +OneWordResponseQuestion::OneWordResponseQuestion(const string& prompt, const string& correctAnswer) + : Question(prompt), correctAnswer(correctAnswer) {} + +void OneWordResponseQuestion::displayQuestion() const { + cout << prompt << " (Answer in one word)" << endl; +} + +bool OneWordResponseQuestion::checkAnswer(const string& answer) const { + string lowerAnswer = answer; + string lowerCorrectAnswer = correctAnswer; + + // Convert both to lowercase + for (auto& ch : lowerAnswer) ch = tolower(ch); + for (auto& ch : lowerCorrectAnswer) ch = tolower(ch); + + return lowerAnswer == lowerCorrectAnswer; +} diff --git a/QuestionClasses/OneWordResponseQuestion.h b/QuestionClasses/OneWordResponseQuestion.h new file mode 100644 index 0000000..c2fde94 --- /dev/null +++ b/QuestionClasses/OneWordResponseQuestion.h @@ -0,0 +1,18 @@ +#ifndef ONE_WORD_RESPONSE_QUESTION_H +#define ONE_WORD_RESPONSE_QUESTION_H + +#include "Question.h" + +class OneWordResponseQuestion : public Question { +private: + string correctAnswer; + +public: + OneWordResponseQuestion(const string& prompt, const string& correctAnswer); + + void displayQuestion() const override; + + bool checkAnswer(const string& answer) const override; +}; + +#endif diff --git a/QuestionClasses/Question.h b/QuestionClasses/Question.h new file mode 100644 index 0000000..0bc73f2 --- /dev/null +++ b/QuestionClasses/Question.h @@ -0,0 +1,21 @@ +#ifndef QUESTION_H +#define QUESTION_H + +#include +using namespace std; + +class Question { +protected: + string prompt; + +public: + Question(const string& prompt) : prompt(prompt) {} + + virtual ~Question() {} + + virtual void displayQuestion() const = 0; + + virtual bool checkAnswer(const string& answer) const = 0; +}; + +#endif diff --git a/QuestionClasses/TrueFalseQuestion.cpp b/QuestionClasses/TrueFalseQuestion.cpp new file mode 100644 index 0000000..18e6493 --- /dev/null +++ b/QuestionClasses/TrueFalseQuestion.cpp @@ -0,0 +1,21 @@ +#include "TrueFalseQuestion.h" +#include + +TrueFalseQuestion::TrueFalseQuestion(const string& prompt, bool correctAnswer) + : Question(prompt), correctAnswer(correctAnswer) {} + +void TrueFalseQuestion::displayQuestion() const { + cout << prompt << " (True/False)" << endl; +} + +bool TrueFalseQuestion::checkAnswer(const string& answer) const { + string lowerAnswer = answer; + for (auto& ch : lowerAnswer) ch = tolower(ch); + + if (lowerAnswer == "true" || lowerAnswer == "t") { + return correctAnswer; + } else if (lowerAnswer == "false" || lowerAnswer == "f") { + return !correctAnswer; + } + return false; +} diff --git a/QuestionClasses/TrueFalseQuestion.h b/QuestionClasses/TrueFalseQuestion.h new file mode 100644 index 0000000..14635dc --- /dev/null +++ b/QuestionClasses/TrueFalseQuestion.h @@ -0,0 +1,18 @@ +#ifndef TRUE_FALSE_QUESTION_H +#define TRUE_FALSE_QUESTION_H + +#include "Question.h" + +class TrueFalseQuestion : public Question { +private: + bool correctAnswer; + +public: + TrueFalseQuestion(const string& prompt, bool correctAnswer); + + void displayQuestion() const override; + + bool checkAnswer(const string& answer) const override; +}; + +#endif diff --git a/QuestionClasses/main.cpp b/QuestionClasses/main.cpp new file mode 100644 index 0000000..da45109 --- /dev/null +++ b/QuestionClasses/main.cpp @@ -0,0 +1,31 @@ +#include "MultipleChoiceQuestion.h" +#include "TrueFalseQuestion.h" +#include "OneWordResponseQuestion.h" +#include +#include + +using namespace std; + +int main() { + // Multiple Choice + try { + vector choices = {"Red", "Blue", "Green", "Yellow"}; + MultipleChoiceQuestion mcq("What is the color of the sky?", choices, 2); + mcq.displayQuestion(); + cout << (mcq.checkAnswer("2") ? "Correct!" : "Incorrect.") << endl; + } catch (const exception& e) { + cerr << e.what() << endl; + } + + // True/False + TrueFalseQuestion tfq("The Earth is flat.", false); + tfq.displayQuestion(); + cout << (tfq.checkAnswer("false") ? "Correct!" : "Incorrect.") << endl; + + // One Word Response + OneWordResponseQuestion owq("What is the capital of France?", "Paris"); + owq.displayQuestion(); + cout << (owq.checkAnswer("paris") ? "Correct!" : "Incorrect.") << endl; + + return 0; +}