2013-04-08 70 views
0

我正在Java中制作俄罗斯方块,并且正在旋转一块。Java俄罗斯方块 - 旋转

首先,我只是旋转条形件。

我觉得我现在这样做的方式不仅是越野车,而且是完全矫枉过正。但我不确定还有什么其他的解决方法。

首先,我有一个keylistener设置int[] rotatedCoords等于calcRotation("right") ...如果向左旋转,rotationsCounter+=1;将被递减。

if (keycode == KeyEvent.VK_D) { 

     int[] rotatedCoords = calcRotation("right"); 
     rotationsCounter+=1; 
     clearCurrPosition(); 
     rotate(rotatedCoords); 
     System.out.println(rotationsCounter); 
     if (rotationsCounter == 4) { 
      rotationsCounter = 0; 
     } 
     System.out.println(rotationsCounter); 
    } 

calcRotation(字符串右或左)获得在一块所有4个瓷砖的当前坐标和基于其旋转方向将它们发送到int[] getRotation(String shape, String direction, int[] current X coords, int[] current Y Coords)

public int[] calcRotation(String direction) { 

     for (int i = 0; i < tile.length; i++) { 
      currPositionX[i] = tile[i].getX(); 
      currPositionY[i] = tile[i].getY(); 
      System.out.println(currPositionX[i] + ", " + currPositionY[i]); 
     } 
     return getRotation("Bar", direction, currPositionX, currPositionY); 
    } 

然后getRotation []设定新的坐标被选择(右或左),它是形状,并且其旋转计数器它是在(0度,90度,180或270 ...)

if (direction == "right") { 
     if (shape == "Bar") { 
      if (rotationsCounter == 0) { 
        currXs[0] += 1; 
        currYs[0] += -1; 

        currXs[1] += 0; 
        currYs[1] += 0; 

        currXs[2] += -1; 
        currYs[2] += 1; 

        currXs[3] += -2;  
        currYs[3] += 2;    

        rightRotate1 = new int[] {currXs[0], currYs[0], currXs[1], currYs[1], currXs[2], currYs[2], currXs[3], currYs[3]};   
       }  
      if (rotationsCounter == 1) { 
       ... etc 

然后,坐标(pieceRotations)将被适当地设置:

 //handle on left rotations 
    if (direction == "right") { 
     if (shape == "Bar") { 
      if (rotationsCounter == 0) {  
       pieceRotations = rightRotate1;      
      }  
      if (rotationsCounter == 1) { 
       pieceRotations = rightRotate2;  
      }     
      if (rotationsCounter == 2) { 
       pieceRotations = rightRotate3; 
      } 
      if (rotationsCounter == 3) { 
       pieceRotations = rightRotate0; 
      }    
     } 
    } 
    if (direction == "left") { 
     if (shape == "Bar") { 
      if (rotationsCounter == 0) {  
       pieceRotations = rightRotate3;      
      }  
      if (rotationsCounter == 1) { 
       pieceRotations = rightRotate0;  
      }     
      if (rotationsCounter == 2) { 
       pieceRotations = rightRotate1; 
      } 
      if (rotationsCounter == 3) { 
       pieceRotations = rightRotate2; 
      } 
     }   
    } 
    return pieceRotations; 
} 

于是最后,rotate(rotatedCoords)将与正确的坐标称为旋转过所有的瓷砖...

public void rotate(int[] rotatedCoordinates) { 
    int counterX = 0, counterY = 1; 
    if (movePieceValid()) { 
     for (int i = 0; i < tile.length; i++) { 
      tile[i].setLocation(rotatedCoordinates[counterX], rotatedCoordinates[counterY]);     
      counterX+=2; 
      counterY+=2; 
     } 
    } else { 
     for (int i = 0; i < tile.length; i++) { 
      tile[i].setLocation(currPositionX[i], currPositionY[i]);     
     } 
    }   
} 

所以,我目前的方式根据leftright的每个形状的当前位置计算新坐标显然是矫枉过正。有没有我可以遵循的一般指南来大大简化?我想不出另一种方式来获得每个形状的位置?

+3

顺便说一句,'方向==“如果你的目标是比较字符串,那么“正确”是错误的。你应该使用'.equals'方法。 – Maroun 2013-04-08 16:15:36

+2

@MarounMaroun在我的代码中注明并修复。谢谢! – Growler 2013-04-08 16:23:20

回答

4

只有这么多件。尝试有这个:

public abstract class Piece { 
    public abstract void rotate(Direction dir); 
} 

public class BarPiece extends Piece { 
    public void rotate(Direction dir) { 
     // implement 
    } 
} 

public class TPiece extends Piece { 
    // ... 
} 

public class LeftSPiece extends Piece { 
    // ... 
} 

这似乎有点脏特别,但做数学的所有时间将是缓慢的,而且因为只有这么多件可能...