Home > Software engineering >  What is the best way to implement a text-based adventure game in fluter/dart with a decision tree mo
What is the best way to implement a text-based adventure game in fluter/dart with a decision tree mo

Time:07-23

I am building a text-based adventure game as follows: I have 2 models Node and Message, in Node will contain:

  • 1 list of messages
  • 1 list of branching conditions
  • 1 list of child nodes My idea is to use Listview to print to the screen and when the user interacts with the conditions the data in that listview will be updated accordingly. The problem I have here is that if I use If - else it will take a long time to process all the cases if the plot is long. And the nodes are nested in a tree model, so it's quite complicated. Let me know the best solution to implement it. Thank you! enter image description here

CodePudding user response:

For the presentation I would not care about any if else statement. I would completely separate the story logic from the presentation. Thus, you could have a field in your UI which is always holds the latest message and you could always show that to the UI. Or a list of all previous messages and you always show the last element of the list.

Additionally, I would write a story manager class which handles the different routings of the story tree and updates the field in your UI which holds the latest message.

CodePudding user response:

There's no need to use any if-else if your condition points to the specific id of the next message and next conditions from two big lists with all messages and all conditions.

So, my humble solution is to have a list of all Messages and a list of all Conditions. Each Message has an id, and each Condition has an id. So the click on condition will invoke loading of the specific Message.id and several Condition.id. Condition model looking like:

class Condition {
final String id;
final String nextMessage; // here will be an id of the next message to display
final List<String> nextConditions; // and then you display next conditions

So in reality each condition will look like:

Condition(id: 'A1', nextMessage: 'B', nextConditions: ['B1','B2','B3']);

The architecture management of lists you can always improve and maybe come up with some clever ids so it is easier to understand which lead to which.

  • Related