Part2:Bug Variations

Answer the following questions on Matrix
Set 2 The source code for the BoxBug class can be found in the boxBug directory.

  1. What is the role of the instance variable sideLength?
    在本题中, sideLength这个变量在BoxBug这个类中表示限定每一次bug沿着某一方向走的最远步长(在不遇到障碍或者边界的情况下),在到达sideLength之后,Boxbug就要调转运动方向,如下则会turn()。
    举例如下:
 public void act()
    {
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
            turn();
            turn();
            steps = 0;
        }
    }
  1. What is the role of the instance variable steps?
    steps用于记录BoxBug这个类中任一个Boxbug在当前所处的这条边上已经移动的步数,如下steps在没有转向时增加1,转向后置为0
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
            turn();
            turn();
            steps = 0;
        }
  1. Why is the turn method called twice when steps becomes equal to sideLength?
    因为boxbug调用一次turn()只转45度,但是转向需要90度,,所以需要连续调用两次
    这是bug(GridWorldCode/framework/info/gridworld/actor/Bug.java)里的turn函数定义:
    /**
     * Turns the bug 45 degrees to the right without changing its location.
     */
    public void turn()
    {
        setDirection(getDirection() + Location.HALF_RIGHT);
    }
  1. Why can the move method be called in the BoxBug class when there is no move method in the BoxBug code?
    因为BoxBug继承了Bug,并且在BoxBug中并没有overwrite Bug的move()函数,所以BoxBug继承了Bug的move函数,所以可以在直线运动遇到障碍时选择调用move函数。
    这是bug(GridWorldCode/framework/info/gridworld/actor/Bug.java)里的move函数定义:
    /**
     * Moves the bug forward, putting a flower into the location it previously
     * occupied.
     */
    public void move()
    {
        Grid<Actor> gr = getGrid();
        if (gr == null)
            return;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (gr.isValid(next))
            moveTo(next);
        else
            removeSelfFromGrid();
        Flower flower = new Flower(getColor());
        flower.putSelfInGrid(gr, loc);
    }

  1. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not?
    当BoxBug创建之后,它移动形成的所有的正方形都会一样;因为sideLength在构造函数中已经确定,同时Boxbug类也未提供其他可用的方法来改变sideLength,所以Boxbug移动形成的所有的正方形大小无法都不能被改变了。
/**
     * Constructs a box bug that traces a square of a given side length
     * @param length the side length
     */
    public BoxBug(int length)
    {
        steps = 0;
        sideLength = length;
    }

  1. Can the path a BoxBug travels ever change? Why or why not?

不会,如果BoxBug 遇到障碍物他就会做出一些调整,所以我们可以设置一些不同的障碍物,这样移动路径也就会随之不同了
如下,它每走一步都要检测canMove(),如果我们防止障碍物就可以改变canMove的值了

        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
  1. When will the value of steps be zero?
    当转向后步长会变为0,还有刚开始初始化时也是0
    public BoxBug(int length)
    {
        steps = 0;
        sideLength = length;
    }

    /**
     * Moves to the next location of the square.
     */
    public void act()
    {
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
            turn();
            turn();
            steps = 0;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值