Skip to content

Commit

Permalink
Fix end-of-game removed stones being used when analyzing past moves a…
Browse files Browse the repository at this point in the history
…nd score estimations

fixes online-go/online-go.com#2735
  • Loading branch information
anoek committed Jul 5, 2024
1 parent f4f7731 commit 561f105
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/engine/GobanEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
decodePrettyCoordinates,
encodeMove,
encodeMoves,
makeMatrix,
positionId,
prettyCoordinates,
sortMoves,
Expand Down Expand Up @@ -751,7 +752,7 @@ export class GobanEngine extends BoardState {
//this.goban_callback?.setState(state.udata_state);
this.goban_callback?.setState?.();

const redrawn: { [s: string]: boolean } = {};
const redrawn = makeMatrix<boolean>(this.width, this.height, false);

for (let y = 0; y < this.height; ++y) {
for (let x = 0; x < this.width; ++x) {
Expand All @@ -760,10 +761,23 @@ export class GobanEngine extends BoardState {
(this.cur_move.x === x && this.cur_move.y === y)
) {
this.board[y][x] = state.board[y][x];
if (this.goban_callback) {
this.goban_callback.set(x, y, this.board[y][x]);
}
redrawn[x + "," + y] = true;
redrawn[y][x] = true;
}

if (
this.removal[y][x] !== state.removal[y][x] ||
(this.cur_move.x === x && this.cur_move.y === y)
) {
this.removal[y][x] = state.removal[y][x];
redrawn[y][x] = true;
}
}
}

if (this.goban_callback) {
for (let y = 0; y < this.height; ++y) {
for (let x = 0; x < this.width; ++x) {
this.goban_callback.set(x, y, this.board[y][x]);
}
}
}
Expand Down Expand Up @@ -914,9 +928,7 @@ export class GobanEngine extends BoardState {
return;
}
this.move_before_jump = this.cur_move;
if (node.state) {
this.setState(node.state);
}
this.setState(node.state);
this.cur_move = node;
if (node.player_update) {
//console.log("Engine jumpTo doing player_update...");
Expand Down Expand Up @@ -2531,4 +2543,14 @@ export class GobanEngine extends BoardState {
}
return ret;
}

public override setRemoved(
x: number,
y: number,
removed: boolean,
emit_stone_removal_updated: boolean = true,
): void {
this.cur_move.state.setRemoved(x, y, removed, false);
super.setRemoved(x, y, removed, emit_stone_removal_updated);
}
}
27 changes: 27 additions & 0 deletions test/unit_tests/GoEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,3 +726,30 @@ describe("groups", () => {
expect(engine.getStoneRemovalString()).toBe("aabb");
});
});

describe("state", () => {
const engine = new GobanEngine({});

engine.place(0, 0);
const move1 = engine.cur_move;
engine.place(1, 1);
const move2 = engine.cur_move;

expect(engine.board[0][0]).toBe(1);
expect(engine.board[1][1]).toBe(2);
expect(engine.removal[1][1]).toBe(false);
expect(engine.cur_move.state.removal[1][1]).toBe(false);

engine.setRemoved(1, 1, true);
expect(engine.removal[1][1]).toBe(true);
expect(engine.cur_move.state.removal[1][1]).toBe(true);
expect(engine.getStoneRemovalString()).toBe("bb");

engine.jumpTo(move1);
expect(engine.removal[1][1]).toBe(false);
expect(engine.getStoneRemovalString()).toBe("");

engine.jumpTo(move2);
expect(engine.removal[1][1]).toBe(true);
expect(engine.getStoneRemovalString()).toBe("bb");
});

0 comments on commit 561f105

Please sign in to comment.