Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Tetris;
- import java.util.Random;
- import java.lang.Math;
- import java.awt.*;
- public class Shape {
- final static int NO_SHAPE = 0;
- final static int T_SHAPE = 1;
- final static int SQUARE = 2;
- final static int STICK = 3;
- final static int L_LEFT = 4;
- final static int L_RIGHT = 5;
- final static int DOG_LEFT = 6;
- final static int DOG_RIGHT = 7;
- private int pieceShape;
- private Point[] coords;
- private Point[][] coordsTable;
- public Shape() {
- coords = new Point[4];
- setShape(Shape.NO_SHAPE);
- }
- public void setShape(int shape) {
- coordsTable = new Point[][]{
- {new Point(0, 0), new Point(0, 0), new Point(0, 0), new Point(0, 0)}, // no shape
- {new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 0)}, // t-shape
- {new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)}, // square
- {new Point(0, 0), new Point(0, 1), new Point(0, 2), new Point(0, 3)}, // stick
- {new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(1, 2)}, // l-shape left isomer
- {new Point(0, 0), new Point(0, 1), new Point(0, 2), new Point(1, 0)}, // l-shape right isomer
- {new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(2, 0)}, // dog left isomer
- {new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1)}, // dog right isomer
- };
- for (int i = 0; i < 4 ; i++) {
- coords[i] = coordsTable[shape][i];
- }
- pieceShape = shape;
- }
- private void setX(int index, int x) { coords[index].x = x; }
- private void setY(int index, int y) { coords[index].y = y; }
- public int x(int index) { return coords[index].x; }
- public int y(int index) { return coords[index].y; }
- public int getShape() { return pieceShape; }
- public void setRandomShape(){
- Random r = new Random();
- int x = Math.abs(r.nextInt()) % 7 + 1;
- setShape(x);
- }
- public int minX(){
- int m = coords[0].x;
- for (int i=0; i < 4; i++) {
- m = Math.min(m, coords[i].x);
- }
- return m;
- }
- public int minY() {
- int m = coords[0].y;
- for (int i=0; i < 4; i++) {
- m = Math.min(m, coords[i].y);
- }
- return m;
- }
- public Shape rotateLeft(){
- if (pieceShape == Shape.SQUARE)
- return this;
- Shape result = new Shape();
- result.pieceShape = pieceShape;
- for (int i = 0; i < 4; ++i) {
- result.setX(i, y(i));
- result.setY(i, -x(i));
- }
- return result;
- }
- public Shape rotateRight()
- {
- if (pieceShape == Shape.SQUARE)
- return this;
- Shape result = new Shape();
- result.pieceShape = pieceShape;
- for (int i = 0; i < 4; ++i) {
- result.setX(i, -y(i));
- result.setY(i, x(i));
- }
- return result;
- }
- }
Add Comment
Please, Sign In to add comment