Flipping a 10x10 grid horizontally
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
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
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
What about
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;};
this is what I have, but it doesn''t work:
Should this 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?
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.
basically you are doing
swap( 0, 10 );
.
.
.
swap( 10, 0 );
You must stop in the middle.
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?
temp2 = cell[ x ][ y ];
and then...
temp2.symbol = cell[ x ][ y ].symbol;
or does the first line pass all the members anyway?
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...)).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement