如何生成FEN字符串并将其发送给Stockfish?

时间:2018-05-01 13:03:13

标签: java string chess uci

我正在建立一个国际象棋GUI,它应该与Stockfish交谈。我听说我必须生成一个FEN字符串,告诉Stockfish已经做出的举动。所以问题是,我该怎么做?我真的遇到了死胡同......我正在使用Eclipse IDE。

2 个答案:

答案 0 :(得分:-1)

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

使Stockfish工作的热门提示是观看此视频: https://www.youtube.com/watch?list=PLQV5mozTHmacMeRzJCW_8K3qw2miYqd0c&v=vuvTFNreykk

视频中链接的Stackoverflow:Using the Universal Chess Interface

所以要解决你的问题:

  

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

嗯,简单的解决方案是查找已经实现的FEN字符串项目。我知道他们中有很多。如果你想要一个简单的,又笨拙的在Java中制作FEN字符串的方法,我就这样做了:

注意:此实现认为您将整个电路板放在String [] []中(我没有在这些晚些时候使其更先进的麻烦)

注意2:它不会生成整个FEN字符串。它缺少活动颜色,Castling可用性,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;
}

答案 1 :(得分:-1)