Converting a DFA to efficient code?

  • Thread starter Thread starter kolleamm
  • Start date Start date
  • Tags Tags
    Code Dfa
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.
 
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
 

Similar threads

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