Function call changing the value of the parameter

Viewed 32

The following code is a piece of a A* solver for the 8 puzzle problem in a 4x4 matrix (multidimensional array). It takes an 4x4 matrix as input and shifts the 0 to a specified direction(up, down, rigth or left).

The problem that's the output of the called function is changing the current value of the original matrix used as the input for it when it wasn't supposed to and I haven't figured out why. Pretty sure I'm passing the original matrix as a value rather than a reference to the function but it's being updated by the function regardless. Can someone please point out why? (lines: 36, 43, 58, 72, 86, 93, 110, 124)

public static int[,] Solver(int[,] input, int[,] target)
{
    int[,] current = input;
    List<int[,]> previousStates = new List<int[,]>();
    
    Console.WriteLine("start with:");
    PrintMatrix(input);
    
    if(current == target)
    {
        Console.WriteLine("ended with:");
        PrintMatrix(current);

        return current;
    }
    else
    {
        int limiter = 0; //FOR DEBUG ONLY!
        while(current != target && limiter < 4)
        {
            List<int[,]> branches = new List<int[,]>();
            
            for(int i=0; i<4; i++)
            {
                for(int j=0; j<4; j++) // bug somewhere arount here: updating the current matrix before calculating lowest
                {
                    if(current[i,j] == 0)
                    {
                        Console.WriteLine("0 found at: ["+i+"]["+j+"]");
                        
                        if(j > 0 && j < 3) // can only move to four positions if it is > 0 && < 3 
                        {
                            Console.WriteLine("enter case: j > 0 && j < 3");
                            
                            //add moves right and left
                            int[,] moveToRight = CreateMovedMatrix(current, 2);
                            
                            Console.WriteLine("moved right and created the following matrix:");
                            PrintMatrix(moveToRight);
                            Console.WriteLine("The original matrix is: ");
                            PrintMatrix(current);
                            
                            int[,] moveToLeft = CreateMovedMatrix(current, 3);
                            
                            Console.WriteLine("moved left and created the following matrix:");
                            PrintMatrix(moveToLeft);
                            Console.WriteLine("The original matrix is: ");
                            PrintMatrix(current);
                            
                            branches.Add(moveToRight);
                            branches.Add(moveToLeft);
                        }
                        else if(j == 0)
                        {
                            Console.WriteLine("enter case: j == 0");
                            
                            //add only right move
                            int[,] moveToRight = CreateMovedMatrix(current, 2);
                            
                            Console.WriteLine("moved right and created the following matrix:");
                            PrintMatrix(moveToRight);
                            branches.Add(moveToRight);
                            Console.WriteLine("The original matrix is: ");
                            
                            PrintMatrix(current);
                        }
                        else if(j == 3)
                        {
                            Console.WriteLine("enter case: j == 3");
                            
                            //add only left move
                            int[,] moveToLeft = CreateMovedMatrix(current, 3);
                            branches.Add(moveToLeft);
                            
                            Console.WriteLine("moved left and created the following matrix:");
                            PrintMatrix(moveToLeft);
                            Console.WriteLine("The original matrix is: ");
                            PrintMatrix(current);
                        }
                        
                        if( i > 0 && i < 3)
                        {
                            Console.WriteLine("enter case: i > 0 && i < 3");
                            
                            //add moves up and down
                            int[,] moveUp = CreateMovedMatrix(current, 0);
                            
                            Console.WriteLine("moved up and created the following matrix:");
                            PrintMatrix(moveUp);
                            Console.WriteLine("the original matrix is: ");
                            PrintMatrix(current);
                            
                            int[,] moveDown = CreateMovedMatrix(current, 1);
                            
                            Console.WriteLine("moved down and created the following matrix:");
                            PrintMatrix(moveDown);
                            Console.WriteLine("the original matrix is:");
                            PrintMatrix(current);
                            
                            
                            branches.Add(moveUp);
                            branches.Add(moveDown);

                        }
                        else if( i == 0 )
                        {
                            Console.WriteLine("enter case: i == 0");
                            
                            //add only move down
                            int[,] moveDown = CreateMovedMatrix(current, 1);
                            
                            Console.WriteLine("moved down and created the following matrix:");
                            PrintMatrix(moveDown);
                            Console.WriteLine("the original matrix is:");
                            PrintMatrix(current);
                            
                            branches.Add(moveDown);
                        }
                        else if( i == 3 )
                        {
                            Console.WriteLine("enter case: i == 3");
                            
                            //add only move up
                            int[,] moveUp = CreateMovedMatrix(current, 0);
                            branches.Add(moveUp);
                            
                            Console.WriteLine("moved up and created the following matrix:");
                            PrintMatrix(moveUp);
                            Console.WriteLine("the original matrix is: ");
                            PrintMatrix(current);
                        }
                    }
                }
            }
            Console.WriteLine("calculating the lowest");
            int[,] lowestPath = FindLowest(branches, target, previousStates);
            previousStates.Add(current);
            Console.WriteLine("updating current");
            current = lowestPath;
            
            Console.WriteLine("branch choosen: ");
            PrintMatrix(current);
            limiter++;
        }//condition met
        Console.WriteLine("ended with:");
        PrintMatrix(current);
        
        return current;
    }
}

output:

enter image description here

enter image description here

0 Answers
Related