实训Part2-code

文件结构

 .
 ├── CircleBug
 │   ├── CircleBug.java
 │   └── CircleBugRunner.java
 ├── DancingBug
 │   ├── DancingBug.java
 │   └── DancingBugRunner.java
 ├── SpiralBug
 │   ├── SpiralBug.java
 │   └── SpiralBugRunner.java
 ├── ZBug
 │  ├── ZBug.java
 │  └── ZBugRunner.java
 └── Readme

编程练习

  1. Write a class CircleBug that is identical to BoxBug, except that in the act method the turn method is called once instead of twice. How is its behavior different from a BoxBug?
    BoxBug每次转向都是向右转90°。
    CircleBug每次转向都是向右转45°。
    代码设计:
    修改act()函数,减少一次turn()

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

    BoxBug转向前后对比:
    在这里插入图片描述
    在这里插入图片描述
    CircleBug转向前后对比:
    在这里插入图片描述
    在这里插入图片描述

  2. Write a class SpiralBug that drops flowers in a spiral pattern. Hint: Imitate BoxBug, 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++; // 每一次转向都增加一个步长
        }
    }
    

    运行结果:
    在这里插入图片描述

  3. Write a class ZBug to implement bugs that move in a “Z” pattern, starting in the top left corner. After completing one “Z” pattern, a ZBug should stop moving. In any step, if a ZBug can’t move and is still attempting to complete its “Z” pattern, the ZBug 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 a ZBug 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++;
            	}
            }
        }
    }
    
    

    运行结果:
    在这里插入图片描述

  4. Write a class DancingBug that “dances” by making different turns before each move. The DancingBug 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.
    The DancingBugRunner class should create an array and pass it as aparameter to the DancingBug 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次):
    在这里插入图片描述
    在这里插入图片描述

  5. Study the code for the BoxBugRunner class. Summarize the steps you would use to add another BoxBug 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);
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值