我知道这已经被问了很多,但我想知道如何旋转俄罗斯方块?我已经提出了一个漫长而糟糕的解决方案(大约 170 行代码),但应该有更简单的方法来做到这一点。
我的俄罗斯方块由 4 个块组成,它们都知道它们在矩阵中的位置(行和列)。矩阵本身是字符型的,所以 4 个块都是字母。例如,它看起来像这样:
......
..T...
.TTT..
......
我试图通过计算中间行和列并将其用作原始矩阵来模拟我的矩阵作为坐标系,然后尝试应用我发现的这个简单算法:90 度旋转 (x,y) = (-y,x)
看来只有当我的作品位于矩阵的中心时它才有效。我不知道该怎么办,我整天都在想这个。这是我的方法:
public void rotatePiece(ArrayList<Block> random) {
int distance = 0; // how far is the origo
for (int i=0; i < 4; ++i)
board[random.get(i).getRow()][random.get(i).getColumn()] = '.'; // erases the current location of the piece
for (int i=0; i < 4; ++i) {
distance = Math.abs(random.get(i).getColumn()-middleColumn);
if (random.get(i).getColumn() < middleColumn)
random.get(i).setColumn(random.get(i).getColumn()+(distance*2)); // "throws" the location of the block to the other side of the origo
else
random.get(i).setColumn(random.get(i).getColumn()-(distance*2));
int help = random.get(i).getColumn();
random.get(i).setColumn(random.get(i).getRow()); // (x, y) = (-y, x)
random.get(i).setRow(help);
}
for (int i=0; i < 4; ++i)
board[random.get(i).getRow()][random.get(i).getColumn()] = random.get(0).getStyle(); // saves the new location of the piece in the matrix