一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 654|回复: 4
收起左侧

[CS61B_Spring 2015] LAB6 HUGLIFE

[复制链接] |试试Instant~ |关注本帖
karte_polo 发表于 2015-7-5 00:45:23 | 显示全部楼层 |阅读模式

[其他]CS61B DATA STRUCTURES #6 - 2015-06-01@UC BERKLEY

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
RT~元胞自动机!
 楼主| karte_polo 发表于 2015-7-5 00:46:10 | 显示全部楼层
Plip.java


package creatures;
import huglife.Creature;
import huglife.Direction;
import huglife.Action;
import huglife.Occupant;
import huglife.HugLifeUtils;
import java.awt.Color;
import java.util.Map;
import java.util.List;

/** An implementation of a motile pacifist photosynthesizer.
*  @author Josh Hug
*/
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 = 99;
        g = 0;
        b = 76;
        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() {
        g = (int)(63+96*this.energy);
        return color(r, g, b);
    }

    /** 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() {
            this.energy-=0.15;
    }


    /** Plips gain 0.2 energy when staying due to photosynthesis. */
    public void stay() {
            this.energy+=0.2;
            if (this.energy>=2.0){
                    this.energy = 2.0;
            }
    }

    /** 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 np = new Plip(this.energy/2.0);
            this.energy = this.energy*0.5;
        return np;
    }

    /** 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) {
            List<Direction> emptyn = getNeighborsOfType(neighbors,"empty");
            if (!emptyn.isEmpty()){
                    List<Direction> cloruses = getNeighborsOfType(neighbors,"clorus");
                    if(!cloruses.isEmpty()){
                            return new Action(Action.ActionType.MOVE, HugLifeUtils.randomEntry(emptyn));
                    }
                    if (this.energy>=1.0){
                            return new Action(Action.ActionType.REPLICATE, HugLifeUtils.randomEntry(emptyn));
                    }
            }
        return new Action(Action.ActionType.STAY);
    }
}


Clorus.java

package creatures;
import huglife.Creature;
import huglife.Direction;
import huglife.Action;
import huglife.Occupant;
import huglife.HugLifeUtils;

import java.awt.Color;
import java.util.Map;
import java.util.List;

public class Clorus extends Creature {
            /** red color. */
            private int r;
            /** green color. */
            private int g;
            /** blue color. */
            private int b;
            
            public Clorus(double e) {
                super("clorus");
                r = 34;
                g = 0;
                b = 231;
                energy = e;
            }

            public Clorus() {
                this(1);
            }

            public Color color() {
                return color(r, g, b);
            }

            public void attack(Creature c) {
                    this.energy+=c.energy();
            }

            public void move() {
                    this.energy-=0.03;
            }

            public void stay() {
                    this.energy-=0.01;
            }

            public Clorus replicate() {
                    Clorus nc = new Clorus(this.energy/2.0);
                    this.energy = this.energy*0.5;
                return nc;
            }


            public Action chooseAction(Map<Direction, Occupant> neighbors) {
                    List<Direction> emptyn = getNeighborsOfType(neighbors,"empty");
                    if (emptyn.isEmpty()){
                            return new Action(Action.ActionType.STAY);
                    }else{
                            List<Direction> plips = getNeighborsOfType(neighbors,"plip");
                            if(!plips.isEmpty()){
                                    return new Action(Action.ActionType.ATTACK, HugLifeUtils.randomEntry(plips));
                            }else if (this.energy>=1.0){
                                    return new Action(Action.ActionType.REPLICATE, HugLifeUtils.randomEntry(emptyn));
                            }else{
                                    return new Action(Action.ActionType.MOVE, HugLifeUtils.randomEntry(emptyn));
                            }
                    }
            }
        }            
回复 支持 反对

使用道具 举报

ilyak 发表于 2015-7-11 15:05:01 | 显示全部楼层
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类似就没写
回复 支持 反对

使用道具 举报

