Java 如何获取对象列表中的下一个对象?
我在应用程序中使用地图将每个位置存储在虚构的游戏地图中。当前的实现允许我获取开始节点,即Java 如何获取对象列表中的下一个对象?,java,arrays,dictionary,collections,iteration,Java,Arrays,Dictionary,Collections,Iteration,我在应用程序中使用地图将每个位置存储在虚构的游戏地图中。当前的实现允许我获取开始节点,即Tiberius 但我不确定如何才能到达地图上相对于玩家当前位置的下一个位置。例如,当我创建地图时,默认情况下我在“Tiberius”,因此我想知道如何创建一个方法来迭代到下一个位置,即“沙漠”,而随后调用move()会将玩家带到“耶路撒冷” 该图如下图所示连接起来,每个位置都有一个子位置 通过快速搜索,我发现可以通过ID中的字段对列表进行迭代,就像这样,但是在这个实现中,我需要获得相对于当前位置的下一个位置
Tiberius
但我不确定如何才能到达地图上相对于玩家当前位置的下一个位置。例如,当我创建地图时,默认情况下我在“Tiberius”,因此我想知道如何创建一个方法来迭代到下一个位置,即“沙漠”,而随后调用move()
会将玩家带到“耶路撒冷”
该图如下图所示连接起来,每个位置都有一个子位置
通过快速搜索,我发现可以通过ID中的字段对列表进行迭代,就像这样,但是在这个实现中,我需要获得相对于当前位置的下一个位置
有人知道如何实现这个方法吗
这就是我目前在主课上设置地图的方式:
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;
}
}
这是用于为位置建模的位置类:
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) + "]";
}
}
Brian,从位置上清除这个字段:
Map<Location,Integer> children;
然后,在游戏控制器中,保留对角色当前位置的引用
例如:
位置currentLoc=提比略
然后,每次调用move()时,只需说
currentLoc=currentLoc.next
我想您是说希望Location类上有一个名为next()的方法返回下一个位置。那么为什么不这样做呢
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;
}
似乎每个位置都有多个“下一个”位置。那么,您如何知道下一个地点是要去的?您所说的“下一个”是什么意思?每个位置
都有一个子对象的地图,但您只能在每个位置
的地图中放置一个子对象。子映射是否可能有多个子映射?如果没有,我会删除地图,只需将子对象存储为位置
,并添加一个方法来返回子对象。在这个实现中,每个位置只有一个子对象,我需要将其保留为地图,因为我正在代码中的其他位置对其执行深度优先搜索。如果您想要一个包含多个子对象的子对象地图,但是顺序很重要(即添加子对象的顺序),请看。@satnam在这个应用程序中,每个位置只有一个下一个位置,所以我想知道如何创建一个方法,迭代到当前位置的子对象。然后当它在下一个位置时,迭代到该位置的子级,依此类推,直到到达目标位置。尝试了第一种方法,抛出了这个错误:“只能迭代一个数组或java.lang.iterable的实例”,这让我大吃一惊!显示你没有先尝试的结果。。。现在应该可以工作了。:-)好吧,我现在就来试试,不是100%地说这个应该怎么叫?您可以看到我是如何在上面的主类中创建图形的,我是否只是创建一个Location对象并调用它的下一个方法?如果我这样称呼它,下一个位置就不会打印出来,GameMap playerMap=new GameMap();playerMap.getStartNode();Location locObj=新位置();locObj.next()代码>您可能需要执行Location startNode=playerMap.getStartNode(),然后执行Location nextLocation=startNode.next()等等。除非你明确告诉它,否则不会打印出任何内容,比如System.out.println(nextLocation.getName())。所以我终于让它工作了,但它只会迭代到下一个位置,即沙漠,当我调用move时,它仍然会将沙漠作为下一个加载,尽管下一个应该是耶路撒冷,你知道为什么吗?这是目前的主要类:hastebin.com/ayadixudev.avrasm我认为next()没有将下一个位置从上一个位置移动后设置为当前位置代码>我得到提比略无法解析为一个变量,我猜上面的说法不正确?
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;
}