-->

How to generate a FEN-string and send it to Stockf

2020-08-01 11:43发布

问题:

I am building a chess GUI, which is supposed to talk to Stockfish. I have heard that I have to generate a FEN-string, to tell Stockfish about the move that has been made. So the question is, how do I do this? I have really met a dead end here.. I am using the Eclipse IDE.

回答1:

I don't want to repeat my answers. Please review what I had posted:

  • https://chess.stackexchange.com/questions/16601/connecting-chess-engine-with-a-java-program/16602#16602
  • https://chess.stackexchange.com/questions/6110/which-interface-protocol-should-i-implement-for-my-chess-engine
  • https://chess.stackexchange.com/questions/8462/uci-engine-state-notion


回答2:

I am not sure what you have done or in what programming language, but since you are using the Eclipse IDE, I'm suggesting it is Java.

A hot tips for making the Stockfish to work is by looking at this video: https://www.youtube.com/watch?list=PLQV5mozTHmacMeRzJCW_8K3qw2miYqd0c&v=vuvTFNreykk

The Stackoverflow linked in the video: Using the Universal Chess Interface

So to tackle your question:

So the question is, how do I do this?

Well, the simple solution would be to look for already implemented projects making FEN-strings. I know there are a lot of them. If you want a simple, yet clumsy way of making a FEN-string in Java I made you this:

Note: This implementation believes that you have your entire board in a String[][] (I didn't make the trouble making it more advanced at these late hours)

Note 2: It does not make the entire FEN-string. It is missing the Active color, Castling availability, En passant, Halfmove clock and the Fullmove number, but I'm sure you will be able to implement that easily

Output:

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR

private final String RANK_SEPARATOR = "/";

private String[][] board = {
        {"r","n","b","q","k","b","n","r"},
        {"p","p","p","p","p","p","p","p"},
        {"","","","","","","",""},
        {"","","","","","","",""},
        {"","","","","","","",""},
        {"","","","","","","",""},
        {"P","P","P","P","P","P","P","P"},
        {"R","N","B","Q","K","B","N","R"}
};

public String translateBoardToFEN(String[][] board) {
    String fen = "";
    for (int rank = 0; rank < board.length; rank++) {
        // count empty fields
        int empty = 0;
        // empty string for each rank
        String rankFen = "";
        for (int file = 0; file < board[rank].length; file++) {
            if(board[rank][file].length() == 0) {
                empty++;
            } else {
                // add the number to the fen if not zero.
                if (empty != 0) rankFen += empty;
                // add the letter to the fen
                rankFen += board[rank][file];
                // reset the empty
                empty = 0;
            }
        }
        // add the number to the fen if not zero.
        if (empty != 0) rankFen += empty;
        // add the rank to the fen
        fen += rankFen;
        // add rank separator. If last then add a space
        if (!(rank == board.length-1)) {
            fen += RANK_SEPARATOR;
        } else {
            fen += " ";
        }
    }
    return fen;
}