//function used to perform the move of the enemy
    public void performEnemyMove(MonteCarloPosition _p)
    {
        //increments the total moves
        this._totalMoves++;

        //sets old enemy position to current enemy position
        MonteCarloPosition _oldPos = FindPlayer(_enemyVal);

        //finds the adjacent tiles for the enemy to check for collision with the player object, returning true if the player object is adjacent to the enemy object
        bool _opponentCheck = CheckOpponentAdjacent();

        //checks if the opponent check value is true
        if (_opponentCheck)
        {
            //reduces the player health by 1
            this._playerHealth--;
        }
        else
        {
            //sets board value of the old enemy position to 0, whilst setting the new enemy position to the player value
            _boardValues [_oldPos.GetX(), _oldPos.GetZ()] = 0;
            _boardValues [_p.GetX(), _p.GetZ()]           = _enemyVal;

            //finds the adjacent tiles for the enemy to check for collision with the player object, returning true if the player object is adjacent to the enemy object
            bool _opponentCheck2 = CheckOpponentAdjacent();

            //checks if the opponent check value is true
            if (_opponentCheck2)
            {
                //reduces the player health by 1
                this._playerHealth--;
            }
        }
    }
    //function that gets all legal positions for a selected player
    public List <MonteCarloPosition> GetLegalPositions(int _playerNo)
    {
        //creates list to store legal positions
        List <MonteCarloPosition> _legalPositions = new List <MonteCarloPosition> ();

        MonteCarloPosition _pos;

        //finds the object on the board dependant on the player number referenced
        _pos = FindPlayer(_playerNo);

        //sets the board x and z to the current object x and z position
        int _boardX = _pos.GetX();
        int _boardZ = _pos.GetZ();

        int _checkX;
        int _checkZ;

        //loops used to check all adjacent tiles, except diagonal tiles
        for (int x = -1; x <= 1; x++)
        {
            for (int z = -1; z <= 1; z++)
            {
                if (x == 0 || z == 0)
                {
                    if (x == 0 && z == 0)
                    {
                    }
                    else
                    {
                        //sets the check x and z to the current board x and z position plus the current x and z iteration values
                        _checkX = _boardX + x;
                        _checkZ = _boardZ + z;

                        //checks if the check x and check z values are within the grid
                        if (_checkX >= 0 && _checkX < GridSystem._gridSizeX && _checkZ >= 0 && _checkZ < GridSystem._gridSizeZ)
                        {
                            //checks if the board values are set to 0 (being empty)
                            if (_boardValues [_checkX, _checkZ] == 0)
                            {
                                MonteCarloPosition _position = new MonteCarloPosition();

                                //sets the position values to the current check x and check z values
                                _position.SetX(_checkX);
                                _position.SetZ(_checkZ);

                                //adds the position to the list of legal positions
                                _legalPositions.Add(_position);
                            }
                        }
                    }
                }
            }
        }

        //returns the list of legal positions
        return(_legalPositions);
    }
