-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/nchaimov/cabs
- Loading branch information
Showing
5 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Grass 15 | ||
EatingRabbit 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters