mirror of
https://github.com/sockspls/badfish
synced 2025-04-29 16:23:09 +00:00
Cleanup Position::to_fen()
Less invasive then previous patches, but still a good enhancement. Also some indulge on STL algorithms :-) No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
c2048136ec
commit
02882dfe81
2 changed files with 54 additions and 51 deletions
|
@ -22,6 +22,7 @@
|
||||||
//// Includes
|
//// Includes
|
||||||
////
|
////
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
@ -43,6 +44,8 @@ using std::string;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
|
|
||||||
|
static inline bool isZero(char c) { return c == '0'; }
|
||||||
|
|
||||||
struct PieceLetters : std::map<char, Piece> {
|
struct PieceLetters : std::map<char, Piece> {
|
||||||
|
|
||||||
PieceLetters() {
|
PieceLetters() {
|
||||||
|
@ -54,6 +57,17 @@ struct PieceLetters : std::map<char, Piece> {
|
||||||
operator[]('N') = WN; operator[]('n') = BN;
|
operator[]('N') = WN; operator[]('n') = BN;
|
||||||
operator[]('P') = WP; operator[]('p') = BP;
|
operator[]('P') = WP; operator[]('p') = BP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char from_piece(Piece p) const {
|
||||||
|
|
||||||
|
map<char, Piece>::const_iterator it;
|
||||||
|
for (it = begin(); it != end(); ++it)
|
||||||
|
if (it->second == p)
|
||||||
|
return it->first;
|
||||||
|
|
||||||
|
assert(false);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
////
|
////
|
||||||
|
@ -69,6 +83,7 @@ Key Position::zobExclusion;
|
||||||
Score Position::PieceSquareTable[16][64];
|
Score Position::PieceSquareTable[16][64];
|
||||||
|
|
||||||
static bool RequestPending = false;
|
static bool RequestPending = false;
|
||||||
|
static PieceLetters pieceLetters;
|
||||||
|
|
||||||
|
|
||||||
/// Constructors
|
/// Constructors
|
||||||
|
@ -155,8 +170,6 @@ void Position::from_fen(const string& fen) {
|
||||||
6) Fullmove number: The number of the full move. It starts at 1, and is incremented after Black's move.
|
6) Fullmove number: The number of the full move. It starts at 1, and is incremented after Black's move.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static PieceLetters pieceLetters;
|
|
||||||
|
|
||||||
char token;
|
char token;
|
||||||
std::istringstream ss(fen);
|
std::istringstream ss(fen);
|
||||||
Rank rank = RANK_8;
|
Rank rank = RANK_8;
|
||||||
|
@ -299,67 +312,59 @@ bool Position::set_castling_rights(char token) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::to_fen() converts the position object to a FEN string. This is
|
/// Position::to_fen() returns a FEN representation of the position. In case
|
||||||
/// probably only useful for debugging.
|
/// of Chess960 the Shredder-FEN notation is used. Mainly a debugging function.
|
||||||
|
|
||||||
const string Position::to_fen() const {
|
const string Position::to_fen() const {
|
||||||
|
|
||||||
static const string pieceLetters = " PNBRQK pnbrqk";
|
|
||||||
string fen;
|
string fen;
|
||||||
int skip;
|
Square sq;
|
||||||
|
char emptyCnt = '0';
|
||||||
|
|
||||||
for (Rank rank = RANK_8; rank >= RANK_1; rank--)
|
for (Rank rank = RANK_8; rank >= RANK_1; rank--)
|
||||||
{
|
{
|
||||||
skip = 0;
|
|
||||||
for (File file = FILE_A; file <= FILE_H; file++)
|
for (File file = FILE_A; file <= FILE_H; file++)
|
||||||
{
|
{
|
||||||
Square sq = make_square(file, rank);
|
sq = make_square(file, rank);
|
||||||
if (!square_is_occupied(sq))
|
|
||||||
{ skip++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (skip > 0)
|
|
||||||
{
|
|
||||||
fen += (char)skip + '0';
|
|
||||||
skip = 0;
|
|
||||||
}
|
|
||||||
fen += pieceLetters[piece_on(sq)];
|
|
||||||
}
|
|
||||||
if (skip > 0)
|
|
||||||
fen += (char)skip + '0';
|
|
||||||
|
|
||||||
fen += (rank > RANK_1 ? '/' : ' ');
|
if (square_is_occupied(sq))
|
||||||
|
{
|
||||||
|
fen += emptyCnt;
|
||||||
|
fen += pieceLetters.from_piece(piece_on(sq));
|
||||||
|
emptyCnt = '0';
|
||||||
|
} else
|
||||||
|
emptyCnt++;
|
||||||
|
}
|
||||||
|
fen += emptyCnt;
|
||||||
|
fen += '/';
|
||||||
|
emptyCnt = '0';
|
||||||
}
|
}
|
||||||
fen += (sideToMove == WHITE ? "w " : "b ");
|
|
||||||
|
fen.erase(std::remove_if(fen.begin(), fen.end(), isZero), fen.end());
|
||||||
|
fen.erase(--fen.end());
|
||||||
|
fen += (sideToMove == WHITE ? " w " : " b ");
|
||||||
|
|
||||||
if (st->castleRights != NO_CASTLES)
|
if (st->castleRights != NO_CASTLES)
|
||||||
{
|
{
|
||||||
if (initialKFile == FILE_E && initialQRFile == FILE_A && initialKRFile == FILE_H)
|
const bool Chess960 = initialKFile != FILE_E
|
||||||
{
|
|| initialQRFile != FILE_A
|
||||||
if (can_castle_kingside(WHITE)) fen += 'K';
|
|| initialKRFile != FILE_H;
|
||||||
if (can_castle_queenside(WHITE)) fen += 'Q';
|
|
||||||
if (can_castle_kingside(BLACK)) fen += 'k';
|
if (can_castle_kingside(WHITE))
|
||||||
if (can_castle_queenside(BLACK)) fen += 'q';
|
fen += Chess960 ? char(toupper(file_to_char(initialKRFile))) : 'K';
|
||||||
}
|
|
||||||
else
|
if (can_castle_queenside(WHITE))
|
||||||
{
|
fen += Chess960 ? char(toupper(file_to_char(initialQRFile))) : 'Q';
|
||||||
if (can_castle_kingside(WHITE))
|
|
||||||
fen += char(toupper(file_to_char(initialKRFile)));
|
if (can_castle_kingside(BLACK))
|
||||||
if (can_castle_queenside(WHITE))
|
fen += Chess960 ? file_to_char(initialKRFile) : 'k';
|
||||||
fen += char(toupper(file_to_char(initialQRFile)));
|
|
||||||
if (can_castle_kingside(BLACK))
|
if (can_castle_queenside(BLACK))
|
||||||
fen += file_to_char(initialKRFile);
|
fen += Chess960 ? file_to_char(initialQRFile) : 'q';
|
||||||
if (can_castle_queenside(BLACK))
|
|
||||||
fen += file_to_char(initialQRFile);
|
|
||||||
}
|
|
||||||
} else
|
} else
|
||||||
fen += '-';
|
fen += '-';
|
||||||
|
|
||||||
fen += ' ';
|
fen += (ep_square() == SQ_NONE ? " -" : " " + square_to_string(ep_square()));
|
||||||
if (ep_square() != SQ_NONE)
|
|
||||||
fen += square_to_string(ep_square());
|
|
||||||
else
|
|
||||||
fen += '-';
|
|
||||||
|
|
||||||
return fen;
|
return fen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -180,10 +180,8 @@ inline Square square_from_string(const std::string& str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const std::string square_to_string(Square s) {
|
inline const std::string square_to_string(Square s) {
|
||||||
std::string str;
|
return std::string(1, file_to_char(square_file(s)))
|
||||||
str += file_to_char(square_file(s));
|
+ std::string(1, rank_to_char(square_rank(s)));
|
||||||
str += rank_to_char(square_rank(s));
|
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool file_is_ok(File f) {
|
inline bool file_is_ok(File f) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue