5x5 tic tac toe problem using backtracking to make the computer's move (using C)

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.

Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
What you want is a function that estimates the value of a board state. This value might represent the likelyhood of the computer winning from that state. Thus, the function might return 1 for a winning state, 0 for a losing state, and 0.5 for a draw state.

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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
You may also want to take a look at this tic-tac-toe sourceforge project. It will do from 3x3 to 7x7 sizes:

http://ttt.sourceforge.net/

-Seth
Commented:
Hi benjambb,

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)
Commented:
it has to be a split between me and SethHoyt .... 250:250 ?
Commented:
How many X's or O's in a row do you need to win?
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.