Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: Java  |  size: 3.90 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package G53IDS;
  2.  
  3. //IMPORTS
  4. import java.util.*;
  5.  
  6. //CODE.
  7. class agentCreator{
  8.         void spawn (int[][] map, ArrayList<agent> agentArray){
  9.                
  10.                 //get the width and height of the array
  11.                 int width = map.length;
  12.                 int height = map[0].length;
  13.                
  14.                 //get how large our agentArray is.
  15.                 int length = agentArray.size();
  16.                
  17.                 //set some useful variables that we'll use in our calculations and checks.
  18.                 int failCount = 0;
  19.                 int failX=0, failY=0;
  20.                 boolean possible = false;
  21.                
  22.                 //create an object to work with.
  23.                 agent tempAgent = new agent();
  24.                
  25.                 //cycle through our agents.
  26.                 for (int n =0; n <length; n++){
  27.                        
  28.                         //reset our fail count.
  29.                         failCount = 0;
  30.                        
  31.                         //assign our tempAgent to the object in the agentArray.
  32.                         tempAgent = agentArray.get(n);
  33.                        
  34.                         //try some random X and Y coords for our agent.
  35.                         tempAgent.xCoord = usefulFunctions.randomIntegerGen(1, width-1);
  36.                         tempAgent.yCoord = usefulFunctions.randomIntegerGen(1, height-1);
  37.                        
  38.                         //While our chosen coordinates are an obstacle keep trying new ones.
  39.                         while (map[tempAgent.xCoord][tempAgent.yCoord]==-1){
  40.                                
  41.                                 //Keep track of how hard we're failing.
  42.                                 failCount++;
  43.                                
  44.                                 //set the coords to new random coords inside our area.
  45.                                 tempAgent.xCoord = usefulFunctions.randomIntegerGen(1, width-1);
  46.                                 tempAgent.yCoord = usefulFunctions.randomIntegerGen(1, height-1);
  47.                                
  48.                                 //if we've failed lots check if there exists a location in our map that is not an obstacle.
  49.                                 if(failCount==25){
  50.                                        
  51.                                         //cycle through the x-coords
  52.                                         for (failX =0; failX<map.length; failX++){
  53.                                        
  54.                                                 //cycle through the y-coords
  55.                                                 for (failY = 0; failY<map[0].length; failY++){
  56.                                                
  57.                                                         //if we find a coordinate that isn't an obstacle then state that it's possible.
  58.                                                         if (map[failX][failY] != -1){
  59.                                                        
  60.                                                                 possible = true;
  61.                                                        
  62.                                                         }//end if not obstacle.
  63.                                                 }//end Y loop.
  64.                                         }//end X loop.
  65.                                        
  66.                                         //if no coordinate has returned possible then close the program and report the problem.
  67.                                         if (!possible){
  68.                                                 System.out.println("No free space for agents");
  69.                                                 System.exit(0);
  70.                                         }//end if not possible.
  71.                                 }//end if failCount == 25.
  72.                                
  73.                                 //if we've failed loads but it is possible then we need to look at different ways of placing our agent.
  74.                                 if(failCount>400){
  75.                                        
  76.                                         //search through map x coords.
  77.                                         for (failX =0; failX<map.length; failX++){
  78.                                        
  79.                                                 //search through map y coords.
  80.                                                 for (failY = 0; failY<map[0].length; failY++){
  81.                                                        
  82.                                                         //Check if the coordinate is not an obstacle.
  83.                                                         if (map[failX][failY] != -1){
  84.                                                                
  85.                                                                 //if the coordinate we're at isn't an obstacle then check if we've already got a viable agent location set.
  86.                                                                 if (map[tempAgent.xCoord][tempAgent.yCoord] != -1){
  87.                                                                                                                                          
  88.                                                                         //if we've already got a random agent location set only overide it with a very low probability so that we maintain our relative randomness of agent start locations.
  89.                                                                         if (usefulFunctions.randomIntegerGen(0, 1000000) > 999900){
  90.                                                                                
  91.                                                                                 //if our random check passes then set the agent X and Y coords.
  92.                                                                                 tempAgent.xCoord = failX;
  93.                                                                                 tempAgent.yCoord = failY;
  94.                                                                         }// end low probability of success if.
  95.                                                                        
  96.                                                                 }//end if agent is already assigned to viable location.
  97.                                                                
  98.                                                                 //If the agent hasn't already been assigned to a viable location then assign him to the one we've found..
  99.                                                                 else {
  100.                                                                        
  101.                                                                         //assign him to his coords.
  102.                                                                         tempAgent.xCoord = failX;
  103.                                                                         tempAgent.yCoord = failY;
  104.                                                                 }//end else.
  105.                                                         }//end check if obstacle.
  106.                                                 }//end y loop
  107.                                         }//end x loop
  108.                                 }//end failCount>400 check.
  109.                         }// end while agent location is obstacle loop.
  110.                        
  111.                         //overwrite our old agent with the new.
  112.                         agentArray.set(n, tempAgent);
  113.                 }//end agent loop.
  114.         }// end spawn method.
  115. }//end agentCreator class.