Converting a DFA to efficient code?

  • Thread starter Thread starter kolleamm
  • Start date Start date
  • Tags Tags
    Code Dfa
Click For Summary
Creating a deterministic finite automaton (DFA) for game character dialogue can be streamlined by utilizing appropriate data structures and programming techniques. Instead of relying on complex nested "if" statements, a more efficient approach involves using records to represent states and transitions. Each record can include the current state, next state, question text, and answer text, allowing for straightforward management of dialogue flow. For implementation, using a programming language like Java or C# is suggested, where relevant packages can facilitate the transition function and terminal states. Storing dialogue data in a structured format, such as XML, can enhance organization and scalability. The discussion also touches on the efficiency of using linked lists to manage state names and the potential advantages of using a data file over nested directories for better clarity and access. Overall, the focus is on simplifying the dialogue management process while maintaining efficiency and clarity in the code structure.
kolleamm
Messages
476
Reaction score
44
I've created a DFA for a game character's dialogue. I could probably code this using "if, if else, for, while" statements, but I feel like this would be impractical since there are about 20 states with 4 different inputs each.
Any ideas on how I could make this work? I would really like to avoid a lot of nested statements if possible.
 
Technology news on Phys.org
Maybe a case statement would be useful.
 
kolleamm said:
Any ideas on how I could make this work? I would really like to avoid a lot of nested statements if possible.

In order to simplify things as much as possible you have to use the right data structures combined with relevant premade code that the programming language you use offers you.

I don't know how complex is the DFA you refer to but just to give you a general idea about a complete DFA, if I had to implement it in Java, I would import the relevant packages (like List, LinkedList, Iterator ... just to name a few) and see what to use to implement the transition function, the terminal states etc. Then I would develop the right methods to make it work.
 
I would put everything in data. Just a whole bunch of records.
startstate#, nextstate#, "Question text", "Answertext"
display all of the questions with startstate# = currentstate, let the user choose one, and then set currentstate to the nextstate# belonging to the question text that was chosen and give the answer belonging to that question.
{1, 1, "Good morning", "Good morning"},
{1, 2, "I'd like to buy a book", "We don't have any"},
(2, 3, "But I was told to come here", "I hear the gooseberry's are doing well this year"}
etc.
More sophisticated systems would store this in an XML file. something like this
https://stackoverflow.com/questions/372915/game-logic-in-xml-files
 
I currently use C#.
I was thinking of using linked lists to store the name of the states which would then be called using methods, although I'm still unsure since efficiency is a concern.
willem2 said:
I would put everything in data. Just a whole bunch of records.
startstate#, nextstate#, "Question text", "Answertext"
display all of the questions with startstate# = currentstate, let the user choose one, and then set currentstate to the nextstate# belonging to the question text that was chosen and give the answer belonging to that question.
{1, 1, "Good morning", "Good morning"},
{1, 2, "I'd like to buy a book", "We don't have any"},
(2, 3, "But I was told to come here", "I hear the gooseberry's are doing well this year"}
etc.
More sophisticated systems would store this in an XML file. something like this
https://stackoverflow.com/questions/372915/game-logic-in-xml-files
Would a data file be better as opposed to nested directories? Such as C:/State/State#/Transition_State#...n
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 6 ·
Replies
6
Views
4K
Replies
11
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K