IPVGO: Add history, and details to status, to go API (#1348)

This commit is contained in:
Michael Ficocelli 2024-06-05 21:24:48 -04:00 committed by GitHub
parent 30a6419b11
commit cf48d666f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 44 additions and 2 deletions

@ -612,6 +612,15 @@ export function boardStringFromBoard(board: Board): string {
* ``` * ```
*/ */
export function boardFromBoardString(boardString: string): Board { export function boardFromBoardString(boardString: string): Board {
const simpleBoardArray = simpleBoardFromBoardString(boardString);
return boardFromSimpleBoard(simpleBoardArray);
}
/**
* Slices a string representation of a board into an array of strings representing the rows on the board
*/
export function simpleBoardFromBoardString(boardString: string): SimpleBoard {
// Turn the SimpleBoard string into a string array, allowing access of each point via indexes e.g. [0][1] // Turn the SimpleBoard string into a string array, allowing access of each point via indexes e.g. [0][1]
const boardSize = Math.round(Math.sqrt(boardString.length)); const boardSize = Math.round(Math.sqrt(boardString.length));
const boardTiles = boardString.split(""); const boardTiles = boardString.split("");
@ -621,7 +630,7 @@ export function boardFromBoardString(boardString: string): Board {
.fill("") .fill("")
.map((_, index) => boardTiles.slice(index * boardSize, (index + 1) * boardSize).join("")); .map((_, index) => boardTiles.slice(index * boardSize, (index + 1) * boardSize).join(""));
return boardFromSimpleBoard(simpleBoardArray); return simpleBoardArray;
} }
/** Creates a board object from a simple board. The resulting board has no analytics (liberties/chains) */ /** Creates a board object from a simple board. The resulting board has no analytics (liberties/chains) */

@ -1,4 +1,4 @@
import { Play, SimpleOpponentStats } from "../Types"; import { Play, SimpleBoard, SimpleOpponentStats } from "../Types";
import { Player } from "@player"; import { Player } from "@player";
import { AugmentationName, GoColor, GoOpponent, GoPlayType, GoValidity } from "@enums"; import { AugmentationName, GoColor, GoOpponent, GoPlayType, GoValidity } from "@enums";
@ -10,6 +10,7 @@ import {
getControlledSpace, getControlledSpace,
getPreviousMove, getPreviousMove,
simpleBoardFromBoard, simpleBoardFromBoard,
simpleBoardFromBoardString,
} from "../boardAnalysis/boardAnalysis"; } from "../boardAnalysis/boardAnalysis";
import { endGoGame, getOpponentStats, getScore, resetWinstreak } from "../boardAnalysis/scoring"; import { endGoGame, getOpponentStats, getScore, resetWinstreak } from "../boardAnalysis/scoring";
import { WHRNG } from "../../Casino/RNG"; import { WHRNG } from "../../Casino/RNG";
@ -223,6 +224,8 @@ export function getControlledEmptyNodes() {
* Gets the status of the current game. * Gets the status of the current game.
* Shows the current player, current score, and the previous move coordinates. * Shows the current player, current score, and the previous move coordinates.
* Previous move coordinates will be [-1, -1] for a pass, or if there are no prior moves. * Previous move coordinates will be [-1, -1] for a pass, or if there are no prior moves.
*
* Also provides the white player's komi (bonus starting score), and the amount of bonus cycles from offline time remaining
*/ */
export function getGameState() { export function getGameState() {
const currentPlayer = getCurrentPlayer(); const currentPlayer = getCurrentPlayer();
@ -234,9 +237,15 @@ export function getGameState() {
whiteScore: score[GoColor.white].sum, whiteScore: score[GoColor.white].sum,
blackScore: score[GoColor.black].sum, blackScore: score[GoColor.black].sum,
previousMove, previousMove,
komi: score[GoColor.white].komi,
bonusCycles: Go.storedCycles,
}; };
} }
export function getMoveHistory(): SimpleBoard[] {
return Go.currentGame.previousBoards.map((boardString) => simpleBoardFromBoardString(boardString));
}
/** /**
* Returns 'None' if the game is over, otherwise returns the color of the current player's turn * Returns 'None' if the game is over, otherwise returns the color of the current player's turn
*/ */

@ -250,6 +250,7 @@ const go = {
makeMove: 4, makeMove: 4,
passTurn: 0, passTurn: 0,
getBoardState: 4, getBoardState: 4,
getMoveHistory: 0,
getCurrentPlayer: 0, getCurrentPlayer: 0,
getGameState: 0, getGameState: 0,
getOpponent: 0, getOpponent: 0,

@ -17,6 +17,7 @@ import {
getCurrentPlayer, getCurrentPlayer,
getGameState, getGameState,
getLiberties, getLiberties,
getMoveHistory,
getOpponentNextMove, getOpponentNextMove,
getStats, getStats,
getValidMoves, getValidMoves,
@ -58,6 +59,9 @@ export function NetscriptGo(): InternalAPI<NSGo> {
getBoardState: () => () => { getBoardState: () => () => {
return simpleBoardFromBoard(Go.currentGame.board); return simpleBoardFromBoard(Go.currentGame.board);
}, },
getMoveHistory: () => () => {
return getMoveHistory();
},
getCurrentPlayer: () => () => { getCurrentPlayer: () => () => {
return getCurrentPlayer(); return getCurrentPlayer();
}, },

@ -4049,6 +4049,21 @@ export interface Go {
*/ */
getBoardState(): string[]; getBoardState(): string[];
/**
* Returns all the prior moves in the current game, as an array of simple board states.
*
* For example, a single 5x5 prior move board might look like this:
*
* [<br/>
* "XX.O.",<br/>
* "X..OO",<br/>
* ".XO..",<br/>
* "XXO.#",<br/>
* ".XO.#",<br/>
* ]
*/
getMoveHistory(): string[][];
/** /**
* Returns the color of the current player, or 'None' if the game is over. * Returns the color of the current player, or 'None' if the game is over.
* @returns "White" | "Black" | "None" * @returns "White" | "Black" | "None"
@ -4065,6 +4080,8 @@ export interface Go {
whiteScore: number; whiteScore: number;
blackScore: number; blackScore: number;
previousMove: [number, number] | null; previousMove: [number, number] | null;
komi: number;
bonusCycles: number;
}; };
/** /**

@ -104,6 +104,8 @@ describe("Netscript Go API unit tests", () => {
whiteScore: 6.5, whiteScore: 6.5,
blackScore: 6, blackScore: 6,
previousMove: [0, 2], previousMove: [0, 2],
bonusCycles: 0,
komi: 5.5,
}); });
}); });
}); });