What is Tic-tac-toe?
Tic-tac-toe is a two player turn based game played on a 3×3 grid with X and O tokens. Usually X plays first. The objective of the game is to be the first player to place three tokens of the same kind in a row, column or diagonal on the grid.
Here is a game in which the first player, X, wins on column 1 after 7 moves:
An Overview: Architecture
Now that you have been reminded about the game of tic-tac-toe, let me describe to you the design and architecture of the command-line application I wrote to play the game.
The application is composed of three sub-components. The game engine, the computer AI and the command-line interface.
The game engine implements and enforces the rules for the game of tic-tac-toe, i.e. it abstracts and modularizes the game’s logic. This helps to keep the parts of the game that impose the rules completely separate from the interface used to present and interact with the game.
The computer AI sub-component implements three levels of computer intelligence: novice, intermediate and expert.
And finally, the command-line interface is the sub-component that handles the intercommunication between the other two sub-components. It also provides the display and control mechanisms for a human user to view and play the game.
Another Overview: Implementation
I decided to implement the application in the Scala programming language. “Why Scala?”, you may ask. Well, for the simple reason that it is currently the language in which I am trying to become competent and proficient. As a rule, when I’m learning a new language I try to implement some non-trivial programs that will get me to utilize a wide variety of features of the language. Over the years I have found that writing a tic-tac-toe application is one of the best ways for me to accomplish that goal. As I proceed to delve deep into the implementation details of the application in other posts I will try to remember to point out various language features that were convenient or absolutely necessary in order to achieve the particular result I desired.
There are four packages that comprise the application:
- tt.dwaynecrooks.games.tictactoe – It contains the core classes. Of importance are the Engine, Grid and Token classes.
- tt.dwaynecrooks.tools.fsm – It contains a set of classes that together implement a simple domain specific language (DSL) for a command driven finite state machine. The Engine class makes full use of the DSL and is a lot more maintainable because of it. This DSL was a joy to implement. The Scala language really made this relatively easy to do.
- tt.dwaynecrooks.games.tictactoe.ai – It contains the classes that implement the artificial intelligence for the novice, intermediate and expert computer players. There is a Strategy trait that the classes Novice, Intermediate and Expert implement.
- tt.dwaynecrooks.games.tictactor.cli – It contains the TicTacToe class which implements the command-line interface for the game. The TicTacToe class is also the entry point for the application.
And finally, here is a diagram showing the relationship between the various classes:
The particular way in which the TicTacToe class makes use of the Strategy classes will be discussed when I am explaining the implementation details of the TicTacToe class.
Hopefully at this point you have a general idea of the design, architecture and implementation of the application.
This post has gotten long enough and I am simply getting exhausted explaining all this stuff and drawing all these diagrams. So, in Part 2 I will begin to dig deep into the implementation details. Until then, happy coding.