View Javadoc
1   package org.woehlke.simulation.evolution.model;
2   
3   import java.util.Random;
4   
5   /**
6    * The Cell of one Bacterium.
7    * It's state is position, orientation and LifeCycle.
8    * The Cell has a CellCore with the DNA Genome for Moving around.
9    *
10   * Simulated Evolution.
11   * Artificial Life Simulation of Bacteria Motion depending on DNA.
12   *
13   * @see org.woehlke.simulation.evolution.model.CellCore
14   * @see org.woehlke.simulation.evolution.model.LifeCycle
15   * @see org.woehlke.simulation.evolution.model.LifeCycleStatus
16   *
17   * © 2006 - 2008 Thomas Woehlke.
18   * http://thomas-woehlke.de/p/simulated-evolution/
19   * @author Thomas Woehlke
20   * Date: 04.02.2006
21   * Time: 19:06:43
22   */
23  public class Cell {
24  
25      /**
26       * Contains the DNA for Random based Moving
27       */
28      private CellCore cellCore;
29  
30      /**
31       * The Cell's state is position, orientation and LifeCycle
32       */
33      private Point position;
34  
35      /**
36       * The Cell's state is position, orientation and LifeCycle
37       */
38      private Orientation orientation;
39  
40      /**
41       * The Cell's state is position, orientation and LifeCycle
42       */
43      private LifeCycle lifeCycle;
44  
45      /**
46       * The World Dimensions in which this Cell can move.
47       */
48      private Point max;
49  
50      /**
51       * Random Generator is set from outside by Constructor.
52       */
53      private Random random;
54  
55      public Cell(Point max, Point position, Random random) {
56          this.max = new Point(max);
57          this.position = new Point(position);
58          this.random = random;
59          this.cellCore = new CellCore(random);
60          this.max.killNagative();
61          this.position.setX(random.nextInt() % max.getX());
62          this.position.setY(random.nextInt() % max.getY());
63          this.position.killNagative();
64          this.orientation = getRandomOrientation();
65          this.lifeCycle = new LifeCycle();
66      }
67  
68      private Cell(int fat, CellCore rna, Point position, Point max, Random random) {
69          lifeCycle = new LifeCycle(fat);
70          this.max = new Point(max);
71          this.position = new Point(position);
72          this.random = random;
73          this.cellCore = rna;
74          orientation = getRandomOrientation();
75      }
76  
77      private Orientation getRandomOrientation() {
78          int dnaLength = Orientation.values().length;
79          int dnaBase = random.nextInt(dnaLength);
80          if (dnaBase < 0) {
81              dnaBase *= -1;
82          }
83          return Orientation.values()[dnaBase];
84      }
85  
86      private void getNextOrientation() {
87          Orientation randomOrientation = cellCore.getRandomOrientation();
88          int iOrientation = orientation.ordinal();
89          int iRandomOrientation = randomOrientation.ordinal();
90          int newOrientation = (iOrientation + iRandomOrientation) % Orientation.values().length;
91          orientation = Orientation.values()[newOrientation];
92      }
93  
94      /**
95       * The Cell moves on the Step in a Direction choosen by Random and DNA.
96       */
97      public void move() {
98          if(lifeCycle.move()){
99              getNextOrientation();
100             position.add(orientation.getMove());
101             position.add(max);
102             position.normalize(max);
103         }
104     }
105 
106     /**
107      * After performing Reproduction by Cell Division this Cell is one of the two Children this Method returns the other Child.
108      *
109      * @see CellCore#performMitosis()
110      *
111      * @return the other Child
112      */
113     public Cell performReproductionByCellDivision() {
114         CellCore rna = cellCore.performMitosis();
115         lifeCycle.haveSex();
116         Cell child = new Cell(lifeCycle.getFat(), rna, position, max, random);
117         return child;
118     }
119 
120     /**
121      * @return The new Position after the last move.
122      */
123     public Point getPosition() {
124         return position;
125     }
126 
127     /**
128      * @return true, if this Cell is able to perform Reproduction by Cell Division
129      */
130     public boolean isPregnant() {
131         return lifeCycle.isPregnant();
132     }
133 
134     /**
135      * Eat the available Food in this Position
136      * @param food the available Food in this Position
137      */
138     public void eat(int food) {
139         lifeCycle.eat(food);
140     }
141 
142     /**
143      * @return true, if this Cell died of hunger
144      */
145     public boolean died() {
146         return lifeCycle.isDead();
147     }
148 
149     /**
150      * @return the LifeCycleStatus of this Cell
151      */
152     public LifeCycleStatus getLifeCycleStatus(){
153         return lifeCycle.getLifeCycleStatus();
154     }
155 
156 }