Solved

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

Posted on 2003-11-04
Medium Priority
16,889 Views
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.

0
Question by:benjambb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 5

Accepted Solution

SethHoyt earned 1000 total points
ID: 9684676
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
0

LVL 5

Expert Comment

ID: 9684752
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
0

LVL 45

Assisted Solution

sunnycoder earned 1000 total points
ID: 9685740
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)
0

LVL 45

Expert Comment

ID: 9835205
it has to be a split between me and SethHoyt .... 250:250 ?
0

LVL 84

Expert Comment

ID: 9934700
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
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month11 days, 23 hours left to enroll