public double Simulate(PieceType currentPieceType) { BoardState currentState = State; Move[] moves = board.GetPossibleMoves(State); while (moves.Length != 0) { Move randomMove = moves[random.Next(moves.Length)]; currentState = currentState.PlayMove(randomMove); moves = board.GetPossibleMoves(currentState); } return(new MCTSNode(board, currentState).GetTerminalScore(currentPieceType)); }
private void run() { while (gameState != GameState.End) { if (gameState == GameState.Start) { HandleStart(); } else if (gameState == GameState.RedToMove) { // Ask the player for a move Move move = redPlayer.GetMove(board, boardState); boardState = boardState.PlayMove(move); // Fire the event player moved if (OnPlayerMove != null) { PlayerMoveEventArgs playerMoveEventArgs = new PlayerMoveEventArgs(board, boardState, redPlayer, move); OnPlayerMove(this, playerMoveEventArgs); } gameState = GameState.RedMoved; } else if (gameState == GameState.RedMoved) { // Check if there are any possible moves Move[] moves = board.GetPossibleMoves(boardState); if (moves.Length == 0) { if (OnGameEnd != null) { GameBoardEventArgs gameBoardEventArgs = new GameBoardEventArgs(board, boardState); OnGameEnd(this, gameBoardEventArgs); } gameState = GameState.End; } else { if (OnPlayerChanged != null) { PlayerEventArgs playerEventArgs = new PlayerEventArgs(board, boardState, blackPlayer); OnPlayerChanged(this, playerEventArgs); } gameState = GameState.BlackToMove; } } else if (gameState == GameState.BlackToMove) { // Ask the player for a move Move move = blackPlayer.GetMove(board, boardState); boardState = boardState.PlayMove(move); // Fire the event player moved if (OnPlayerMove != null) { PlayerMoveEventArgs playerMoveEventArgs = new PlayerMoveEventArgs(board, boardState, blackPlayer, move); OnPlayerMove(this, playerMoveEventArgs); } gameState = GameState.BlackMoved; } else if (gameState == GameState.BlackMoved) { // Check if there are any possible moves Move[] moves = board.GetPossibleMoves(boardState); if (moves.Length == 0) { if (OnGameEnd != null) { GameBoardEventArgs gameBoardEventArgs = new GameBoardEventArgs(board, boardState); OnGameEnd(this, gameBoardEventArgs); } gameState = GameState.End; } else { if (OnPlayerChanged != null) { PlayerEventArgs playerEventArgs = new PlayerEventArgs(board, boardState, redPlayer); OnPlayerChanged(this, playerEventArgs); } gameState = GameState.RedToMove; } } } }