【问题标题】:How to add an Object to Grid world based on a Boolean expression如何基于布尔表达式将对象添加到网格世界
【发布时间】:2013-03-21 01:02:26
【问题描述】:

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

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

        }

    }
}

【问题讨论】:

    标签: java object boolean gridworld


    【解决方案1】:

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

    【讨论】:

    • 这解释了我遇到的一个问题,感谢您的反馈!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2020-03-12
    • 2023-03-04
    • 2021-09-18
    相关资源
    最近更新 更多