阅读棋盘后在Unity中创建FEN字符串

我使用C#在Unity中制作了一个功能齐全的国际象棋游戏。现在我想添加AI,对于国际象棋引擎,我选择了Stockfish。我在游戏中安装了引擎,但它什么也没做,因为它无法与棋盘通信。


要进行通信,我需要每行创建一个FEN字符串,从左上角开始,FEN字符串如下所示:


rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

小写是黑色的棋子,大写的白色棋子,数字是黑色的空格,w表示白色的下一个转弯,KQkq表示可用的施法,-表示通过是可用的,0 1个移动次数。


有没有人知道创建和操作字符串以制作FEN字符串的教程或提示?


我将把我到目前为止所做的代码粘贴到Stockfish进程中,我没有做任何与FEN字符串相关的事情,因为我真的不知道如何启动它。


欢迎任何链接或提示


void RunProcess()

{

    ProcessStartInfo startInfo = new ProcessStartInfo();

    startInfo.UseShellExecute = false;

    startInfo.RedirectStandardInput = true;

    startInfo.RedirectStandardOutput = true;

    startInfo.RedirectStandardError = false;

    startInfo.CreateNoWindow = true;

    startInfo.FileName = Application.streamingAssetsPath + "/stockfish_9_x64.exe";


    Process process = new Process();

    process.StartInfo = startInfo;

    process.Start();


    string output;


    process.StandardInput.WriteLine("uci");

    process.StandardInput.WriteLine("isready");

    process.StandardInput.WriteLine("position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");

    process.StandardInput.WriteLine("go");

    process.StandardInput.WriteLine("stop");

    process.StandardInput.WriteLine("quit");


    do

    {

        output = process.StandardOutput.ReadLine();

    } while (!output.Contains("move"));


    UnityEngine.Debug.Log(output);

}


void OnMouseDown()

{

    RunProcess();

}


www说
浏览 214回答 2
2回答

侃侃尔雅

