Java Tetris轮换 [英] Java Tetris rotation

查看:156
本文介绍了Java Tetris轮换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已被问了很多,但我想知道如何旋转俄罗斯方块片?
我已经做了很长很难的解决方案(大约170行代码),但应该有更简单的方法。

I know this has been asked a lot but I'd like to know how to rotate a Tetris piece? I already made a long and bad solution (~170 lines of code) but there should be easier way to do it.

我的俄罗斯方块包含4个所有知道矩阵中位置(行和列)的块。矩阵本身是字符型的,因此4个字块都是字母。它看起来像这样:

My tetris pieces consist of 4 blocks which all know their location (row and column) in the matrix. Matrix itself is char-typed, so 4 blocks are all letters. It looks like this for example:

......
..T...
.TTT..
......

我试图模拟我的矩阵作为坐标系统,通过计算中间的行和列并将其用作原点然后尝试应用这个简单的算法我发现:
90度旋转(x,y)=( - y,x)

I tried to simulate my matrix as coordinate system by counting the middle row and column and using it as an origo and then tried to apply this simple algorithm I found: 90 degree rotation (x,y) = (-y,x)

它似乎只有在我的作品位于矩阵的中心时才有效。我不知道我应该做什么,我一整天都在想这个。这是我的方法:

It appears that it works only if my piece is in the center of matrix. I have no idea what I should do, I've been thinking this all day. Here's my method:

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


推荐答案

我建议为每个块组定义四种状态。

I would recommend defining four states for each block-group.

enum ROTATION {
    UP, DOWN, LEFT, RIGHT;

    ROTATION rotateLeft() {
        switch(this) {
             case UP: return LEFT;
             case LEFT: return DOWN;
             case DOWN: return RIGHT;
             case RIGHT: return UP;
        }
        return null; // wont happen;
    }

    ROTATION rotateRight() {
        ROTATION r = this;
         // wow I'm lazy, but I've actually seen this in production code!
        return r.rotateLeft().rotateLeft().rotateLeft();
    }
}

abstract class Brick {
    Point centerPos;
    ROTATION rot;
    abstract List<Point> pointsOccupied();
}

class TBrick extends Brick {

    List<Point> pointsOccupied() {
        int x = centerPos.x();
        int y = centerPos.y();
        List<Point> points = new LinkedList<Point>();
        switch(rot) {
            case UP: points.add(new Point(x-1,y);
                     points.add(new Point(x,y);
                     points.add(new Point(x+1,y);
                     points.add(new Point(x, y+1);
                break;
            case Down: points.add(new Point(x-1,y);
                       points.add(new Point(x,y);
                       points.add(new Point(x+1,y);
                       points.add(new Point(x, y-1);
                break;
            // finish the cases
        }

   }

}

这篇关于Java Tetris轮换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