我有这个方法:
public bool CantMoveIfCreatedCheck(Pieces[,] pieces, int rowStart, int columnStart,
int rowEnd, int columnEnd)
{
pieces[rowEnd, columnEnd] = pieces[rowStart, columnStart];
// shift one piece to another location
pieces[rowStart, columnStart] = null;
if (whitePiece)
{
for (int z1 = 0; z1 < 9; z1++)
{ //Find Threatening piece
for (int j = 0; j < 9; j++)
{
if (pieces != null && pieces[whitekingRow, whitekingColumn] != null))// i have some check algorithm
{
canCheck = true;
chess.YouWillBeCheckedCantMove();
}
}
}
}
pieces[rowStart, columnStart] = pieces[rowEnd, columnEnd];// shift it back to the end location
pieces[rowEnd, columnEnd] = null;
return canCheck;
}我在这里尝试做的是,在数组中暂时改变棋子的位置,并在两个循环结束后将它们移位回来。
但是,因为一切都是通过引用来处理数组/对象的,所以将数组中一个位置的一块移动到数组的另一个位置会产生问题。有没有办法以某种方式将一个数组更改为另一个数组,而不影响原来的数组
例如,创建temperoryArray[,]=pieces[,]; (它对我不起作用,因为它复制了引用并影响了片段数组)。
有没有别的办法?
发布于 2011-03-23 12:18:24
为了将数组复制到临时数组,您需要深度复制数组中的对象,而不是浅复制(简单地使用赋值)引用。关于C#中的深度复制,Here's有一个很好的帖子。
因为你有一个对象数组(实际上,这是一个引用数组),赋值只是简单地复制引用。
https://stackoverflow.com/questions/5400812
复制相似问题