T_McMillan97
T_McMillan97

Reputation: 43

How to switch places of elements in a 2D Array?

So I'm relatively new to C#, I'm trying to make a picture puzzle game where the image is split up into blocks. What I am having trouble with is finding a way to make elements of the 2D array change places, this is to allow me to add a random shuffle and player control for the game. So for example the top left is 0 and the bottom right is 9, I would want those to swap places.

int[,] grid = new int[4, 4];

        for (int y = 0; y < 4; y++)
        {
            Console.WriteLine("*********************");

            for (int x = 0; x < 4; x++)
            {
                grid[x, y] = x * y;

                Console.Write("|" + grid[x, y] + "| ");
            }

            Console.WriteLine();
        }

        Console.WriteLine("*********************");

        Console.ReadKey();

So far I have it working to a point where it will create a grid of the array values, I am stuck for ideas on how I can make the values switch places.

Upvotes: 2

Views: 400

Answers (2)

ES2018
ES2018

Reputation: 438

I added the random shuffle using @Nick Dechiara swap function. I am using GetLength to get the size of the grid

    static void Main(string[] args)
    {
        int[,] grid = new int[7, 4];

        for (int y = 0; y < grid.GetLength(1); y++)
        {
            for (int x = 0; x < grid.GetLength(0); x++)
            {
                grid[x, y] = x * y;
            }
        }

        PrintGrid(grid);

        int numberOfShuffles = 5;
        Random rand = new Random();
        for (int i = 0; i < numberOfShuffles; i++)
        {
            int x1 = rand.Next(0, grid.GetLength(0));
            int x2 = rand.Next(0, grid.GetLength(0));
            int y1 = rand.Next(0, grid.GetLength(1));
            int y2 = rand.Next(0, grid.GetLength(1));

            Console.WriteLine();
            Console.WriteLine("Swapping ({0},{1}) with ({2},{3})", x1,y1,x2,y2);

            Swap(x1,y1,x2,y2,ref grid);

            PrintGrid(grid);
        }
    }

    public static void Swap(int x1, int y1, int x2, int y2, ref int[,] grid)
    {
        int temp = grid[x1, y1]; // store the value we're about to replace
        grid[x1, y1] = grid[x2, y2]; // replace the value
        grid[x2, y2] = temp; // push the stored value into the other spot
    }

    private static void PrintGrid(int[,] grid)
    {
        Console.WriteLine("****************************");
        for (int y = 0; y < grid.GetLength(1); y++)
        {
            for (int x = 0; x < grid.GetLength(0); x++)
            {
                Console.Write("|" + grid[x, y] + "| ");
            }
            Console.WriteLine();
        }
    }

Upvotes: 1

N.D.C.
N.D.C.

Reputation: 1601

We can make a helper function for this. The way it works is we store the value of one spot in a temporary variable, so it doesn't get lost, then replace that spot with the other. We then push the temporary variable's value into the other spot.

We pass the int[,] in as a ref so that when you call Swap on a grid, the actual grid gets changed outside of the function.

    public static void Swap(int x1, int y1, int x2, int y2, ref int[,] grid)
    {
        int temp = grid[x1, y1]; // store the value we're about to replace
        grid[x1, y1] = grid[x2, y2]; // replace the value
        grid[x2, y2] = temp; // push the stored value into the other spot
    }

Example of use:

        int[,] grid = new int[4, 4];
        grid[0, 0] = 5;
        grid[1, 1] = 7;
        Console.WriteLine(" " + grid[0, 0] + " | " + grid[1, 1]);
        Swap(0, 0, 1, 1, ref grid);
        Console.WriteLine(" " + grid[0, 0] + " | " + grid[1, 1]);

Gives:

 5 | 7
 7 | 5

Upvotes: 2

Related Questions