I hope you don’t mind me posting a “game” that took me a day. Even though the result isn’t (and was never meant) to be something anyone would want to play, it’s a major accomplishment for me.
About a week ago I went to a lecture about AI, machine learning and related topics. The first part was dedicated to explaining the principle behind neural networks - something I never really thought about much, as I considered it way beyond my reach. What I failed to realize was that the governing principle behind it is relatively simple and throughout the talk I kept thinking that I just have to try this myself.
And so I did. The next morning I put together a simple Tic-Tac-Toe game and then created a simple learning AI. When I say simple, I really mean simple. There’s only one layer of “neurons” which note which positions on the board are taken by which sign. The AI starts by making random moves. If you play the same opening against it, you’ll notice that it’s trying other approaches if you beat it originally, but the progress is slow.
There’s an option to have it play 100 000 games against a random opponent. It takes a while, but the AI will be making better moves after that. After a million games (possibly sooner), the AI finally feels like a somewhat clever opponent (if you can call anyone who’s capable of losing a game of tic-tac-toe “clever”). It will make offensive moves, protect itself, and punish you if you leave two of the same for it unblocked. A lot of the time at least.
The play is far from perfect, and I’m reasonably sure that the AI peaks at this level and will never learn perfect play, but that’s exactly why I’m so stoked about this: making a perfect-play AI is easy. I’ve done it when I was a kid in Q-Basic - by listing every possible boardstate and giving it the best play. This AI, however, feels somewhat clever but it is still capable of mistakes, which is something much more useful when creating AI opponents. And the fact that it reaches this level of play despite the fact that I don’t know how to program an imperfect AI from scratch simply blows my mind. Really. I have created something that really feels like an intelligence. I have become god
the destroyer of worlds.
Oh and fun fact? This whole mess including the learning algorithm has fewer lines than that brute-force AI I made back in the day. I’ve definitely come a long way.
You can try it out on the community page.
- Tic-Tac-Toe is a lot easier to break apart once you realize you can mirror and rotate the moves, meaning for example that there are only three possible opening moves. I did not take advantage of this, because I was lazy and I really only wanted to see the AI learning in action. If I did, I’m sure the AI would reach its full potential way sooner.
- There’s an option for an AI vs AI battle, but note that it will not learn anything from it. I would have to split its memory into two, otherwise it would think its losing moves were correct because the match was won by an “AI”. That’s me being lazy again. While it seems like the AI would learn more playing against itself, though, it would probably end up just playing the same drawn game over and over again, since drawing a game is considered a slight success.
- If it seems some buttons are unresponsive, it probably means a game has started and waits for your input. A game in progress has to be finished before another is started.
- Creating an AI for games on a larger board would be a fun challenge and I might get into it at some point, but I’m way too busy with other projects (coding and otherwise) to even consider starting. I had to make this. I wouldn’t sleep until I knew I could. That’s done, though. Now I will focus elsewhere.
- Starting player is chosen randomly every time a game starts. It can be a little frustrating if you want to try to teach the AI to react to a certain opening. Again - me, lazy.
- If you’re interested in how neural networks work, you can play around with one here. Highly recommended.