Casualet 发表于 2015-8-12 20:58:00 | 显示全部楼层
public class Plip extends Creature {
    /** red color. */
    private int r;
    /** green color. */
    private int g;
    /** blue color. */
    private int b;
   // public static final String name="pilp";
    /** creates plip with energy equal to E. */
    public Plip(double e) {
        super("plip");//in this case, final is not initialized
        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() {
        g = 63;
        this.r=99;
        this.b=76;
        this.g=g+(int)(96*this.energy);
        return color(r, (int)g, b);//how can this be linear??
    }

    /** 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() {
      this.energy-=0.15;
      if(this.energy>2) this.energy=2;

    }


    /** Plips gain 0.2 energy when staying due to photosynthesis. */
    public void stay() {
      this.energy+=0.2;
      if(this.energy>2) this.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 temp = new Plip(this.energy/2);
        this.energy/=2;
        return temp;
    }

    /** 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) {
       List<Direction> empties = getNeighborsOfType(neighbors, "empty");
       if (empties.size() == 0) {
          return new Action(Action.ActionType.STAY);
       }else{
           if(this.energy-1.0>=1e-5){
           //randomly choose an empty direction
           Direction moveDir = HugLifeUtils.randomEntry(empties);
           return new Action(Action.ActionType.REPLICATE, moveDir);
           }else{
             List<Direction> moveOrnot = new ArrayList<Direction>();
             moveOrnot.add(Direction.TOP);
             moveOrnot.add(Direction.BOTTOM);
             List<Direction> clorus = getNeighborsOfType(neighbors, "clorus");
             Direction move=HugLifeUtils.randomEntry(moveOrnot);
             if(move==Direction.TOP)
                  
                Direction move50= HugLifeUtils.randomEntry(empties);
                return new Action(Action.ActionType.MOVE,move50);     
             }else{              
               return new Action(Action.ActionType.STAY);
             }
        }
    }//randomly choose an empty place      
  }
}
Clorus.java
public class Clorus extends Creature{
    private int r;
    private int b;
    private int g;
    public Clorus(double e){
       super("clorus");
       this.energy = e;
       r=0;
       b=0;
       g=0;
    }

    public Clorus(){
      this(1);
    }
   
    public Color color() {
       this.g = 0;
       this.r = 34;
       this.b = 231;
       return color(r, g, b);//how can this be linear??
    }

    public Clorus replicate() {
         Clorus temp = new Clorus(this.energy/2);
         this.energy/=2;
         return temp;
    }
   
    public void move(){
        this.energy-=0.03;   
    }
    public void stay(){
         this.energy-=0.01;
    }
    public void attack(Creature c){
          this.energy+=c.energy();
    }
    //what if neighbors.size()!=4?
    public Action chooseAction(Map<Direction, Occupant> neighbors){
        List<Direction> empties = getNeighborsOfType(neighbors,"empty");
        if(empties.size()==0)//if there is not empty space, then it will stay
          return new Action(Action.ActionType.STAY);
        else{
          List<Direction> plips = getNeighborsOfType(neighbors,"plip");
          if(plips.size()!=0){
              Direction attackDir=HugLifeUtils.randomEntry(plips);
              return new Action(Action.ActionType.ATTACK,attackDir);
          }else{//no plips around
               List<Direction> empties1 = getNeighborsOfType(neighbors,"empty");
               Direction replicDir = HugLifeUtils.randomEntry(empties1);      
                if(this.energy-1>1e-5){
                   return new Action(Action.ActionType.REPLICATE,replicDir);
                 }else{                     
                   return new Action(Action.ActionType.MOVE,replicDir);   
                 }
          }
          //this design allow us to attack upon getting the direction
         // which is really good.
        }
    }
}


这门课还是很有意义的,全部完成以后发一个总结贴



回复 支持 反对

使用道具 举报

HNAKXR 发表于 2016-2-13 00:01:03 | 显示全部楼层
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-6 20:58

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表