Aliaks, I hope you are rested up. I like to pop in occasionally to see how your project is coming along. If you are finding that adding a room to your world is getting difficult, don’t be discouraged. You’ve learned a lot.
I really wanted to talk about your quiz idea. I assume from your posts that you want to generate random questions. Maybe you want questions of different types, like riddles and arithmetic questions. If I understand correctly, I would separate the questions into different categories, and implement each category with a class because each category would probably have a different implementation. The idea is to keep your program flexible. A good design would allow a new question category to be added without changing existing code. Being able to add stuff without changing existing code is the big benefit of object oriented design. You do this by creating an interface. Your program uses only the interface and all your question classes implement the interface.
For example here is an interface for a question generator
Its functions are pure virtual meaning that this is an interface.Code:class QuestionGenerator
{
/*
Defines the public interface to question generators
*/
public:
virtual const std::string& poseQuestion(void) = 0;
virtual bool isCorrectAnswer(const std::string& answer) const = 0;
};
Anything that claims to inherit from this interface must implement the functions advertized by the interface.
Now you can have a number of question categories. For example, say you want an arithmetic question generator.
You can also have a RiddleQuestionGenerator that has a different implementation from the ArithmeticQuestionGenerator, but the same interface.Code:class ArithmeticQuestionGenerator: public QuestionGenerator
{
/*
Creates arithmetic question
*/
public:
virtual const std::string& poseQuestion(void)
{
// return a string for the current question
}
virtual bool isCorrectAnswer(const std::string& answer) const
{
// check answer and return result
}
};
How do we put all these question generators together? We can have a QuizMaster class that creates all the question generators and picks one at random to use.
Notice that the QuizMaster is also a QuestionGenerator. The poseQuestion and isCorrectAnswer methods simply forward calls to a randomly chosen QuestionGenerator.Code:class QuizMaster : public QuestionGenerator
{
/*
Responsible for holding different types of question generators
*/
public:
QuizMaster()
{
mGenerators.push_back(new RiddleGenerator());
mGenerators.push_back(new ArithmeticQuestionGenerator());
}
virtual const std::string& poseQuestion(void)
{
}
virtual bool isCorrectAnswer(const std::string& answer) const
{
}
std::vector<QuestionGenerator*> mGenerators;
};
Then all we need is to create a QuizMaster in main and use it.
Notice that main does not care about how QuizMaster works, or about what question generators are available.Code:int main()
{
QuizMaster quizMaster;
while(true)
{
std::cout << quizMaster.poseQuestion().c_str() << std::endl;
std::string answer;
getline(std::cin, answer);
if (quizMaster.isCorrectAnswer(answer))
std::cout << "Thats right" << std::endl;
else
std::cout << "Better luck next time !" << std::endl;
}
}
If you want to add a new QuestionGenerator, you don’t change any existing ones. You just create a new class and add its instance to the list in QuizMaster.
Please feel free to ask questions. If you want a working example, I can post it.