IPVGO: Do not update captures on passed analysis boards (#2415)

This commit is contained in:
Michael Ficocelli
2025-12-21 16:44:24 -05:00
committed by GitHub
parent 49e231fd41
commit 4218b01dfb
8 changed files with 127 additions and 20 deletions

View File

@@ -90,8 +90,7 @@ export function getNewBoardStateFromSimpleBoard(
newState.previousPlayer = GoColor.black;
}
}
updateCaptures(newState.board, newState.previousPlayer ?? GoColor.white);
updateChains(newState.board);
return newState;
}

View File

@@ -242,9 +242,11 @@ export function getChains(_board?: Board) {
export function getLiberties(_board?: Board) {
const board = _board || Go.currentGame.board;
return board.map((column) =>
column.reduce((libertyArray: number[], point) => {
libertyArray.push(point?.liberties?.length || -1);
return libertyArray;
column.map((point) => {
if (!point?.liberties || point.color === GoColor.empty) {
return -1;
}
return point.liberties.length;
}, []),
);
}
@@ -278,12 +280,13 @@ export function getControlledEmptyNodes(_board?: Board) {
* Resets the active game to be a new board with "No AI" as the opponent. Applies the specified board state and komi to the new game.
* Used for testing scenarios.
*/
export function setTestingBoardState(ctx: NetscriptContext, board: Board, komi?: number) {
resetBoardState(ctx, GoOpponent.none, board.length);
Go.currentGame.board = board;
export function setTestingBoardState(ctx: NetscriptContext, state: BoardState, komi?: number) {
resetBoardState(ctx, GoOpponent.none, state.board.length);
Go.currentGame = state;
if (komi != undefined) {
Go.currentGame.komiOverride = komi;
}
updateCaptures(Go.currentGame.board, Go.currentGame.previousPlayer ?? GoColor.white, true);
GoEvents.emit();
}
@@ -444,7 +447,7 @@ export function validateBoardState(
return getNewBoardStateFromSimpleBoard(
simpleBoard,
priorSimpleBoard,
undefined,
GoOpponent.none,
playAsWhite ? GoColor.black : GoColor.white,
);
} catch (e) {

View File

@@ -114,13 +114,14 @@ export function NetscriptGo(): InternalAPI<NSGo> {
const resetAll = helpers.boolean(ctx, "resetAll", _resetAll ?? false);
resetStats(resetAll);
},
setTestingBoardState: (ctx) => (_boardState, _komi) => {
const State = validateBoardState(ctx, _boardState);
setTestingBoardState: (ctx) => (_boardState, _komi, _nextPlayerIsWhite) => {
const nextPlayerIsWhite = helpers.boolean(ctx, "nextPlayerIsWhite", _nextPlayerIsWhite ?? false);
const State = validateBoardState(ctx, _boardState, null, nextPlayerIsWhite);
if (!State) {
throw errorMessage(ctx, "Invalid board state passed to setTestingBoardState()");
}
const komi: number | undefined = _komi !== undefined ? helpers.number(ctx, "komi", _komi) : undefined;
return setTestingBoardState(ctx, State.board, komi);
const komi = helpers.number(ctx, "komi", _komi ?? 5.5);
return setTestingBoardState(ctx, State, komi);
},
highlightPoint: (ctx) => (_x, _y, _color, _text) => {
const x = helpers.number(ctx, "x", _x);

View File

@@ -4821,8 +4821,9 @@ export interface GoAnalysis {
*
* @param boardState - The initial board state to use for the new game, in the format used by getBoardState().
* @param komi - Optional komi value to set for the game. Defaults to 5.5.
* @param nextPlayerIsWhite - Optional. Whether or not the next player to play is the white player. Defaults to false.
*/
setTestingBoardState(boardState: string[], komi?: number): void;
setTestingBoardState(boardState: string[], komi?: number, nextPlayerIsWhite?: boolean): void;
/**
* Adds a colored circle indicator to the specified point. These indicators are removed once a move is played.

View File

@@ -521,5 +521,15 @@ export const breakingChanges300: VersionBreakingChange = {
'gets the generated contract with a new optional parameter. Your code was migrated to specify "home" as the host.',
showWarning: false,
},
{
brokenAPIs: [
{ name: "ns.go.analysis.getValidMoves" },
{ name: "ns.go.analysis.getChains" },
{ name: "ns.go.analysis.getLiberties" },
{ name: "ns.go.analysis.getControlledEmptyNodes" },
],
info: "ns.go.analysis methods no longer apply captures to custom board states passed to them, and instead evaluate the given board exactly as-is.",
showWarning: false,
},
],
};