# Multi Dimensional Array Compare

• 05-09-2006
guitarist809
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...?

-Matt
• 05-09-2006
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...
• 05-09-2006
guitarist809
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
• 05-09-2006
guitarist809
Hello? I do not get the number trick at all.
• 05-09-2006
Bench82
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 :)
• 05-10-2006
Perspective
• 05-10-2006
jafet
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.
• 05-10-2006
Bench82
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 :)
• 05-10-2006
Nah, it can be done with a single if statement......(and about 24 logical operators!!)
• 05-10-2006
hk_mp5kpdw
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.```
• 05-10-2006
guitarist809
that should work, but, I already did like 16 if statements. Thanks, this should help in other programs of mine
• 05-10-2006
sigfriedmcwild
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)).