Guest User

Untitled

a guest
Dec 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. package Tetris;
  2. import java.util.Random;
  3. import java.lang.Math;
  4.  
  5. import java.awt.*;
  6.  
  7. public class Shape {
  8.  
  9. final static int NO_SHAPE = 0;
  10. final static int T_SHAPE = 1;
  11. final static int SQUARE = 2;
  12. final static int STICK = 3;
  13. final static int L_LEFT = 4;
  14. final static int L_RIGHT = 5;
  15. final static int DOG_LEFT = 6;
  16. final static int DOG_RIGHT = 7;
  17.  
  18. private int pieceShape;
  19. private Point[] coords;
  20. private Point[][] coordsTable;
  21.  
  22. public Shape() {
  23. coords = new Point[4];
  24. setShape(Shape.NO_SHAPE);
  25. }
  26.  
  27. public void setShape(int shape) {
  28.  
  29. coordsTable = new Point[][]{
  30. {new Point(0, 0), new Point(0, 0), new Point(0, 0), new Point(0, 0)}, // no shape
  31. {new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 0)}, // t-shape
  32. {new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)}, // square
  33. {new Point(0, 0), new Point(0, 1), new Point(0, 2), new Point(0, 3)}, // stick
  34. {new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(1, 2)}, // l-shape left isomer
  35. {new Point(0, 0), new Point(0, 1), new Point(0, 2), new Point(1, 0)}, // l-shape right isomer
  36. {new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(2, 0)}, // dog left isomer
  37. {new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1)}, // dog right isomer
  38. };
  39.  
  40. for (int i = 0; i < 4 ; i++) {
  41. coords[i] = coordsTable[shape][i];
  42. }
  43. pieceShape = shape;
  44. }
  45.  
  46. private void setX(int index, int x) { coords[index].x = x; }
  47. private void setY(int index, int y) { coords[index].y = y; }
  48. public int x(int index) { return coords[index].x; }
  49. public int y(int index) { return coords[index].y; }
  50. public int getShape() { return pieceShape; }
  51.  
  52. public void setRandomShape(){
  53. Random r = new Random();
  54. int x = Math.abs(r.nextInt()) % 7 + 1;
  55. setShape(x);
  56. }
  57.  
  58. public int minX(){
  59. int m = coords[0].x;
  60. for (int i=0; i < 4; i++) {
  61. m = Math.min(m, coords[i].x);
  62. }
  63. return m;
  64. }
  65.  
  66. public int minY() {
  67. int m = coords[0].y;
  68. for (int i=0; i < 4; i++) {
  69. m = Math.min(m, coords[i].y);
  70. }
  71. return m;
  72. }
  73.  
  74. public Shape rotateLeft(){
  75. if (pieceShape == Shape.SQUARE)
  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 == Shape.SQUARE)
  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. }
Add Comment
Please, Sign In to add comment