【问题标题】:Cannot work out how to compare 2 dimensional arrays of different sizes无法弄清楚如何比较不同大小的二维数组
【发布时间】:2021-03-31 04:38:12
【问题描述】:

所以我正在构建一个项目以构建俄罗斯方块风格的游戏,并且我希望能够测试该形状是否能够添加到 5 x 5 网格中。形状由二维数组建模,其中 1 被认为是形状的单个块(形状由几个块组成)。形状使用 3 x 3 网格建模。我必须做的事情是检查网格的形状是否能够适应它。举个例子,在网格的顶部正方形放置一个线条形状,线条会超出边界并且不应该工作,或者另一个例子是网格上可能已经有一个形状,所以线条应该不能放在上面。

这是迄今为止我得到的代码,但它不起作用,我真的很难概念化要做什么。提前谢谢你。

请注意,cols 是网格中的列数 (5),而行数是相同的 (5)。游戏块是形状,坐标是用户在 5x5 网格上单击的位置。

另外:形状的锚点是 3x3 网格的 1,1(所以锚点正好在网格的中间)。而 get(int x, int y) 方法是获取存储在 5x5 网格中的值。

很抱歉,如果一开始没有说清楚,但我试图基本上看看存储在 3x3 网格(由块组成)中的形状是否可以放在 5x5 网格的顶部。包含块的 3x3 网格有一个中心锚点,因此它将是 1,1(因为数组从 0 开始)。如果 5x5 网格有其他块与正在添加的新形状处于相同坐标,那么我希望它返回 false 或者如果形状在放置在 5x5 网格上时超出范围,但如果可以添加成功则返回true。

public boolean canPlayPiece (GamePiece piece, int x, int y) {

        logger.info("canPlayPiece - Block clicked coordinates: "  + x + "," + y);
        // Piece co-ordinates are 3 x 3, each element that is 1 means there is a block there
        int[][] pieceCoordinates = piece.getBlocks();

        // For loop to iterate through the grid
        // first looping through x values
        for (int i = x - 1; i < cols; i++) {

            System.out.println("i= " + i);

            // nested for loop to find the y values stored inside the x
            for (int j = y - 1; j < rows; j++) {

                System.out.println("j: " + j);


                if (pieceCoordinates[x][y] == 1 && get(i,j) != 0) {


                    logger.info("canPlayPiece: FALSE");
                    return false;


                }

            }

        }
        logger.info("canPlayPiece: TRUE");
        return true;
    }

【问题讨论】:

  • 以及游戏中的锚点在哪里。是不是中间所以索引[1][1]。 (我的意思是当放置在网格中时,应该与给定的 x 和 y 值匹配的片段中的相对点)
  • 是的!它是 1,1(所以它在 3x3 网格的中间)。
  • 什么不完全有效?它是否位于唯一提供的代码 canPlayPiece 内?你能edit 描述一下吗!
  • 嘿,我刚刚添加了一些编辑,请原谅我的经验不足,这是我第一次发布问题。
  • @iiicecream 我希望我的回答(在下方)对你有用。 (也请阅读下面的描述)

标签: java javafx tetris


【解决方案1】:

好的,我为你做了以下:

public boolean canPlayPiece(GamePiece piece, int x, int y) {
    int[][] pc = piece.getBlocks();
    
    final int w = 3, h = 3, e = w - 1;
    final int offX = -1, offY = -1; // The offset of the left top corner from 'x' and 'y'
    int i, si, ei, ax, ay, rx, ry;
    for (ei = w * h - 1; ei >= 0 && pc[ei / w][ei % w] == 0; ei--);
    for (si = 0; si <= ei && pc[si / w][si % w] == 0; si++);
    for (i = si + 1, ax = si % w; ax > 0 && i <= ei; i++) if (pc[i / w][rx = i % w] != 0) { si += Math.min(rx - ax, 0); ax = rx; }
    for (i = ei - 1, ax = ei % w; ax < e && i >= si; i--) if (pc[i / w][rx = i % w] != 0) { ei += Math.max(rx - ax, 0); ax = rx; }
    if (si > ei) return true; // There is no block in the piece's grid
    int sx = si % w, sy = si / w, ex = ei % w, ey = ei / w; // The bounds of the shape inside of pc
    int asx = x + offX + sx, asy = y + offY + sy, aex = asx + ex - sx, aey = asy + ey - sy;
    if ((asx | asy | aex | aey | cols - 1 - aex | rows - 1 - aey) < 0) return false; // Would be out of bounds
    for (rx = sx, ax = asx; rx <= ex; rx++, ax++) {
        for (ry = sy, ay = asy; ry <= ey; ry++, ay++) {
            // if (grid[ay][ax] != 0 && pc[ry][rx] != 0) return false; // Block overlaps another block
            if (get(ax, ay) != 0 && pc[ry][rx] != 0) return false; // Block overlaps another block
        }
    }
    return true;
}
  1. 首先它计算出“pc”网格(由“piece.getBlocks()”返回的网格)内的形状边界
  2. 如果 'pc' 内没有形状,它将返回 true,因为可以将空形状放置在任何地方(如果您想在这种情况下返回 false,请将返回值更改为 false)
  3. 如果内部形状超出范围,插入时返回false
  4. 最后它会遍历网格(使用你的'get(x: int, y: int)函数)和'pc'来检查'pc'中的形状是否与网格内任何预先存在的块重叠.如果不是,则返回 true。

我真的希望这对你有用。我测试了它,它至少对我有用。

【讨论】:

  • 嘿,非常感谢,我可以告诉您,您为此付出了很多努力,像您这样的人就是互联网如此伟大的原因!对我来说有很多要理解的,我会尝试让它在我的代码中工作,再次感谢。
  • @iiicecream 我在这里发帖时犯了一个错误。它现在应该可以工作了
  • @iiicecream 您需要在最后一行中添加 offX 和 offY 。只需将“-”替换为“+”即可。我更新了我的答案,所以它现在应该可以工作了。希望你明白这一点。
  • 非常感谢我的朋友 :)
猜你喜欢
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 2023-02-08
  • 2014-12-18
  • 2015-10-14
  • 2020-12-01
相关资源
最近更新 更多