0

I am facing the issue that copy of an 2 dimensional array is not made in each stack frame of recursive call. I am doing indirect recursion.

I also tried sending my data in function call from main() function but the copy was not made. Same address was used in every recursive call.

class board
{


public:

    int board_arr[8][8];

public:
    board()
    {

    }

    void player1Turn()
    {

        for (int i = 0; i < rowCount; i++)
        {
            for(int j = 0; j < rowCount; j ++ )
            {
                if (board_arr[i][j] == 1)
                {
                    //checking if the pawn can move anywhere
                    if (i + 1 <=7 && j - 1 >= 0 && board_arr[i + 1][j - 1] == 0 )
                    {
                        board_arr[i][j] = 0;
                        board_arr[i + 1][j - 1] = 1;

                        player2Turn();

                    }
                    if (i + 1 <=7 && j + 1 <= 7 && board_arr[i + 1][j + 1] == 0)
                    {
                        board_arr[i][j] = 0;
                        board_arr[i + 1][j + 1] = 1;


                        player2Turn();

                    }
                    //opponent infront 
                    //killing
                    //if opponent is infront and checking if you can kill it or not
                    if (i + 2 <= 7
                        && i + 1 <= 7 
                        && j - 2 >=0 

                        && j - 1 >= 0 
                        && board_arr[i + 1][j - 1] == 2
                        && (board_arr[i + 2][j - 2]==0)) 
                    {
                            board_arr[i][j] = 0;
                            board_arr[i + 2][j - 2] = 1;
                            board_arr[i + 1][j - 1] = 0;

                            cout << endl << "kill by p1 " << endl;


                            player2Turn();

                    }
                    if (i + 2 <= 7 
                        && i + 1 <= 7 
                        && j + 2 <= 7

                        && j + 1 <=7 
                        && board_arr[i + 1][j + 1] == 2 
                        && (board_arr[i + 2][j + 2]==0))
                    {
                        board_arr[i][j] = 0;
                        board_arr[i + 1][j + 1] = 0;
                        board_arr[i + 2][j + 2] = 1;


                        cout << endl << "kill by p1 " << endl;

                        player2Turn();

                    }
                }

            }

        }

    }
    void player2Turn()
    {

        for (int i = rowCount-1; i >= 0; i--)
        {
            for (int j = rowCount-1; j >= 0; j--)
            {
                if (board_arr[i][j] == 2)
                {
                    //checking if the pawn can move anywhere
                    if (i - 1 >= 0 && j - 1 >= 0 && board_arr[i - 1][j - 1] == 0)
                    {
                        board_arr[i][j] = 0;
                        board_arr[i - 1][j - 1] = 2;


                        player1Turn();

                    }
                    if (i - 1 >= 0 && j + 1 <=7 && board_arr[i - 1][j + 1] == 0)
                    {
                        board_arr[i][j] = 0;
                        board_arr[i - 1][j + 1] = 2;


                        player1Turn();

                    }
                    //opponent infront 
                    //killing
                    //if opponent is infront and checking if you can kill it or not
                     if (i - 2 >= 0
                        && i - 1 >= 0
                        && j - 2 >= 0

                        && j - 1 >= 0
                        && board_arr[i - 1][j - 1] == 1
                        && (board_arr[i - 2][j - 2] ==0))
                    {
                        board_arr[i][j] = 0;
                        board_arr[i - 2][j - 2] = 2;
                        board_arr[i - 1][j - 1] = 0;

                        cout << endl << "kill by p2 " << endl;

                        player1Turn();



                    }
                    if (i + 2 <= 7
                        && i - 1 >= 0
                        && j + 2 <=7

                        && j + 1 <= 7
                        && board_arr[i - 1][j + 1] == 1
                        && (board_arr[i - 2][j + 2] ==0))
                    {
                        board_arr[i][j] = 0;
                        board_arr[i - 2][j + 2] = 1;
                        board_arr[i - 1][j + 1] = 0;

                        cout << endl << "kill by p1 " << endl;

                        player1Turn();

                    }
                }

            }

        }
    }

};

same copy of the board_arr was used in each call.

Rajan Sharma
  • 2,211
  • 3
  • 21
  • 33
  • 1
    It usually too inefficient to copy the board on each recursive call (which you aren't doing anyway as explained in the answers). Normally what you do is have only one board, make the changes you need to make, make your recursive call, then after you have returned from the recursive call **undo the changes you just made**, You can save enough information on the stack to undo that last move you made (i.e. from and to squares and the piece that was in the to square before the move).. – john Jul 18 '19 at 09:28
  • so I should create a temp array inside the function so it is there in the stack frame and i will use it for backtracking – Syed Najam Ul Hasan Jul 18 '19 at 09:32
  • No that would as bad as copying the array. Before each recusive call you make a move, just store enough information to undo that move. That's only a few variables. – john Jul 18 '19 at 09:43

2 Answers2

3

You don't pass the array from function to function, but even if you did, arrays are (simplification) pointers, so even passing them manually from function to function will not create copies.

Use std::vector<> or std::array<>.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
3

You are not passing board_arr to a recursive method, that is those methods do not have that array in their parameters. So board_arr is not being copied.

Because those methods are instance methods of board class, everything is passed in each method call is this pointer to the instance of board.

Renat
  • 7,718
  • 2
  • 20
  • 34