I’m making an iPad app that will display a few choices and based on your choice will display a series of questions with each question triggering a different set of questions. I was wondering if there was a name for this or any open-source library that someone has made for this kind of app.
If not, would it be best to hard code all the questions and responses into the app, or to read them from a file?
3
This is a tree.
Your nodes are the questions, and each question has as many children as there are answers to it. Your session context contains the current question, and it leaps forward by asking a new one.
We could also say that this tree is a directed bipartite graph with each question node connecting to multiple answers, and each answer node connecting to exactly one question node.
A good storage would be, for example, XML:
<questions>
<question id='3'>
How are you?
<answers>
<answer next_question_id='4'>Fine, thx</answer>
<answer next_question_id='5'>Badly</answer>
</answers>
</question>
<question id='4'>
Boy or girl?
<answers>
<answer next_question_id='8'>Boy</answer>
<answer next_question_id='9'>Girl</answer>
</answers>
</question>
<!-- ...-->
</questions>
Or, if you want to keep the tree visualized, you could try YAML:
root:
title: How are you?
answers: [
{
text: Fine, thx
next_question:
title: Boy or girl?
answers: [
{
}
]
}
{
text: Badly
}
]
A session might look the following:
So, how it is implemented MVC-wise is a different matter.
You have similar things in adventure games (infocom games) btw, but that’s much more advanced.
Edit: this thing is called a Dialog Tree, and you can find answers on StackExchange as well.
What kind of framework do you really need? A few view controllers, some nicely-laid-out views, and a sensible way to represent the data: you’re done. If you’re having trouble seeing how to write such a thing, I’d suggest that you start by deciding how to organize the data:
- What’s a question?
- Are questions organized into groups?
- How does the answer to one question affect the question that the program asks next?
“Twenty questions”-like programs have been around for decades. Here’s some ancient BASIC source code for Animals. And here’s output, thanks to Jeff Atwood’s article on a similar theme. Back when personal computers were very new, question-and-answer games were some of the first programs that were used widely in classrooms.
Also very old are text-based adventure games where the player is presented with a situation and a number of options. These kinds of programs aren’t very different from the kind of thing you’re describing.
So, all this is to say that the structure of your program is probably going to be pretty simple — I doubt you’ll need a framework to get you there.