在世界边缘转身

时间:2018-09-28 22:51:14

标签: java

对于一个项目,我应该编写代码,让演员有机会左转或右转25%,然后再移动50%,然后还要避免超出范围。到目前为止,下面的代码是我所拥有的,但是我不确定什么是行不通的,因为我仍然无法确保actor的入站。谢谢

/**
 * Creates a new Food object.
 */
public Food()
{
    super();
}


//~ Methods ...............................................................

// ----------------------------------------------------------
/**
 * Take one step, either forward or after turning left or right.
 */
public void act()
{
    int x = Random.generator().nextInt(100);
    this.changeDirection();

    if (x < 25)
    {
        this.turn(LEFT);
    }
    else if ((25 < x) && ( x < 50))
    {
        this.turn(RIGHT);
    }
    else 
    {
        this.move();
    }
}

public void turnAround()
{
    this.turn(RIGHT);
    this.turn(RIGHT);
}

public void changeDirection()
{
    if ((this.getGridY() == this.getWorld().getHeight() ||
    this.getGridX() == this.getWorld().getWidth()))
    {
        this.turnAround();
        this.move();
    }
    else if ((this.getGridY() == 0 || this.getGridX() == 0))
    {
        this.turnAround();
        this.move();
    }
    else if ((this.getGridY() == this.getWorld().getHeight()) &&
    this.getGridX() == this.getWorld().getWidth())
    {
        this.turnAround();
        this.move();
    }
    else if ((this.getGridY() == 0) && (this.getGridX() == 0))
    {
        this.turnAround();
        this.move();
    }
}

0 个答案:

没有答案