Thread: Arrays

  1. #1
    mycron1
    Guest

    Arrays

    I’m creating a tic tac toe game and would like to use one character array to hold the X”s & O’s, and another int array to hold all the possible winning combinations.
    For example:
    int LINES[] [] = {
    {0, 1, 2},
    {3, 4, 5},
    {6, 7, 8},
    {0, 3, 6},
    {1, 4, 7},
    {2, 5, 8},
    {0, 4, 8},
    {2, 4, 6}};

    How can I check the contents of the character array against the numeric combinations found in the int array ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd probably just make it a macro, because I like macros...
    Code:
    #define CA0(xo) \
        ( \
            ( BOARD[0] == BOARD[1] == BOARD[2] ) && BOARD[0] == (xo)
        )
    #define CA1(xo) \
        ( \
            ( BOARD[3] == BOARD[4] == BOARD[5] ) && BOARD[3] == (xo)
        )
    #define CA2(xo) \
        ( \
            ( BOARD[3] == BOARD[4] == BOARD[5] ) && BOARD[3] == (xo)
        )
    #define ACROSS(xo) \
        ( CA0(xo) || CA1(xo) || CA2(xo) )
    That'll get you started. I personally wouldn't bother using a lookup table like you've got. I suppose it's a nifty way to do it, but we don't really need one.
    Code:
    if( ACROSS( 'x' ) || DOWN( 'x' ) || DIAG( 'x' ) )
        printf("'x' wins!\n");
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    In the code that you have posted,

    Code:
    #define CA0(xo) \
        ( \
            ( BOARD[0] == BOARD[1] == BOARD[2] ) && BOARD[0] == (xo)
        )
    #define CA1(xo) \
        ( \
            ( BOARD[3] == BOARD[4] == BOARD[5] ) && BOARD[3] == (xo)
        )
    #define CA2(xo) \
        ( \
            ( BOARD[3] == BOARD[4] == BOARD[5] ) && BOARD[3] == (xo)
        )
    #define ACROSS(xo) \
        ( CA0(xo) || CA1(xo) || CA2(xo) )
    The final #define, is to check the diagonals of the board. I can't understand how that statment works. (if it checks diagonals). And there is a typo error in #define CA2(x0) . It should be
    Code:
     #define CA2(xo) \
         ( \
            ( BOARD[6] == BOARD[7] == BOARD[8] ) && BOARD[6] == (xo)
    )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM