index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html
![]()
本文介绍了如何将使用TensorFlow和JAX训练并部署在Android上的强化学习游戏应用移植到Flutter平台。通过TensorFlow Lite插件,开发者可以轻松在Android和iOS上运行简单的棋盘游戏,如Plane Strike。文章展示了加载TFLite模型、进行推理以及前端渲染的代码实现,并提供了进一步探索的方向,如转换TFAgents模型或构建新的井字棋游戏代理。
🎮 使用TensorFlow/JAX训练强化学习代理,并转换为TFLite模型,用于在Android应用中玩棋盘游戏。
📱 通过TensorFlow Lite Plugin for Flutter,将训练好的TFLite模型移植到Flutter平台,实现跨平台运行。
🧪 本文以Plane Strike游戏为例,展示了加载模型、进行推理以及前端渲染的完整代码实现,方便开发者参考。
🔄 开发者可以尝试将TFAgents训练的模型转换为TFLite,或利用RL技术构建新的井字棋游戏代理,并从零开始训练模型。
🤝 鼓励开发者利用TensorFlow/JAX工具构建更多有趣的游戏,并与社区分享成果。
Posted by Wei Wei, Developer Advocate

In our previous blog posts Building a board game app with TensorFlow: a new TensorFlow Lite reference app and Building a reinforcement learning agent with JAX, and deploying it on Android with TensorFlow Lite, we demonstrated how to train a reinforcement learning (RL) agent with TensorFlow, TensorFlow Agents and JAX respectively, and then deploy the converted TFLite model in an Android app using TensorFlow Lite, to play a simple board game ‘Plane Strike’.
While these end-to-end tutorials are helpful for Android developers, we have heard from the Flutter developer community that it would be interesting to make the app cross-platform. Inspired by the officially released
TensorFlow Lite Plugin for Flutter recently, we are going to write one last tutorial and port the app to Flutter.

Since we already have the model trained with TensorFlow and converted to TFLite, we can just load the model with TFLite interpreter:void _loadModel() async {
// Create the interpreter
_interpreter = await Interpreter.fromAsset(_modelFile);
} Then we pass in the user board state and help the game agent identify the most promising position to strike next (please refer to our previous blog posts if you need a refresher on the game rules) by running TFLite inference:
int predict(List<List<double>> boardState) {
var input = [boardState];
var output = List.filled(_boardSize * _boardSize, 0)
.reshape([1, _boardSize * _boardSize]);
// Run inference
_interpreter.run(input, output);
// Argmax
double max = output[0][0];
int maxIdx = 0;
for (int i = 1; i < _boardSize * _boardSize; i++) {
if (max < output[0][i]) {
maxIdx = i;
max = output[0][i];
}
}
return maxIdx;
}That's it! With some additional Flutter frontend code to render the game boards and track game progress, we can immediately run the game on both Android and iOS (currently the plugin only supports these two mobile platforms). You can find the complete code on GitHub.
|
If you want to dig digger, there are a couple of things you can try:Convert the TFAgents-trained model to TFLite and run it with the pluginLeverage the RL technique we have used and build a new agent for the tic tac toe game in the Flutter Casual Games Toolkit. You will need to create a new RL environment and train the model from scratch before deployment, but the core concept and technique are pretty much the same.
This concludes this mini-series of blogs on leveraging TensorFlow/JAX to build games for Android and Flutter. And we very much look forward to all the exciting things you build with our tooling, so be sure to share them with @googledevs, @TensorFlow, and your developer communities!