Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow most parts of fen to be optional. #1234

Merged
merged 3 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/chess/board.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@

namespace lczero {

using std::string;

const char* ChessBoard::kStartposFen =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";

Expand Down Expand Up @@ -969,22 +967,34 @@ MoveList ChessBoard::GenerateLegalMoves() const {
return result;
}

void ChessBoard::SetFromFen(const std::string& fen, int* rule50_ply,
int* moves) {
void ChessBoard::SetFromFen(std::string fen, int* rule50_ply, int* moves) {
Clear();
int row = 7;
int col = 0;

std::istringstream fen_str(fen);
string board;
string who_to_move;
string castlings;
string en_passant;
int rule50_halfmoves;
int total_moves;
fen_str >> board >> who_to_move >> castlings >> en_passant >>
rule50_halfmoves >> total_moves;
// Remove any trailing whitespaces to detect eof after the last field.
fen.erase(std::find_if(fen.rbegin(), fen.rend(),
[](char c) { return !std::isspace(c); })
.base(),
fen.end());

std::istringstream fen_str(fen);
std::string board;
fen_str >> board;
std::string who_to_move = "w";
if (!fen_str.eof()) fen_str >> who_to_move;
// Assume no castling rights. Other engines, e.g., Stockfish, assume kings and
// rooks on their initial rows can each castle with the outer-most rook. Our
// implementation currently supports 960 castling where white and black rooks
// have matching columns, so it's unclear which rights to assume.
std::string castlings = "-";
if (!fen_str.eof()) fen_str >> castlings;
std::string en_passant = "-";
if (!fen_str.eof()) fen_str >> en_passant;
int rule50_halfmoves = 0;
if (!fen_str.eof()) fen_str >> rule50_halfmoves;
int total_moves = 1;
if (!fen_str.eof()) fen_str >> total_moves;
if (!fen_str) throw Exception("Bad fen string: " + fen);
Mardak marked this conversation as resolved.
Show resolved Hide resolved

for (char c : board) {
Expand Down Expand Up @@ -1127,8 +1137,8 @@ bool ChessBoard::HasMatingMaterial() const {
return light_bishop && dark_bishop;
}

string ChessBoard::DebugString() const {
string result;
std::string ChessBoard::DebugString() const {
std::string result;
for (int i = 7; i >= 0; --i) {
for (int j = 0; j < 8; ++j) {
if (!our_pieces_.get(i, j) && !their_pieces_.get(i, j)) {
Expand Down
2 changes: 1 addition & 1 deletion src/chess/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ChessBoard {
// If @rule50_ply and @moves are not nullptr, they are filled with number
// of moves without capture and number of full moves since the beginning of
// the game.
void SetFromFen(const std::string& fen, int* rule50_ply = nullptr,
void SetFromFen(std::string fen, int* rule50_ply = nullptr,
int* moves = nullptr);
// Nullifies the whole structure.
void Clear();
Expand Down
24 changes: 24 additions & 0 deletions src/chess/board_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ TEST(ChessBoard, PseudolegalMovesStartingPos) {
EXPECT_EQ(moves.size(), 20);
}

TEST(ChessBoard, PartialFen) {
ChessBoard board;
int rule50ply;
int gameply;
board.SetFromFen("k/1R//K", &rule50ply, &gameply);
auto moves = board.GeneratePseudolegalMoves();

EXPECT_EQ(moves.size(), 19);
EXPECT_EQ(rule50ply, 0);
EXPECT_EQ(gameply, 1);
}

TEST(ChessBoard, PartialFenWithSpaces) {
ChessBoard board;
int rule50ply;
int gameply;
board.SetFromFen(" k/1R//K w ", &rule50ply, &gameply);
auto moves = board.GeneratePseudolegalMoves();

EXPECT_EQ(moves.size(), 19);
EXPECT_EQ(rule50ply, 0);
EXPECT_EQ(gameply, 1);
}

namespace {
int Perft(const ChessBoard& board, int max_depth, bool dump = false,
int depth = 0) {
Expand Down
6 changes: 0 additions & 6 deletions src/chess/pgn.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ class PgnReader {
if (uc_line.find("[FEN \"", 0) == 0) {
auto start_trimmed = line.substr(6);
cur_startpos_ = start_trimmed.substr(0, start_trimmed.find('"'));
// Some 'opening books' omit the last 2 fields, so there is only 3
// space delimiters.
if (std::count(cur_startpos_.begin(), cur_startpos_.end(), ' ') ==
3) {
cur_startpos_ += " 0 1";
}
cur_board_.SetFromFen(cur_startpos_);
}
continue;
Expand Down