GO: Alternate fix for race conditions (#1260)

This commit is contained in:
Snarling
2024-05-11 19:58:59 -04:00
committed by GitHub
parent 1b8205e9d5
commit e23db93c8b
8 changed files with 119 additions and 146 deletions
+25
View File
@@ -1,6 +1,7 @@
import type { Board, BoardState, Neighbor, PointState, SimpleBoard } from "../Types";
import { GoValidity, GoOpponent, GoColor } from "@enums";
import { Go } from "../Go";
import {
findAdjacentPointsInChain,
findNeighbors,
@@ -638,3 +639,27 @@ export function getColorOnSimpleBoard(simpleBoard: SimpleBoard, x: number, y: nu
if (char === ".") return GoColor.empty;
return null;
}
/** Find a move made by the previous player, if present. */
export function getPreviousMove(): [number, number] | null {
const priorBoard = Go.currentGame?.previousBoards[0];
if (Go.currentGame.passCount || !priorBoard) {
return null;
}
for (const rowIndexString in Go.currentGame.board) {
const row = Go.currentGame.board[+rowIndexString] ?? [];
for (const pointIndexString in row) {
const point = row[+pointIndexString];
const priorColor = point && priorBoard && getColorOnSimpleBoard(priorBoard, point.x, point.y);
const currentColor = point?.color;
const isPreviousPlayer = currentColor === Go.currentGame.previousPlayer;
const isChanged = priorColor !== currentColor;
if (priorColor && currentColor && isPreviousPlayer && isChanged) {
return [+rowIndexString, +pointIndexString];
}
}
}
return null;
}