Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/nchaimov/cabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Stellard committed Dec 7, 2009
2 parents cf17e3d + 1104b0a commit 36d1d1b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
2 changes: 2 additions & 0 deletions grass.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Grass 15
EatingRabbit 10
2 changes: 1 addition & 1 deletion src/world/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public abstract class Agent {

transient LocalCell cell;
protected transient LocalCell cell;
int turn = 0;
public boolean hasMoved = false;

Expand Down
52 changes: 52 additions & 0 deletions src/world/impl/EatingRabbit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package world.impl;

import java.util.Collection;
import java.util.Random;

import world.Agent;

public class EatingRabbit extends Agent {

public static final int EAT_AMOUNT = 5;
public static final int METABOLISM_PER_TURN = 2;
public static final int REPRODUCE_ENERGY = 100;
public static final int DEATH_ENERGY = 10;

private Random random = new Random();

public int health = 50;

@Override
public void go() {
System.out.println("Rabbit at " + cell.x + ", " + cell.y + " has energy " + health);

if (health <= DEATH_ENERGY) {
System.out.println("Rabbit at " + cell.x + ", " + cell.y + " is dying");
this.die();
return;
}
Collection<? extends Agent> agents = look(0, 0);
for (Agent a : agents) {
if (a instanceof Grass) {
Grass g = (Grass) a;
System.out.println("Rabbit at " + cell.x + ", " + cell.y + " sees grass of height "
+ g.height);
if (g.height > Grass.MIN_EAT_HEIGHT) {
System.out.println("Rabbit at " + cell.x + ", " + cell.y + " eats grass");
health += EAT_AMOUNT;
}
}
}
if (health >= REPRODUCE_ENERGY) {
this.cell.add(new EatingRabbit());
System.out.println("Rabbit at " + cell.x + ", " + cell.y + " reproduced");
}

int x = random.nextInt(2);
int y = random.nextInt(2);

System.out.println("Rabbit at " + cell.x + ", " + cell.y + " moving by " + x + ", " + y);

move(x, y);
}
}
31 changes: 31 additions & 0 deletions src/world/impl/Grass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package world.impl;

import java.util.Collection;

import world.Agent;

public class Grass extends Agent {

public static final int GROW_AMOUNT = 1;
public static final int MIN_EAT_HEIGHT = 20;

public int height = 50;

@Override
public void go() {
height += GROW_AMOUNT;
System.out.println("Grass at " + cell.x + ", " + cell.y + " grew to " + height);

if (height >= MIN_EAT_HEIGHT) {
Collection<? extends Agent> agents = look(0, 0);
for (Agent a : agents) {
if (a instanceof EatingRabbit) {
height -= EatingRabbit.EAT_AMOUNT;
System.out.println("Grass at " + cell.x + ", " + cell.y
+ " eaten by rabbit; height is now " + height);
}
}
}
}

}
5 changes: 1 addition & 4 deletions src/world/impl/Rabbit.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package world.impl;

import java.util.Collection;

import world.Agent;

public class Rabbit extends Agent {
Expand All @@ -12,7 +10,6 @@ public void go() {

// move(r.nextInt(5),r.nextInt(5));
move(1, 0);
Collection<? extends Agent> agents = look(0, 1);
System.out.println(">>>>>>> I can see " + agents.size() + " agents below me");

}
}

0 comments on commit 36d1d1b

Please sign in to comment.