-1

我想用一个“汽车”演员和一个修复生成位置创建一个 Gridworld:

      package gridworld.blatt3;
    import gridworld.framework.actor.*;
    import gridworld.framework.grid.Grid;
    import gridworld.framework.grid.Location;
    
    public class Traffic {
    
        public static void main(String[] args) {
            TrafficWorld world = new TrafficWorld();
            System.out.println("Anwendung startet...");
            Location loc1 = new Location(0,0);
            Grid.put(loc1, new Car());
            world.show();
        }
    }
    package gridworld.framework.actor;

import gridworld.framework.actor.bugs.Flower;
import gridworld.framework.grid.Grid;
import gridworld.framework.grid.Location;

import java.awt.*;

public class Car extends AbstractActor {
    //Schrittgröße//
    int speed=5;
    int sideLenght=50;
    int step = 0;


    //Richtung //
    public Car () {
    setDirection(Location.EAST);
    setColor(Color.BLUE);
    }

    public void act () {
       move();
    }

    public void move() {
        int speedCount=0;
        //Springt immer um 'speed'-Schritte //
        do {
            step++;
        speedCount++;
            if (step < sideLenght) {
                System.out.println("Step: "+step);
                Location loc = getLocation();
                Location next = loc.getAdjacentLocation(getDirection());
                moveTo(next);
            }
            else {
                System.out.println("car drives off the grid");
                removeSelfFromGrid();
                break;
            }

        } while (speedCount!=speed);
    }

}



   package gridworld.framework.actor;

import gridworld.framework.grid.BoundedGrid;

public class TrafficWorld extends ActorWorld {
    private static final int rowSize = 1;
    private static final int colSize = 50;

    public TrafficWorld() {
        super(new BoundedGrid<Actor>(rowSize, colSize));
    }
}

如何为“汽车”定义特定的生成位置?例如,我希望 Actors 在网格的左上角生成,我创建了一个 Location 对象“loc1”以与 Grid.put() 一起使用,但出现错误:java: non-static method put(gridworld.framework .grid.Location,E) 不能从静态上下文中引用

我正在使用 openjdk-15.0.1

4

1 回答 1

0

您可以使用ActorWorld.add()带有Location参数的方法将参与者放置在特定位置:

添加

public void add(Location loc, Actor occupant)

在给定位置向这个世界添加一个演员。

在您的情况下,它将类似于:

world.add(new Location(0, 0), new Car());
于 2020-12-06T17:51:32.820 回答