Results 1 to 13 of 13

Thread: Newbie - looping AI in TicTacToe

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Why would you use a loop? You mean like
    Code:
    dim active as boolean
    while active =true
         'some AI stuff here
    loop
    something like that?

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You could go with a four step AI
    Task 1: Check if there are empty squares which causes user to win.
    Task 2: Check if there are empty squares which causes comp to win
    Task 3: Check if there are empty squares which causes user to win in next round whatever comp does.
    Task 4: Check if there are empty squares which causes comp to win in next round whatever user does.

    predefine the eight possible win-lines, having an array of 8X3 for each square coordinate winlineX(7,2) winlineY(7,2) which are pointers to the gamefield arena 3X3 Gamefield(2,2). Go systematically trough these eight lines and look for 2 possibilities, sum of (X)'s is 2 and sum of (0) are 0 user threatens comp, sum of (0)'s is 2 and sum of (X) is 0, comp threatens user. Now if youre smart enough you could solve task 3 and 4 with a little help of recursion.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    Wow - that just went right over my head - I am pretty new at this. Could you show me an example that i could deconstruct and figure out?

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Are you sure you have to do this to get Jr programmer position? If you want to create a simple game that is, don't even think about artificial intelligence.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    I think he just wants to see what my capabilities are. Even if it does not work very well. He wants to see that I have a very good knowledge on 2 things: If-Then statements and loops. I have already proven the If-Then on a Pay Calculator program I wrote - now he wants a single player TTT game with loops for the AI and I have the job. The 2 books I have don't really explain loops very well and I am having a tough time with them. I know this is sort of fudging it, but I really want to get into programming - and I know if could get my foot in the door I could make up for it after. Any help would be extremely appreciated.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    you can use a loop to check for a tie...like this:

    Code:
    dim IsBoardFull as boolean 'when true the board is full
    IsBoardFull = true
    dim i as integer 'used to the loop
    
    for i = 1 to 9  'this will check your array (board)
         if board(i) = "" then IsBoardFull = false
    next i
    
    if IsBoardFull = true then 'the board is full
    P.S. if your boss is trying to see what your capabilities are, I think you should try this on your own.

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Okay I'll try to explain what I think Kedaman is suggesting.

    You have your board, a 3x3 array like this
    _ _ _
    |_|_|_|
    |_|_|_|
    |_|_|_|

    If you number them 1 to 9 like this:

    123
    456
    789

    then you have the 8 possible winning lines of 3 here in this list:

    123,
    456,
    789,
    147,
    258,
    369,
    159,
    357.

    So what you do is you create a second array to represent the list you have, so that each row in the array represents a winning line. This array will be 8 rows by 3 columns. Call this the lookup array.

    When you add a 0 or X to the 3x3 grid array, you would add that same 0 or X to each of the entries In the lookup array. So, say you had this grid:

    0|_|_
    0|X|_
    X| |0

    Your lookup array would be like this:

    0 _ _
    0 X _
    X _ 0
    0 0 X
    _ X _
    _ _ 0
    0 X 0
    _ X X

    Now what you do is go through each row (this is a loop) and check each of the cases on Kedaman's list (that could be another loop, or not). So you would pick the best row from this (if a move is an instant win you pick that one, otherwise if there's one to stop your opponent winning pick that, otherwise check cases 3 and 4 as Kedaman said). Based on this you could pick the appropriate row.

    You can either keep this separate array up to date with a duplicate of the info in the grid array, by adding a 0 or X to it in the appropriate places every time a move is made, or you can keep the X & Y coordinates of the equivalent square in the grid array, and look up in the grid array based on those coordinates whenever you want to know what's in the lookup array.

    I hope that's fairly clear, I can see it might seem confusing from the way i've written it.
    Harry.

    "From one thing, know ten thousand things."

  8. #8
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I was going to say something like that, but HarryW beat me to it. But, he probably explained it better than I would have. I suggest doing a loop where it starts when you take your move. I suggest also to have 2 or 3 difficulty levels (if you want) which would do a better job than the last (easy, medium, difficult, maybe even impossible) (impossible would always make the right move, so the only way to beat him would be to set him up.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  9. #9
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    Quite helpfull, guys!

    I really appreciate it!

  10. #10
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    I am almost done with this - just need to finish the End Game win-check loop. I did end up using 3 difficulty levels:

  11. #11
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116

    Here is what I have so far...

    Please let me know if you can help me condense some of the massive code via loops...
    Attached Files Attached Files

  12. #12
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    ...

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    !!!!!!!!!!!!!!!!!!!!

    Why so much code? Loops should cut down on that a lot! And I'd use a control array instead of seperate controls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width