0

我正在构建一个国际象棋 GUI,它应该与 Stockfish 对话。我听说我必须生成一个 FEN 字符串,以告诉 Stockfish 已采取的行动。所以问题是,我该怎么做?我在这里真的遇到了死胡同。我正在使用Eclipse IDE。

4

2 回答 2

0

我不确定你做了什么或使用什么编程语言,但由于你使用的是 Eclipse IDE,我建议它是 Java。

制作 Stockfish 的一个热门技巧是观看此视频: https ://www.youtube.com/watch?list=PLQV5mozTHmacMeRzJCW_8K3qw2miYqd0c&v=vuvTFNreykk

视频中链接的 Stackoverflow:使用通用国际象棋界面

所以要解决你的问题:

所以问题是,我该怎么做?

好吧,简单的解决方案是寻找已经实施的制作 FEN 字符串的项目。我知道他们有很多。如果你想用一种简单但笨拙的方式在 Java 中制作 FEN 字符串,我给你做了这个:

注意:此实现认为您将整个电路板放在 String[][] 中(我没有麻烦在这些深夜使它更高级)

注 2:它不构成整个 FEN 字符串。它缺少 Active 颜色、Casling 可用性、En passant、Halfmove 时钟和 Fullmove 编号,但我相信您将能够轻松实现

输出:

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;
}
于 2018-05-01T22:44:33.947 回答