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