Tic Tac Toe AI

This is a discussion on Tic Tac Toe AI within the C++ Programming forums, part of the General Programming Boards category; The examples I've been able to find on this board seem to use numerous if statements.. I was wondering if ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    3

    Tic Tac Toe AI

    The examples I've been able to find on this board seem to use numerous if statements.. I was wondering if there was another way? I've tried the following for statements, but they cause the program to crash when the function is called.

    Code:
    for(Row = 0; Row < Board.numrows(); Row++)
       {
       	if((Board[Row][0] == Board[Row][1]) && (Board[Row][2] == ' ') && (Board[Row][0] != ' ') && (Board[Row][1] != ' '))
          {
          	//Row = Row;
             Col = 2;
          }
          if((Board[Row][0] == Board[Row][2]) && (Board[Row][1] == ' ') && (Board[Row][0] != ' ') && (Board[Row][2] != ' '))
          {
            //	Row = Row;
             Col = 1;
          }
          if((Board[Row][1] == Board[Row][2]) && (Board[Row][0] == ' ') && (Board[Row][1] != ' ') && (Board[Row][2] != ' '))
          {
            //	Row = Row;
             Col = 0;
          }
       }
    
       for(Col = 0; Col < Board.numcols(); Col++)
       {
       	if((Board[0][Col] == Board[1][Col]) && (Board[2][Col] == ' ') && (Board[0][Col] != ' ') && (Board[1][Col] != ' '))
          {
           	Row = 2;
            // Col = Col;
          }
          if((Board[0][Col] == Board[2][Col]) && (Board[1][Col] == ' ') && (Board[0][Col] != ' ') && (Board[2][Col] != ' '))
          {
          	Row = 1;
            // Col = Col;
          }
          if((Board[1][Col] == Board[2][Col]) && (Board[0][Col] == ' ') && (Board[1][Col] != ' ') && (Board[2][Col] != ' '))
          {
          	Row = 0;
            // Col = Col;
          }
       }
    Yep.. this is a school assignment, but I'm not expecting it to be written for me, I just need a little nudge in the right direction. Any help would be appreciated, thanks.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    small snippet from recent game made for kid....
    Should be enough to help ya out...
    Code:
    int MainWindow::IsWinner()
    {
        // this holds the patterns we will check for
        static int pattern[8][3]=
        {
            0,1,2,
            3,4,5,
            6,7,8,
            0,3,6,
            1,4,7,
            2,5,8,
            0,4,8,
            2,4,6
        };
        // this loop does the checking
        for( int i = 0; i < 8; ++i)
        {
            if( (GameGrid_[pattern[i][0]] == EX) &&
                (GameGrid_[pattern[i][1]] == EX) &&
                (GameGrid_[pattern[i][2]] == EX))
                return EX;
            if( (GameGrid_[pattern[i][0]] == OH) &&
                (GameGrid_[pattern[i][1]] == OH) &&
                (GameGrid_[pattern[i][2]] == OH))
                return OH;
        }
        // no winner so return 0
        return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was wondering if there was another way?
    There are plenty of ways to write an AI, but for Tic-Tac-Toe, a short series of if statements is sufficient. When you get into games with more cases for every move, such as checkers or chess, you will need to look at more sophisticated techniques.

    >but they cause the program to crash when the function is called.
    You should step through the function's execution in the debugger. Quite likely you are accessing an index outside of your table's boundaries.

    >I just need a little nudge in the right direction.
    You'll hate me for this, but www.google.com should be your first stop when it comes to research.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    Ah, alright, thanks for the help.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: Tic Tac Toe AI

    Two comments.

    1) format your code a tad better. Long lines are very cumbersome to read no matter whether in an editor or on the forum. Statements can be listed on multiple lines:
    Code:
    if((Board[Row][0] == Board[Row][1]) &&
       (Board[Row][2] == ' ') && 
       (Board[Row][0] != ' ') && 
       (Board[Row][1] != ' '))

    Originally posted by Isamo
    The examples I've been able to find on this board seem to use numerous if statements.. I was wondering if there was another way? I've tried the following for statements, but they cause the program to crash when the function is called.
    Which statement causes it to crash?

    2) If you aren't into the debugger yet, start adding printf() statements at key positions, displaying key information and a label of some kind. This can pinpoint exactly which statement is causing problems.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    Thanks, I can see why starting a new line makes it easier to read

    Once the program hits the for loops, it will crash.. but I was able to get something about an Illegal matrix - 3 max.. something
    Last edited by Isamo; 01-01-2004 at 12:09 AM.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Since we have
    -- no idea what value is returned by Board.numrows()
    -- no idea what value is returned by Board.numcols()
    -- no idea how Board[][] is defined
    we can't tell you much.

    It appears that Board is a structure or class.
    It also appears that said structure might be a two-dim array

    If above is true, what value could be in Board[Row][0] ? And which Board.numrows() is being called?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    I made a tic-tac-toe program in BASIC a long time ago. I have it available for download here: http://www.jasondoucette.com/ai.html#TicTacToe

    It computes moves by looking at patterns in the board - not by a search tree. This obviously required me to solve the game by hand, to be able to respond to any pattern that may arise. I have not looked at the code since I wrote it, and it was only for a simple computer course project, so I do not know how well it is written, or how much it will help you.

  9. #9
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    i had posted my tic tact toe game on thi sboard back some time with some level of AI...

    it was based on a search tree.. Believe me this is not an efficient way of doing this and the if statements will be really huge in numbers... A better option would be for the game to analyze the players next 2 or 3 possible moves and make its move.... I used this kind of an AI in my chees games (posted here long back) which was quite good at defeating novice players...

  10. #10
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by vasanth
    it was based on a search tree.. Believe me this is not an efficient way of doing this and the if statements will be really huge in numbers...
    Do you mean that the search tree is not an efficient way, or using the if statements? I assume you mean the if statements, as you end your sentence mentioning them.

    There are not really that many patterns in the board, from what I remember of my program (link in my previous post). I think you just have to be a little smart about the if statements you choose, so that one statement can solve many board positions at once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 07:25 PM
  2. tic tac toe AI roadblock >:-(|)
    By dark_rocket in forum Game Programming
    Replies: 5
    Last Post: 06-12-2006, 06:13 AM
  3. Tic Tac Toe AI help please...
    By Rune Hunter in forum Game Programming
    Replies: 12
    Last Post: 11-05-2004, 04:24 PM
  4. tic tac toe AI
    By abrege in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2002, 07:10 AM
  5. not 3x3 tic tac toe game AI
    By Unregistered in forum Game Programming
    Replies: 9
    Last Post: 08-29-2001, 05:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21