IPVGO: Add optional board state argument to the go analysis functions (#1716)

This commit is contained in:
Michael Ficocelli
2024-10-27 20:31:06 -04:00
committed by GitHub
parent ecc2d92edb
commit 6df3dcdc82
11 changed files with 231 additions and 39 deletions
+31 -4
View File
@@ -1,15 +1,16 @@
import type { Board, BoardState, Move, Neighbor, PointState } from "../Types";
import { Board, BoardState, Move, Neighbor, PointState, SimpleBoard } from "../Types";
import { GoOpponent, GoColor, GoValidity } from "@enums";
import { GoColor, GoOpponent, GoValidity } from "@enums";
import { bitverseBoardShape } from "../Constants";
import { getExpansionMoveArray } from "../boardAnalysis/goAI";
import {
boardFromSimpleBoard,
boardStringFromBoard,
evaluateIfMoveIsValid,
findAllCapturedChains,
findLibertiesForChain,
getAllChains,
boardFromSimpleBoard,
boardStringFromBoard,
updatedBoardFromSimpleBoard,
} from "../boardAnalysis/boardAnalysis";
import { endGoGame } from "../boardAnalysis/scoring";
import { addObstacles, resetCoordinates, rotate90Degrees } from "./offlineNodes";
@@ -59,6 +60,32 @@ export function getNewBoardState(
return newBoardState;
}
/**
* Generates a new BoardState object from a given SimpleBoard string array, and an optional prior move board state
*/
export function getNewBoardStateFromSimpleBoard(
simpleBoard: SimpleBoard,
priorSimpleBoard?: SimpleBoard,
ai: GoOpponent = GoOpponent.Netburners,
): BoardState {
const newState = getNewBoardState(simpleBoard.length, ai, false, updatedBoardFromSimpleBoard(simpleBoard));
if (priorSimpleBoard) {
newState.previousBoards.push(priorSimpleBoard.join(""));
// Identify the previous player based on the difference in pieces
const priorWhitePieces = priorSimpleBoard.join("").match(/O/g)?.length ?? 0;
const priorBlackPieces = priorSimpleBoard.join("").match(/X/g)?.length ?? 0;
const currentWhitePieces = simpleBoard.join("").match(/O/g)?.length ?? 0;
const currentBlackPieces = simpleBoard.join("").match(/X/g)?.length ?? 0;
if (priorWhitePieces - priorBlackPieces > currentWhitePieces - currentBlackPieces) {
newState.previousPlayer = GoColor.black;
}
}
updateCaptures(newState.board, newState.previousPlayer ?? GoColor.white);
return newState;
}
/**
* Determines how many starting pieces the opponent has on the board
*/