View Javadoc
1   package org.woehlke.simulation.evolution.model;
2   
3   import java.util.Random;
4   
5   /**
6    * (C) 2006 - 2013 Thomas Woehlke.
7    * http://thomas-woehlke.de/p/simulated-evolution/
8    * @author Thomas Woehlke
9    * User: thomas
10   * Date: 24.08.13
11   * Time: 12:37
12   */
13  public class WorldMapFood {
14  
15      private int worldMapFood[][];
16      private Random random;
17      private final int FOOD_PER_DAY = 10;
18      private final boolean EABLE_GARDEN_OF_EDEN = true;
19      private Point dimensions;
20  
21      public WorldMapFood(Point dimensions,Random random){
22          this.dimensions=dimensions;
23          worldMapFood = new int[this.dimensions.getX()][this.dimensions.getY()];
24          this.random=random;
25      }
26  
27      public void letFoodGrow() {
28          int f = 0;
29          while (f < FOOD_PER_DAY) {
30              f++;
31              int x = random.nextInt(this.dimensions.getX());
32              int y = random.nextInt(this.dimensions.getY());
33              if (x < 0) {
34                  x *= -1;
35              }
36              if (y < 0) {
37                  y *= -1;
38              }
39              worldMapFood[x][y]++;
40          }
41          if(EABLE_GARDEN_OF_EDEN){
42              f = 0;
43              int startx = this.dimensions.getX() / 5;
44              int starty = this.dimensions.getY() / 5;
45              while (f < FOOD_PER_DAY*4) {
46                  f++;
47                  int x = random.nextInt(startx);
48                  int y = random.nextInt(starty);
49                  if (x < 0) {
50                      x *= -1;
51                  }
52                  if (y < 0) {
53                      y *= -1;
54                  }
55                  worldMapFood[x+startx*2][y+starty*2]++;
56              }
57          }
58      }
59  
60      public boolean hasFood(int x, int y) {
61          return worldMapFood[x][y] > 0;
62      }
63  
64      public int eat(Point position) {
65          Point neighbourhood[] = position.getNeighbourhood(this.dimensions);
66          int food=0;
67          for (Point neighbourhoodPosition:neighbourhood){
68              food += worldMapFood[neighbourhoodPosition.getX()][neighbourhoodPosition.getY()];
69              worldMapFood[neighbourhoodPosition.getX()][neighbourhoodPosition.getY()]=0;
70          }
71          return food;
72      }
73  }