View Javadoc

1   package org.woehlke.simulation.evolution.dom;
2   
3   import org.woehlke.simulation.evolution.beans.SimGenPoint;
4   
5   import java.util.*;
6   
7   /**
8    * (C) 2006 - 2008 Thomas Woehlke
9    * http://www.thomas-woehlke.de
10   * @author Thomas Woehlke
11   * User: thomas
12   * Date: 04.02.2006
13   * Time: 19:06:20
14   */
15  public class SimGenWorld implements ISimGenWorld {
16      private ArrayList<ISimGenCell> cells;
17      private int initialPopulation = 20;
18      private long seed;
19      private Random random;
20      private int X = 320;
21      private int Y = 234;
22      private int foodPerDay = 1;
23      private int worldMapFood[][];
24      private ArrayList<SimGenPoint> positions;
25      SimGenPoint max;
26  
27      public SimGenWorld() {
28          worldMapFood = new int[X][Y];
29          max = new SimGenPoint(X, Y);
30          createPopulation();
31      }
32  
33      public SimGenWorld(int x, int y) {
34          X = x;
35          Y = y;
36          worldMapFood = new int[X][Y];
37          max = new SimGenPoint(X, Y);
38          createPopulation();
39      }
40  
41      private void createPopulation() {
42          positions = new ArrayList<SimGenPoint>();
43          cells = new ArrayList<ISimGenCell>();
44          seed = new Date().getTime();
45          random = new Random(seed);
46          for (int i = 0; i < initialPopulation; i++) {
47              int x = random.nextInt(X);
48              int y = random.nextInt(Y);
49              if (x < 0) {
50                  x *= -1;
51              }
52              if (y < 0) {
53                  y *= -1;
54              }
55              SimGenPoint pos = new SimGenPoint(x, y);
56              ISimGenCell cell = new SimGenCell(max, pos, random);
57              cells.add(cell);
58          }
59      }
60  
61      public void letFoodGrow() {
62          int f = 0;
63          while (f < foodPerDay) {
64              f++;
65              int x = random.nextInt(X);
66              int y = random.nextInt(Y);
67              if (x < 0) {
68                  x *= -1;
69              }
70              if (y < 0) {
71                  y *= -1;
72              }
73              worldMapFood[x][y]++;
74          }
75      }
76  
77      public void letLivePopulation() {
78          letFoodGrow();
79          SimGenPoint pos;
80          ArrayList<ISimGenCell> children = new ArrayList<ISimGenCell>();
81          Iterator<ISimGenCell> i = cells.iterator();
82          while (i.hasNext()) {
83              ISimGenCell cell = i.next();
84              cell.move();
85              pos = cell.getPos();
86              int x = pos.getX();
87              int y = pos.getY();
88              if (hasFood(x, y)) {
89                  cell.eat();
90                  worldMapFood[x][y]--;
91              }
92              if (cell.isPregnant()) {
93                  ISimGenCell child = cell.cellDivisionFactory();
94                  children.add(child);
95              }
96          }
97          cells.addAll(children);
98          positions = new ArrayList<SimGenPoint>();
99          i = cells.iterator();
100         while (i.hasNext()) {
101             ISimGenCell cell = i.next();
102             SimGenPoint p = cell.getPos();
103             positions.add(p);
104         }
105     }
106 
107     public ArrayList<SimGenPoint> getPositionsOfAllCells() {
108         return positions;
109     }
110 
111     public boolean hasFood(int x, int y) {
112         return worldMapFood[x][y] > 0;
113     }
114 
115     public int getWidth() {
116         return X;
117     }
118 
119     public void setWidth(int x) {
120         X = x;
121     }
122 
123     public int getHeight() {
124         return Y;
125     }
126 
127     public void setHeight(int y) {
128         Y = y;
129     }
130 
131     public int getFoodPerDay() {
132         return foodPerDay;
133     }
134 
135     public void setFoodPerDay(int foodPerDay) {
136         this.foodPerDay = foodPerDay;
137     }
138 
139     public int getY() {
140         return Y;
141     }
142 
143     public void setY(int y) {
144         Y = y;
145     }
146 
147     public int getX() {
148         return X;
149     }
150 
151     public void setX(int x) {
152         X = x;
153     }
154 
155     public int getInitialPopulation() {
156         return initialPopulation;
157     }
158 
159     public void setInitialPopulation(int initialPopulation) {
160         this.initialPopulation = initialPopulation;
161     }
162 
163 }