Advertisement

Complete TTT tutorial?

Started by January 14, 2005 11:18 PM
2 comments, last by Nice Coder 19 years, 10 months ago
I've been looking around the internet for a complete tic tac toe tutorial which implements an minimax algorthm, but one that also explains how the different functions of the program work, such as evaluation. I have found countless tutorials on minimax, but they automatically assume you know how to implement the aval function. Any help would be appreciated.
TTT eval is easy.

Basically, you look for a set of things.
First thing, is a winning situation (which is easy to find, you can do it with a few if statements, or a few xors...)
if you find one, give it +inf if you win. otherwise -inf.

if you find it, you return that value.

The next thing, is 2-in-a-rows.

Thats

0|N|0

Or
0|0|N

And so on.

N = Empty square

You give them a moderate score, say 115 each.

You then get each and every square, and for each one, you give it a value.

I would say something like

50|45|50
45|50|45
50|45|50

Of cource, you should change those values.


Now, you sum up both of those values, and return the sum, as your evaluation.

in code
If iswin(board, player) {   Return +inf;};if iswin(board, !player) {   Return -inf;};Cout = count2inrows(board, player) * 115for each square in the board.If it is taken then   if i took it then      count += squareval[index]   else      count -= squareval[index]   end ifend ifnext squareReturn count.


Simple, yet effective.

And very fast.

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Advertisement
Actually; TicTacToe is straight forward enough that you can hardcode moves, or do a full transveral of every move possible through the entire game. Just remember that good TicTacToe AI doesn't have to always win - it just has to never lose.
With TTT, And connect4, theres a neat way to handle the evaluation function (for the 2 in a row, the 3 in a row, ect.

What you do, is you have an array of bits.

Each bit represents wether something is possible.

For eg. If o makes a move, in spot 3, then it would be imposisble to X to get a diagonal win.

It would also be inpossibe for X to get a diagonal 2-in-a-row. (with intent of making a 3).

Also, A vertical and a horizontal 2 & 3 are impossible now, leaving only the sides for X to win.

You then only need to check the sides later and, if there has been a move on the side otherwise, nothing would change.

Just on the road, to a fast Evaluation function.

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.

This topic is closed to new replies.

Advertisement