tic tac toe question (logic question)

This is a discussion on tic tac toe question (logic question) within the C++ Programming forums, part of the General Programming Boards category; assuming i have everything else working, other than the logic to figure out if 3 in a row have been ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    tic tac toe question (logic question)

    assuming i have everything else working, other than the logic to figure out if 3 in a row have been made or not

    also assuming i am working with indexes for each of the boxes (0-8) and not using a 2d matrix

    other than checking for every combo there is to win (i.e 0,1,2 or 0,4,8) is there a way to check to see if 3 in a row have been made?

    i mean, i CAN check for every single combo, but i dont think thats good programming nor really efficiant

  2. #2
    Unregd
    Guest
    It's a little simpler with a two-dimensional matrix, but I'm sure there's a way to do this using your method, too:

    Consider this: If the top left space does not have the winning token, you can throw out three different winning combinations (horizontal, vertical, and diagonal). If you do this recursively, you can eliminate combinations that would not result in end of game. This way, you won't need to run through every single possible combination every turn of the game.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    i dont quite getcha
    if the topleft isnt a winning token, then yea, the 3 and the only 3 winning combos would be thrown out. how would u do this recursively?

  4. #4
    Unregd
    Guest
    Second thought, not recursively. You can nest ifs to do this:

    Code:
    if (space[0] == token)
    {
        if (space[1] == token) // row 1 across check
        {
            ...
        }
        else if (space[3] == token) // diagonal left to right check
        {
            ...
        }
        ... // column 1 down check
    }
    
    else if (space[1] == token) // column 2 check
    {
        if (space[4] == token)
        {
            ...
        }
    }
    
    ...
    If you do this correctly, some combinations will not have to be checked. If you really don't want to check combinations that wouldn't be winning anyway, you could only check possible combinations that have had a token placed on them, but that would be more work than the easy way.

    Another possibility is that, when a token is placed, the function that sets it on the tic tac toe board could check only the winning combinations for that space. To make sure all spaces haven't been taken, you'll probably want to increment a counter after each space is successfully taken. If there is no win after the counter reaches 8, it's a draw.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe Neural Network
    By glo in forum General AI Programming
    Replies: 4
    Last Post: 06-15-2008, 04:39 PM
  2. Making Tic Tac Toe Smarter
    By abh!shek in forum C Programming
    Replies: 15
    Last Post: 06-05-2008, 11:43 AM
  3. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 07:25 PM
  4. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 09:58 PM
  5. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 09:33 AM

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