在 Unity 中使用 Stockfish Chess AI

问题描述 投票:0回答:2

早上好。我正在尝试将 Stockfish 实施到 Unity 国际象棋游戏中,有人告诉我最好的方法是使用 Spawn.Process 有谁知道我可以查看并作为参考的现有代码?

不同的游戏状态是与 AI 交流的最佳方式吗?

谢谢!

c# unity3d artificial-intelligence uci
2个回答
5
投票

如果您可以用 Forsyth-Edwards Notation 表示您的游戏状态并阅读 Algebraic Notation 来推进您的棋盘状态,这应该有效:

string GetBestMove(string forsythEdwardsNotationString){
    var p = new System.Diagnostics.Process();
    p.StartInfo.FileName = "stockfishExecutable";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();  
    string setupString = "position fen "+forsythEdwardsNotationString;
    p.StandardInput.WriteLine(setupString);
    
    // Process for 5 seconds
    string processString = "go movetime 5000";
    
    // Process 20 deep
    // string processString = "go depth 20";

    p.StandardInput.WriteLine(processString);
    
    string bestMoveInAlgebraicNotation = p.StandardOutput.ReadLine();

    p.Close();

    return bestMoveInAlgebraicNotation;
}

0
投票

几年前,我使用 http://chessforeva.blogspot.com/2010/10/unity3d-chess-project.html 作为我的 MacOS、iOS、Android、Windows 国际象棋应用程序/游戏的起点。

它使用 FEN 的代数符号,你想做的事情的主要内容在 c0_4unity_chess.js 和 Scriptings.js 中。还有 StockFishCall.js,在 Update() 函数下你会看到一个类似于上面的 GetBestMove() 函数,就像 Ruzihm 提到的使用 ProcessLineIN() 作为“bestmove”。

在 c0_4unity_chess.js 脚本中,有称为 c0_get_FEN()、c0_put_to_PGN() 和 set_pos() 的函数,它们应该可以帮助您指明正确的方向。

更新:https://chessforeva.gitlab.io/ 有 gitlab.io 链接。

© www.soinside.com 2019 - 2024. All rights reserved.