Tic Tac Toe AI

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator:Moderators

Post Reply
User avatar
ghosstt
Senior Member
Posts:1551
Joined:Mon Feb 26, 2007 4:14 pm
Tic Tac Toe AI

Post by ghosstt » Mon Jan 07, 2008 8:08 pm

can someone explain to me how to do a tic tac toe AI?(im using vb.net, but if you could just explain it to me somehow)

User avatar
gannon
Moderator
Posts:6974
Joined:Sun Apr 04, 2004 4:48 pm
Location:Near that one big lake
Contact:

Post by gannon » Mon Jan 07, 2008 8:17 pm

Because of the low complexity, I'd generate all placement permutations and allow for the AI to pick the next best placement based on the current layout.

User avatar
ghosstt
Senior Member
Posts:1551
Joined:Mon Feb 26, 2007 4:14 pm

Post by ghosstt » Mon Jan 07, 2008 8:37 pm

thats the problem.. how do you figure out the next best move? :lol:

User avatar
gannon
Moderator
Posts:6974
Joined:Sun Apr 04, 2004 4:48 pm
Location:Near that one big lake
Contact:

Post by gannon » Mon Jan 07, 2008 8:47 pm

You find all the permutations left in the game and use the next move which results in the highest number of possible wins for you (while making sure your move won't automatically make the other player win).
Say you have a game with (AI as O):

Code: Select all

XOX
OXX
O**
Permutations under it are:

Code: Select all

XOX
OXX
OOX

Code: Select all

XOX
OXX
OXO
You'd choose the latter since it doesn't give a win to the other player.

User avatar
G-force
Moderator
Posts:3609
Joined:Mon Apr 19, 2004 1:43 pm
Location:Sweet home Indiana
Contact:

Post by G-force » Mon Jan 07, 2008 11:14 pm

I just watched WarGames last night. Now I see this. :P The only defense against a war robot and you're making it useless. I tell you, them robots will be the death of us all!!!

Adam G
Posts:212
Joined:Wed Aug 22, 2007 7:33 pm
Location:Olympia, WA
Contact:

Post by Adam G » Tue Jan 08, 2008 1:34 am

First, I use VB6 but it's the same concept...
Well, I'm not very advanced in VB6, at all, but couldn't you do something like...
1. Check to see if you can win...

Code: Select all

   -  If Btn1.Caption = "x" Then
      -  If Btn2.Caption = "x" Then
         -  Btn3.Caption = "x"  (Probably use a function, this is an example.)
         -  Msg = Msgbox("I win!")
         - Yada Yada
      - End If
      - Repeat for all surrounding buttons
   - Repeat for all buttons
2. Check if the opponent is going to win...

Code: Select all

   -  If Btn1.Caption = "o" Then
      -  If Btn2.Caption = "o" Then
         -  Btn3.Caption = "x"  (Probably use a function, this is an example.)
         - Yada Yada
      - End If
      - Repeat for all surrounding buttons
   - Repeat for all buttons
3. Pick a spot...idk really, random or maybe based on a Tic-Tac-Toe guide? Like, a corner, adjacent corner, ajacent corner, open spot between for win or w/e


Hope that helps! Doubt it but you never know. :P

User avatar
ghosstt
Senior Member
Posts:1551
Joined:Mon Feb 26, 2007 4:14 pm

Post by ghosstt » Tue Jan 08, 2008 3:21 pm

lol, i already have all the wins and stuff. 2 player works fine. I just need to do the AI for picking smart spots.

User avatar
Edutainment
Posts:423
Joined:Tue Feb 20, 2007 8:33 pm
Location:Ontario, Canada

Post by Edutainment » Sat Feb 02, 2008 2:16 pm


Crimehawk
Posts:43
Joined:Thu Apr 19, 2007 5:59 pm

Post by Crimehawk » Mon Feb 11, 2008 8:46 pm

Do you want it to win or draw everytime? There's a very simple way to do this on offense, and then defense there's a couple more options.

Or do you want there to be a slight chance that the player can win? (I guess you could do like something with random to easily do that)

Matt

bobbypig
Posts:6
Joined:Sun Sep 06, 2009 10:04 am

Re: Tic Tac Toe AI

Post by bobbypig » Sun Sep 06, 2009 10:37 am

use the
xxx
xxx
ooo

way of coding, although I'd just make it one string xxxxxxooo

the invincible tic tac toe strategy is easy to learn (just google it). Then just code in every situation

User avatar
hackmodford
Portablizer Extraordinaire
Posts:651
Joined:Fri Aug 21, 2009 10:44 am
Location:Michigan
Contact:

Re: Tic Tac Toe AI

Post by hackmodford » Sat Sep 12, 2009 3:17 pm

I made a tic tac toe game a long time ago with AI.

This is basically what I did if I remember right. I just coded for every possible move. And then to make the computer stupid :wtf: I would add a little randomness so that it couldn't win or draw every time.

Post Reply