In the last week, I created a program to play tic-tac-toe. I chose this challenge for a couple of reasons:
- I wanted to see if I could design a mathematical algorithm that would evaluate the winning player or next best move.
- I wanted to see if I could design a game where the computer could play against a human player.
- I had toyed with the idea of programming a tic-tac-toe game before, but I had never taken my concepts to completion.
I’m happy to say that I was successful in creating a tic-tac-toe game where a player competes against the computer. Depending on the human player’s move, the computer decides what the next best move is.
I purposely chose not to look at any other examples of how to code a tic-tac-toe game during my adventure. I wanted to see if I was able to do it my way, without being influenced by another person’s approach. I was a little worried that my approach would be the most convoluted and inefficient approach imaginable. Once the game was complete, I did look at other people’s solutions and found out that I wasn’t that much different than what was already out there.
Lessons Learned and Data Collection
There are two interesting outcomes from programming a tic-tac-toe game.
Lesson Learned: With a little bit of modification, I now can have a game that can play tic-tac-toe against itself. If anyone chose, you could create a game that started on a random square, and the algorithms would do the work to play the game after that. The computer could play against itself for hours.
I also set up the game to log all of the moves that each player made. The data collection includes the way that games are played to completion. The most interesting thing about collecting this data is that potentially you could write machine learning code later that would optimize the way that the computer can win at the game of tic-tac-toe. With a large enough dataset, you could have the computer examine whether the static algorithms that are hard-coded are the most efficient or if there is a better way to win at tic-tac-toe.
Coding a simple game of tic-tac-toe can be fun. It also provides for more entertainment in the future if you were to use this as the basis of future data collection and machine learning experiments.