检查形状是否适合空间

时间:2015-04-26 19:39:42

标签: java

人。对于方法public boolean shapeFitsAt(int row, int col, Shape shape),我通过了一半的测试用例而其余的都没有通过。我觉得我在代码中做错了,我错过了什么。例如,如果我有以下形式的String类型:

 aaa
 aa.

和String类型的空格:

  ......
  ......
  ......
  ......
  ......
  .....|

当我在row = 4col = 3的上方空格中放置一个形状时,它应如下所示:

  ......
  ......
  ......
  ..aaa.
  ..aa..
  .....|

意思是形状适合!每当形状的角色与'.'以外的空间角色碰撞时,该方法返回false,因为形状不适合!但是我的测试用例不合适,形状应该合适!有人可以帮忙吗?提前谢谢!

import java.util.*;

public class CreateSpace implements Space{

    private int height;
    private int width;
    private String layout;
    private char[][] space;
    private Shape originalShape;
    private ArrayList<CreateShape> shapes = new ArrayList<CreateShape>();

    public CreateSpace(int height, int width, char[][] space, String layout)
    {
        this.height = height;
        this.width = width;
        this.space = space;
        this.layout = layout;
    }



    public boolean isFilledAt(int row, int col)
    {

        if(space[row][col] == '.')
            return false;
        else if(row > height || row < height || col > width || col < width)
            return true;
        else            
            return true;
    }

    public boolean shapeFitsAt(int row, int col, Shape shape)
    {

        if(row < 0 || row >= height || col < 0 || col >= width)
            throw new FitItException("Oops! Out of bounds in CreateSpace class! Go and check!");

        else if(isFilledAt(row, col)==true)
            throw new FitItException("The space position is already filled out with a wall or a character. GO CHECK CreateSpace class!");

        else
        {
            for(int r = 0; r < height; r++)
                for(int c = 0; c < width; c++)
                {
                    if(space[r][c] == '.')
                        return true;
                }
            return false;
        }

    }

测试失败的示例:

  @Test(timeout=1000) public void space_fits2(){
    String shLayout =
      "aaa\n"+
      "";
    int row=0, col=1;
    String spLayout =
      "|....|\n"+
      "......\n"+
      "..||..\n"+
      "......\n"+
      "|....|\n"+
      "";
    boolean expect = true;
    char dc = getDisplayChar(shLayout);
    Space space = FitIt.makeSpace(spLayout);
    Shape shape = FitIt.makeShape(shLayout,dc);
    boolean actual = space.shapeFitsAt(row,col,shape);
    if(actual != expect){
      failFmt("\nshapeFitsAt() problem\nExpect: %s\nActual: %s\nrow: %s; col %s\n\n%s\n%s",
              expect,actual,row,col,shape,space);
    }
  }

2 个答案:

答案 0 :(得分:0)

检查区域尺寸的唯一位置是else-if条件,如果为true则返回true。但是,由于最终的else也返回true,它会降级为:

 public boolean isFilledAt(int row, int col)
    {
        if(space[row][col] == '.')
            return false;
        else            
            return true;
    }

public boolean isFilledAt(int row, int col)
{
   return space[row][col] != '.';
}

没有考虑到该区域的大小。

答案 1 :(得分:0)

当检查是否存在困难条件时,一旦发现问题,您应该急于返回假,并且只有在您发现没有问题时才能最终返回true。

此代码段一找到true就会返回'.'(正如@Scott Sosna指出的那样) - 因此,嵌套循环永远不会被执行:

   {
        for(int r = 0; r < height; r++)
            for(int c = 0; c < width; c++)
            {
                if(space[r][c] == '.')
                    return true;
            }
        return false;
   }

这会好得多:

   {
        for(int r = 0; r < height; r++) {
            for(int c = 0; c < width; c++) {
                if (shape.isFilledAt(r, c) && space[r+row][c+col] != '.') {
                    return false; // eager to fail
                }
            }
        }
        return true; // slow to succeed: only if it never failed
   }

请注意shape.isFilledAt(r, c) - 如果您没有查看shape以检查它是否合适(在您的代码中,您忘记在shapeFitsAt内以任何方式使用形状)那么你怎么能期望你的程序工作?