只是为了获得基本部分,你可以做这样的事情(注意:未测试):public enum ChessPieces{&nbsp; &nbsp; King, Queen, Rook, // ... etc.&nbsp;}public class ChessPiece : MonoBehavior{&nbsp; &nbsp; public string FenId { get; }&nbsp; &nbsp; private readonly Dictionary<ChessPiece, string> FenIds = {&nbsp; &nbsp; &nbsp; &nbsp; { ChessPieces.King, "K" },&nbsp; &nbsp; &nbsp; &nbsp; { ChessPieces.Queen, "Q" },&nbsp; &nbsp; &nbsp; &nbsp; // ... etc.&nbsp; &nbsp; };&nbsp; &nbsp; // assuming you create the set of pieces programatically, use this constructor&nbsp; &nbsp; public ChessPiece(ChessPiece piece, ChessColor color)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; FenId = color == ChessColor.Black&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? FenIds[piece].ToLower()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : FenIds[piece].ToUpper();&nbsp; &nbsp; }}然后,假设您将电路板存储在行数组中,将布局转储到一个字符串中,我可能会在我的类上覆盖(也未经过测试):ToStringChessBoard// somewhere in your code set the board up_chessBoard.Rows.Add(new [] {&nbsp; &nbsp; new ChessPiece(ChessPieces.Rook, ChessColor.Black),&nbsp; &nbsp; new ChessPiece(ChessPieces.Knight, ChessColor.Black),&nbsp; &nbsp; // ... etc.&nbsp; &nbsp; })_chessBoard.Rows.Add(new [] { /* next row ... */ });// ... etc.// to create your output, put this into the override of ToString:var output = ""; // should be StringBuilder, but for clarity and since this isn't likely performance limiting...var rowIndex = 0;foreach (var row in _chessBoard.Rows){&nbsp; &nbsp; rowIndex++;&nbsp; &nbsp; var blankSpaces = 0;&nbsp; &nbsp; foreach(var piece in row)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (piece == null)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blankSpaces++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output += blankSpaces == 0&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? piece.FenId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : string.Format("{0}{1}", blankspaces, piece.FenId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blankSpaces = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (blankSpaces > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output += blankSpaces;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (rowIndex != 8)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; output += "/";&nbsp; &nbsp; }}此时,您已经在字符串中获得了基本布局,并且应该具有添加其他FEN字段的基本想法。我应该注意,我已经选择了一组数组来存储您的电路板。这可能不是最有效的存储机制(即在最好的情况下,你存储了50%的空值,这些值只会随着游戏的进行而增加),但是由于我们只谈论总共64个项目,因此我们可能在内存上没问题。

交互式爱情

public void LoadFEN(string fen){&nbsp; &nbsp; //Removes all pieces&nbsp; &nbsp; foreach (Piece piece in pieces)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Destroy(piece.gameObject);&nbsp; &nbsp; }&nbsp; &nbsp; pieces = new List<Piece>();&nbsp; &nbsp; AddSquareCoordinates(); // Add "local" coordinates to all squares&nbsp; &nbsp; #region FENStuff&nbsp; &nbsp; int xPos = 0;&nbsp; &nbsp; int yPos = 7;&nbsp; &nbsp; string[] fenChunks = fen.Split(' '); //Fen parts separated&nbsp; &nbsp; for (int x = 0; x < fenChunks[0].Length; x++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; switch (fenChunks[0][x])&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'K':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.King, xPos, yPos, -1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'k':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.King, xPos, yPos, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'Q':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.Queen, xPos, yPos, -1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'q':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.Queen, xPos, yPos, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'R':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.Rook, xPos, yPos, -1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'r':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.Rook, xPos, yPos, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'N':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.Knight, xPos, yPos, -1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'n':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.Knight, xPos, yPos, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'B':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.Bishop, xPos, yPos, -1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'b':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.Bishop, xPos, yPos, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'P':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.Pawn, xPos, yPos, -1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'p':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlacePiece(PieceType.Pawn, xPos, yPos, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (char.IsDigit(fenChunks[0][x]))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xPos += (int)char.GetNumericValue(fen[x]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xPos += 1;&nbsp; &nbsp; &nbsp; &nbsp; if (fenChunks[0][x] == '/')&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yPos -= 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xPos = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; SetStartPiecesCoor(); // Update all piece's coordinate&nbsp; &nbsp; AddCastleRooks(); // Add rooks to the king piece&nbsp; &nbsp; PawnFirstSquareAdjust(); //Checks if the pawns have already moved&nbsp; &nbsp; curTurn = fenChunks[1] == "w" ? -1 : 1;&nbsp; &nbsp; //fen cadtling priviledges code&nbsp; &nbsp; Piece kingWhite = GetKingPiece(-1);&nbsp; &nbsp; Piece kingBlack = GetKingPiece(1);&nbsp; &nbsp; bool castleWhiteKing = true, castleWhiteQueen = true, castleBlackKing = true, castleBlackQueen = true;&nbsp; &nbsp; for(int i = 0; i < fenChunks[2].Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; switch(fenChunks[2][i])&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'K':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; castleWhiteKing = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'Q':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; castleWhiteQueen = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'k':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; castleBlackKing = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'q':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; castleBlackQueen = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; kingWhite.started = castleWhiteKing && castleWhiteQueen;&nbsp; &nbsp; if(kingWhite.castlingRooks[0] != null)&nbsp; &nbsp; &nbsp; &nbsp; kingWhite.castlingRooks[0].started = castleWhiteKing;&nbsp; &nbsp; if(kingWhite.castlingRooks[1] != null)&nbsp; &nbsp; &nbsp; &nbsp; kingWhite.castlingRooks[1].started = castleWhiteQueen;&nbsp; &nbsp; kingBlack.started = castleBlackKing && castleBlackQueen;&nbsp; &nbsp; if (kingBlack.castlingRooks[1] != null)&nbsp; &nbsp; &nbsp; &nbsp; kingBlack.castlingRooks[0].started = castleBlackKing;&nbsp; &nbsp; if (kingBlack.castlingRooks[1] != null)&nbsp; &nbsp; &nbsp; &nbsp; kingBlack.castlingRooks[1].started = castleBlackQueen;&nbsp; &nbsp; if (fenChunks[3] != "-")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string coordinate = fenChunks[3];&nbsp; &nbsp; &nbsp; &nbsp; string row = coordinate[1] == '3' ? "4" : "5";&nbsp; &nbsp; &nbsp; &nbsp; coordinate = coordinate[0] + row;&nbsp; &nbsp; &nbsp; &nbsp; GetSquareFromLetterCoordinate(coordinate).holdingPiece.enPassantAvailable = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; halfMoveClock = Convert.ToInt32(fenChunks[4]);&nbsp; &nbsp; fullMoveClock = Convert.ToInt32(fenChunks[5]);&nbsp; &nbsp; #endregion&nbsp; &nbsp; UpdateGameTheme(curTheme);}public void ExportFEN(){&nbsp; &nbsp; int freeCellCount = 0;&nbsp; &nbsp; fen = "";&nbsp; &nbsp; for (int y = 7; y > -1; y--)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for (int x = 0; x < 8; x++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Piece piece = GetSquareFromCoordinate(new Vector2Int(x, y)).holdingPiece;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (piece == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; freeCellCount += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (freeCellCount != 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += freeCellCount.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; freeCellCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (piece.pieceType == PieceType.King)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (piece.team == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "K";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "k";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (piece.pieceType == PieceType.Queen)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (piece.team == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "Q";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "q";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (piece.pieceType == PieceType.Rook)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (piece.team == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "R";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "r";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (piece.pieceType == PieceType.Bishop)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (piece.team == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "B";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "b";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (piece.pieceType == PieceType.Knight)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (piece.team == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "N";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (piece.pieceType == PieceType.Pawn)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (piece.team == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "P";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "p";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (freeCellCount != 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += freeCellCount.ToString();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; freeCellCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; if (y != 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += '/';&nbsp; &nbsp; }&nbsp; &nbsp; fen += " ";&nbsp; &nbsp; string turnChar = curTurn == -1 ? "w" : "b";&nbsp; &nbsp; fen += turnChar + " ";&nbsp; &nbsp; Piece kingWhite = GetKingPiece(-1);&nbsp; &nbsp; Piece kingBlack = GetKingPiece(1);&nbsp; &nbsp; if (!kingWhite.started)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (kingWhite.castlingRooks[0] != null && !kingWhite.castlingRooks[0].started)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "K";&nbsp; &nbsp; &nbsp; &nbsp; if (kingWhite.castlingRooks[1] != null && !kingWhite.castlingRooks[1].started)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "Q";&nbsp; &nbsp; }&nbsp; &nbsp; if (!kingBlack.started)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (kingBlack.castlingRooks[0] != null && !kingBlack.castlingRooks[0].started)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "k";&nbsp; &nbsp; &nbsp; &nbsp; if (kingBlack.castlingRooks[1] != null && !kingBlack.castlingRooks[1].started)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fen += "q";&nbsp; &nbsp; }&nbsp; &nbsp; fen += " ";&nbsp; &nbsp; fen += enPassantSquare + " ";&nbsp; &nbsp; fen += halfMoveClock.ToString() + " " + fullMoveClock.ToString();}private void PlacePiece(PieceType type, int xCoord, int yCoord, int team){&nbsp; &nbsp; Square square = GetSquareFromCoordinate(new Vector2Int(xCoord, yCoord));&nbsp; &nbsp; GameObject pieceObj;&nbsp; &nbsp; Piece piece;&nbsp; &nbsp; int prefabIndex = -1;&nbsp; &nbsp; switch (type)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case PieceType.King:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefabIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case PieceType.Queen:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefabIndex = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case PieceType.Rook:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefabIndex = 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case PieceType.Knight:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefabIndex = 3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case PieceType.Bishop:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefabIndex = 4;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case PieceType.Pawn:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefabIndex = 5;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; pieceObj = Instantiate(piecePrefabs[prefabIndex], pieceParent.transform);&nbsp; &nbsp; pieceObj.transform.position = square.transform.position;&nbsp; &nbsp; piece = pieceObj.GetComponent<Piece>();&nbsp; &nbsp; piece.team = team;&nbsp; &nbsp; piece.curSquare = square;&nbsp; &nbsp; piece.board = this;&nbsp; &nbsp; pieces.Add(piece);}private void AddCastleRooks(){&nbsp; &nbsp; foreach (Piece piece in pieces)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (piece.pieceType == PieceType.King)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (piece.team == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Piece rook1 = GetSquareFromCoordinate(new Vector2Int(7, 0)).holdingPiece;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rook1 != null && rook1.pieceType == PieceType.Rook)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; piece.castlingRooks.Add(rook1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else piece.castlingRooks.Add(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Piece rook2 = GetSquareFromCoordinate(new Vector2Int(0, 0)).holdingPiece;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rook2 != null && rook1.pieceType == PieceType.Rook)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; piece.castlingRooks.Add(rook2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else piece.castlingRooks.Add(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Piece rook1 = GetSquareFromCoordinate(new Vector2Int(7, 7)).holdingPiece;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rook1 != null && rook1.pieceType == PieceType.Rook)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; piece.castlingRooks.Add(rook1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else piece.castlingRooks.Add(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Piece rook2 = GetSquareFromCoordinate(new Vector2Int(0, 7)).holdingPiece;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rook2 != null && rook1.pieceType == PieceType.Rook)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; piece.castlingRooks.Add(rook2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else piece.castlingRooks.Add(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}private void PawnFirstSquareAdjust(){&nbsp; &nbsp; int startRank;&nbsp; &nbsp; foreach (Piece piece in pieces)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; startRank = piece.team == -1 ? 1 : 6;&nbsp; &nbsp; &nbsp; &nbsp; if (piece.pieceType == PieceType.Pawn)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (piece.curSquare.coor.y != startRank)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; piece.started = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我还在开发我的国际象棋应用程序,我知道可能为时已晚。但希望这有帮助。我有 PieceType 作为枚举。我想你可以找出变量。另外,我在部分Move()函数中重置了moveClocks。
打开App,查看更多内容
随时随地看视频慕课网APP