Posted in .NET Development

Machine Learning – Tic Tac Toe in C#

A while back I created a Tic Tac Toe game in Python and trained an opponent (or bot) using an Artificial Neural Networks (ANN). At that stage I really wanted to try out some reinforcement learning, but since it was a pet project on the side and I’m not as comfortable with Python, I just didn’t end up getting to it.

So…. recently I thought, let me accept the challenge but do it in C#. It certainly was a lot easier to implement in .NET for me. I also got to finally get my hands dirty with Blazor WebAssembly which was great. Being able to run client side code in C# (no JavaScript) is really a revelation. There were some limitations with running MiniMax algorithm in WebAssembly on the Client-side if you want to check that out in the Part 2 video (below)

I’m happy to have finally wrapped this up, so I hope you enjoy it as much as I’ve enjoyed making it.

Play the game here: https://tictactoe.filteredcode.com/
Code on GitHub: https://github.com/NielsFilter/TicTacToe.Net
YouTube videos: YouTube: filteredCode – TicTacTic AI in C# Playlist

Part 1: Creating the game in c#

Creating the game and laying the foundations to create AI bots

Part 2: Creating a MiniMax algorithm bot

Creating a MiniMax algorithm bot

Part 3: Creating the Q-Learning bot

Created a Reinforcement Learning (Q-Learning) bot

More bots coming?

My goal was Reinforcement Learning and it worked out really well. Now that I’ve ticked that box I’m happy to leave it as is (for now). All this is done in my own private capacity and spare time is a luxury I don’t have too much of at the moment.

But…

If I were to pick this up again in the future, 2 ideas that I wanted to play around with are:

  1. Supervised learning algorithm – Linear Regression (or something similar)
    • The thought is to capture all states from other games and “featurize” the various states. For instance features like “Occupies Middle”, “2 of yours with an empty space” (naming is hard…)
    • With decent features defined, the plan would be to make use of ML.Net to train
  2. Use a Neural Network to solve
    • Not as important to me since I’ve done this in Python already
    • But the thought of testing out a package like TensorFlow.NET package and seeing if the feature set and level of support is good enough to do some production Neural Networks purely in .NET.