0

我将如何检查错误类前面的两个空格是否是一个没有任何东西并且没有超出边界的空白点?现在这就是我所拥有的

public void act()
{
  if(canMove())
  {
     Location loc = getLocation();
     Location nextLocation = loc.getAdjacentLocation(getDirection());
     nextLocation = nextLocation.getAdjacentLocation(getDirection());
     if (nextLocation == null)
     {
       move();
       move();
     }

  }
}

这似乎不起作用,因为该错误什么都不做。

4

2 回答 2

0

你有两个问题。

  1. 不检查下一个位置是否在您知道的网格中
  2. 没有移动到您找到的位置。Move() 只是在当前方向上移动,不管它是否在网格中。所以调用它两次向前移动了两次,但没有使用您在网格中发现的位置。

尝试将您的代码更改为:

        public void act()
        {
           if(canMove())
           {
               Location loc = getLocation();
               Location nextLocation = loc.getAdjacentLocation(getDirection());
               nextLocation = nextLocation.getAdjacentLocation(getDirection());
               Grid<Actor> gr = getGrid(); 

               if (gr.isValid(nextLocation))
               {
                   moveTo(nextLocation);
               }
               else
                   move(); //This could be move() so you only move forward one 
                          //space or turn() to turn 

           }
        }
于 2014-04-04T21:26:54.807 回答
0

你可能的意思是if(nextLocation != null)。我们不希望虫子从板上跳下来(到一个null空间)。这是我所记得的GridWorld;我手头没有这个程序。

于 2014-04-01T00:49:27.177 回答