I used to do a fair amount of game programming, so all the AI talk piqued my interest. Most game AI's are simpler than you might think, and don't need to employ machine learning. So I thought it'd be a fun exercise to create a machine learning based AI.
I chose the very simple game of Tic Tac Toe for ease of implementation. When creating my ML-AI, I strictly adhered to only using statistical analysis to make the next move. It would be trivial to design an AI using determinism to play a "perfect" game (always resulting in win/tie) since there are only 8 winning combinations for either 'X' or 'O'. But where's the fun in that?
The program allows you to build historical data by running simulated games using either a "random position" AI, or our ML-AI. The more data points you can collect, the better. Each game is serialized, saved, and then used for analysis by the AI when deciding its next move.
The game data file looks like this. We can run thousands, even millions of simulated games to build our position stats.
Each AI move is given a rollup of stats for each available position, so it can decide which position on the game board most often leads to a victory. There's a little more to the AI, but we are strictly enforcing a non-deterministic policy in our algorithm.
The AI is far from perfect, and certainly makes some bone headed moves sometimes. But you can see the AI learn and get better the more game data points you feed it.
You can find the .Net Core console app source here on GitHub. I've made it easy to implement your own AI by implementing the IAnalysis interface.
Contribute to mtraindog/TicTacoToe development by creating an account on GitHub.
github.com