c# - 在没有序列化的情况下深度复制数组c#

标签 c# arrays deep-copy minmax

基本上我遇到的问题是我的 MinMax 算法没有真正按预期工作。

我需要的是将我的数组片段复制到newPieces,这样piecesnewPieces<时不会改变 是。

这是 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 类。

[StructLayout(LayoutKind.Sequential)]
public class Piece
{

    public CellReference Location;
    public bool isBlack { get; set; }
    public bool isKing { get; set; }
    private int Value { get; set; }
    public bool taken { get; set; }

    public Piece()
    {

    }

    public Piece(int i, bool isBlack, bool isKing, int CellsEast, int CellsNorth, bool taken)
    {
        this.Value = i;
        Location.CellsEast = CellsEast;
        Location.CellsNorth = CellsNorth;
        this.isBlack = isBlack;
        this.isKing = isKing;
        this.taken = taken;
    }
}

最佳答案

我会实现 ICloneable ICloneable<T>Piece 上类。

public interface ICloneable<T>
{
    T Clone();
}

pieces.Select(p => p.Clone()).ToArray();或者只使用 foreach循环。

关于c# - 在没有序列化的情况下深度复制数组c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15368699/

相关文章:

c# - html 链接和悬停事件在图像上的某些位置

c# - 跨进程/平台的字符串列表的一致 HashCode

c# - Dapper 查询结果在对象中将表的 Pk 设置为 null

c++ - 实现深拷贝

c# - 如何手动创建深拷贝

c++ - 可以将 C++ 复制构造函数用于其他目的吗?

c# - 您发现了扩展方法的哪些优势?

java - 如何使用 java 8 流将 float ArrayList 转换为原始 float 数组?

javascript - 使用javascript计算两个数组之间的相似性

arrays - 将指针数组传递给接受二维数组的函数