# Building a board game with the TFLite plugin for Flutter

DevFeed: [Building a board game with the TFLite plugin for Flutter](<https://devfeed.tech/articles/building-a-board-game-with-the-tflite-plugin-for-flutter-7389.md>)

Original publisher: [Read original article](<https://blog.tensorflow.org/2023/10/building-board-game-with-tflite-plugin-for-flutter.html>)

Author: TensorFlow Blog (noreply@blogger.com)

Published: 2023-10-18T17:00:00Z

Content type: tutorial

Language: en

Sources: [The TensorFlow Blog](<https://devfeed.tech/sources/the-tensorflow-blog.md>)

Topics: [Flutter](<https://devfeed.tech/topics/flutter.md>), [TensorFlow Lite](<https://devfeed.tech/topics/tensorflow-lite.md>), [Inference](<https://devfeed.tech/topics/inference.md>), [Reinforcement learning](<https://devfeed.tech/topics/reinforcement-learning.md>), [Tensorflow](<https://devfeed.tech/topics/tensorflow.md>), [TensorFlow Agents](<https://devfeed.tech/topics/tensorflow-agents.md>), [cross-platform](<https://devfeed.tech/topics/cross-platform.md>), [Mobile](<https://devfeed.tech/topics/mobile.md>), [board-game](<https://devfeed.tech/topics/board-game.md>), [Android](<https://devfeed.tech/topics/android.md>), [iOS](<https://devfeed.tech/topics/ios.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [building](<https://devfeed.tech/tags/building.md>), [cross-platform](<https://devfeed.tech/tags/cross-platform.md>), [flutter](<https://devfeed.tech/tags/flutter.md>), [games](<https://devfeed.tech/tags/games.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [inference](<https://devfeed.tech/tags/inference.md>), [ios](<https://devfeed.tech/tags/ios.md>), [learn](<https://devfeed.tech/tags/learn.md>), [mobile](<https://devfeed.tech/tags/mobile.md>), [plugin](<https://devfeed.tech/tags/plugin.md>), [reinforcement-learning](<https://devfeed.tech/tags/reinforcement-learning.md>), [tensorflow](<https://devfeed.tech/tags/tensorflow.md>), [tensorflow-agents](<https://devfeed.tech/tags/tensorflow-agents.md>), [tensorflow-lite](<https://devfeed.tech/tags/tensorflow-lite.md>), [tutorial](<https://devfeed.tech/tags/tutorial.md>), [tutorials](<https://devfeed.tech/tags/tutorials.md>)

## AI overview

This tutorial shows how to port a TensorFlow Lite reinforcement-learning board game to Flutter. It loads a converted model, runs inference to choose the next move, and renders the game for Android and iOS using the Flutter frontend and TensorFlow Lite plugin.

## Source excerpt

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 ar