public bool simulate() { GameResult gameResult = judgeLastMove(); if (gameResult == GameResult.ComputerWon) { return(true); } // 建立模拟所需数据结构 GobangGameState localGameState = (GobangGameState)this.Clone(); /*GobangPoint[,] localBoardState = gameState.boardState; // 棋盘 * HashSet<GobangPoint> localUnplacedPoints = findUnplacedPoints(localBoardState);*/ PieceType curPieceType = PieceType.Computer; // 模拟 do { curPieceType = (curPieceType == PieceType.Computer ? PieceType.Player : PieceType.Computer); GobangPoint point = randomlySelectPoint(localGameState.unplacedPoints); if (point == null) { return(false); } point.pieceType = curPieceType; GobangMove move = new GobangMove(point); localGameState.placePiece(move); gameResult = localGameState.judgeLastMove(); // TODO: check unplacedPoints } while (gameResult == GameResult.NoOutcome); return(gameResult == GameResult.ComputerWon); }
public MCTSGameState expand(List <MCTSGameState> existingChildren) { // 随机选择落子位置 HashSet <GobangPoint> unexpandedPoints = findUnexpandedPoints(existingChildren); GobangPoint point = randomlySelectPoint(unexpandedPoints); GobangGameState nextState = (GobangGameState)this.Clone(); nextState.placePiece(new GobangMove(new GobangPoint(point.coord, PieceType.Computer, point.gameObj))); return(nextState); }
/// <summary> /// 依据传入的子节点,找出可展开的位置 /// </summary> /// <param name="existingChildren">已经存在的子节点</param> /// <returns>可展开位置的集合</returns> private HashSet <GobangPoint> findUnexpandedPoints(List <MCTSGameState> existingChildren) { HashSet <GobangPoint> unplacedPointsCopy = new HashSet <GobangPoint>(unplacedPoints); foreach (var mctsChild in existingChildren) { GobangGameState child = (GobangGameState)mctsChild; unplacedPointsCopy.Remove(child.lastMove.point); } return(unplacedPointsCopy); }