如何获取对象列表中的下一个对象?

时间:2015-04-11 23:25:20

标签: java arrays dictionary collections iteration

我在我的应用程序中使用Map来存储虚构游戏地图中的每个位置。当前的实现允许我获取起始节点,即Tiberius

但是我不确定如何相对于玩家所在的当前位置来到地图上的下一个location。例如,当我创建地图时,默认情况下我处于“Tiberius”,所以我想知道如何创建一个迭代到下一个位置的方法,这将是“沙漠”,同时后续调用{{1}将玩家带到“耶路撒冷”。

如下图所示,图表已连线,每个位置都有子位置。

从快速搜索中我看到列表可以通过ID中的字段进行迭代,例如get element by ID ,但在此实现中,我需要获取相对于当前位置的下一个位置。

有谁知道如何实施这种方法?

这就是我在主课程中设置地图的方式:

move()

这是用于为位置建模的Location类:

public class Main implements Commands{


    public static void main(String[] args)  {

        //Boolean to signify game state 
        boolean gameNotOver = true;

        //main game loop
        while (gameNotOver) {

            GameMap playerMap = new GameMap();
            playerMap.getStartNode();

            Main mainObj = new Main();

            //Take in user commands here
            //and parse commands
            String input = Input.getInput();
            if (input.equals("description")) {
                System.out.println("Description: " );
            } else if (input.equals("move")) {
                System.out.println("Moving to the next location in map.. " );
                mainObj.move();     
        }

        //Game over 
        System.out.println("Game Over!");
        System.exit(0);
    }

    //Game player commands inherited from GameCharacterInterface:

    public String move() {
         //want to create a method here that will call the next 
         //location based on the current location that the player is in.
        // TODO Auto-generated method stub
        return null;
    }

}

这是显示每个位置和getStartNode()的GameMap:

public class Location {

    private Location[] location;

    private int id;

    private String description;

    private String weight;

    private String name;

    private Item item;

    private Exit[] exit;

    private boolean visited = false;
    private boolean goalLocation;
    private int approximateDistanceFromGoal = 0;
    private Location parent;

    private Map<Location, Integer> children = new HashMap<Location, Integer>();

    public Location() {
        super();
    }

    public Location(String name){
        this.name = name;
    }

    public Location(String name, int goalDistance){
        this.name = name;
        this.approximateDistanceFromGoal = goalDistance;
    }



    public Location[] children(){
        return (Location[]) children.keySet().toArray(new Location[children.size()]);
    }

    public int getDistance(Location loc){
        if(children.get(loc) == null) System.out.println(this.name + ": " + loc.getName());
        return children.get(loc);
    }


    public int getChildLocationCount(){
        return children.size();
    }

    public void addChildLocation(Location child, int distance){
        children.put(child, distance);
    }

    public boolean isLeaf(){
        if (children.size() > 0){
            return false;
        }else{
            return true;
        }
    }


    public void removeChild(Location child){
        children.remove(child);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDescription ()
    {
        return description;
    }

    public void setDescription (String description)
    {
        this.description = description;
    }


    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    public Exit[] getExit() {
        return exit;
    }

    public void setExit(Exit[] exit) {
        this.exit = exit;
    }


    public Location[] getLocation() {
        return location;
    }

    public void setLocation(Location[] location) {
        this.location = location;
    }

    public boolean isVisited() {
        return visited;
    }

    public void setVisited(boolean visited) {
        this.visited = visited;
    }

    public boolean isGoalLocation() {
        return goalLocation;
    }

    public void setGoalLocation(boolean goalLocation) {
        this.goalLocation = goalLocation;
    }

    public int getApproximateDistanceFromGoal() {
        return approximateDistanceFromGoal;
    }

    public void setApproximateDistanceFromGoal(int approximateDistanceFromGoal) {
        this.approximateDistanceFromGoal = approximateDistanceFromGoal;
    }

    public Location getParent() {
        return parent;
    }

    public void setParent(Location parent) {
        this.parent = parent;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }


    @Override
    public String toString() {
        return "Location [location=" + Arrays.toString(location) + ", id=" + id
                + ", description=" + description + ", name=" + name + ", item="
                + item + ", exit=" + Arrays.toString(exit) + "]";
    }


}

2 个答案:

答案 0 :(得分:1)

Brian,从位置摆脱这个字段:

Map<Location,Integer> children;

将其替换为两个字段。

Location next;
int goalDistance;

然后,在您的游戏控制器中,保持对角色当前位置的引用。

例如:

  

位置currentLoc = tiberius;

然后,每次调用move()时,只需说

  

currentLoc = currentLoc.next;

答案 1 :(得分:1)

我认为你说你想要一个名为next()的Location类的方法,它将返回下一个位置。那么为什么不这样做?

public Location next() {
    Location nextLocation = null;
    for (Location child : children.keySet()) {
        nextLocation = child;
        break;
    }
    return nextLocation;
}

这是基于你的断言,当然,儿童地图中只会有一个条目。如果将来发生这种变化,您可以添加一个测试(此时设计未知),即孩子是您想要的那个:

public Location next() {
    Location nextLocation = null;
    for (Location child : children.keySet()) {
        if (isCorrectChild(child)) {
            nextLocation = child;
            break;
        }
    }
    return nextLocation;
}