GridWorld将鲜花添加到随机位置

时间:2014-05-18 00:57:00

标签: java actor gridworld

我正试图做一个'#34; GrowingFlower"只是在随机位置添加鲜花。添加的鲜花量基于随机数。当我在花了6次之后运行花朵时,它会像它应该的那样死亡,但它从不添加任何花朵,所以我需要你的帮助来解决这个问题。提前谢谢。

以下是" GrowingFlower"

的代码
package projects.flowers;

import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Flower;

public class GrowingFlower extends Flower {

private int Growing = (int) (Math.random() * 4);
private int age = 0;
ActorWorld world = new ActorWorld();



public void act(){
    Growing = (int) (Math.random() * 4);
    age++;
    if (age == 1){
        switch(Growing){
        case 0:
            removeSelfFromGrid();
        case 1:
            world.add(new GrowingFlower());
        case 2:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        case 3:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        case 4:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        }
    }
    else if (age == 2){
        switch(Growing){
        case 0:
            removeSelfFromGrid();
        case 1:
            world.add(new GrowingFlower());
        case 2:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        case 3:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        case 4:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        }
    } else if(age == 3)  {
        switch(Growing) {
        case 0:
            removeSelfFromGrid();
        case 1:
            world.add(new GrowingFlower());
        case 2:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        case 3:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        case 4:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());

        }
    } else if (age == 4){
        switch(Growing) {

        case 0:
            removeSelfFromGrid();
        case 1:
            world.add(new GrowingFlower());
        case 2:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        case 3:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        case 4:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());

        }

    } else if (age == 5){
        switch(Growing) {

        case 0:
            removeSelfFromGrid();
        case 1:
            world.add(new GrowingFlower());

        case 2:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        case 3:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
        case 4:
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());
            world.add(new GrowingFlower());

        }

    } else if (age == 6){
        removeSelfFromGrid();
    }



}

}

这是我的跑步者

package projects.firstProject;



import projects.flowers.GrowingFlower;
import info.gridworld.actor.ActorWorld;

public class FlowerRunner
{
    public static void main(String[] args)
    {
    ActorWorld world = new ActorWorld();
    world.add(new GrowingFlower());
    world.show();
    }
}

1 个答案:

答案 0 :(得分:1)

我对这个“GridWorld”框架并不熟悉,所以可能不是一个可以接受的答案,但是对于评论来说太长了,并且......可能会有所帮助:

认为问题是您在GrowingFlower个实例中创建了一个新世界:

public class GrowingFlower extends Flower {
    ...
    ActorWorld world = new ActorWorld();

稍后,在act方法中,您要向此世界添加新的GrowingFlower个实例,但到您创建(并显示!)的世界中main方法。

因此解决方案可能会更改代码,如下所示:

1。更改GrowingFlower课程的构造函数,以接受应添加新花的“世界”:

public class GrowingFlower extends Flower {

    private int Growing = (int) (Math.random() * 4);
    private int age = 0;

    ActorWorld world; // Don't create a new world here!

    // Instead, use the world that is passed in 
    // in the constructor:
    GrowingFlower(ActorWorld world)
    {
        this.world = world;
    }

    ...

2。从主要方法传递给您的第一个GrowingFlower

public static void main(String[] args)
{
    ActorWorld world = new ActorWorld();
    world.add(new GrowingFlower(world)); // Pass it in here!
    world.show();
}

除此之外,你应该稍微重构一下act方法:

public void act(){
    Growing = (int) (Math.random() * 4);
    age++;
    if (age < 6){
        handleGrowing();
    }
    else {
        removeSelfFromGrid();
    }
}

private void handleGrowing()
{
    switch(Growing){
    case 0:
        removeSelfFromGrid();
    case 1:
        world.add(new GrowingFlower());
    case 2:
        world.add(new GrowingFlower());
        world.add(new GrowingFlower());
    case 3:
        world.add(new GrowingFlower());
        world.add(new GrowingFlower());
        world.add(new GrowingFlower());
    case 4:
        world.add(new GrowingFlower());
        world.add(new GrowingFlower());
        world.add(new GrowingFlower());
        world.add(new GrowingFlower());
    }
}

(据我所知,这应该仍然有相同的行为)