如何在 10x10 网格中创建 Z 形状

问题描述

ZedBug 在网格上制作字母“Z”。 ZedBug 构造函数接收一个 表示“Z”大小的整数参数大小。一旦ZedBug 到达“Z”的末端,它应该旋转 180 度并返回到“Z” 另一个方向,以便随着程序继续执行,ZedBug 是 总是刷新“Z”中的花。

目前,我在一个 10x10 的网格上有一个错误,它能够创建一串花。但是,当虫子完成 Z 形状的创建后,虫子就会停止,而不是回到原来的轨迹,然后再返回。

The bug creates a Z shape.

这是创建 Z 形状的错误代码

import info.gridworld.actor.Bug;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Location;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Grid;
import java.awt.*;
public class ZedBug extends Bug {
    private int steps;
    private int sideLength;
    private int totalSteps; // counts the total amount of steps
    public ZedBug(int size) {
        super(Color.PINK); // the color of the bug
        sideLength = size; // 
        // size is the length of the sides of the Z. 
        //In this case size = 4. However,size needs to be able to be anything.
    }
    public void act() {
        if (canMove() && steps < sideLength && getDirection() != 0) {
            move();
            steps++;
            totalSteps++;
            // move allows the bug to move one space.            
        } else if (totalSteps == 12) {
            turn();
            turn();
            turn();
            turn();
            //turns the bug back around 180 ( each turn is 45* )
        } else if (canMove() && steps < sideLength && getDirection() == 0) {
            turn();
            turn();
            steps = 0;
            move();
            steps++;
            totalSteps++;

        } else if (getDirection() != 0) {
            turn();
            turn();
            turn();
            steps = 0;
            // allows for the bug to make the diagonal movement
        }


    }
} 

> ```

I cannot change the canMove() method to be able to avoid flowers ( what the bug drops )

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)