// Class to define a tetris piece and its various movements. import java.awt.*; public class TetrisPiece { private TetrisBlock[] blocks; private int[] initial_position; // Which rotation position the piece is in. private int rotation_sequence; // Changes in position for right rotations. private int[] rot3to0; private int[] rot0to1; private int[] rot1to2; private int[] rot2to3; // Changes in position for left rotations. private int[] rot0to3; private int[] rot1to0; private int[] rot2to1; private int[] rot3to2; public TetrisPiece (int tetroid_code, int[] initial_position, int[] rot3to0, int[] rot0to1, int[] rot1to2, int[] rot2to3, int[] rot0to3, int[] rot1to0, int[] rot2to1, int[] rot3to2) { this.initial_position = initial_position; this.rot3to0 = rot3to0; this.rot0to1 = rot0to1; this.rot1to2 = rot1to2; this.rot2to3 = rot2to3; this.rot0to3 = rot0to3; this.rot1to0 = rot1to0; this.rot2to1 = rot2to1; this.rot3to2 = rot3to2; blocks = new TetrisBlock[4]; rotation_sequence = 0; blocks[0] = new TetrisBlock(tetroid_code, initial_position[0], initial_position[1]); blocks[1] = new TetrisBlock(tetroid_code, initial_position[2], initial_position[3]); blocks[2] = new TetrisBlock(tetroid_code, initial_position[4], initial_position[5]); blocks[3] = new TetrisBlock(tetroid_code, initial_position[6], initial_position[7]); } // Shift the tetris piece down. public void moveDown() { for (int i = 0; i < 4; i++) blocks[i].moveDown(); } // Check to see if the current position of the tetris piece is a valid one in the array. public boolean checkPosition(TetrisMatrix tm) { for (int i = 0; i < 4; i++) if (blocks[i].checkPosition(tm) == false) return false; return true; } // Move the tetris piece left. public void moveLeft(TetrisMatrix tm) { if (checkLeft(tm)) for (int i = 0; i < 4; i++) blocks[i].moveLeft(); } // Check to see if the tetris piece can move left. public boolean checkLeft(TetrisMatrix tm) { for (int i = 0; i < 4; i++) if (blocks[i].checkLeft(tm) == false) return false; return true; } // Move the tetris piece to an arbitrary location. public void move(int x, int y) { for (int i = 0; i < 4; i++) blocks[i].modifyPosition(x, y); } // Check to see if the tetris piece can move right. public boolean checkRight(TetrisMatrix tm) { for (int i = 0; i < 4; i++) if (blocks[i].checkRight(tm) == false) return false; return true; } // Move the tetris piece to the right. public void moveRight(TetrisMatrix tm) { if (checkRight(tm)) for (int i = 0; i < 4; i++) blocks[i].moveRight(); } // Check to see if the tetris piece can move down. public boolean checkDown(TetrisMatrix tm) { for (int i = 0; i < 4; i++) if (blocks[i].checkDown(tm) == false) return false; return true; } // Rotate the tetris piece left. public void rotateLeft(TetrisMatrix tm) { int[] p = new int[8]; switch (rotation_sequence) { case 0: p = rot0to3; break; case 1: p = rot1to0; break; case 2: p = rot2to1; break; case 3: p = rot3to2; break; } blocks[0].modifyPosition(p[0], p[1]); blocks[1].modifyPosition(p[2], p[3]); blocks[2].modifyPosition(p[4], p[5]); blocks[3].modifyPosition(p[6], p[7]); if (rotation_sequence == 0) rotation_sequence = 3; else rotation_sequence--; // Check to see if valid rotation and try to correct for (int i = 0; i < 4; i++) if (blocks[i].checkRotatePosition(tm) == false) rotateRight(tm); } // Rotate the tetris piece right. public void rotateRight(TetrisMatrix tm) { int[] p = new int[8]; switch (rotation_sequence) { case 0: p = rot0to1; break; case 1: p = rot1to2; break; case 2: p = rot2to3; break; case 3: p = rot3to0; break; } blocks[0].modifyPosition(p[0], p[1]); blocks[1].modifyPosition(p[2], p[3]); blocks[2].modifyPosition(p[4], p[5]); blocks[3].modifyPosition(p[6], p[7]); if (rotation_sequence == 3) rotation_sequence = 0; else rotation_sequence++; // Check to see if valid rotation and try to correct for (int i = 0; i < 4; i++) if (blocks[i].checkRotatePosition(tm) == false) rotateLeft(tm); } // Get the current x position of the tetris piece. public int getX() { return blocks[1].getX(); } // Get the current y position of the tetris piece. public int getY() { return blocks[1].getY(); } // Return the array of tetris blocks making up this piece. public TetrisBlock[] getBlocks () { return blocks; } // Draw the tetris piece one block at a time. public void draw(Graphics g) { for (int i = 0; i < 4; i++) blocks[i].draw(g); } }