文件结构
.
├── CircleBug
│ ├── CircleBug.java
│ └── CircleBugRunner.java
├── DancingBug
│ ├── DancingBug.java
│ └── DancingBugRunner.java
├── SpiralBug
│ ├── SpiralBug.java
│ └── SpiralBugRunner.java
├── ZBug
│ ├── ZBug.java
│ └── ZBugRunner.java
└── Readme
编程练习
-
Write a class
CircleBug
that is identical toBoxBug
, except that in theact
method theturn
method is called once instead of twice. How is its behavior different from aBoxBug
?
BoxBug每次转向都是向右转90°。
CircleBug每次转向都是向右转45°。
代码设计:
修改act()
函数,减少一次turn()
。public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { // turn(); turn(); steps = 0; } }
BoxBug转向前后对比:
CircleBug转向前后对比:
-
Write a class
SpiralBug
that drops flowers in a spiral pattern. Hint: ImitateBoxBug
, but adjust the side length when the bug turns. You may want to change the world to an UnboundedGrid to see the spiral pattern more clearly.
代码设计:
修改act()
函数,增加一条每次转向则增加一个步长的语句。public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); turn(); steps = 0; sideLength++; // 每一次转向都增加一个步长 } }
运行结果:
-
Write a class
ZBug
to implement bugs that move in a “Z” pattern, starting in the top left corner. After completing one “Z” pattern, aZBug
should stop moving. In any step, if aZBug
can’t move and is still attempting to complete its “Z” pattern, theZBug
does not move and should not turn to start a new side. Supply the length of the “Z” as a parameter in the constructor. The following image shows a “Z” pattern of length 4. Hint: Notice that aZBug
needs to be facing east before beginning its “Z” pattern.
代码设计:import info.gridworld.actor.Bug; import info.gridworld.grid.Location; /** * A <code>ZBug</code> traces out a square "box" of a given size. <br /> * The implementation of this class is testable on the AP CS A and AB exams. */ public class ZBug extends Bug { private int steps; private int sideLength; private int count; // 计数Bug走过了多少段,一共要走3段 /** * Constructs a box bug that traces a square of a given side length * @param length the side length */ public ZBug(int length) { setDirection(Location.EAST); steps = 0; sideLength = length; count = 0; } /** * Moves to the next location of the square. */ public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else if(steps == sideLength) // 步数到了则根据当前在三段中的哪一段来进行转向,若步数未到且遇到障碍则停止运动 { if(count == 0){ setDirection(Location.SOUTHWEST); steps = 0; count++; } else if (count == 1){ setDirection(Location.EAST); steps = 0; count++; } } } }
运行结果:
-
Write a class
DancingBug
that “dances” by making different turns before each move. TheDancingBug
constructor has an integer array as parameter. The integer entries in the array represent how many times the bug turns before it moves. For example, an array entry of 5 represents a turn of 225 degrees (recall one turn is 45 degrees). When a dancing bug acts, it should turn the number of times given by the current array entry, then act like a Bug. In the next move, it should use the next entry in the array. After carrying out the last turn in the array, it should start again with the initial array value so that the dancing bug continually repeats the same turning pattern.
TheDancingBugRunner
class should create an array and pass it as aparameter to theDancingBug
constructor.
代码设计:import info.gridworld.actor.Bug; //import info.gridworld.grid.Location; public class DancingBug extends Bug { private int[] danceArray; // 转向次数数组 private int index; // 记录此时要用数组中的第几个元素 public DancingBug(int[] danceArr) { danceArray = danceArr; index = 0; } /** * Moves if it can move, turns otherwise. */ public void act() { if (canMove()) move(); else{ if(index == danceArray.length) index = 0; for(int i = 0; i < danceArray[index]; i++){ // 根据数组中的元素来确定转向次数 turn(); } index++; } } }
运行结果:
//转向次数数组实例 int[] danceArr = {1,2,3,4,5,6};
第一次转向(转1次):
第二次转向(转2次):
第三次转向(转3次):
第四次转向(转4次):
第五次转向(转5次):
第六次转向(转6次):
回到数组起点(转1次):
-
Study the code for the
BoxBugRunner
class. Summarize the steps you would use to add anotherBoxBug
actor to the grid./** * BoxBugRunner.java */ import info.gridworld.actor.ActorWorld; import info.gridworld.grid.Location; import java.awt.Color; /** * This class runs a world that contains box bugs. * This class is not tested on the AP CS A and AB exams. */ public class BoxBugRunner { public static void main(String[] args) { ActorWorld world = new ActorWorld(); BoxBug alice = new BoxBug(6); alice.setColor(Color.ORANGE); BoxBug bob = new BoxBug(3); world.add(new Location(7, 8), alice); world.add(new Location(5, 5), bob); world.show(); } }
步骤:
创建一个BoxBug对象并初始化:BoxBug car = new BoxBug(7);
将对象加入到网格中:
world.add(new Location(8, 8), car);