我使用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();
}
答案 0 :(得分:0)
仅需基本内容,您可以做类似的事情(注意:未经测试):
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();
}
}
然后,假设您将电路板存储在一行行中,以将布局转储为一个字符串,我可能会在我的ToString
类(同样未经测试)上覆盖ChessBoard
:>
// 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 += "/";
}
}
这时,您已经将基本布局放在了一个字符串中,并且应该具有添加其他FEN字段的基本思想。
我应该注意,我选择了一组阵列来存储您的电路板。那可能不是最有效的存储机制(在最佳情况下,您存储的是50%的空值,随着游戏的进行只会增加),但是由于我们只讨论了64个项目,因此没关系。
答案 1 :(得分:0)
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。