Teaching the Computer to Play Tic Tac Toe
Jan 10, 2023Tic Tac Toe is one of those games it seems everyone knows how to play. The rules are simple, it takes no special pieces or equipment, and it is a great way to pass a few minutes of idle time. When both players play well, games almost always end in a tie.
The origins of Tic Tac Toe can be traced back to ancient Roman times. There is even some evidence that the ancient Egyptians played a game that might have been similar.
Games like Tic Tac Toe make great projects for those just learning computer programming. Because the rules are simple, the programmer does not need to focus much on learning how to play. Instead, the programmer may concentrate on analyzing the game and thinking about all the individual steps that occur while playing. Collectively, these steps form an algorithm from which a computer program may be written.
Assume that you are a programmer creating an application where a human plays Tic Tac Toe against the computer. The human player will make selections by clicking in one of the grid squares. How do we know if the program really should award that location to the human player?
When two people play the game, both will most likely know the rules and will almost intuitively know when they have been violated. The computer, though, must be given explicit instructions for how to enforce the rules. So, when the human player clicks on a location, the computer must:
- Ensure it is actually the player's turn
- Ensure the selected location has not already been taken by either the computer or the player
Once the player has made a valid selection, what happens next? You might say that it is now the computer's turn. However, there's a step that we as humans perform, perhaps without recognizing it as an isolated task. Before allowing the opponent to take a turn, the two players must determine whether the previous move won the game. Further, since ties in Tic Tac Toe are so common, it is important to determine also whether there are any moves left to be made. That, too, is a condition which causes the game to end. Two humans will do all this with very little thought, but the computer needs much more guidance.
The task of checking for a winner is another thing we as humans probably do not really think about. We glance at the board, and if we see three in a row, we know whoever is using that symbol has won the game. Again, though, the computer has to be told precisely how to check for all the winning combinations. (Can you identify all eight of the unique winning combinations?)
Some people believe that one has to be super smart to be a computer programmer. While it so happens that many computer programmers are super smart, I do not believe that to be a pre-requisite. Instead, I think anyone who has the ability to think logically about how people accomplish tasks can become a very successful computer programmer. Much of what one does as a programmer is not hard. Instead, it requires an ability to think in very small steps and about all the possible options that can occur.
Stay connected with news and updates!
Join our mailing list to receive the latest news and updates from our team.
Don't worry, your information will not be shared.
We hate SPAM. We will never sell your information, for any reason.