How to write a Tic Tac Toe game , and the source code for android ?
Among other things, the aim of writing this tic tac toe game, is showing what a computer is really about, which is just a machine which follow instructions. Instructions which are written by a human.
So, in other words, a computer is devoid from intelligence, or of a know-how. It can only follow instructions, which are written by a human being.
For the tic tac toe game, the programmer did some analysis of how the game is played, when a win is possible, when a tie is possible, and when losing is a possibility. He translated his findings into machine instructions, which the machine executes without any understanding.
So, for the tic tac toe game, winning is a certainty, when either of the player starts, leave the center open for grab, and the other player does not go for the center. If this happens, then the starting player aim, is to arrive to something similar, to what is shown in the following figure.
The other player can have a tie, if and only if, he goes after the center. Dumb moves, must also be considered by the programmer.
This being said, if the computer starts, then the instructions that he must follow, to win or to tie are:
If the computer is not the one to start, but the human player is, and if the human player leaves the center open, as to apply the aforementioned method, the computer can go for a tie, by just taking the center.
The first player in such an instance, can try to outsmart his opponent, hoping to open two threats, by choosing a square, which is not on the same row, or column, as the first square he has chosen.
The second player if he is smart to notice this strategy, can go for block, by using a two steps strategy. First, he just selects any square on the center row, where he has already chosen the center. The first player is forced to block him, and the second player just finishes his strategy, by checking the rows and columns, where the first player might have a win, and choosing the coordinate of the row and the column as his next move. In such a case the game is a tie.
On the other hand, if the human player starts, and goes for the center, a win, is to try to have two open threats, and a counter strategy, is just for the second player to occupy corners, which will block any threat.
The code for the machine instructions, when the human is to start playing are:
So, in this tic tac toe game implementation, the machine will always go, for either a win, or a draw. The computer being unable to think, it must be instructed what to do. The thinking part, or the instructions, is done beforehand by the programmer.
When the game is being played, what is being played against, is actually the strategies and understanding of another human. This tic tac toe game, can have additional strategies, as to make the game more enticing, for example the computer when starting, can try to go for a win, even if the player has chosen center.
The source code of this game will be posted shortly on GitHub, along with a link to the Google play store, when it is finally published, in the meantime, an APK will also be available from GitHub.