有点搞砸了间距哈哈,但bfs到最后一个方法的方法属于LyannaMormont类。这是我们教授给出的机器问题。基本上游戏中有英雄,小兵,消耗品和装备。我们的教授为我们提供了一些包含我们需要的方法的javadoc。可定位的对象可以是这四个中的任何一个(英雄,蠕变,消耗品,设备)。我基本上可以获取一个可定位的x和y位置,通过将其设置为目标属性然后使用bfs()获取路径然后移动角色(在这种情况下,我创建了go方法,它具有moveUp(),moveDown(),moveRight(),moveLeft()方法,这些方法包含在javadocs中,并且完全符合它们的名称。 action方法包含角色所做的所有事情。我的问题是,当我激活游戏的jar文件时,角色不会移动。代码有问题吗?如果有,有人可以告诉我在哪里?我现在基本上坚持这个问题3天了。任何帮助/建议将非常感激。谢谢:)
注意: WorldState w = getWorld();返回我的角色所在世界的副本.w.isPassable(int x,int y)如果我可以遍历(x,y)则返回true,否则返回false。
import com.th.*;
import java.util.*;
public class LyannaMormont extends HeroBot{
ArrayList<Node> way = new ArrayList<Node>();
private Locatable destination;
private int count = 0;
private boolean where = false;
private final int counter = 0;
public class Node implements Comparable<Node>{
private int xPosition;
private int yPosition;
private Node parentNode;
private double minDist;
public Node(int x, int y, Node p, double n){
this.xPosition = x;
this.yPosition = y;
this.parentNode = p;
this.minDist = n;
}
public int getXPosition(){
return this.xPosition;
}
public int getYPosition(){
return this.yPosition;
}
public void setXPosition(int x){
this.xPosition = x;
}
public void setYPosition(int y){
this.yPosition = y;
}
public Node getParentNode(){
return this.parentNode;
}
public void setParentNode(Node p){
this.parentNode = p;
}
public double getMinDist(){
return this.minDist;
}
public void setMinDist(double m){
this.minDist = m;
}
public int compareTo(Node otherNode){
return Double.compare(this.minDist, otherNode.getMinDist());
}
public boolean equals(Node node){
if((this.xPosition == node.getXPosition())&&(this.yPosition == node.getYPosition())){
return true;
}
else{
return false;
}
}
}
public void bfs(){
WorldState w = getWorld();
ArrayList<Node> found = new ArrayList<Node>();
Queue<Node> queue = new LinkedList<Node>();
Node source = new Node(this.getXLocation(), this.getYLocation(), null, 0.0);
queue.add(source);
found.add(source);
ArrayList<Node> path = new ArrayList<Node>();
outerloop:
while(queue.peek()!=null){
Node node = queue.poll();
if((node.getXPosition() == this.destination.getXLocation())&&(node.getYPosition() == this.destination.getYLocation())){
path.add(node);
queue.clear();
found.clear();
break outerloop;
}
int x = node.getXPosition();
int y = node.getYPosition();
if(w.isPassable(x-1, y)){
Node newNode = new Node(x-1, y, node, node.getMinDist() + 1.0);
if(!found.contains(newNode)){
queue.add(newNode);
found.add(newNode);
}
}
if(w.isPassable(x, y-1)){
Node newNode = new Node(x, y-1, node, node.getMinDist() + 1.0);
if(!found.contains(newNode)){
queue.add(newNode);
found.add(newNode);
}
}
if(w.isPassable(x+1, y)){
Node newNode = new Node(x+1, y, node, node.getMinDist() + 1.0);
if(!found.contains(newNode)){
queue.add(newNode);
found.add(newNode);
}
}
if(w.isPassable(x, y+1)){
Node newNode = new Node(x, y+1, node, node.getMinDist() + 1.0);
if(!found.contains(newNode)){
queue.add(newNode);
found.add(newNode);
}
}
}
Node goal = path.get(0);
this.way = new ArrayList<Node>();
while(goal.getParentNode()!=null){
this.way.add(0, goal);
goal = goal.getParentNode();
}
if (goal.getParentNode() == null){
this.way.add(0, goal);
}
Collections.sort(this.way);
this.destination = null;
this.where = false;
}
答案 0 :(得分:0)
除非您有progress
变量的某些特殊原因,否则您希望完全取消它。当你不想要时它会消耗Node
。基本上你想测试你是否在每次迭代时达到你的目标,如果你已经找到它就会中断。否则你就分出来了。
outerloop:
while(queue.peek()!=null){
Node node = queue.poll();
if((node.getXPosition() == this.destination.getXLocation())&&(node.getYPosition() == this.destination.getYLocation())){
path.add(node);
queue.clear();
found.clear();
break outerloop;
}
int x = node.getXPosition();
int y = node.getYPosition();
if(w.isPassable(x-1, y)){
Node newNode = new Node(x-1, y, node, node.getMinDist() + 1.0);
if(!found.contains(newNode)){
queue.add(newNode);
found.add(newNode);
}
}
if(w.isPassable(x, y-1)){
Node newNode = new Node(x, y-1, node, node.getMinDist() + 1.0);
if(!found.contains(newNode)){
queue.add(newNode);
found.add(newNode);
}
}
if(w.isPassable(x+1, y)){
Node newNode = new Node(x+1, y, node, node.getMinDist() + 1.0);
if(!found.contains(newNode)){
queue.add(newNode);
found.add(newNode);
}
}
if(w.isPassable(x, y+1)){
Node newNode = new Node(x, y+1, node, node.getMinDist() + 1.0);
if(!found.contains(newNode)){
queue.add(newNode);
found.add(newNode);
}
}
}