Expose position.fen's showPromoted and countStarted in ffish.js
authorAda Joule <ada.fulmina@gmail.com>
Sun, 3 Oct 2021 05:11:58 +0000 (12:11 +0700)
committerFabian Fichter <ianfab@users.noreply.github.com>
Thu, 7 Oct 2021 06:42:07 +0000 (08:42 +0200)
src/ffishjs.cpp
src/position.h
src/pyffish.cpp
tests/js/test.js

index 7668aab..b6defdd 100644 (file)
@@ -175,6 +175,10 @@ public:
     return this->pos.fen();
   }
 
+  std::string fen(bool showPromoted, int countStarted) const {
+    return this->pos.fen(false, showPromoted, countStarted);
+  }
+
   void set_fen(std::string fen) {
     resetStates();
     moveStack.clear();
@@ -615,7 +619,8 @@ EMSCRIPTEN_BINDINGS(ffish_js) {
     .function("pop", &Board::pop)
     .function("reset", &Board::reset)
     .function("is960", &Board::is_960)
-    .function("fen", &Board::fen)
+    .function("fen", select_overload<std::string()const>(&Board::fen))
+    .function("fen", select_overload<std::string(bool, int)const>(&Board::fen))
     .function("setFen", &Board::set_fen)
     .function("sanMove", select_overload<std::string(std::string)>(&Board::san_move))
     .function("sanMove", select_overload<std::string(std::string, Notation)>(&Board::san_move))
index b930fa5..a8a523d 100644 (file)
@@ -1176,7 +1176,7 @@ inline int Position::game_ply() const {
 }
 
 inline int Position::counting_ply(int countStarted) const {
-  return countStarted == 0 || (count<ALL_PIECES>(WHITE) <= 1 || count<ALL_PIECES>(BLACK) <= 1) ? st->countingPly : std::min(st->countingPly, std::max(1 + gamePly - countStarted, 0));
+  return countStarted == 0 || (count<ALL_PIECES>(WHITE) <= 1 || count<ALL_PIECES>(BLACK) <= 1) ? st->countingPly : countStarted < 0 ? 0 : std::min(st->countingPly, std::max(1 + gamePly - countStarted, 0));
 }
 
 inline int Position::rule50_count() const {
index ab2015d..785a778 100644 (file)
@@ -230,7 +230,6 @@ extern "C" PyObject* pyffish_getFEN(PyObject* self, PyObject *args) {
     if (!PyArg_ParseTuple(args, "ssO!|pppi", &variant, &fen, &PyList_Type, &moveList, &chess960, &sfen, &showPromoted, &countStarted)) {
         return NULL;
     }
-    countStarted = std::min<unsigned int>(countStarted, INT_MAX); // pseudo-unsigned
 
     StateListPtr states(new std::deque<StateInfo>(1));
     buildPosition(pos, states, variant, fen, moveList, chess960);
@@ -304,7 +303,6 @@ extern "C" PyObject* pyffish_isOptionalGameEnd(PyObject* self, PyObject *args) {
     if (!PyArg_ParseTuple(args, "ssO!|pi", &variant, &fen, &PyList_Type, &moveList, &chess960, &countStarted)) {
         return NULL;
     }
-    countStarted = std::min<unsigned int>(countStarted, INT_MAX); // pseudo-unsigned
 
     StateListPtr states(new std::deque<StateInfo>(1));
     buildPosition(pos, states, variant, fen, moveList, chess960);
index 36dadb9..6c60626 100644 (file)
@@ -200,6 +200,15 @@ describe('board.fen()', function () {
   });
 });
 
+describe('board.fen(showPromoted, countStarted)', function () {
+  it("it returns the current position in fen format. showPromoted makes promoted pieces always followed by the symbol ~ regardless of variant. countStarted overwrites the start of makruk's board honor counting.", () => {
+    let board = new ffish.Board("makruk", "8/6ks/3M~2r1/2K1M3/8/3R4/8/8 w - 128 18 50");
+    chai.expect(board.fen(true, 0)).to.equal("8/6ks/3M~2r1/2K1M3/8/3R4/8/8 w - 128 18 50");
+    chai.expect(board.fen(true, -1)).to.equal("8/6ks/3M~2r1/2K1M3/8/3R4/8/8 w - 128 0 50");
+    chai.expect(board.fen(true, 89)).to.equal("8/6ks/3M~2r1/2K1M3/8/3R4/8/8 w - 128 10 50");
+  });
+});
+
 describe('board.setFen(fen)', function () {
   it("it sets a custom position via fen", () => {
     let board = new ffish.Board();