Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send copy of array in c# [duplicate]

Tags:

arrays

c#

  • So, I'm trying to make simple Console Tic Tac Toe Game, with AI.
  • Current board state is stored inside multi-dimensional array.
  • To check what the best move for AI is, im using recursion.
  • The function "CheckMove" changes board position, and then calls itself to see where it goes.
  • But the problem is that if i change board state inside called function, it will also change inside a caller.

How to avoid that? Simplified code:

static void Main()
    {
        int[] board = { 1 };
        CheckMove(board);
        //board = 2
    }

    static void CheckMove(int[] board)
    {
        board[0] = 2;
    }

full code (WIP):

enum Sym
{
    E, X, O
}

class Program
{
    static void Main(string[] args)
    {
        Sym[,] board = new Sym[3,3];
        int x, y;
        while (End(board) == 2)
        {
            Display(board);

            Console.WriteLine("Make your move - column: ");
            x = Convert.ToInt32(Console.ReadLine())-1;
            Console.WriteLine("Make your move - row: ");
            y = Convert.ToInt32(Console.ReadLine())-1;

            board[y, x] = Sym.X;

            int[,] chances = new int[3, 3];

            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    chances[i, j] = CheckMove(board, i, j, true);
                }
        }
        Display(board);
        Console.WriteLine("GG");
        Console.WriteLine("____________________________________________________________");

        Console.ReadLine();
    }

    static void Display(Sym[,] board) //Displays whole board
    {
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine($"{board[i,0]} {board[i,1]} {board[i,2]}");
        }
    }

    static int End(Sym[,] board) //Chcecks if the game shall end (-1 player won) (0 tie) (1 cpu won) (2 game in progress)
    {
        bool Full = true;

        for (int i = 0; i < 3; i++)
        { //This part is currently broken
            if (board[i, 0] == Sym.E || board[i, 1] == Sym.E || board[i, 2] == Sym.E) Full = false;
            if (board[i, 0] == board[i, 1] && board[i, 1] == board[i, 2] && board[i, 0] == Sym.X) return -1;
            if (board[0, i] == board[1, i] && board[1, i] == board[2, i] && board[0, i] == Sym.X) return -1;
            if (board[i, 0] == board[i, 1] && board[i, 1] == board[i, 2] && board[i, 0] == Sym.O) return 1;
            if (board[0, i] == board[1, i] && board[1, i] == board[2, i] && board[0, i] == Sym.O) return 1;
        }
        if (board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2] && board[0, 0] == Sym.X) return -1;
        if (board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2] && board[0, 0] == Sym.O) return 1;
        if (board[2, 0] == board[1, 1] && board[1, 1] == board[0, 2] && board[2, 0] == Sym.X) return -1;
        if (board[2, 0] == board[1, 1] && board[1, 1] == board[0, 2] && board[2, 0] == Sym.O) return 1;

        if (Full == true) return 0;
        return 2;
    }

    static int CheckMove(Sym[,] board, int a, int b, bool cpuTurn) //Check how good subjected move is
    {
        if (board[a, b] == Sym.E)
            if (cpuTurn == true) board[a, b] = Sym.O;
            else board[a, b] = Sym.X;
        else return 0;

        if (End(board) != 2) return End(board);

        int Value = 0;

        for (int m = 0; m < 3; m++)
            for(int n = 0; n < 3; n++)
            {
                Value += CheckMove(board, m, n, !cpuTurn);
            }
        return Value;
    }
}
like image 838
tymotylis Avatar asked Jun 08 '18 11:06

tymotylis


People also ask

How to copy an array in C?

Copy an Array in C 1 Create a new array with a similar data type and size. 2 Use a loop to iterate through the original array. 3 Copy the ith element of the original array to the ith element of the new array. More ...

What is the use of the function copy () in C++?

The function copy () is the user defined function which copies the elements of one array into another array. 2) The main () function calls the copy () by passing a,b arrays and n as arguments to the function copy ().

How to read an array in C programming?

Thus, the different ways to do so in C programming are as follows: Read the array size and store it into the variable n. 2) Read the entered elements using scanf () function and store the elements into the array a [] using for loop fir (i=0;i<n;i++).Then all entered elements will be stored into the array a [].

Can I use memcpy to copy an array in C++?

In C++ you can also use memcpy if your array members are POD (that is, essentially types which you could also have used unchanged in C), but in general, memcpy will not be allowed. As others mentioned, the function to use is std::copy.


1 Answers

As far as I can see, you have problems with 2D array Sym[,] in the

static int CheckMove(Sym[,] board, int a, int b, bool cpuTurn)

method; since board is 2D array, typical solutions like board.ToArray() don't work (they don't even compile). Try Clone() the board instance:

// Let's rename board into value...
static int CheckMove(Sym[,] value, int a, int b, bool cpuTurn) {
  // ... in order to preserve all the other code:
  // we are now working with the copy of the passed board
  Sym[,] board = value.Clone() as Sym[,];

  // Your code from CheckMove here
  ... 
} 

Since Sym is a enum (i.e. a value type), shallow copy is enough

like image 88
Dmitry Bychenko Avatar answered Oct 19 '22 20:10

Dmitry Bychenko