Ejemplo n.º 3
0
    //function used to perform MCTS
    public MonteCarloPosition StartMCTS(float _curPlayerHealth, float _curEnemyHealth, Node _objNode)
    {
        //updates the board to fit the current grid
        MonteCarloBoard _board = UpdateBoard(_curPlayerHealth, _curEnemyHealth, _objNode);

        //performs MCTS on the updated board
        _board = _mcts.findNextMove(_board);

        MonteCarloPosition _enemyPos = new MonteCarloPosition();

        //sets the new enemy pos to the positions returned from MCTS
        _enemyPos = _board.FindPlayer(MonteCarloBoard._enemyVal);

        //returns the enemy position
        return(_enemyPos);
    }
    //performs enemy turn
    public void EnemyTurn()
    {
        //checks if enemy player exists
        if (_player != null)
        {
            //unselects any selected tiles
            _gridSystem.GetComponent <GridSystem>().UnselectTiles();

            //finds all enemies on the grid
            _enemies = FindEnemies();

            //checks if there are enemies within the enemies array
            if (_enemies.Length > 0)
            {
                //checks if the current enemy array index is null
                if (_enemies [_curEnemy] == null)
                {
                    //goes to the next enemy
                    NextEnemy();
                }
                else
                {
                    //finds the selected enemy in the array index
                    Node _node = GridSystem.FindSelectedObject(_enemies [_curEnemy], 0);

                    //finds the position to move to using MCTS
                    _positionToMoveTo = _enemies[_curEnemy].GetComponent <MonteCarloEnemy> ().StartMCTS(_player.GetComponent <Player_Controller> ()._health, _enemies[_curEnemy].GetComponent <EnemyController>()._health, _node);

                    //checks whether an action should be performed and which action
                    CheckForAction();
                }
            }
            else
            {
                //changes to player turn if no enemy objects in enemies array
                _gameManager.GetComponent <GameManager> ().ChangeTurn();
            }
        }
        else
        {
            //displays if no player on grid
            Debug.Log("Enemy Manager Player Null");
        }
    }
    //function used to find an object on the board
    public MonteCarloPosition FindPlayer(int _playerNo)
    {
        MonteCarloPosition pos = new MonteCarloPosition();

        int _player = 0;

        //checks for the player no, setting player as the referenced player value
        if (_playerNo == _enemyVal)
        {
            _player = _enemyVal;
        }
        else if (_playerNo == _playerVal)
        {
            _player = _playerVal;
        }

        //loops through all board values on the board
        for (int x = 0; x < GridSystem._gridSizeX; x++)
        {
            for (int z = 0; z < GridSystem._gridSizeZ; z++)
            {
                //checks if the current board value is equal to the player defined by the player no
                if (_boardValues [x, z] == _player)
                {
                    //sets the position x and z values to the current board x and z values
                    pos.SetX(x);
                    pos.SetZ(z);

                    //returns the position
                    return(pos);
                }
            }
        }

        //returns null if no object found
        return(null);
    }
    public MonteCarloBoard(MonteCarloBoard _board)
    {
        this._boardValues = new int[GridSystem._gridSizeX, GridSystem._gridSizeZ];

        int[,] _boardValues = _board.getBoardValues();

        this._movedPosition = _board._movedPosition;

        this._playerHealth = _board._playerHealth;

        this._enemyHealth = _board._enemyHealth;

        this._originalPlayerHealth = _board._originalPlayerHealth;

        this._originalEnemyHealth = _board._originalEnemyHealth;

        for (int x = 0; x < GridSystem._gridSizeX; x++)
        {
            for (int z = 0; z < GridSystem._gridSizeZ; z++)
            {
                this._boardValues [x, z] = _boardValues [x, z];
            }
        }
    }
    //function used to perform the move of the player
    public void performPlayerMove(MonteCarloPosition _p)
    {
        //increments the total moves
        this._totalMoves++;

        //finds the shootable tiles for the player to check for collision with the enemy object, returning true if an enemy object is in line of sight
        bool _opponentCheck = FindPlayerShootTiles();

        //checks if the opponent check value is true
        if (_opponentCheck)
        {
            //reduces the enemy health by 1
            this._enemyHealth--;
        }
        //performs player action if opponent check value is false
        else
        {
            //sets old player position to current player position
            MonteCarloPosition _oldPos = FindPlayer(_playerVal);

            //randomly decides whether to move the player 1 or 2 places
            int move = (int)Random.Range(0, 2);

            //if move value is 0, moves the player 1 position
            if (move == 0)
            {
                //sets board value of the old player position to 0, whilst setting the new player position to the player value
                _boardValues [_oldPos.GetX(), _oldPos.GetZ()] = 0;
                _boardValues [_p.GetX(), _p.GetZ()]           = _playerVal;

                //finds the shootable tiles for the player to check for collision with the enemy object, returning true if an enemy object is in line of sight
                bool _opponentCheck2 = FindPlayerShootTiles();

                //checks if the opponent check value is true
                if (_opponentCheck2)
                {
                    //reduces the enemy health by 1
                    this._enemyHealth--;
                }
            }
            //if move value is not 0, moves the player 2 positions
            else
            {
                //sets board value of the old player position to 0, whilst setting the new player position to the player value
                _boardValues [_oldPos.GetX(), _oldPos.GetZ()] = 0;
                _boardValues [_p.GetX(), _p.GetZ()]           = _playerVal;

                //finds the legal positions for the player
                List <MonteCarloPosition> _legalPositions = this.GetLegalPositions(_playerVal);

                //sets a new int to the number of legal positions found
                int _totalLegalPossibilities = _legalPositions.Count;

                //selects a random number between 0 and the number of legal positions
                int _selectRandom = (int)(Random.Range(0, _totalLegalPossibilities));

                //sets the second move position to that of the randomly selected legal position
                MonteCarloPosition _p2 = _legalPositions [_selectRandom];

                //sets board value of the old player position to 0, whilst setting the new player position to the player value
                _boardValues [_p.GetX(), _p.GetZ()]   = 0;
                _boardValues [_p2.GetX(), _p2.GetZ()] = _playerVal;


                //finds the shootable tiles for the player to check for collision with the enemy object, returning true if an enemy object is in line of sight
                bool _opponentCheck2 = FindPlayerShootTiles();

                //checks if the opponent check value is true
                if (_opponentCheck2)
                {
                    //reduces the enemy health by 1
                    this._enemyHealth--;
                }
            }
        }
    }