Part3

Part3

Set 3
  1. loc1.getRow();
  2. false

  3. (4,4)

  4. 135度

  5. getAdjacentLocation方法返回沿着给定方向旋转之后最接近的相邻cell的坐标(行、列)位置

Set 4
  1. Grid g;
    int using = g.getOccupiedLocations().size();该方法返回grid对象中所有被占用的位置数量。
    int number = g.getNumRows()*g.getNumCols()计算grid中所有的位置数量。
    int empty = number-using;即可获得所有未被占用的位置数量。
    
  2. Grid g;
    g.isValid(new location(10,10));如果在grid中,则返回true,否则返回false
  3. Grid是一个接口,在java中接口声明的方法可以由其它类继承并实现,Grid的方法实现可以在AbstractGrid 、 BoundeGrid、UnboundeGrid 类中找到。BoundeGrid、UnboundeGrid 类继承了AbstractGrid抽象类,并且实现了AbstractGrid类中未实现的所有方法。

  4. 在ArrayList返回返回多个对象会更好,因为如果使用数组来返回对象,我们就需要更多的开销来记录对象的数量,位置等信息,声明不同大小的数组来进行记录,加大了工作量。而ArrayList的使用则不需要考虑大小的的问题。

Set 5
  1. 一个Actor拥有color、direction、location三个属性,还有一个和grid的接口。

  2. 初始化的color是blue,初始化的方向是north。

  3. Actor既有状态又有行为,是一个可以实例化的对象,而接口不允许被声明实例对象,也不允许实现具体的方法。

//(a):
//一个actor不能将它本身放入grid两次,如果放入两次,会抛出IllegalStateException异常。
//example:修改BugRunner.java文件
public class BugRunner{
    public static void main(String[] args){
        ActorWorld w = new ActorWorld();
        //create Bug
        Bug b = new Bug();
        //放入两次
        w.add(b);
        b.putSelfInGrid(b.getGrid(),b.getLocation());
        w.show();
    }
}
//(b)
//一个已经被移除grid的actor不能被再次移出
//example:修改BugRunner.java文件
public class BugRunner{
    public static void main(String[] args){
        ActorWorld w = new ActorWorld();
        //create Bug
        Bug b = new Bug();
        //放入两次
        w.add(b);
        w.show();
        b.removeSelfFromGrid();
        b.removeSelfFromGrid();
    }
}
//(c)
//可以,一个actor如果被移出grid,它可以再次将自己放回grid中
//example:修改BugRunner.java文件

public class BugRunner{
    public static void main(String[] args){
        ActorWorld w = new ActorWorld();
        //create Bug
        Bug b = new Bug();
        Bug temp = b.getGrid();
        //放入两次
        w.add(b);
        w.show();
        b.removeSelfFromGrid();
        b.putSelfInGrid(temp,new Location(3,3));
    }
}
  1. 通过两种途径实现

    setDirection(getDirection()+Location.RIGHT);
    setDirection(getDirection()+90);
    
Set 6
  1. 在GridWorld/framework/info/gridworld/actor/Bug.java文件中

  2. 同上

    public boolean canMove()
        {
            Grid<Actor> gr = getGrid();
            if (gr == null)
                return false;
            Location loc = getLocation();
            Location next = loc.getAdjacentLocation(getDirection());
            //下面的if语句保证了Bug不会走出边界。
            if (!gr.isValid(next))
                return false;
            Actor neighbor = gr.get(next);
            //返回语句的判断条件保证Bug不会碰撞到rock
            return (neighbor == null) || (neighbor instanceof Flower);
            // ok to move into empty location or onto flower
            // not ok to move onto any other actor
        }
    }
  3. 在GridWorld/framework/info/gridworld/grid/Grid.java文件中

    boolean isValid(Location loc);
    E get(Location loc);
    //上面两个方法被canMove方法调用,因为这两个方法确定Bug移动到的下一个位置是正确的(无其它actor,未越过grid的边界)。
  4. 在GridWorld/framework/info/gridworld/grid/Location.java

    public Location getAdjacentLocation(int direction);
    //该方法由Bug调用来查找下一个可能的位置。
  5. 在GridWorld/framework/info/gridworld/actor/Actor.java

     public int getDirection();
     public Grid<Actor> getGrid();
     public Location getLocation();
    //以上三个方法由canMove方法继承,确定一个Actor的location,direction,grid
  6. 会将自己移出grid。

    if (gr.isValid(next))
        moveTo(next);
    else
        removeSelfFromGrid();
  7. loc作为变量保存一个Bug移动前的location,也就是行和列的位置,该变量可以用来在Bug移动之前的位置插入flower对象。

  8. public class Flower extends Actor可以看到Flower类继承了Actor类,并可以调用Actor的getColor方法,获得Actor实例对应的颜色,因此Bug和flower的颜色是相同的。

  9. 不会,如果只调用removeSelfFromGrid方法,不会在之前的位置留下花。当在Bug类的move方法中调用该removeSelfFromGrid方法,会在之前位置留下一朵花。在Actor.java中可以看到该方法的具体实现:

    //在Actor.java文件,不会
    public void removeSelfFromGrid()
        {
            if (grid == null)
                throw new IllegalStateException(
                        "This actor is not contained in a grid.");
            if (grid.get(location) != this)
                throw new IllegalStateException(
                        "The grid contains a different actor at location "
                                + location + ".");
    
            grid.remove(location);
            grid = null;
            location = null;
        }
    //在Bug.java文件,会
    if (gr.isValid(next))
        moveTo(next);
    else
        removeSelfFromGrid();
    Flower flower = new Flower(getColor());
    flower.putSelfInGrid(gr, loc);
  10. 在Bug.java文件:

    //loc未Bug移动之前的location
    Flower flower = new Flower(getColor());
    flower.putSelfInGrid(gr, loc);
  11. 调用一次turn方法会旋转45度,需要连续调用4次。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值