Skip to content

Instantly share code, notes, and snippets.

@markhc
Created July 23, 2019 19:05
Show Gist options
  • Save markhc/584bb5b02c5f301c4c7003f65993d8d8 to your computer and use it in GitHub Desktop.
Save markhc/584bb5b02c5f301c4c7003f65993d8d8 to your computer and use it in GitHub Desktop.
std::string Board::prettyPrint(bool useUnicodeChars) const
{
std::string_view charPieces[2][6] = {
{
useUnicodeChars ? "\u2659" : "P",
useUnicodeChars ? "\u2657" : "B",
useUnicodeChars ? "\u2658" : "N",
useUnicodeChars ? "\u2656" : "R",
useUnicodeChars ? "\u2655" : "Q",
useUnicodeChars ? "\u2654" : "K",
},
{
useUnicodeChars ? "\u265F" : "p",
useUnicodeChars ? "\u265D" : "b",
useUnicodeChars ? "\u265E" : "n",
useUnicodeChars ? "\u265C" : "r",
useUnicodeChars ? "\u265B" : "q",
useUnicodeChars ? "\u265A" : "k",
},
};
std::stringstream ss;
auto printIcon = [&](Square square) {
auto const pieces = {
Piece::Pawn,
Piece::Bishop,
Piece::Knight,
Piece::Rook,
Piece::Queen,
Piece::King,
};
for (auto&& color : {Color::White, Color::Black}) {
for (auto&& piece : pieces) {
if (mPieces[color][piece] & square) {
ss << charPieces[color][piece] << ' ';
return true;
}
}
}
return false;
};
ss << " +-----------------+\n";
// For each rank
for (auto r = Rank::Rank8; r >= Rank::Rank1; --r) {
// print the rank number
ss << static_cast<int>(r) + 1 << " | ";
// and each file
for (auto f = File::FileA; f <= File::FileH; ++f) {
if (!printIcon(makeSquare(f, r))) {
ss << ". ";
}
}
ss << "|\n";
}
ss << " +-----------------+\n";
ss << " A B C D E F G H" << std::endl;
return ss.str();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment