At the climax of the 1983 film WarGames, the computer that’s about to start World War III is directed to play a game of tic-tac-toe with itself. Recognizing that the game is silly because experienced players often end play in a draw, the computer determines that nuclear war is futile. It decides not to blow up the world. This conclusion should add some excitement to this chapter, because you can equate any game of tic-tac-toe—even one simulated on a computer—to nuclear war.
Game play for tic-tac-toe is simple. It’s easy to code. If you haven’t yet done so, now is the time to write your own version of the game. Of course, it’s made more complex when you consider such tasks as:
- Coding a game loop
- Programming turns for players
- Determining when the game is over
- Adding the computer as a player
- Giving the computer some intelligence
The biggest hurdle you face when programming a text-mode game like tic-tac-toe is that I/O in C isn’t interactive. Unless you use a third-party library, such as Ncurses, you must rely upon stream I/O for your programs. It can work, but stream I/O brings potential problems to the table that the code must deal with, lest everything get hinky for the user.