SHARE
TWEET

Untitled

a guest Apr 25th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Random;
  2. import java.util.List;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.awt.Color;
  7. /**
  8.  * Responsible for populating a field with foxes and rabbits.
  9.  */
  10. public class PopulationGenerator
  11. {
  12.  
  13.     // The probability that a fox will be created in any given grid position.
  14.     private static final double FOX_CREATION_PROBABILITY = 0.02;
  15.     // The probability that a rabbit will be created in any given grid position.
  16.     private static final double RABBIT_CREATION_PROBABILITY = 0.08;
  17.    
  18.     // Mapping from classes to colors
  19.     private Map colorMap;
  20.    
  21.     // The colors of the animals
  22.     private Color FOX_COLOR = Color.BLUE;
  23.     private Color RABBIT_COLOR = Color.ORANGE;    
  24.    
  25.    
  26.     /**
  27.      * Constructor for objects of class PopulationGenerator
  28.      */
  29.     public PopulationGenerator()
  30.     {        
  31.         colorMap = new HashMap();
  32.         colorMap.put(Fox.class, FOX_COLOR);
  33.         colorMap.put(Rabbit.class, RABBIT_COLOR);        
  34.     }
  35.        
  36.     /**
  37.      * Populate a field with foxes and rabbits.
  38.      * @param field The field to be populated.
  39.      * @param animals The list of animals.
  40.      */
  41.     public void populate(Field field, List animals)
  42.     {
  43.         Random rand = new Random();
  44.         field.clear();
  45.         for(int row = 0; row < field.getDepth(); row++) {
  46.             for(int col = 0; col < field.getWidth(); col++) {
  47.                
  48.                 if(rand.nextDouble() <= FOX_CREATION_PROBABILITY) {
  49.                     Fox fox = new Fox(true);
  50.                     fox.setLocation(row, col);
  51.                     animals.add(fox);
  52.                     field.place(fox);
  53.                 }
  54.                 else if(rand.nextDouble() <= RABBIT_CREATION_PROBABILITY) {
  55.                     Rabbit rabbit = new Rabbit(true);
  56.                     rabbit.setLocation(row, col);
  57.                     animals.add(rabbit);
  58.                     field.place(rabbit);
  59.                 }
  60.                 // else leave the location empty.
  61.             }
  62.         }
  63.         Collections.shuffle(animals);
  64.     }
  65.    
  66.     public Map getColors() {
  67.         return colorMap;
  68.     }
  69.    
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top