gpt4 book ai didi

java - GridWorld 将花添加到随机位置

转载 作者:行者123 更新时间:2023-12-01 12:58:19 26 4
gpt4 key购买 nike

我正在尝试制作一个“GrowingFlower”,它只是将花朵添加到随机位置。添加的花朵数量基于随机数。当我在点击 Action 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();
}
}

最佳答案

我对这个“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. 将世界从 main 方法传递到您的第一个 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());
}
}

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

关于java - GridWorld 将花添加到随机位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23717582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com