I need some example source code to figure out how to make a intelligent computer move using backtracking. I want to make a tic tac toe problem only use a larger (5x5) grid so its not always guaranteed a draw playing a smart user.

I have made code to make a random move, but to make the game competitive I want to use the bactracking/tree method so that the user has to think about how to beat the computer opponent.

I have made code to make a random move, but to make the game competitive I want to use the bactracking/tree method so that the user has to think about how to beat the computer opponent.

http://ttt.sourceforge.net/

-Seth

First you need to identify and construct your search space... intially any move is possible and hence your search space would be the entire tree.

Next your main task would be to design a heuristic function... this function will analyze a move and tell the you how appropriate that move is... How smart your program will be depends entirely on how good your heuristic function will. A very simple heuristic function can be ::

check how close computer is to win or in other words how many more minimum number of moves will secure a win ( irrespective of opponents moves ) e.g. consider this board position

0 | X | 0 | X | X

X | 0 | 0 | | X

| | | |

| X | | 0 |

| | | |

computer is playing with 0 and human with X and its computers turn

In such situation, our heuristic will tell us that (3,3) and (5,5) are the two best moves as both of them will leave us just one step away from winning. On a scale of 1 to 5, these moves would be rated 4. All other moves will leave us farther from victory than these. However, if board is positioned like this

0 | | 0 | X | X

X | | 0 | | X

| | 0 | |

| | | 0 | X

| | | | X

our heuristic will tell us that (3,4) and (3,5) are the best moves. Though they bring us closer to win, but that win will never materialize as opponent will win before we can. This happened because our heuristic function was not "smart" enough to take into consideration the opponents moves. (Never underestimate your enemy ;o) )

So we have to move on to some smarter heuristic like::

check how close computer is to a win and how far opponent is from a win... we use the same logic as before "how many more minimum number of moves will secure a win " but we add to it "how many more minimum number of moves hence will give victory to the opponent". The difference between the two is our guiding light

Clearly with the new heuristic, we would get to play (5,3) in the second board position...

As you would have observed, the smarter the heuristic, the better the play.

If you search through the entire tree, you would like to select a move which maximizes your gain and minimizes the opponents game at every alternate level. This is minimax search.

Minimax is a game-playing algorithm which is used to search a game tree. In games where opponents alternate taking turns which affect a board position (chess, checkers, etc...), a given position can be thought of as a node on a tree. All positions reachable from a given position are, therefore, children nodes on the game tree. Minimax recursively evaluates positions on a tree with the intent of selecting the best move for a given player in a given position. In order for a minimax routine to work a function that maps a board position into a ``score'' is needed. In two-player games often this evaluation function returns real values between -1 and 1.

definition from : http://www.darkridge.com/~jpr5/archive/alg/node181.html

there is wealth of information on the web regarding minimax search... explore it... if you have any queries feel free to ask...

Hope that helps

Cheers

Sunny:o)

If 5, the game is a guaranteed a draw playing a smart user,

If 3, the game is a win for the firsr player.

As a heuristic, I would number of plays on each row, column, and the diagonals

If both players occupy a row or column, that row or column is useless for a win,

otherwise, each row or column could be ranked according to how close it is to a win, you could then play on the square with the highest combined row and column scores

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

For other cases, this function either needs to make an estimate using some kind of heuristic (such as the least number of moves required to win) or by trying different moves and returning the value for the best move for whoever's turn it is. Thus, if it is the opponent's turn, the function would call itself recursively for different boards that result from different moves, then return the minimum of the values returned, since the opponent would choose the move with the worst result for the computer. For boards that represent the computer's turn, the function would return the maximum of the values that result from different turns the computer could take.

Because of the alternating minimum and maximum, this is sometimes called a minimax or min-max search. Try searching google for minimax or try this link for more info and code examples:

http://ai-depot.com/LogicGames/MiniMax.html

-Seth