活跃农民
- 积分
- 354
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-11-11
- 最后登录
- 1970-1-1
|
public class Plip extends Creature {
/** red color. */
private int r;
/** green color. */
private int g;
/** blue color. */
private int b;
/** creates plip with energy equal to E. */
public Plip(double e) {
super("plip");
r = 0;
g = 0;
b = 0;
energy = e;
}
/** creates a plip with energy equal to 1. */
public Plip() {
this(1);
}
/** Should return a color with red = 99, blue = 76, and green that varies
* linearly based on the energy of the Plip. If the plip has zero energy,
* it should have a green value of 63. If it has max energy, it should
* have a green value of 255. The green value should vary with energy
* linearly in between these two extremes. It's not absolutely vital
* that you get this exactly correct.
*/
public Color color() {
r=99;
b=76;
g=63+(int)(9.6*((int)energy*10));
return color(r, g, b);
}
/* public Color color(int r1,int g1, int b1){
return color(r1,g1,b1);
}*/
/** Do nothing with C, Plips are pacifists. */
public void attack(Creature c) {
}
/** Plips should lose 0.15 units of energy when moving. If you want to
* to avoid the magic number warning, you'll need to make a
* private static final variable. This is not required for this lab.
*/
public void move() {
energy -= 0.15;
}
/** Plips gain 0.2 energy when staying due to photosynthesis. */
public void stay() {
energy += 0.2;
if(energy>=2){
energy=2;
}
}
/** Plips and their offspring each get 50% of the energy, with none
* lost to the process. Now that's efficiency! Returns a baby
* Plip.
*/
public Plip replicate() {
Plip babyPlip= new Plip();
babyPlip.energy=energy/2;
energy = energy/2;
return babyPlip;
}
/** Plips take exactly the following actions based on NEIGHBORS:
* 1. If no empty adjacent spaces, STAY.
* 2. Otherwise, if energy >= 1, REPLICATE.
* 3. Otherwise, if any Cloruses, MOVE with 50% probability.
* 4. Otherwise, if nothing else, STAY
*
* Returns an object of type Action. See Action.java for the
* scoop on how Actions work. See SampleCreature.chooseAction()
* for an example to follow.
*/
public Action chooseAction(Map<Direction, Occupant> neighbors) {
//find the number of empty position
List<Direction> empties = getNeighborsOfType(neighbors, "empty");
if (empties.size()!=0){
if (energy >= 1) {
Direction moveDir = HugLifeUtils.randomEntry(empties);
return new Action(Action.ActionType.REPLICATE,moveDir);
}
else if (getNeighborsOfType(neighbors,"colorus")!=null) {
if (HugLifeUtils.random() < 0.5) {
Direction moveDir = HugLifeUtils.randomEntry(empties);
return new Action(Action.ActionType.MOVE, moveDir);
}
}
}
else{
return new Action(Action.ActionType.STAY);
}
return new Action(Action.ActionType.STAY);
}
}
public class Clorus extends Creature {
/** red color. */
private int r;
/** green color. */
private int g;
/** blue color. */
private int b;
/** creates Clorus with energy equal to E. */
public Clorus(double e) {
super("Clorus");
r = 0;
g = 0;
b = 0;
energy = e;
}
/** creates a Clorus with energy equal to 1. */
public Clorus() {
this(1);
}
/** Should return a color with red = 99, blue = 76, and green that varies
* linearly based on the energy of the Plip. If the plip has zero energy,
* it should have a green value of 63. If it has max energy, it should
* have a green value of 255. The green value should vary with energy
* linearly in between these two extremes. It's not absolutely vital
* that you get this exactly correct.
*/
public Color color() {
r=34;
b=231;
g=0;
return color(r, g, b);
}
public void attack(Creature c) {
energy=c.energy();
}
/** Plips should lose 0.15 units of energy when moving. If you want to
* to avoid the magic number warning, you'll need to make a
* private static final variable. This is not required for this lab.
*/
public void move() {
energy -= 0.03;
}
/** Plips gain 0.2 energy when staying due to photosynthesis. */
public void stay() {
energy -= 0.01;
}
/** Plips and their offspring each get 50% of the energy, with none
* lost to the process. Now that's efficiency! Returns a baby
* Plip.
*/
public Clorus replicate() {
Clorus babyPlip= new Clorus();
babyPlip.energy=energy/2;
energy = energy/2;
return babyPlip;
}
/** Plips take exactly the following actions based on NEIGHBORS:
* 1. If there are no empty squares the Clorus will STAY
(even if there are Plips nearby they could attack).
* 2. Otherwise, if any Plips are seen, the Clorus will
ATTACK one of them randomly.
* 3. Otherwise, if the Clorus has energy greater than or
equal to one, it will REPLICATE to a random empty square.
* 4. Otherwise, the Clorus will MOVE.
*
* Returns an object of type Action. See Action.java for the
* scoop on how Actions work. See SampleCreature.chooseAction()
* for an example to follow.
*/
public Action chooseAction(Map<Direction, Occupant> neighbors) {
//find the number of empty position
List<Direction> empties = getNeighborsOfType(neighbors, "empty");
if (empties.size()!=0){
List<Direction> pli=getNeighborsOfType(neighbors,"plip");
if (pli!=null) {
Direction moveDir = HugLifeUtils.randomEntry(pli);
return new Action(Action.ActionType.ATTACK, moveDir);
} else if (energy >= 1) {
Direction moveDir = HugLifeUtils.randomEntry(empties);
return new Action(Action.ActionType.REPLICATE,moveDir);
}
}
else{
return new Action(Action.ActionType.STAY);
}
Direction moveDir = HugLifeUtils.randomEntry(empties);
return new Action(Action.ActionType.MOVE,moveDir);
}
}
public class TestPlip {
@Test
public void testBasics() {
Plip p = new Plip(2);
assertEquals(2, p.energy(), 0.01);
assertEquals(new Color(99, 255, 76), p.color());
p.move();
assertEquals(1.85, p.energy(), 0.01);
p.move();
assertEquals(1.70, p.energy(), 0.01);
p.stay();
assertEquals(1.90, p.energy(), 0.01);
p.stay();
assertEquals(2.00, p.energy(), 0.01);
}
@Test
public void testReplicate() {
Plip p=new Plip(2);
HashMap<Direction, Occupant> surrounded = new HashMap<Direction, Occupant>();
surrounded.put(Direction.TOP, new Impassible());
surrounded.put(Direction.BOTTOM, new Impassible());
surrounded.put(Direction.LEFT, new Impassible());
surrounded.put(Direction.RIGHT, new Empty());
Action actual=p.chooseAction(surrounded);
Action expected=new Action(Action.ActionType.REPLICATE, Direction.RIGHT);
assertEquals(expected, actual);
}
@Test
public void testChoose() {
Plip p = new Plip(1.2);
HashMap<Direction, Occupant> surrounded = new HashMap<Direction, Occupant>();
surrounded.put(Direction.TOP, new Impassible());
surrounded.put(Direction.BOTTOM, new Impassible());
surrounded.put(Direction.LEFT, new Impassible());
surrounded.put(Direction.RIGHT, new Impassible());
//You can create new empties with new Empty();
//Despite what the spec says, you cannot test for Cloruses nearby yet.
//Sorry!
Action actual = p.chooseAction(surrounded);
Action expected = new Action(Action.ActionType.STAY);
assertEquals(expected, actual);
}
/* @Test
public void testChoose() {
Plip p = new Plip(1.2);
HashMap<Direction, Occupant> surrounded = new HashMap<Direction, Occupant>();
surrounded.put(Direction.TOP, new Impassible());
surrounded.put(Direction.BOTTOM, new Impassible());
surrounded.put(Direction.LEFT, new Impassible());
surrounded.put(Direction.RIGHT, new Impassible());
//You can create new empties with new Empty();
//Despite what the spec says, you cannot test for Cloruses nearby yet.
//Sorry!
Action actual = p.chooseAction(surrounded);
Action expected = new Action(Action.ActionType.STAY);
assertEquals(expected, actual);
}*/
public static void main(String[] args) {
System.exit(jh61b.junit.textui.runClasses(TestPlip.class));
}
}
Clorus的test类似就没写 |
|