Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Tic-Tac-Toe A.I. WalkThrough.
Author Message
Cinjection

Posted: 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.

diqua

Posted: Sat Oct 01, 2005 2:37 pm   Post subject: (No subject)

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

Thanks for the superb tutorial
Tony

Posted: Tue Oct 04, 2005 8:29 am   Post subject: (No 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.
wtd

Posted: Fri Oct 14, 2005 1:59 pm   Post subject: (No subject)

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

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

Also remember to block forks
A.J

Posted: 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...
USEC_OFFICER

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

Or a sticky of course!
A.J

Posted: 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.

RandomLetters

Posted: 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.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 9 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: