I have made a Tic-Tac-Toe game for 2 players. Now, I want to give the game Artificial Intelligence.
So that game can be played between 1 player and computer.
Please, help How do I start?
解决方案
With Tic Tac Toe it's not so much an AI but a lookup table: For each possible board layout, find the best spot.
XKCD has such a lookup table. Basically each Board Layout gets a unique ID and the address of the field where to set the next mark. Wikipedia has that table in another format.
The table works like this: X goes first, then O.
X puts his X into one of the 9 cells. When O goes, there are now 9 possible Board Layouts, depending on which Cell has the X:
X | |
----+----+----
| |
----+----+----
| |
If you look at the map of O, there are 9 big grids in it, and the one in the top left has X in the top left spot, so that is the one to use. Place O in the Middle.
Now when X goes again, it needs to find this board layout:
X | |
----+----+----
| O |
----+----+----
| |
You will find this in the middle. Red is where to put the X in the XKCD image, and that shows you put it in the lower right:
X | |
----+----+----
| O |
----+----+----
| | X
Now, O goes again and looks for the above board layout, which is in the bottom right small grid in the top left big grid. O needs to be placed into the middle bottom:
X | |
----+----+----
| O |
----+----+----
| O | X
And so forth. The diagram is a bit hard to read at first (click on it to enlarge it) as it's nested, but as said: You create a Lookup table that has each unique board layout and information where to put the next mark.
This creates a perfect opponent though: The computer will never ever lose. How to make him more human is then fine-tuning (e.g., randomly discard the choice and place the mark in a random cell)