[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Just finished TicTacToe, but How would I do some AI?



On Sep 22, 2007, at 3:38 PM, Lamonte Harris wrote:

Yah, http://wecodepython.info/code/TicTacToe/TicTacToe.py is the code and http://wecodepython.info/games/TicTacToe.zip is the exe, Does anyone know how to do AI, if so can someone explain the mechanism to doing it?

Depends on what your goal is. Tic Tac Toe is a simple "solved" game so it is possible to create an "ai" based on knowledge of every possible tic tac toe board and the ideal move to make in every possible situation. Mathematically correct, but IMHO not very much fun 8^)

Another approach would be a simple algorithm that picks moves like a human might. Think how you play the game; how do you decide what move to make. Here's a sketch of a strategy an ai might take. Note this looks ahead only for the next move (like a child player typically would):

1. Look for winning moves (i.e., a move that makes three in a row for the ai) 2. Look for blocking moves (i.e., a move that prevents a loss on the next turn)
3. Look for moves that make two in a row or surround an empty space.
4. Choose an arbitrary empty space.

The simplest approach would be to just go down the list and use the first strategy that returns a possible move. Since some may return multiple possibilities, a better approach might be to assign each strategy a score (Except for #1 which you always choose immediately if present) and score each move based on the strategies that choose it. That way if #2 and #3 select the same move, you would prefer it over others because it is probably a better move overall. You may be a better tic tac toe player than I, so you may come up with a better list that would make that type of scoring more beneficial.

Another totally different approach would be to make an ai that "learns" to play (sorta like the wopr in wargames). It has no preprogrammed strategy, but gets better the more it plays by learning from its opponent. This could be done any number of ways, some much simpler than others.

Since this is obviously a vehicle for you to learn with, you need to decide which one you want to learn. Of course nothing says you can't have multiple different AIs as well.

-Casey