#### Archived

This topic is now archived and is closed to further replies.

# Flipping a 10x10 grid horizontally

This topic is 6091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a 2D array , as such: cell[columns][rows] which is a 10x10 grid. I have characters on individual cells. I wish to flip the maze horizontally so that each character is flipped horizontally on the maze. I have tried without luck to do this. I can't seem to do it. Any help is appreciated! Edit(using C++ in a console prog) Edited by - randomDecay on November 18, 2001 1:06:15 AM

##### Share on other sites
In-game, you can do something like this:

for (int y = 0; y < 5; y++)
{
for (int x = 0; x < 10; x++)
{
SwapStruct(cell[x][y], cell[x][9-y]);
}
}

SwapStruct could be just a basic swaping function. It could be modelled like a SwapInt, which goes like this:

void SwapInt(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}

Hope this helps

##### Share on other sites

  int x,y;for( x = 0; x < columns/2; ++x )for( y = 0; y < rows; ++y ){ cell_t tmp; // assuming cell_t is the type of your cell; tmp = cell[x][y]; cell[x][y] = cell[columns - x - 1][y] cell[columns - x - 1][y] = tmp;};

##### Share on other sites
I was going to let him/her do the optimization etc., but that''s great too.

##### Share on other sites
How did you do that ? You type like they do... (You beat me to it ;p )

##### Share on other sites
this is what I have, but it doesn''t work:

  Cell temp2; for ( int x = 0; x < 10; x++ ) { for ( int y = 0; y < 10; y++ ) { temp2 = cell[ y ][ x ]; cell[ y ][ x ] = cell[ 10 - y - 1 ][ x ]; cell[ 10 - y - 1][ x ] = temp2; } }

Should this work?

##### Share on other sites
No, you are flipping your cells twice : when y goes from 0 to 4 then when y goes from 5 to 10.

basically you are doing

swap( 0, 10 );
.
.
.
swap( 10, 0 );

You must stop in the middle.

##### Share on other sites
One more q...when I use the cell tmp2 variable do I also have to pass the members of the cell struct, like this:

temp2 = cell[ x ][ y ];
and then...

temp2.symbol = cell[ x ][ y ].symbol;

or does the first line pass all the members anyway?

##### Share on other sites
No, that''s the advantage of using a struct (or a *SIMPLE* class (i.e. one for which you don''t have to write your own assignment operator...)).

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631776
• Total Posts
3002299
×