//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); }
//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); }