Thread: Multi Dimensional Array Compare

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Multi Dimensional Array Compare

    I've been recently working on a Tic Tac Toe game for my C++ class. As I was working on the "winner" part of the game, I thought that I could use a strcmp to compare my arrays like:


    **NOT MY ACTUAL CODE**
    Code:
    char board[10][10]; // lets just assume all the data is already in here
    char winner[10][10] = 
    {
    "X", "", "",
    "", "X" ,""
    "", "", "X"
    };
    if (strcmp(board, winner) == 0)
    {
         cout << "You Win!";
    }
    It didn't work.


    Is there a way to compare two arrays, or should i just use a strcmp(board[0],board[1])==0...?

    Thanks in advance
    -Matt
    ~guitarist809~

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    strcmp doesn't compare 2D arrays - you will have to compare each string seperately.

    Since you are writing a noughts and crosses game (That's what we call "tic-tac-toe" in the UK), you may want to consider this:
    Code:
    2   9   4   
    7   5   3
    6   1   8
    Notice all the winning lines are 3 numbers which add up to 15. You can use this to your advantage...

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Bench82
    strcmp doesn't compare 2D arrays - you will have to compare each string seperately.

    Since you are writing a noughts and crosses game (That's what we call "tic-tac-toe" in the UK), you may want to consider this:
    Code:
    2   9   4   
    7   5   3
    6   1   8
    Notice all the winning lines are 3 numbers which add up to 15. You can use this to your advantage...
    Thx, but I don't get how the numbers work. like, how did you get the 7, 8, 6, etc?


    -Matt
    ~guitarist809~

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Hello? I do not get the number trick at all.
    ~guitarist809~

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    OK - in the game, you need a row of O's or X's, eg
    Code:
     O |   | X
    -----------
     X | O | X
    -----------
       |   | O
    Now, according to the diagram, the numbers belonging to Os there are 2, 8 and 5.
    2+8+5 = 15.

    so, in order to check a win, all you have to do is collect 3 numbers and see if they add up to 15.. 7,6 and 8 do not sum to 15, therefore a player who occupies only those 3 squares has not won.

    You have to be slightly careful in this calculation - make sure that there are 3 numbers comprising the sum, no more, and no less - eg, 8+7 = 15 (No win) and 1+2+3+9 = 15 (again, no win there).

    this calculation of "Is equal to 15" should be much simpler than comparing strings

  6. #6

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You do realize that generating the possible combinations themselves will pose a bit of a problem, and to me they do seem like overkill...

    What I'd do: I'd track the last piece to be added. Then I'd check it's column, row (and possibly diagonal) for a match. Simple and scalable.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You don't need to generate any combinations, that would completely bypass the whole point of using the magic square - you can have a container of integers and check the sum of all permutation combinations of 3 integers against 15

    at the risk of spoonfeeding homework answers, here's what i mean:
    Code:
    #include <algorithm>
    #include <iterator>
    #include <vector>
    
    typedef std::vector<int>::iterator vIter;
    int sum(vIter first, vIter last)
    {
        int temp(0);
        for(vIter it=first; it!=last; ++it)
            temp+= *it;
        return temp;
    }
    
    bool magic_square(std::vector<int> in)
    {
        const int combinations(3);
        const int magicNumber(15);
        if ( in.size() < combinations)
            return false; //ensure a minimum of 3 numbers
    
        do
            if (sum(in.begin(), in.begin()+combinations) == magicNumber)
                //Check the first 3 numbers of 'in'
                return true;
        while(std::next_permutation(in.begin(), in.end()) );
        return false;
    }
    The problem with this code is that it is slightly inefficient - in that magic_square() will almost certainly check the same permutations more than once. I can think of more efficient ways, but they might not be so readable, eg,
    Code:
    bool magic_square(std::vector<int> in)
    {
        const int magicNumber(15);
        
        typedef std::vector<int>::size_type size_t;
        size_t size = in.size();
    
        for(size_t i=0; i < size; ++i)
            for(size_t j=i+1; j < size; ++j)
                for(size_t k=j+1; k < size; ++k)
                    if (in.at(i) + in.at(j) + in.at(k) == magicNumber)
                        return true;
        return false;
    }
    OK, maybe it is a little bit overkill, but the alternative with regards to comparing strings along lines would mean alot of if/else statements (aka "if-else-heimer's disease" ) - and you want to avoid that as much as possible
    Last edited by Bench82; 05-10-2006 at 10:23 AM.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Nah, it can be done with a single if statement......(and about 24 logical operators!!)
    You're only born perfect.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Maybe memcmp?

    Code:
    char array1[4][4] = { "Abe", "Bob", "Mat", "Joe" };
    char array2[4][4] = { "Bob", "Mat", "Joe", "Abe" };
    char array3[4][4] = { "Abe", "Bob", "Mat", "Joe" };
    
    // Compare array1 and array2
    if( !memcmp(array1,array2,sizeof(array1)) )
        cout << "They are the same." << endl;
    else 
        cout << "They are different." << endl;
    
    // Compare array1 and array3
    if( !memcmp(array1,array3,sizeof(array1)) )
        cout << "They are the same." << endl;
    else 
        cout << "They are different." << endl;
    Should output:
    Code:
    They are different.
    They are the same.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    that should work, but, I already did like 16 if statements. Thanks, this should help in other programs of mine
    ~guitarist809~

  12. #12
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by Bench82
    Code:
    bool magic_square(std::vector<int> in)
    {
        const int magicNumber(15);
        
        typedef std::vector<int>::size_type size_t;
        size_t size = in.size();
    
        for(size_t i=0; i < size; ++i)
            for(size_t j=i+1; j < size; ++j)
                for(size_t k=j+1; k < size; ++k)
                    if (in.at(i) + in.at(j) + in.at(k) == magicNumber)
                        return true;
        return false;
    }
    You could also keep 2 vector<int> that hold the cells owner by each player respectively.
    When a player makes a move you take the value of the cell that was just occupied and test it together with all pairs of the cells the player alrady occupies. If there is no winning combination you append the current value.
    This way the algorithms is in O(n^2) on the number of cells occupied by the player instead of being in O(n^3) on the number of cells in the board (note that the number of cells occupied is maximum ceil(board size / 2)).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem within multi dimensional array
    By lolguy in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 08:02 AM
  2. multi dimensional array at runtime
    By sawer in forum C++ Programming
    Replies: 21
    Last Post: 05-10-2006, 10:59 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 11:09 AM