0

我正在尝试创建一个程序,如果这些位置是空的,则将另一个花对象放置到原始位置的东北南和西部,但我不明白如何向世界添加另一个对象。这是我的代码

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);

        }

    }
}
4

1 回答 1

0

el.get(x).equals(n) 和 !el.get(x).equals(n) 是相反的布尔表达式。至少其中一个总是正确的 这意味着你的长长的 if-elseifs 列表将总是停在其中一个上。我不知道你认为你的代码在做什么,但我可以告诉你,E、S 和 W 的值永远不会改变它们的默认值。

于 2013-03-21T01:28:21.957 回答