Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.List;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Map;
- import java.awt.Color;
- /**
- * Responsible for populating a field with foxes and rabbits.
- */
- public class PopulationGenerator
- {
- // The probability that a fox will be created in any given grid position.
- private static final double FOX_CREATION_PROBABILITY = 0.02;
- // The probability that a rabbit will be created in any given grid position.
- private static final double RABBIT_CREATION_PROBABILITY = 0.08;
- // Mapping from classes to colors
- private Map colorMap;
- // The colors of the animals
- private Color FOX_COLOR = Color.BLUE;
- private Color RABBIT_COLOR = Color.ORANGE;
- /**
- * Constructor for objects of class PopulationGenerator
- */
- public PopulationGenerator()
- {
- colorMap = new HashMap();
- colorMap.put(Fox.class, FOX_COLOR);
- colorMap.put(Rabbit.class, RABBIT_COLOR);
- }
- /**
- * Populate a field with foxes and rabbits.
- * @param field The field to be populated.
- * @param animals The list of animals.
- */
- public void populate(Field field, List animals)
- {
- Random rand = new Random();
- field.clear();
- for(int row = 0; row < field.getDepth(); row++) {
- for(int col = 0; col < field.getWidth(); col++) {
- if(rand.nextDouble() <= FOX_CREATION_PROBABILITY) {
- Fox fox = new Fox(true);
- fox.setLocation(row, col);
- animals.add(fox);
- field.place(fox);
- }
- else if(rand.nextDouble() <= RABBIT_CREATION_PROBABILITY) {
- Rabbit rabbit = new Rabbit(true);
- rabbit.setLocation(row, col);
- animals.add(rabbit);
- field.place(rabbit);
- }
- // else leave the location empty.
- }
- }
- Collections.shuffle(animals);
- }
- public Map getColors() {
- return colorMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement