IF语句检查(不正常工作)

时间:2015-12-01 06:10:13

标签: java arrays loops if-statement boolean

randomEmpty()在n x n网格上返回一个空的随机坐标(Method有效)。 randomAdjacent()使用randomEmpty()在地图上选择EMPTY坐标。然后进行比较以查看该坐标是否具有非空的VALID相邻坐标。问题是randomAdjacent并不总是返回具有相邻NON-EMPTY空间的空间坐标。它将始终返回有效坐标,但不返回后者。我无法发现问题。有人可以帮我识别问题吗?

public int[] randomEmpty()
{
    Random r = new Random();
    int[] random = new int[2];
    int row = r.nextInt(array.length);
    int column = r.nextInt(array.length);
    while(!(isEmpty(row,column)))
    {
        row = r.nextInt(array.length);
        column = r.nextInt(array.length);
    }
    random[0] = row+1;
    random[1] = column+1;
    return random;        
}

public int[] randomAdjacent()
{
    int[] adjacentToX = new int[8];
    int[] adjacentToY = new int[8];
    int[] adjacentFrom = randomEmpty();
    int count;
    boolean isTrue = false;
    boolean oneAdjacentNotEmpty = false;

    while(!(oneAdjacentNotEmpty))
    {
        count = 0;

        if(validIndex(adjacentFrom,1,-1))
        {
            adjacentToX[count] = adjacentFrom[0]+1;
            adjacentToY[count] = adjacentFrom[1]-1;
            count++;
        }
        if(validIndex(adjacentFrom,0,-1))
        {               
            adjacentToX[count] = adjacentFrom[0];
            adjacentToY[count] = adjacentFrom[1]-1;
            count++;
        }
        if(validIndex(adjacentFrom,-1,-1))
        {          
            adjacentToX[count] = adjacentFrom[0]-1;
            adjacentToY[count] = adjacentFrom[1]-1;
            count++;
        }
        if(validIndex(adjacentFrom,-1,0))
        {        
            adjacentToX[count] = adjacentFrom[0]-1;
            adjacentToY[count] = adjacentFrom[1];
            count++;
        }
        if(validIndex(adjacentFrom,-1,1))
        {       
            adjacentToX[count] = adjacentFrom[0]-1;
            adjacentToY[count] = adjacentFrom[1]+1;
            count++;
        }
        if(validIndex(adjacentFrom,0,1))
        {         
            adjacentToX[count] = adjacentFrom[0];
            adjacentToY[count] = adjacentFrom[1]+1;
            count++;
        }
        if(validIndex(adjacentFrom,1,1))
        {         
            adjacentToX[count] = adjacentFrom[0]+1;
            adjacentToY[count] = adjacentFrom[1]+1;
            count++;
        }
        if(validIndex(adjacentFrom,1,0))
        {        
            adjacentToX[count] = adjacentFrom[0]+1;
            adjacentToY[count] = adjacentFrom[1];
            count++;
        }
        for(int i = 0; i < count; i++)
        {
            if(!(isEmpty(adjacentToX[i],adjacentToY[i])))  
            {
                oneAdjacentNotEmpty = true;
                isTrue = true;
            }
        }
        if(isTrue)
            break;
        else
            adjacentFrom = randomEmpty();           
    }
    return adjacentFrom;
}

public boolean validIndex(int[] a,int i, int j)
{
    try
    {
        Pebble aPebble = array[a[0]+i][a[1]+j];
        return true;
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
        return false;
    }
}
public void setCell(int xPos, int yPos, Pebble aPebble)
{
    array[xPos-1][yPos-1] = aPebble;
}

public Pebble getCell(int xPos, int yPos)
{
    return array[xPos-1][yPos-1];
}

JUNIT测试执行:

@Test
public void testRandomAdjacent() {
    final int size = 5;
    final Board board2 = new Board(size);
    board2.setCell(1, 1, Pebble.O);
    board2.setCell(5, 5, Pebble.O);
    int[] idx = board2.randomAdjacent();
    int x = idx[0];
    int y = idx[1];
    boolean empty = true;
    for (int i = x - 1; i <= x + 1; i++) {
        for (int j = y - 1; j <= y + 1; j++) {
            if ((i == x && j == y) || i < 1 || j < 1 || i > size || j > size) {
                continue;
            }
            if (board2.getCell(i, j) != Pebble.EMPTY)
                empty = false;
        }

    }
    assertFalse(empty);// NEVER gets SET TO FALSE
    assertEquals(Pebble.EMPTY, board2.getCell(x, y));
}

1 个答案:

答案 0 :(得分:1)

至于答案:为了便于阅读,我放弃了优化代码。我认为最有可能

if (board2.getCell(i, j) != Pebble.EMPTY)
            empty = false;

导致问题,因为getCell在基于1的坐标中运行,但i,j基于0。

你应该考虑整体的逻辑。我看到它的方式,你的代码可能永远不会终止,因为randomEmpty()可以在一段不确定的时间内一遍又一遍地返回相同的字段。

我冒昧地将if-if-if级联重新编码为实用方法更容易阅读:

public boolean hasNonEmptyNeighbor(int[] adjacentFrom) {
    for(int i = -1; i <= 1; ++i) {
      for(int j = -1; j <= 1; ++j) {
        if(validIndex(adjacentFrom, i, j) //Still inside the board
             &&                           // AND
           !isEmpty(adjacentFrom[0]+i     //not empty
                      ,adjacentFrom[1]+j)) {
          return true;
        }
      }
    }
    return false;
  }

鉴于我之前关于random()的评论并不是最好的选择,如果你需要覆盖整个董事会,你的主要检查(给我一个空的单元格与非空的邻居)可以这样重写:

  public void find() {
     List<Point> foundPoints = new ArrayList<Point>();
     for(int i = 0; i < Board.height; ++i) { //Assumes you have stored your height
       for(int j = 0; j < Board.width; ++j) { //and your width
         if(isEmpty(i, j) && hasNonEmptyNeighbor(new int[]{i,j})) {
            //Found one.  
            foundPoints.add(new Point(i, j));
         }
       }
     }
     //If you need to return a RANDOM empty field with non-empty neighbor
     //you could randomize over length of foundPoints here and select from that list.
   }