1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 08:43:09 +00:00

Space inflate Position::to_fen()

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2008-09-29 19:02:48 +02:00
parent e9e51da4b8
commit 849809e97e

View file

@ -70,7 +70,7 @@ Position::Position(const std::string &fen) {
void Position::from_fen(const std::string &fen) { void Position::from_fen(const std::string &fen) {
static const std::string piecesStr = "KQRBNPkqrbnp"; static const std::string pieceLetters = "KQRBNPkqrbnp";
static const Piece pieces[] = { WK, WQ, WR, WB, WN, WP, BK, BQ, BR, BB, BN, BP }; static const Piece pieces[] = { WK, WQ, WR, WB, WN, WP, BK, BQ, BR, BB, BN, BP };
clear(); clear();
@ -93,7 +93,7 @@ void Position::from_fen(const std::string &fen) {
rank--; rank--;
continue; continue;
} }
size_t idx = piecesStr.find(fen[i]); size_t idx = pieceLetters.find(fen[i]);
if (idx == std::string::npos) if (idx == std::string::npos)
{ {
std::cout << "Error in FEN at character " << i << std::endl; std::cout << "Error in FEN at character " << i << std::endl;
@ -214,40 +214,50 @@ void Position::from_fen(const std::string &fen) {
/// probably only useful for debugging. /// probably only useful for debugging.
const std::string Position::to_fen() const { const std::string Position::to_fen() const {
char pieceLetters[] = " PNBRQK pnbrqk";
std::string result; static const std::string pieceLetters = " PNBRQK pnbrqk";
std::string fen;
int skip; int skip;
for(Rank rank = RANK_8; rank >= RANK_1; rank--) { for (Rank rank = RANK_8; rank >= RANK_1; rank--)
{
skip = 0; skip = 0;
for(File file = FILE_A; file <= FILE_H; file++) { for (File file = FILE_A; file <= FILE_H; file++)
Square square = make_square(file, rank); {
if(square_is_occupied(square)) { Square sq = make_square(file, rank);
if(skip > 0) result += (char)skip + '0'; if (!square_is_occupied(sq))
result += pieceLetters[piece_on(square)]; { skip++;
continue;
}
if (skip > 0)
{
fen += (char)skip + '0';
skip = 0; skip = 0;
} }
else skip++; fen += pieceLetters[piece_on(sq)];
} }
if(skip > 0) result += (char)skip + '0'; if (skip > 0)
result += (rank > RANK_1)? '/' : ' '; fen += (char)skip + '0';
fen += (rank > RANK_1 ? '/' : ' ');
} }
fen += (sideToMove == WHITE ? 'w' : 'b') + ' ';
if (castleRights != NO_CASTLES)
{
if (can_castle_kingside(WHITE)) fen += 'K';
if (can_castle_queenside(WHITE)) fen += 'Q';
if (can_castle_kingside(BLACK)) fen += 'k';
if (can_castle_queenside(BLACK)) fen += 'q';
} else
fen += '-';
result += (sideToMove == WHITE)? 'w' : 'b'; fen += ' ';
result += ' '; if (ep_square() != SQ_NONE)
if(castleRights == NO_CASTLES) result += '-'; fen += square_to_string(ep_square());
else { else
if(can_castle_kingside(WHITE)) result += 'K'; fen += '-';
if(can_castle_queenside(WHITE)) result += 'Q';
if(can_castle_kingside(BLACK)) result += 'k';
if(can_castle_queenside(BLACK)) result += 'q';
}
result += ' '; return fen;
if(ep_square() == SQ_NONE) result += '-';
else result += square_to_string(ep_square());
return result;
} }