我正在尝试创建一个程序,如果这些位置为空,我将不知道如何向世界添加另一个对象,将另一个花对象放置到原始位置的东北南和西。这是我的代码
import info.gridworld.actor.Flower;
import info.gridworld.grid.Location;
import java.util.ArrayList;
public class Weed extends Flower{
static boolean N;
static boolean E;
static boolean S;
static boolean W;
static boolean done = true;
static Location n; //new Location(this.getLocation().getRow()-1,this.getLocation().getCol());
static Location e; //= new Location(this.getLocation().getRow(),this.getLocation().getCol()+1);
static Location s; //= new Location(this.getLocation().getRow()+1,this.getLocation().getCol());
static Location w; //= new Location(this.getLocation().getRow(),this.getLocation().getCol()-1);
public void act(){
ArrayList<Location> EL = this.getGrid().getEmptyAdjacentLocations(getLocation());
checkDirection(EL);
}
public void checkDirection(ArrayList<Location> el){
n = new Location(this.getLocation().getRow()-1,this.getLocation().getCol());
e = new Location(this.getLocation().getRow(),this.getLocation().getCol()+1);
s = new Location(this.getLocation().getRow()+1,this.getLocation().getCol());
w = new Location(this.getLocation().getRow(),this.getLocation().getCol()-1);
for(int x=0; x<el.size(); x++){
System.out.println("el"+el.get(x));
if(el.get(x).equals(n)){
N = false;
}
else if(!el.get(x).equals(n)){
N = true;
}
else if(el.get(x).equals(e)){
E = false;
}
else if(!el.get(x).equals(e)){
E = true;
}
else if(el.get(x).equals(s)){
S = false;
}
else if(!el.get(x).equals(s)){
S = true;
}
else if(el.get(x).equals(w)){
W = false;
}
else if(!el.get(x).equals(w)){
W = true;
}
}
if(N==false&&W==false&&E==false&&S==false) done = true;
System.out.println(N);
System.out.println(E);
System.out.println(S);
System.out.println(W);
System.out.println(n);
System.out.println(e);
System.out.println(s);
System.out.println(w);
}
}
import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Flower;
import info.gridworld.grid.Location;
public class WeedRunner extends Weed {
public static void main(String args[]){
ActorWorld world = new ActorWorld();
Weed we = new Weed();
world.add(new Location (5,4), we);
world.show();
while(done == false){
if(N==false)world.add(new Location(n.getRow(),n.getCol()),we);
if(E==false)world.add(new Location(e.getRow(),e.getCol()),we);
if(S==false)world.add(new Location(s.getRow(),s.getCol()),we);
if(W==false)world.add(new Location(w.getRow(),w.getCol()),we);
}
}
}
答案 0 :(得分:0)
el.get(x).equals(n)和!el.get(x).equals(n)与布尔表达式相反。至少其中一个永远是真的 这意味着您的if-elseifs长列表将始终停留在其中一个上。我不知道你认为你的代码在做什么,但我可以告诉你,E,S和W的值永远不会改变它们的默认值。