Skip to content

Commit

Permalink
Split off fog area bitboard creation and use it as additional fen() a…
Browse files Browse the repository at this point in the history
…rgument
  • Loading branch information
gbtami committed Nov 21, 2024
1 parent 25a72ba commit 45dc695
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 42 deletions.
41 changes: 3 additions & 38 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ Position& Position::set(const string& code, Color c, StateInfo* si) {
/// Position::fen() returns a FEN representation of the position. In case of
/// Chess960 the Shredder-FEN notation is used. This is mainly a debugging function.

string Position::fen(bool sfen, bool showPromoted, int countStarted, std::string holdings) const {
string Position::fen(bool sfen, bool showPromoted, int countStarted, std::string holdings, Bitboard fogArea) const {

int emptyCnt;
std::ostringstream ss;
Expand All @@ -690,15 +690,15 @@ string Position::fen(bool sfen, bool showPromoted, int countStarted, std::string
{
for (File f = FILE_A; f <= max_file(); ++f)
{
for (emptyCnt = 0; f <= max_file() && !(pieces() & make_square(f, r)); ++f)
for (emptyCnt = 0; f <= max_file() && !(pieces() & make_square(f, r)) && !(fogArea & make_square(f, r)); ++f)
++emptyCnt;

if (emptyCnt)
ss << emptyCnt;

if (f <= max_file())
{
if (empty(make_square(f, r)))
if (empty(make_square(f, r)) || fogArea & make_square(f, r))
// Wall square
ss << "*";
else if (unpromoted_piece_on(make_square(f, r)))
Expand Down Expand Up @@ -825,41 +825,6 @@ string Position::fen(bool sfen, bool showPromoted, int countStarted, std::string
}


/// Position::fog_fen() returns a Fog of War (dark chess) FEN representation
/// of the position. Squares where current player can't move to are filled with
/// NO_PIECE (wall squares)

string Position::fog_fen(bool sfen, bool showPromoted, int countStarted, std::string holdings) {
Color us = sideToMove;
Bitboard fog;

// Our own pieces are visible
Bitboard visible = pieces(us);

// Squares where we can move to are visible as well
for (const auto& m : MoveList<LEGAL>(*this))
{
Square to = to_sq(m);
visible |= to;
}

// Everything else is invisible
fog = ~visible & board_bb();

// Fill in invisible squares with walls
while (fog)
{
Square sq = pop_lsb(fog);
if (piece_on(sq)) remove_piece(sq);

st->wallSquares |= sq;
byTypeBB[ALL_PIECES] |= sq;
}

return fen(sfen, showPromoted, countStarted, holdings);
}


/// Position::slider_blockers() returns a bitboard of all the pieces (both colors)
/// that are blocking attacks on the square 's' from 'sliders'. A piece blocks a
/// slider if removing that piece from the board would result in a position where
Expand Down
18 changes: 16 additions & 2 deletions src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ class Position {
// FEN string input/output
Position& set(const Variant* v, const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th, bool sfen = false);
Position& set(const std::string& code, Color c, StateInfo* si);
std::string fen(bool sfen = false, bool showPromoted = false, int countStarted = 0, std::string holdings = "-") const;
std::string fog_fen(bool sfen = false, bool showPromoted = false, int countStarted = 0, std::string holdings = "-");
std::string fen(bool sfen = false, bool showPromoted = false, int countStarted = 0, std::string holdings = "-", Bitboard fogArea = 0) const;

// Variant rule properties
const Variant* variant() const;
Expand Down Expand Up @@ -329,6 +328,7 @@ class Position {
Score psq_score() const;
Value non_pawn_material(Color c) const;
Value non_pawn_material() const;
Bitboard fog_area() const;

// Position consistency check, for debugging
bool pos_is_ok() const;
Expand Down Expand Up @@ -1408,6 +1408,20 @@ inline Piece Position::captured_piece() const {
return st->capturedPiece;
}

inline Bitboard Position::fog_area() const {
Bitboard b = board_bb();
// Our own pieces are visible
Bitboard visible = pieces(sideToMove);
// Squares where we can move to are visible as well
for (const auto& m : MoveList<LEGAL>(*this))
{
Square to = to_sq(m);
visible |= to;
}
// Everything else is invisible
return ~visible & b;
}

inline const std::string Position::piece_to_partner() const {
if (!st->capturedPiece) return std::string();
Color color = color_of(st->capturedPiece);
Expand Down
4 changes: 2 additions & 2 deletions src/pyffish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,15 @@ extern "C" PyObject* pyffish_getFogFEN(PyObject* self, PyObject *args) {
Position pos;
const char *fen, *variant;

int chess960 = false;
int chess960 = false, sfen = false, showPromoted = false, countStarted = 0;
if (!PyArg_ParseTuple(args, "ss|p", &fen, &variant, &chess960)) {
return NULL;
}
StateListPtr states(new std::deque<StateInfo>(1));
buildPosition(pos, states, variant, fen, moveList, chess960);

Py_XDECREF(moveList);
return Py_BuildValue("s", pos.fog_fen().c_str());
return Py_BuildValue("s", pos.fen(sfen, showPromoted, countStarted, "-", pos.fog_area()).c_str());
}

static PyMethodDef PyFFishMethods[] = {
Expand Down

0 comments on commit 45dc695

Please sign in to comment.