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="points">选择范围(点集)</param> /// <returns>代表被选中点的对象。如果点集为空,将返回null</returns> static GobangPoint randomlySelectPoint(ISet <GobangPoint> points) { if (points.Count == 0) { return(null); } List <GobangPoint> pointList = new List <GobangPoint>(points); Random random = new Random(); int index = random.Next(pointList.Count); GobangPoint selected = pointList[index]; return(selected); }
// ======================================== 重写的函数 ======================================== public override bool Equals(object obj) { if (obj == null) { return(false); } if (!(obj is GobangPoint)) { return(false); } GobangPoint other = (GobangPoint)obj; return(this.coord[0] == other.coord[0] && this.coord[1] == other.coord[1]); }
public object Clone() { GobangPoint[,] stateClone = new GobangPoint[boardSideLength, boardSideLength]; for (int x = 0; x < boardSideLength; x++) { for (int y = 0; y < boardSideLength; y++) { stateClone[x, y] = (GobangPoint)boardState[x, y].Clone(); } } GobangMove lastMoveClone = (GobangMove)lastMove.Clone(); return(new GobangGameState(stateClone, lastMoveClone)); }
/// <summary> /// 创建全新的棋盘 /// </summary> /// <param name="boardSideLength">棋盘边长</param> public GobangGameState(int boardSideLength) { this.boardSideLength = boardSideLength; boardState = new GobangPoint[boardSideLength, boardSideLength]; for (int x = 0; x < boardSideLength; x++) { for (int y = 0; y < boardSideLength; y++) { int[] coord = { x, y }; boardState[x, y] = new GobangPoint(coord); } } lastMove = null; unplacedPoints = findUnplacedPoints(boardState); }
public GobangMove(GobangPoint point) { this.point = point; }