View Javadoc
1   package org.woehlke.simulation.evolution.model;
2   
3   
4   import java.io.Serializable;
5   import java.util.ArrayList;
6   import java.util.Date;
7   import java.util.List;
8   import java.util.Random;
9   
10  /**
11   * The World contains Water, Cells and Food.
12   * It is the Data Model of the Simulation in a MVC Pattern.
13   *
14   * @see Cell
15   * @see WorldMapFood
16   *
17   * Simulated Evolution.
18   * Artificial Life Simulation of Bacteria Motion depending on DNA.
19   *
20   * © 2006 - 2008 Thomas Woehlke.
21   * http://thomas-woehlke.de/p/simulated-evolution/
22   * @author Thomas Woehlke
23   * User: thomas
24   * Date: 04.02.2006
25   * Time: 19:06:20
26   */
27  public class World implements Serializable {
28  
29      static final long serialVersionUID = 242L;
30  
31      /**
32       * List of the Simulated Bacteria Cells.
33       */
34      private List<Cell> cells;
35  
36      /**
37       * Start with 20 Cells.
38       */
39      private final int INITIAL_POPULATION = 20;
40  
41      /**
42       * Random Generator used for Bacteria Motion.
43       */
44      private Random random;
45  
46      /**
47       * Definition of the World's Size in Pixel Width and Height.
48       */
49      private Point worldDimensions;
50  
51      /**
52       * Map of the World monitoring growth and eating food.
53       */
54      private WorldMapFood worldMapFood;
55  
56      public World(Point worldDimensions) {
57          long seed = new Date().getTime();
58          random = new Random(seed);
59          this.worldDimensions = worldDimensions;
60          worldMapFood = new WorldMapFood(this.worldDimensions,random);
61          createPopulation();
62      }
63  
64      /**
65       * Create the initial Population of Bacteria Cells and give them their position in the World.
66       */
67      private void createPopulation() {
68          cells = new ArrayList<Cell>();
69          for (int i = 0; i < INITIAL_POPULATION; i++) {
70              int x = random.nextInt(worldDimensions.getX());
71              int y = random.nextInt(worldDimensions.getY());
72              if (x < 0) {
73                  x *= -1;
74              }
75              if (y < 0) {
76                  y *= -1;
77              }
78              Point pos = new Point(x, y);
79              Cell cell = new Cell(worldDimensions, pos, random);
80              cells.add(cell);
81          }
82      }
83  
84      /**
85       * One Step of Time in the World in which the Population of Bacteria Cell perform Life:
86       * Every Cell moves, eats, dies of hunger, and it has sex: splitting into two children with changed DNA.
87       */
88      public void letLivePopulation() {
89          worldMapFood.letFoodGrow();
90          Point pos;
91          List<Cell> children = new ArrayList<Cell>();
92          List<Cell> died = new ArrayList<Cell>();
93          for (Cell cell:cells) {
94              cell.move();
95              if(cell.died()){
96                  died.add(cell);
97              } else {
98                  pos = cell.getPosition();
99                  int food = worldMapFood.eat(pos);
100                 cell.eat(food);
101                 if (cell.isPregnant()) {
102                     Cell child = cell.performReproductionByCellDivision();
103                     children.add(child);
104                 }
105             }
106         }
107         for(Cell dead:died){
108             cells.remove(dead);
109         }
110         cells.addAll(children);
111     }
112 
113     public List<Cell> getAllCells(){
114         return cells;
115     }
116 
117     public boolean hasFood(int x, int y) {
118         return worldMapFood.hasFood(x,y);
119     }
120 }