Java字符比较无法正常工作

时间:2015-04-19 05:29:20

标签: java multidimensional-array

所以我在二维数组中移动。根据某些坐标处的字符,我可能想要也可能不想将对象添加到数组列表中。 ' R'是一个合法的运动' X'是一种非法运动。

以下代码检查它是否在网格的范围内,然后在创建对象并将其添加到我的数组之前检查合法移动。 if语句是这样的,因为它们需要按特定顺序打开。

 private static void OpenNodes()
 {
     if(currentNode.GetX() > 0 && currentNode.GetY() > 0 && currentNode.GetX() < rowsCols && currentNode.GetY() < rowsCols)
     {//check R
         if(CheckAccessibility(currentNode.GetX() + 1, currentNode.GetY()))
         {
             CreateNode(currentNode.GetX() + 1, currentNode.GetY(), 2, "R");
         }
         //check RD
         if(CheckAccessibility(currentNode.GetX() + 1, currentNode.GetY() + 1))
         {
             CreateNode(currentNode.GetX() + 1, currentNode.GetY() + 1, 2, "RD");
         }
         //check D
         if(CheckAccessibility(currentNode.GetX(), currentNode.GetY() + 1))
         {
             CreateNode(currentNode.GetX(), currentNode.GetY() + 1, 2, "D");
         }
         //check DL
         if(CheckAccessibility(currentNode.GetX() - 1, currentNode.GetY() + 1))
         {
             CreateNode(currentNode.GetX() - 1, currentNode.GetY() + 1, 2, "DL");
         }
         //check L
         if(CheckAccessibility(currentNode.GetX() - 1, currentNode.GetY()))
         {
             CreateNode(currentNode.GetX() - 1, currentNode.GetY(), 2, "L");
         }
         //check UL
         if(CheckAccessibility(currentNode.GetX() - 1, currentNode.GetY() - 1))
         {
             CreateNode(currentNode.GetX() - 1, currentNode.GetY() - 1, 2, "UL");
         }
         //check U
         if(CheckAccessibility(currentNode.GetX(), currentNode.GetY() - 1))
         {
             CreateNode(currentNode.GetX(), currentNode.GetY() - 1, 2, "U");
         }
         //check UR
         if(CheckAccessibility(currentNode.GetX() + 1, currentNode.GetY() - 1))
         {
             CreateNode(currentNode.GetX() + 1, currentNode.GetY() - 1, 2, "UR");
         }
     }
 }

当我像这样运行CheckAccessibility时:

     private static boolean CheckAccessibility(int varX, int varY)
 {
     if(grid[varX][varY] == 'X')
     {
         //return false;
     }
     return true;
 }

它完全返回我期望的

x :2 y: 1 movement: R type: X
x :2 y: 2 movement: RD type: X
x :1 y: 2 movement: D type: R
x :0 y: 2 movement: DL type: X
x :0 y: 1 movement: L type: R
x :0 y: 0 movement: UL type: R
x :1 y: 0 movement: U type: R
x :2 y: 0 movement: UR type: R

但是当我正确运行时

     private static boolean CheckAccessibility(int varX, int varY)
 {
     if(grid[varX][varY] == 'X')
     {
         return false;
     }
     return true;
 }

我明白了:

x :2 y: 1 movement: R type: X
x :0 y: 2 movement: DL type: X
x :0 y: 1 movement: L type: R
x :0 y: 0 movement: UL type: R
x :1 y: 0 movement: U type: R

我的网格看起来像这样

RRRXG
RSXXR
XRXXR
XRRRR
RRRRX

有人可以解释为什么无法正确识别字符,导致它创建X节点并删除R节点吗?

修改

我的创建节点功能如下所示。 (它有点不整齐)

     private static boolean CreateNode(int xCoordinate, int yCoordinate, int varCost, String movementDirection)
 {   
    boolean visited = false;

     if(closed.size() > 0)//check to see if we have ever previously been to this node
     {
         for(int i = 0; i < closed.size(); i++)
         {
             if(closed.get(i).GetX() == xCoordinate && closed.get(i).GetY() == yCoordinate)
             {
                 visited = true;
             }
         }
     }

     if(!visited)
     {
        if(algorithmType == 1) // if A* check to see if we are reopening a node already opened and if it is cheaper to visit this node from this new path
        {
            Node node = new Node(xCoordinate, yCoordinate, varCost, pathCost, grid[yCoordinate][xCoordinate], FindHeuristic(yCoordinate, xCoordinate), movementDirection);

            for(int i = 0; i < open.size(); i++)
            {
                if(open.get(i).GetX() == xCoordinate && open.get(i).GetY() == yCoordinate)
                {
                    if(node.GetF() < open.get(i).GetF())//if the new path is cheaper remove old node and add new one
                    {
                        open.remove(i);
                    }
                    if(node.GetF() >= open.get(i).GetF())//if new path is more expensive then do not add a node
                    {
                        return false;
                    }
                }
            }

            open.add(node);
        }
        else
        {
            Node node = new Node(xCoordinate, yCoordinate, varCost, pathCost, grid[yCoordinate][xCoordinate], FindHeuristic(yCoordinate, xCoordinate), movementDirection);
            open.add(node);
        }
     }
     return true;
 }

2 个答案:

答案 0 :(得分:0)

您的支票正在执行您认为正在做的事情。 CreateNode()会更改currentNode吗?仔细检查您传入的索引并确保它们符合您的期望。 ;)

答案 1 :(得分:0)

好的,所以我仍然不知道为什么,但似乎我的网格想要x和y值反转。我这样访问网格:

grid[varx][varY] == 'X'

当它想要这样的时候:

grid[varY][varX] == 'X'

它现在按预期运作。