示例#1
0
    //function used to perform simulation of the MCTS cycle
    private int Simulation(MonteCarloNode _nodeToSimulate)
    {
        //sets the turn number to the enemy value, so that the simulation begins within the right turn
        int _turn = MonteCarloBoard._enemyVal;

        //creates a temporary node, setting the values to that of the node to simulate
        MonteCarloNode _tempNode = new MonteCarloNode(_nodeToSimulate);

        //creates a tempporary state, setting the values to that of the temporary node's state
        MonteCarloState _tempState = _tempNode.GetState();

        //checks for the current board status of the temporary node
        int _boardStatus = _tempState.GetBoard().CheckStatus();

        //checks if the current board state produces a win for the player
        if (_boardStatus == MonteCarloBoard._playerVal)
        {
            //sets the score of the temp node's parent's state to the minimum integer value
            _tempNode.GetParent().GetState().SetScore(int.MinValue);

            //returns the board status value
            return(_boardStatus);
        }

        //loops while the board state is in progress
        while (_boardStatus == MonteCarloBoard._inProgress)
        {
            //changes the turn between the player and the enemy
            _turn = 3 - _turn;

            //performs a random play from the current tempstate
            _tempState.randomPlay(_turn);

            //sets the board status to the current board state of the temp state board
            _boardStatus = _tempState.GetBoard().CheckStatus();
        }

        //returns the board status value
        return(_boardStatus);
    }
    //function that returns all legal states available for current action
    public List <MonteCarloState> GetLegalStates()
    {
        //creates a list to store the legal states
        List <MonteCarloState> _legalStates = new List <MonteCarloState>();

        //creates a list to store all legal positions and stores it with all legal positions returned from a function
        List <MonteCarloPosition> _legalPositions = this._board.GetLegalPositions(MonteCarloBoard._enemyVal);

        //loops through each position in the legal positions list
        foreach (MonteCarloPosition _p in _legalPositions)
        {
            //creates a new state with the current board
            MonteCarloState _newState = new MonteCarloState(this._board);

            //performs the enemy action within the new state
            _newState.GetBoard().performEnemyMove(_p);

            //adds the new state to the list of legal states
            _legalStates.Add(_newState);
        }

        return(_legalStates);
    }
 public MonteCarloState(MonteCarloState _state)
 {
     this._board  = new MonteCarloBoard(_state.GetBoard());
     this._visits = _state.GetVisits();
     this._score  = _state.GetScore();
 }