在不序列化的情况下深度复制数组c#
基本上,我的在不序列化的情况下深度复制数组c#,c#,arrays,deep-copy,minmax,C#,Arrays,Deep Copy,Minmax,基本上,我的MinMax算法没有真正按照预期工作 我需要的是将我的数组片段复制到新片段,这样当新片段被复制时,片段不会被更改 以下是MinMax算法的摘录: private int MinMax( int depth, Piece[] pieces, bool blacksTurn, List<Move> Moves, Game game, int alpha, Move nextMove) { Piece[] newPieces=new Piece[24
MinMax
算法没有真正按照预期工作
我需要的是将我的数组片段复制到新片段
,这样当新片段
被复制时,片段
不会被更改
以下是MinMax
算法的摘录:
private int MinMax(
int depth, Piece[] pieces, bool blacksTurn,
List<Move> Moves, Game game, int alpha, Move nextMove) {
Piece[] newPieces=new Piece[24];
Moves=Game.possibleMoves(pieces, blacksTurn, false);
if(depth==0||Moves.Count==0) {
return Evaluation(ref pieces);
}
int value;
if(blacksTurn==true) {
foreach(Move i in Moves) {
newPieces=DeepCopy.ObjectExtensions.Copy(pieces);
game.MovePiece(newPieces, blacksTurn, i.Moving, i.Destination, true);
game.DisplayBoard(pieces);
value=minMax(depth-1, newPieces, !blacksTurn, Moves, game, alpha, nextMove);
if(alpha>value) {
alpha=value;
nextMove=i;
}
// ...
我将在Piece
类上实现ICloneable
public interface ICloneable<T>
{
T Clone();
}
公共接口可克隆
{
T克隆();
}
pieces.Select(p=>p.Clone()).ToArray()
或只使用foreach
循环。我认为可能有助于解决您的问题。它使用iClonable接口使对象知道如何克隆自己。将属性作为该属性添加到类/结构片段中
:
using System.Runtime.InteropServices;
代码如下
namespace DeepCopy {
public static class ObjectExtensions {
public static T[] Copy<T>(this T[] pieces) {
return pieces.Select(x => {
var handle=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T)));
try {
Marshal.StructureToPtr(x, handle, false);
return (T)Marshal.PtrToStructure(handle, typeof(T));
}
finally {
Marshal.FreeHGlobal(handle);
}
}).ToArray();
}
}
}
它是有效的。要执行测试,请调用
TestClass.StartTest();
工件是否足够复杂,足以保证结构上的类?根据这是什么游戏,也可以选择根本不复制棋盘,而是记录信息,让你在评估后恢复移动。这个问题是关于“更高效的深度复制”还是关于“更高效的最小-最大”?@DJKRAZE OP专门要求深度复制,
MemberwiseClose
被记录为执行浅拷贝,这是正确的Peter..我仔细考虑了一下,我将删除我的comment@500我试着把它改成一个结构,所有的东西似乎都坏了,所以我把它改回来了。另外,您建议如何记录所有信息?我建议只实现Clone
方法。实现ICloneable
接口没有任何好处,您甚至不能使用“nice”返回类型,强制将其设置为object
。或者创建一个ICloneable
。当我这样做时,它会说它无法将type object[]转换为Piece[]。在Piece上实现ICloneable,或者您需要将其转换为pieces。选择(p=>(Piece)p、 克隆()).ToArray()
。它仍然无法工作。。。坦白地说,我很茫然,每一份似乎都有相同的结果,而整个事情仍然不起作用。哇。。。你做了什么?以前有什么问题。。?我很想再次发现这一切,但这次至少我知道问题出在哪里了。这根本不是复制,而是隐藏在我的移动函数中的某个地方。不过,我应该能在那里找到问题所在。所以你的工作非常好,只是我把它搞砸了;D.可能需要更加小心地使用AllocHGlobal
。如果它在复制时爆炸,那么你就有内存泄漏。
namespace DeepCopy {
public static class ObjectExtensions {
public static T[] Copy<T>(this T[] pieces) {
return pieces.Select(x => {
var handle=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T)));
try {
Marshal.StructureToPtr(x, handle, false);
return (T)Marshal.PtrToStructure(handle, typeof(T));
}
finally {
Marshal.FreeHGlobal(handle);
}
}).ToArray();
}
}
}
public static partial class TestClass {
public static void TestDeepCopy(Piece[] pieces) {
Piece[] newPieces=new Piece[24];
newPieces=DeepCopy.ObjectExtensions.Copy(pieces);
newPieces[0].isKing=true;
newPieces[0].Value=3;
newPieces[1].isKing=true;
newPieces[1].taken=true;
newPieces[1].Value=4;
Console.WriteLine("=== newPieces ===");
foreach(var x in newPieces)
Console.WriteLine(
"x.isKing={0}; x.isBlack={1}; x.Value={2}",
x.isKing, x.isBlack, x.Value
);
Console.WriteLine();
Console.WriteLine("=== pieces ===");
foreach(var x in pieces)
Console.WriteLine(
"x.isKing={0}; x.isBlack={1}; x.Value={2}",
x.isKing, x.isBlack, x.Value
);
}
public static void StartTest() {
var pieceA=new Piece(1, false, false, 1, 1, false);
var pieceB=new Piece(2, true, false, 1, 1, false);
var pieces=new[] { pieceA, pieceB };
TestDeepCopy(pieces);
}
}
TestClass.StartTest();