问题描述
我正在构建一个国际象棋GUI,应该与Stockfish对话。我听说我必须生成一个FEN字符串,以告知Stockfish已经采取的措施。所以问题是,我该怎么做?我在这里真的遇到了一个死胡同。我正在使用Eclipse IDE。
我不确定您所做的工作或使用哪种编程语言,但是由于您使用的是Eclipse IDE,因此我m表示这是Java。
使Stockfish正常工作的热门提示是观看以下视频:
要解决您的问题:
那么,简单的解决方案就是寻找已经实施的项目ng FEN字符串。我知道他们很多。如果您想使用一种简单的笨拙的方式在Java中制作FEN字符串,我可以这样:
注意:实施过程相信您将整个开发板都放在了String [] []中(在这些晚点时间,我没麻烦让它变得更高级)
注意2:它不会构成整个FEN字符串。它缺少有效颜色,Castle可用性,En passant,Halfmove时钟和Fullmove编号,但是我敢肯定您将能够轻松实现该功能
输出:
private final String RANK_SEPARATOR = /;
private String [] []公告板= {
{ 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 ++){
//计算空字段
int empty = 0;
//每个级别为空字符串
字符串rankFen =;
for(int file = 0; file< board [rank] .length; file ++){
if(board [rank] [file] .length()== 0){
空++;
} else {
//如果不为零,则将数字加到fen中。
if(empty!= 0)rankFen + = empty;
//将字母添加到分
rankFen + = board [rank] [file];
//重置空
empty = 0;
}
}
//如果不为零,则将数字加到分位数。
if(empty!= 0)rankFen + = empty;
//将等级加到分
fen + = rankFen;
//添加等级分隔符。如果最后一个则加一个空格
if(!(rank == board.length-1)){
fen + = RANK_SEPARATOR;
} else {
fen + =;
}
}
return fen;
}
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.
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:
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:
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;
}
这篇关于如何生成FEN字符串并将其发送到Stockfish?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!