Skip to content
Draft
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
26 changes: 21 additions & 5 deletions src/ffishjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,20 @@ class Board {

std::string variation_san(std::string uciMoves, Notation notation, bool moveNumbers) {
std::stringstream ss(uciMoves);
StateListPtr tempStates;
std::vector<Move> moves;
std::string variationSan = "";
std::string uciMove;
bool first = true;
bool invalidMoveEncountered = false;
size_t statesAdded = 0;

while (std::getline(ss, uciMove, ' ')) {
const Move move = UCI::to_move(this->pos, uciMove);
if (is_move_none<true>(move, uciMove, pos))
return "";
moves.emplace_back(UCI::to_move(this->pos, uciMove));
if (is_move_none<true>(move, uciMove, pos)) {
invalidMoveEncountered = true;
break;
}
moves.emplace_back(move);
if (first) {
first = false;
if (moveNumbers) {
Expand All @@ -243,14 +246,22 @@ class Board {
variationSan += SAN::move_to_san(this->pos, moves.back(), Notation(notation));
}
states->emplace_back();
++statesAdded;
pos.do_move(moves.back(), states->back());
}

// recover initial state
for(auto rIt = std::rbegin(moves); rIt != std::rend(moves); ++rIt) {
for (auto rIt = std::rbegin(moves); rIt != std::rend(moves); ++rIt)
pos.undo_move(*rIt);

while (statesAdded > 0) {
states->pop_back();
--statesAdded;
}

if (invalidMoveEncountered)
return "";

return variationSan;
}

Expand All @@ -271,6 +282,10 @@ class Board {
return pos.game_ply();
}

int state_stack_size() const {
return static_cast<int>(states->size());
}

bool has_insufficient_material(bool turn) const {
return Stockfish::has_insufficient_material(turn ? WHITE : BLACK, pos);
}
Expand Down Expand Up @@ -712,6 +727,7 @@ EMSCRIPTEN_BINDINGS(ffish_js) {
.function("fullmoveNumber", &Board::fullmove_number)
.function("halfmoveClock", &Board::halfmove_clock)
.function("gamePly", &Board::game_ply)
.function("stateStackSize", &Board::state_stack_size)
.function("hasInsufficientMaterial", &Board::has_insufficient_material)
.function("isInsufficientMaterial", &Board::is_insufficient_material)
.function("isGameOver", select_overload<bool() const>(&Board::is_game_over))
Expand Down
1 change: 1 addition & 0 deletions tests/js/ffish.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface Board {
fullmoveNumber(): number;
halfmoveClock(): number;
gamePly(): number;
stateStackSize(): number;
hasInsufficientMaterial(turn: boolean): boolean;
isInsufficientMaterial(): boolean;
isGameOver(claimDraw?: boolean): boolean;
Expand Down
21 changes: 21 additions & 0 deletions tests/js/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,27 @@ describe('board.variationSan(uciMoves, notation, moveNumbers)', function () {
});
});

describe('board.stateStackSize()', function () {
it("it remains aligned with the number of real moves after repeated variationSan calls", () => {
let board = new ffish.Board();
const actualMoves = ["e2e4", "e7e5", "g1f3"];
actualMoves.forEach(move => {
chai.expect(board.push(move)).to.equal(true);
});

const expectedStates = actualMoves.length + 1;
chai.expect(board.stateStackSize()).to.equal(expectedStates);

const variationLine = "g8f6 d2d3 Bf8c5";
for (let i = 0; i < 5; ++i) {
chai.expect(board.variationSan(variationLine)).to.not.equal("");
chai.expect(board.stateStackSize()).to.equal(expectedStates);
}

board.delete();
});
});

describe('board.turn()', function () {
it("it returns the side to move", () => {
let board = new ffish.Board();
Expand Down
Loading