Flipping a 10x10 grid horizontally

Started by
7 comments, last by randomDecay 22 years, 5 months ago
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
Advertisement
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
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;};  
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I was going to let him/her do the optimization etc., but that''s great too.
How did you do that ? You type like they do... (You beat me to it ;p )
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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?
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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?
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...)).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement