#include "Trivia.h" #include "NumericQuestion.h" #include "SingleChoiceQuestion.h" #include "TextualQuestion.h" #include #include #include #include #include Trivia::Trivia() { } Trivia::~Trivia() { for ( size_t index = 0; index < questions.size(); ++index ) delete questions[index]; } int Trivia::run() { std::cout << "Welcome to Trivia 1.0..." << std::endl; std::srand( std::time(nullptr) ); if ( ! loadQuestions() ) return 1; while ( askQuestion() ) showStatistics(); showFinalStatistics(); return 0; } bool Trivia::loadQuestions() { const std::string filename = "preguntas.txt"; std::ifstream inputFile(filename); if ( ! inputFile ) { std::cerr << "Could not open file " << filename << std::endl; return false; } std::string questionType; while ( std::getline(inputFile, questionType) ) { Question* question = createQuestion(questionType); if ( question != nullptr ) { inputFile >> *question; questions.push_back( question ); } else { std::cerr << "Error parsing: '" << questionType << "'\n"; assert(false); } } return true; } bool Trivia::askQuestion() { /* std::cout << questions.size() << " total questions\n"; for ( size_t index = 0; index < questions.size(); ++index ) std::cout << "Question " << index + 1 << ":\n" << *questions[index] << std::endl; */ long result = questions[ rand() % questions.size() ]->ask(); if ( result < 0 ) return false; playerScore += result; return true; } void Trivia::showStatistics() const { std::cout << "Your score: " << playerScore << std::endl << std::endl; } void Trivia::showFinalStatistics() const { } Question* Trivia::createQuestion(const std::string& questionType) { if ( questionType == "numerica" ) return new NumericQuestion(); if ( questionType == "seleccion_unica" ) return new SingleChoiceQuestion(); if ( questionType == "textual" ) return new TextualQuestion(); return nullptr; }