Computer Science Canada

Tic-Tac-Toe A.I. WalkThrough.

Author:  Cinjection [ Wed Jun 22, 2005 5:59 pm ]
Post subject:  Tic-Tac-Toe A.I. WalkThrough.

Warning:
If you do not have some exeriance in Visual Basic programming, please don't bother, you'll get too confused. You must know multidimentional arrays and looping.

Intorduction:
OK so you're making a tic-tac-toe game for yourself. Wonder how to make the player think instead of randomly moving. I will show you how to code a A.I. effectivly.

The A.I. :
First, we need to delcare a multidimentional array called grid that will track the values of the X and O's. We do this with this code:
code:

Dim grid(1 To 3, 1 To 3) As Integer

They way this works is, the first array represents the vertical position, while the second represents the horizontal position; It looks like this:

1,1|1,2|1,3
2,1|2,2|2,3
3,1|3,2|3,3

Now when the player does we will assign a value of 2 to the correct grid position. So if, lets say the player goes in the middle, the code would be:
code:

grid(2,2) = 2

And for the computer, we will assign a value of 5 so it would be:
code:

grid(2,2) = 5


Now for the A.I.. First, we will see if the computer can win, because winning should take priority over blocking. So we will check for a value of 10(5+5) over all the possible 2 possitions. We can do this using two for loop. i and j.
We can the use them like this:
code:

AIsum = grid(2, i) + grid(3, i)

That would see if you could win in the following ways

. . .
| . .
| . .

and
. . .
. | .
. | .

and
. . .
. . |
. . |

You would redo this for all the block and win oppertunities by changing the i value in different places. For some of them, namely the diagonals, you can't use this method and will have to type it out.

It will check using a varaible called sumn. For a value of either 10 or 4(4 is needed for a block)(2+2). The difference between the two is the j loop. It works like this:
code:

If (j = 1) Then
sumn = 4
Else
sumn = 10
End If

The loop will occur twice. Once to check for a block and one two check for a win.

Also you could use this to check for both a computer and player win. Just check for a value of 15 or 6.

Conclusion:

Well, I hope you learned somthing. A.I.'s are the funnest thing to code, in my opinion, so enjoy. If you want bit++ me other wise tell me what you thought of this tut. Also, if you want to see a working copy, you can download the one i coded here.

Author:  diqua [ Sat Oct 01, 2005 2:37 pm ]
Post subject: 

its interesting to see how the math really works in many AI instances

Thanks for the superb tutorial

Author:  Tony [ Tue Oct 04, 2005 8:29 am ]
Post subject: 

it's an alright start, though the tutorial is vague. Not much actual code or in-depth explanation.

tic-tac-toe A.I. wrote:

win
if can't then block
if can't then move randomly


That's pretty much it.

Author:  wtd [ Fri Oct 14, 2005 1:59 pm ]
Post subject: 

Add a step to that. Randomly have the AI choose randomly even when it could move for a win or to block.

Author:  RandomLetters [ Fri Jun 11, 2010 5:15 pm ]
Post subject:  Re: Tic-Tac-Toe A.I. WalkThrough.

Also remember to block forks

Author:  A.J [ Fri Jun 11, 2010 7:13 pm ]
Post subject:  RE:Tic-Tac-Toe A.I. WalkThrough.

"...and please make sure not to post on threads that date back to over a month or so ago (unless they are popular threads that are constantly replied to, of course"

Not cool...

Author:  USEC_OFFICER [ Fri Jun 11, 2010 8:08 pm ]
Post subject:  RE:Tic-Tac-Toe A.I. WalkThrough.

Or a sticky of course!

Author:  A.J [ Fri Jun 11, 2010 10:10 pm ]
Post subject:  RE:Tic-Tac-Toe A.I. WalkThrough.

You aren't helping USEC_OFFICER...well, frankly neither am I with this post. Well whatever, I guess this isn't as bad as what people usually do, and you are merely adding to the walkthrough, so I am sorry if I overreacted, RandomLetters.

Author:  RandomLetters [ Fri Jun 11, 2010 10:17 pm ]
Post subject:  Re: Tic-Tac-Toe A.I. WalkThrough.

Sorry, I did not realize a thread on the front page would have been so old.


: