View previous topic :: View next topic |
Author |
Message |
Aviar³ Grandmaster Cheater Reputation: 50
Joined: 03 Jan 2008 Posts: 655 Location: Canada
|
Posted: Sun Mar 03, 2013 8:25 pm Post subject: Anyone here implemented a state machine in code? |
|
|
If so, how did you guys go about implementing transitions?
_________________
This is the inception of deception, checking the depth of your perception.
|
|
Back to top |
|
|
InternetIsSeriousBusiness Grandmaster Cheater Supreme Reputation: 8
Joined: 12 Jul 2010 Posts: 1269
|
Posted: Sun Mar 03, 2013 8:25 pm Post subject: |
|
|
shadeslayer
|
|
Back to top |
|
|
gogodr I post too much Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Sun Mar 03, 2013 10:02 pm Post subject: |
|
|
global variable
int a = 0;
on an event that moves the state to the next one::
- a++;
- if ( a == 3 ) a = 0;
states are 0 1 and 2
machine states are separated by looking at a
if (a == 0){
//do this
}
else if (a == 1){
//do this
}
else if (a == 2){
//do this
}
___
no?
|
|
Back to top |
|
|
Aviar³ Grandmaster Cheater Reputation: 50
Joined: 03 Jan 2008 Posts: 655 Location: Canada
|
Posted: Sun Mar 03, 2013 10:31 pm Post subject: |
|
|
Yeah, hardcoded is the simplest way of doing it, though hardly the optimum way I think. After all, how do you reuse the code? How do you minimize the machine? Etc etc. I'm doing it by having a class for State, StateMachine and Transition. A states simple enough, just a name (and optionally onEntry actions and onExit actions represented by a set of Function objects to be executed) and a statemachine is a collection of states and transitions, the current state and the current input. Transitions are simple in the sense of one having an fromState and toState property, but my problem is modeling the input property which represents what input triggers the transition as well as the best way to capture/represent transitions in a statemachine (for when having to search for what transition to take).
Also I don't think your model/implementation captures the possibility of moving from state 0 to an arbitrary state, such as state 2 or n. Also doesn't capture the idea of moving from a state 2 to a state 1 or 0.
_________________
This is the inception of deception, checking the depth of your perception.
Last edited by Aviar³ on Sun Mar 03, 2013 10:42 pm; edited 1 time in total |
|
Back to top |
|
|
gogodr I post too much Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Sun Mar 03, 2013 10:37 pm Post subject: |
|
|
I can't really assist you, I would like to but your saying is too general x.x I can not fully understand the problem you are trying to solve.
what I mean is that a "state machine" is too vague of a concept to start trying to come up with a solution.
Last edited by gogodr on Sun Mar 03, 2013 10:39 pm; edited 1 time in total |
|
Back to top |
|
|
Aviar³ Grandmaster Cheater Reputation: 50
Joined: 03 Jan 2008 Posts: 655 Location: Canada
|
Posted: Sun Mar 03, 2013 10:39 pm Post subject: |
|
|
The problem is representing the concept of statemachines (particularly finite statemachines without epsilon transitions) in a reusable manner. Reusable here means I can, during runtime, add transitions, states and carry out operations on the same (such as minimizing, finding dead states, paths, etc.).
_________________
This is the inception of deception, checking the depth of your perception.
|
|
Back to top |
|
|
gogodr I post too much Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Sun Mar 03, 2013 11:26 pm Post subject: |
|
|
just so I dont foget.
Code: | Class Transition{
State start,end;
String trigger;
void Transition(State state1, String input, State state2){
this.start = state1;
this.end = state2;
this.trigger = input;
}
}
class StateMachine{
List<State> states;
List<Transition> transitions;
void addTransition(State state1, String input, State state2){
bool check1,check2;
foreach (State ex in This.states){
if (ex == state1) check1 = true;
if (ex == state2) check2 = true;
}
if(state1 && state2){
transitions.add(new Transition(state1,input,state2));
}
else{
alert("Not valid States");
return
}
}
} |
|
|
Back to top |
|
|
|