Dinguz
Dinguz

Reputation: 23

Array's value is changed for no ostensible reason

I'm trying to embed a very basic AI in a game of Tic Tac Toe and I'm running into a problem with the arrays that I use to discern the most suitable square to claim.

I have one array that represents the game grid in its current state and another that represents the way it would look if a certain square were claimed.

The AI kept making weird decisions, so I decided to have it report various information at certain milestones.

string[,] currentGrid;
string[,] hypotheticalGrid;


MessageBox.Show(currentGrid);

hypotheticalGrid = currentGrid;
hypotheticalGrid[x, y] = "O";

MessageBox.Show(currentGrid);

Bear in mind that the above is a very simplified version of the actual code.

The arrays are assigned values along the line and the message boxes are set to display currentGrid as a sequence of its contained values.

Everything works properly except the code between the message boxes.

The problem is that the two message boxes display different results even though the latter is a copy of the former and no changes to currentGrid are specified in the space between the two.

The two lines in between are copied directly from the source code and should only affect the hypothetical grid. As should be obvious, these lines are supposed to reset the hypothetical grid and then add an "O" to the square in question.

However, the "O" also gets placed in the current grid for some reason.

What am I doing wrong?

Upvotes: 2

Views: 129

Answers (3)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112482

Keep in mind, that arrays are reference types. Your assignment copies the reference but does not create a duplicate of the array. After the assignment both array variables point to the same array!

You can create an array duplicate like this:

string[] hypotheticalGrid = new string[currentGrid.Length];
Array.Copy(currentGrid, hypotheticalGrid, currentGrid.Length);

Note that this does not create a duplicate of the items contained in the array, but this is not a problem, since strings are immutable. I.e. if you manipulate a string in one of the arrays, this creates a new string and therefore has no effect on the second array.


EDIT:

By looking at the other posts I see that it can be done easier with the Clone method:

string[] hypotheticalGrid = currentGrid.Clone();

Upvotes: 0

Matt Greer
Matt Greer

Reputation: 62037

The line hypotheticalGrid = currentGrid; does not make a copy of currentGrid, instead both variables point to the same array. So any change made in hypotheticalGrid will be seen in currentGrid too.

You should have hypotheticalGrid be a copy of currentGrid:

hypotheticalGrid = (string[,])currentGrid.Clone();

In this case, Clone() will work, because strings are immutable in .NET. In other cases, Clone might not be good enough, just something to be aware of.

Upvotes: 3

Tudor
Tudor

Reputation: 62449

The problem is that your assignment:

hypotheticalGrid = currentGrid;

is merely a reference assignment, meaning that now they both point to the same array in memory, so any changes to the hypothetical grid will affect the current grid as well.

You'd need a full item copy in order for them to not interfere with each other.

Upvotes: 2

Related Questions