Java 检查形状是否适合空间

Java 检查形状是否适合空间,java,Java,伙计们。对于方法public boolean shapeFitsAt(int row,int col,Shape Shape)我通过了一半的测试用例,而其余的都失败了。我觉得我在代码中又做错了什么,我遗漏了什么。例如,如果我有以下字符串类型的形状: aaa aa. 和字符串类型的空格: ...... ...... ...... ...... ...... .....| 当我将一个形状放置在上述空间中的row=4和col=3处时,它应该是这样的: ......

伙计们。对于方法
public boolean shapeFitsAt(int row,int col,Shape Shape)
我通过了一半的测试用例,而其余的都失败了。我觉得我在代码中又做错了什么,我遗漏了什么。例如,如果我有以下字符串类型的形状:

 aaa
 aa.
和字符串类型的空格:

  ......
  ......
  ......
  ......
  ......
  .....|
当我将一个形状放置在上述空间中的
row=4
col=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;
        }

    }

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

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


它不考虑区域的大小。

在检查困难条件是否为真时,您应该在发现问题后立即返回false,并且只有在没有发现问题时才最终返回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;
   }
{
对于(int r=0;r
这样会更好:

   {
        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
   }
{
对于(int r=0;r
   {
        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
   }