1
\$\begingroup\$

Pretty much all I'm trying to do is revolve my 2D Array by its container. I'm using this array for a background and I seem to be having problems with it revolving.

public class TileTransformer : GridConstants
{
    public Tile[,] Tiles;
    ContentManager Content;
    public TileTransformer(ContentManager content)
    {
        Content = content;

    }
    public Tile[,] Wraping(Tile[,] tiles,Point shift)
    {
        Tiles = tiles;
        for (int x = shift.X; x < 0; x++)//Left shift
        {
            for (int X = 0; X < GridWidth; X++)
            {
                for (int Y = 0; Y < GridHeight; Y++)
                {
                    if (X + 1 >GridWidth-1)
                    {
                        Tiles[0, Y].Container =tiles[X, Y].Container;
                    }
                    else
                    {
                        Tiles[X+1, Y].Container =tiles[X, Y].Container;
                    }
                }
            }
        }
        for (int x = shift.X; x > 0; x--)//right shift
        {
            for (int X = 0; X < GridWidth; X++)
            {
                for (int Y = 0; Y< GridHeight; Y++)
                {
                    if (X-1==-1)
                    {
                        Tiles[GridWidth-1, Y].Container =tiles[0, Y].Container;
                    }
                    else
                    {
                        Tiles[X - 1, Y].Container =tiles[X, Y].Container;
                    }
                }
            }
        }
        for (int y = shift.Y; y > 0; y--)//shift up
        {
            for (int X = 0; X < GridWidth; X++)
            {
                for (int Y = 0; Y < GridHeight; Y++)
                {
                    if (Y - 1 == -1)
                    {
                        Tiles[X, GridHeight-1].Container = tiles[X, Y].Container;
                    }
                    else
                    {
                        Tiles[X, Y - 1].Container = tiles[X, Y].Container;
                    }
                }
            }
        }
        for (int y = shift.Y; y < 0; y++)//shift down
        {
            for (int X = 0; X < GridWidth; X++)
            {
                for (int Y = 0; Y < GridHeight; Y++)
                {
                    if (Y + 1 == GridHeight)
                    {
                        Tiles[X, 0].Container = tiles[X, Y].Container;
                    }
                    else
                    {
                        Tiles[X, Y + 1].Container = tiles[X, Y].Container;
                    }
                }
            }
        }
        return Tiles;
    }

Now the Problems that I'm having is either when I shift up or left it seems the whole array is cleared back to the default state. Also when I'm revolving the array it appears to stretch it upon the sides of the screen that it is shifting towards.

\$\endgroup\$
4
  • \$\begingroup\$ Hint: You're going to have a hard time shifting an array in place without using any temporary storage. \$\endgroup\$
    – House
    Oct 26, 2012 at 15:12
  • \$\begingroup\$ I actually tried using a temporary storage and it came out with the same results. :/ I've been playing with this code for awhile now and it still seems to stump me. It honestly looks fairly simple to achieve but looks can be deceiving. \$\endgroup\$
    – Brett
    Oct 26, 2012 at 15:21
  • 3
    \$\begingroup\$ Write out your algorithm on paper. Use a small grid, you'll get it. \$\endgroup\$
    – House
    Oct 26, 2012 at 15:42
  • \$\begingroup\$ You probably don’t want a “don’t do that” answer, but… maybe don’t do that? You could instead keep a (ox,oy) offset vector and override all array accesses so that they add the offset. Then shifting the array is reduced down to changing the offset value. \$\endgroup\$ Oct 27, 2012 at 12:13

1 Answer 1

1
\$\begingroup\$

It should be as simple as this, obviously untested but should point you in right direction

public Tile[,] Wraping(Tile[,] tiles,Point shift) {
  Tiles = tiles;
  Tile[,] shiftedTiles;

  for (int y = 0; y < GridHeight; y++) {
    for (int x = 0; x < GridWidth; x++) {
      shiftedTiles[(shift.X + x) % GridWidth, (shift.Y + y) % GridHeight] = tiles[x, y];
    }
  }
  return shiftedTiles;
}

The % part is modulus which ensure that the grid X,Y are wrapped round without using if statements to check edge boundaries.

If that doesn't quite work try exchanging the parameters around to:

 shiftedTiles[x,y] = tiles[(shift.X + x) % GridWidth, (shift.Y + y) % GridHeight];
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .