## Monday, April 6, 2009

### Artificial intelligence in games, part 2

In the previous post, we discussed the fact that tic-tac-toe is finite game, and you can even calculate all the possible states of the board: it's 19,683, with 362,880 different ways to get to the legal states.  Now we can talk about how to take advantage of this calculation to always win the game.

Let's imagine that we pit two perfect computer opponents against each other.  The first opponent, playing as "X", will want to evaluate each of the nine possible moves on a blank board.  How does it know which is best?

Well, starting in the corner, the question is: "Do I have a sure win on this path?  If so, then I will definitely make this move.  If not, then does my opponent have a sure win?  If so, then I will definitely not make this move."

But how do you know if you or the opponent has a sure win?  Simple: Imagine that you already made this move, and then try to make the best move for the opponent.

You can easily see that this is a recursive process.  It has a recursive step ("Let me try this move") and it definitely has a terminating condition.  After all, if you come to a step where (a) the board is full, (b) X has won, or (c) O has won, you don't need to look ahead to any other moves.

Here's an example of how the computer would evaluate the first possible path.

`|  X |   |       X | O |       X | O | X| ---+---+---   ---+---+---   ---+---+---|    |   |         |   |         |   || ---+---+---   ---+---+---   ---+---+---|    |   |         |   |         |   |||  X | O | X     X | O | X     X | O | X| ---+---+---   ---+---+---   ---+---+---|  O |   |       O | X |       O | X | O| ---+---+---   ---+---+---   ---+---+---|    |   |         |   |         |   |||  X | O | X| ---+---+---|  O | X | O| ---+---+---|  X |   |`

This game ends in a win for the X player.  So if you are the X player, you like this game.  If you  are the O player, you do not like this game.  So the computer then backs up by two moves ("waking up," in the dream analogy) and assumes that O would not go there, but would instead do this:

`|  X | O | X| ---+---+---|  O | X || ---+---+---|  O |   |`

Of course, this game will also result in a win for X if you follow it to the conclusion, which is this:

`|  X | O | X| ---+---+---|  O | X | X| ---+---+---|  O | O | X`

You get the idea.  The computer will play every game to its ending, at which point it will know exactly which games guarantee a win for one player or the other.

Now the pruning begins.  The X-playing intelligence will try to reach one of the game states that guarantees a win for X.  If it cannot, it will at least avoid reaching any game state that guarantees a win for O.

Writing an unbeatable tic-tac-toe opponent in this way is actually pretty straightforward.  You can write it to pick the first "good" move, where "good" means "First try to pick a move that guarantees a win; then try to pick a move that doesn't guarantee a loss."  If you do this, you'll find that your tic-tac-toe game will always play in the same way.  For instance, in the first move, the upper left corner is as good a move as any (the O play can never force a win).  So your program may well always start in the upper left.  This program will not appear to be intelligent.  But it will never lose.

If you want your program to be more interesting, throw in a randomizer.  If there are multiple moves that guarantee a win, then pick one at random.  Otherwise, if there are multiple moves that don't guarantee a loss, pick one of those.  Then you'll have a program that never loses, and also it will make moves that appear unpredictable.

Even though tic-tac-toe is an unwinnable game with two perfect players, some moves are better than others.  Against novice opponents, there are tricks you can use to moderately increase the likelihood that your opponent will make a mistake and lose.  This program doesn't know that.  It doesn't evaluate different positions as tactically stronger.  It just knows whether or not one player is guaranteed to win.

By the way, needless to say, this tic-tac-toe program does not have sufficient reasoning capacity to prevent it from launching a global thermonuclear war.  ;)  The computer doesn't have abstract thoughts; it's just following instructions.  Completely deterministic instructions for winning if it can.

Next time: Chess as a deterministic game.

1. "The computer... is just following instructions... Completely deterministic."

Just like a much less complicate human brain, eh? ;)

2. Just like the human brain. :D

3. I suppose you were both compelled to write the same message due to your lack of free will.

4. I'm enjoying the games examples, Kazim. Wargames was a seminal movie for me in my adolescence, developing a budding interest in computers (although initially as just a way to play games.)

5. "I suppose you were both compelled to write the same message due to your lack of free will."
Yes, absolutely. I'm on Denis's side on this one.

6. I think the term free will meaningless because the people who support free will and the people who don't support it have different definitions of what free will is. I also don't see the point of the debate because unless someone is going to sink into a pit of despair because they don't think they have free will, whether or not we have free will doesn't effect us.