2 回答

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個贊
只是為了獲得基本部分,你可以做這樣的事情(注意:未測試):
public enum ChessPieces
{
King, Queen, Rook, // ... etc.
}
public class ChessPiece : MonoBehavior
{
public string FenId { get; }
private readonly Dictionary<ChessPiece, string> FenIds = {
{ ChessPieces.King, "K" },
{ ChessPieces.Queen, "Q" },
// ... etc.
};
// assuming you create the set of pieces programatically, use this constructor
public ChessPiece(ChessPiece piece, ChessColor color)
{
FenId = color == ChessColor.Black
? FenIds[piece].ToLower()
: FenIds[piece].ToUpper();
}
}
然后,假設(shè)您將電路板存儲在行數(shù)組中,將布局轉(zhuǎn)儲到一個字符串中,我可能會在我的類上覆蓋(也未經(jīng)過測試):ToStringChessBoard
// somewhere in your code set the board up
_chessBoard.Rows.Add(new [] {
new ChessPiece(ChessPieces.Rook, ChessColor.Black),
new ChessPiece(ChessPieces.Knight, ChessColor.Black),
// ... etc.
})
_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)
{
rowIndex++;
var blankSpaces = 0;
foreach(var piece in row)
{
if (piece == null)
{
blankSpaces++;
}
else
{
output += blankSpaces == 0
? piece.FenId
: string.Format("{0}{1}", blankspaces, piece.FenId);
blankSpaces = 0;
}
if (blankSpaces > 0)
{
output += blankSpaces;
}
}
if (rowIndex != 8)
{
output += "/";
}
}
此時,您已經(jīng)在字符串中獲得了基本布局,并且應(yīng)該具有添加其他FEN字段的基本想法。
我應(yīng)該注意,我已經(jīng)選擇了一組數(shù)組來存儲您的電路板。這可能不是最有效的存儲機(jī)制(即在最好的情況下,你存儲了50%的空值,這些值只會隨著游戲的進(jìn)行而增加),但是由于我們只談?wù)摽偣?4個項(xiàng)目,因此我們可能在內(nèi)存上沒問題。

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個贊
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;
}
}
}
}
我還在開發(fā)我的國際象棋應(yīng)用程序,我知道可能為時已晚。但希望這有幫助。
我有 PieceType 作為枚舉。我想你可以找出變量。
另外,我在部分Move()函數(shù)中重置了moveClocks。
- 2 回答
- 0 關(guān)注
- 186 瀏覽
添加回答
舉報(bào)