Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Make code recognize "wolf" object and run away from it

$
0
0
Hey i am running a wolf eat sheep tournament and i am in charge of writing the sheep player code. I am having trouble with just starting how to find the wolf object in my code. Right now i have the sheep running to the pasture and there will be a wolf in the game and i am trying to get my sheep to run away from the wolf as well as go to the pasture. Any suggestions on how to at least get this started would be great. Thanks!




ublic class SheepPlayer extends was.SheepPlayer {

    private static Random rand = new Random();
    
    
    Move aim = null;
    public SheepPlayer ()
    {
       super();
    }
    @Override
    public Move move() {
         GameBoard myBoard = this.getGameBoard();
     
     int xaxismove, yaxismove;        
     
     GameLocation myLocation = this.getLocation();  
     
     GameLocation wolfLocation = this.getGameBoard().getWolfPosition();  
     
     ArrayList<GameLocation> pastureLocations = myBoard.getPasturePositions();
     
     ArrayList<GameLocation> obstacleLocations = myBoard.getObstaclePositions();
     
        
     
     GameLocation closestPasture = pastureLocations.get(0);
     GameLocation closestObstacle = obstacleLocations.get(0);   
        
     if (closestPasture.x - myLocation.x > 0){
            xaxismove = 1;        
            
     }else if (closestPasture.x - myLocation.x < 0){
        xaxismove = -1;
        
    }else 
        
        xaxismove = 0;
     
     if (closestPasture.y - myLocation.y > 0){
        yaxismove = 1;
        
    }else if (closestPasture.y - myLocation.y < 0){
        yaxismove = -1;
        
    }else yaxismove = 0;
     
     
     
     

    return new Move(xaxismove, yaxismove);
 }
      
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>