Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. public class Shape {
  2.  
  3.     enum Tetrominoes { NoShape, ZShape, SShape, LineShape,
  4.                TShape, SquareShape, LShape, MirroredLShape };
  5.  
  6.     private Tetrominoes pieceShape;
  7.     private int coords[][];
  8.     private int[][][] coordsTable;
  9.  
  10.  
  11.     public Shape() {
  12.  
  13.         coords = new int[4][2];
  14.         setShape(Tetrominoes.NoShape);
  15.  
  16.     }
  17.  
  18.     public void setShape(Tetrominoes shape) {
  19.  
  20.          coordsTable = new int[][][] {
  21.             { { 0, 0 },   { 0, 0 },   { 0, 0 },   { 0, 0 } },
  22.             { { 0, -1 },  { 0, 0 },   { -1, 0 },  { -1, 1 } },
  23.             { { 0, -1 },  { 0, 0 },   { 1, 0 },   { 1, 1 } },
  24.             { { 0, -1 },  { 0, 0 },   { 0, 1 },   { 0, 2 } },
  25.             { { -1, 0 },  { 0, 0 },   { 1, 0 },   { 0, 1 } },
  26.             { { 0, 0 },   { 1, 0 },   { 0, 1 },   { 1, 1 } },
  27.             { { -1, -1 }, { 0, -1 },  { 0, 0 },   { 0, 1 } },
  28.             { { 1, -1 },  { 0, -1 },  { 0, 0 },   { 0, 1 } }
  29.         };
  30.  
  31.         for (int i = 0; i < 4 ; i++) {
  32.             for (int j = 0; j < 2; ++j) {
  33.                 coords[i][j] = coordsTable[shape.ordinal()][i][j];
  34.             }
  35.         }
  36.         pieceShape = shape;
  37.  
  38.     }
  39.  
  40.     private void setX(int index, int x) { coords[index][0] = x; }
  41.     private void setY(int index, int y) { coords[index][1] = y; }
  42.     public int x(int index) { return coords[index][0]; }
  43.     public int y(int index) { return coords[index][1]; }
  44.     public Tetrominoes getShape()  { return pieceShape; }
  45.  
  46.     public void setRandomShape()
  47.     {
  48.         Random r = new Random();
  49.         int x = Math.abs(r.nextInt()) % 7 + 1;
  50.         Tetrominoes[] values = Tetrominoes.values();
  51.         setShape(values[x]);
  52.     }
  53.  
  54.     public int minX()
  55.     {
  56.       int m = coords[0][0];
  57.       for (int i=0; i < 4; i++) {
  58.           m = Math.min(m, coords[i][0]);
  59.       }
  60.       return m;
  61.     }
  62.  
  63.  
  64.     public int minY()
  65.     {
  66.       int m = coords[0][1];
  67.       for (int i=0; i < 4; i++) {
  68.           m = Math.min(m, coords[i][1]);
  69.       }
  70.       return m;
  71.     }
  72.  
  73.     public Shape rotateLeft()
  74.     {
  75.         if (pieceShape == Tetrominoes.SquareShape)
  76.             return this;
  77.  
  78.         Shape result = new Shape();
  79.         result.pieceShape = pieceShape;
  80.  
  81.         for (int i = 0; i < 4; ++i) {
  82.             result.setX(i, y(i));
  83.             result.setY(i, -x(i));
  84.         }
  85.         return result;
  86.     }
  87.  
  88.     public Shape rotateRight()
  89.     {
  90.         if (pieceShape == Tetrominoes.SquareShape)
  91.             return this;
  92.  
  93.         Shape result = new Shape();
  94.         result.pieceShape = pieceShape;
  95.  
  96.         for (int i = 0; i < 4; ++i) {
  97.             result.setX(i, -y(i));
  98.             result.setY(i, x(i));
  99.         }
  100.         return result;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement