gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:35:06 25 4
gpt4 key购买 nike

基本上我遇到的问题是我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com