2

Good Morning. I am trying to implement Stockfish to a Unity chess game, Ive been told that the best way is using Spawn.Process Does anyone know of existing code I can look at and take as reference?

Are different Gamestates the best way to communicate with AI?

Thanks!

Ruzihm
  • 19,749
  • 5
  • 36
  • 48

2 Answers2

5

If you can represent your game state in Forsyth-Edwards Notation and read Algebraic Notation to advance your board state, this should work:

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;
}
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
0

I used http://chessforeva.blogspot.com/2010/10/unity3d-chess-project.html as a starting point for my MacOS, iOS, Android, Windows Chess App/Game a few years back.

It uses algebraic notation using FEN and the meat and potatoes of what you want to do are in c0_4unity_chess.js, and Scriptings.js. Also StockFishCall.js, under the Update() function you will see a similar to GetBestMove() function above, like Ruzihm mentioned using ProcessLineIN() for "bestmove".

In the c0_4unity_chess.js script, there are functions called c0_get_FEN(), c0_put_to_PGN(), and set_pos() which should help point you in the right direction.

Update: https://chessforeva.gitlab.io/ There's the gitlab.io link.

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69