码迷,mamicode.com
首页 > 其他好文 > 详细

Bug Variations

时间:2020-10-21 20:28:49      阅读:21      评论:0      收藏:0      [点我收藏+]

标签:方向   why   ted   转变   after   call   lock   size   eve   

  1. What is the role of the instance variable sideLength?

    的代码当中我们不难看出,该变量是控制该虫子周围的空间大小的,以提供给变量合理的移动空间。

    private int steps;
    private int sideLength;
    
  2. What is the role of the instance variable steps?

    这个也简单,既然上面的变量用来给空间大小,那么自然是用来计量步数的,也就是当前移动的方向已经走了多少步。

    move();
    steps++;
    
  3. Why is the turn method called twice when steps becomes equal to sideLength?

    这个也好理解,我们让虫子一直走,直到走到的时候需要转向,也就是当我们的step等于sideLength的时候,那么这个时候它的转向操作最少需要转动90度才能实现向着另外一边走,但是很明显我们的turn()函数只是一次转动45度,所以需要执行两次。

    	Location.HALF_RIGHT
    	//int info.gridworld.grid.Location.HALF_RIGHT : 45 [0x2d]
    	//The turn angle for turning 45 degrees to the right.
    
  4. Why can the move method be called in the BoxBug class when there is no move method in the BoxBug code?

    这个就更加好解释了,就像是C++项目的文件引用,我们在BoxBug里面虽然未曾定义和实现move()函数,但是我们引用了定义以及实现它的文件:

    import info.gridworld.actor.Bug;
    

    所以我们可以调用该函数。

  5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not?

    一旦一个被构造以后,它的四周饭团大小就不会被改变了,是被定义好了的,不能被用户代码改变。bug.gif定义了它的大小。

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

    会改变方向的,当它碰到另外一个障碍的时候,可能是另一个虫子,也可能是石头,那么它就会改变自己的运动轨迹。这个是Canmove()函数跟move()函数控制的。

  7. When will the value of steps be zero?

    这个取决于我们的act()函数,只要我们的虫子遇到障碍物转变了方向,那么它的就会置0

        public void act(){
            if (steps < sideLength && canMove()){
                move();
                steps++;
            }
            else{
                turn();
                turn();
                steps = 0;
            }
        }
    

Bug Variations

标签:方向   why   ted   转变   after   call   lock   size   eve   

原文地址:https://www.cnblogs.com/marvin-Hua-manlou/p/13849513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!