Java俄罗斯方块 - 使用转置旋转一块

时间:2013-04-18 18:49:06

标签: java swing transpose tetris

我正在用Java构建俄罗斯方块作为一个有趣的项目,现在我正在进行轮换。

我最初对每次轮换进行了硬编码,这被证明是非常繁琐的。

话虽这么说,我当时会尝试使用线性代数进行矩阵旋转,但Mathematics.SE上有人推荐我尝试移调。所以考虑到他的描述,我试着把它画出来。我的图纸是否正确?

enter image description here

从那以后,我不是要编写转置代码,但现在我的代码完全丢失了。

public void getTranspose() {
    Tile[][] gridTranspose = transpose();
    System.out.println();
    System.out.println("B`:");
    for (int j = 0; j < gridTranspose.length; j++) {
        for (int i = 0; i < gridTranspose[j].length-1; i++) {
            System.out.print("(" + i + ", " + j + ") ");
        }
        System.out.println();
    }
}

public Tile[][] transpose() {
    Tile gridT[][];
    gridT = new Tile[width][height];
    System.out.println("B:");
    for(int i = 0; i < grid.length; i++) {
        for(int j = 0; j < grid[i].length-1; j++) {
            System.out.print("(" + i + ", " + j + ") ");
            gridT[j][i] = grid[i][j]; 
        }
        System.out.println();
    }
    return gridT;   
}

这会输出一个看似正确的转置图?

B:

(0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) (0, 7) (0, 8) 
(1, 0) (1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (1, 8) 
(2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (2, 7) (2, 8) 
(3, 0) (3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (3, 6) (3, 7) (3, 8) 
(4, 0) (4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6) (4, 7) (4, 8) 
(5, 0) (5, 1) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7) (5, 8) 
(6, 0) (6, 1) (6, 2) (6, 3) (6, 4) (6, 5) (6, 6) (6, 7) (6, 8) 
(7, 0) (7, 1) (7, 2) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7) (7, 8) 
(8, 0) (8, 1) (8, 2) (8, 3) (8, 4) (8, 5) (8, 6) (8, 7) (8, 8) 
(9, 0) (9, 1) (9, 2) (9, 3) (9, 4) (9, 5) (9, 6) (9, 7) (9, 8) 

B`:

(0, 0) (1, 0) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 0) (8, 0) 
(0, 1) (1, 1) (2, 1) (3, 1) (4, 1) (5, 1) (6, 1) (7, 1) (8, 1) 
(0, 2) (1, 2) (2, 2) (3, 2) (4, 2) (5, 2) (6, 2) (7, 2) (8, 2) 
(0, 3) (1, 3) (2, 3) (3, 3) (4, 3) (5, 3) (6, 3) (7, 3) (8, 3) 
(0, 4) (1, 4) (2, 4) (3, 4) (4, 4) (5, 4) (6, 4) (7, 4) (8, 4) 
(0, 5) (1, 5) (2, 5) (3, 5) (4, 5) (5, 5) (6, 5) (7, 5) (8, 5) 
(0, 6) (1, 6) (2, 6) (3, 6) (4, 6) (5, 6) (6, 6) (7, 6) (8, 6) 
(0, 7) (1, 7) (2, 7) (3, 7) (4, 7) (5, 7) (6, 7) (7, 7) (8, 7) 
(0, 8) (1, 8) (2, 8) (3, 8) (4, 8) (5, 8) (6, 8) (7, 8) (8, 8) 
(0, 9) (1, 9) (2, 9) (3, 9) (4, 9) (5, 9) (6, 9) (7, 9) (8, 9) 

所以我的问题是:

1)我的绘画是否正确解释了他的描述?

2)我是否正确生成了转置图?

3)如果是这样,我应该如何绘制旋转的块...我应该用grid[row][col]替换transpose()吗?

public void paintComponent(Graphics g) {
    g.setColor(Color.black);
    g.fillRect(0, 0, getWidth(), getHeight());
    for(int row = 0; row < grid.length; row++) {
        for(int col = 0; col < grid[row].length; col++) {
            if(grid[row][col] != null) {
                //if there is a non-null space, that is a Tetris piece.. fill it red
                if (grid[row][col].getColor() != null) {
                    g.setColor(grid[row][col].getColor());
                    g.fillRect(row * tilesize, col * tilesize, tilesize, tilesize);
                    g.setColor(Color.WHITE);
                    g.drawString("(" + row + ", " + col + ")", row * tilesize, col * tilesize+10);
                }           
            }
        }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:3)

单独换位对所有正常的俄罗斯方块都不起作用。例如,拿这样的形状:

xx_
_xx
___

当你转置它时,你最终会得到这个:

x__
xx_
_x_

因此,不要考虑某个特定的部分,而要考虑当您使用带有标记角的正方形时会发生什么:

a_b
___
d_c

这使得你的轮换是否良好非常清楚。那么让我们来看看转置:

a_d
___
b_c

为了顺时针旋转,我们可以将它向侧面翻转:

d_a
___
c_b

再次执行相同的操作应该让我们从我们开始的地方开始完全旋转一半。转移:

d_c
___
a_b

翻转它:

c_d
___
b_a

因此,为了顺时针旋转,只需转置并翻转即可。如果我们做相反的事情,翻转它然后采取转置会发生什么?从前一个方向开始,如果我们翻转它,这就是我们得到的:

d_c
___
a_b

现在进行转置:

d_a
___
c_b

我们回到旋转后的状态 - 逆时针旋转。因此,单独调换不起作用,但是移调与水平翻转相结合可以完成所需的一切!

至于你的问题,

  1. 我不确定你的绘图是否正确。根据您留下的评论,我怀疑我可能误解了他们,这就是为什么我上面的图纸显示需要发生的事情。

  2. 是的,这是一个正确转置的图表。您可能不希望在循环中使用gridTranspose[j].length-1,因为这会切断一列。

  3. 要绘制旋转的块,我建议使用单独的变量来保存当前的块。如果作品位于x,y,那么您可以在paintComponent方法中添加类似的内容:

    for(int row = 0; row < piece.length(); row++) {
        for(int col = 0; col < piece.width(); piece++) {
            if(piece.hasBlockAt(row, col) {
                g.setColor(piece.getColorAt(row, col));
                g.fillRect((row+x) * tilesize, (col+y) * tilesize, tilesize, tilesize);
            }
        } 
    }