我知道这已被问了很多,但我想知道如何旋转俄罗斯方块片? 我已经做了一个漫长而糟糕的解决方案(约170行代码),但应该有更简单的方法来做到这一点。爪哇俄罗斯方块轮换
我的俄罗斯方块片由4个块组成,它们都知道它们在矩阵中的位置(行和列)。矩阵本身是字符型的,所以4个块都是字母。它看起来像这样的例子:
......
..T...
.TTT..
......
我试图模仿我的矩阵通过计算中间的行和列,并使用它作为一个ORIGO,然后根据坐标系尝试应用这种简单的算法,我发现: 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
你能在此解释一下?你能提供一个示例实现吗? – Growler 2013-04-08 16:49:16