开发者

Finite State Machine (FSM) and Android's Java

开发者 https://www.devze.com 2023-01-17 01:55 出处:网络
I\'m willing to develop a soccer game for Android. Because the complexity of the AI, i really think i need to design it using a FSM (Finite State Machine) and not with a monster switch.

I'm willing to develop a soccer game for Android.

Because the complexity of the AI, i really think i need to design it using a FSM (Finite State Machine) and not with a monster switch.

Googling around i found some FSM written in Java, but nothing explicitly compatible with Android: I wonder if开发者_如何学C there is someone here that used a FSM before for that platform.


I was looking for a FSM for Android a few months ago but couldn't find anything suitable, so had to create my own.

I used it in a few projects by now and quite happy. It makes code much cleaner and is easy to use. If anyone's still looking for a state machine for java, check EasyFlow out.

You can contribute to the project as well.


Android has an internal Hierarchical State Machine (HSM), which integrates with the Android message Queue.

It is internal, so you're not able to access it explicitly from the SDK.

However, if you follow the terms of the Apache 2.0 licence you can make your own version.
https://android.googlesource.com/platform/frameworks/base/+/gingerbread-release/core/java/com/android/internal/util/HierarchicalStateMachine.java

Edit:
This class is now called StateMachine.


This state machine (FSM) implementation used in Android and provided by Google seems to be a good option. You can get it here on Github.

You can use the StateMachine class to define states, the initial sate, you can perform tasks when you enter or exit a state, you can send events and delayed events in order to implement timeouts for instance.

I did not find many other alternatives, but this implementation contains all the features I was looking for. This is part of the Android project, so I presume this was good tested. Hope this help.


You can try out my Kotlin library with DSL for creating state machines. It helps me in Android projects.

https://github.com/nsk90/kstatemachine


Another open source option is Engine by @doridori

0

精彩评论

暂无评论...
验证码 换一张
取 消