// Tetris tile class // Hao Jiang, April 2, 2008, at Boston College public class Tetromino { private int id; private int r; private int c; private int w; private int h; private int[][] mask; private int[][] board; private int bW; private int bH; public boolean atBottom = false; public Tetromino(int id, int[][] board) { this.board = board; this.id = id; bH = board.length; bW = board[0].length; if (id == 1) { r = 0; c = bW/2; w = 4; h = 1; mask = new int[h][w]; mask[0][0] = 1; mask[0][1] = 1; mask[0][2] = 1; mask[0][3] = 1; } if (id == 2) { r = 0; c = bW/2; w = 3; h = 2; mask = new int[h][w]; mask[0][0] = 1; mask[0][1] = 0; mask[0][2] = 0; mask[1][0] = 1; mask[1][1] = 1; mask[1][2] = 1; } if (id == 3) { r = 0; c = bW/2; w = 3; h = 2; mask = new int[h][w]; mask[0][0] = 0; mask[0][1] = 0; mask[0][2] = 1; mask[1][0] = 1; mask[1][1] = 1; mask[1][2] = 1; } if (id == 4) { r = 0; c = bW/2; w = 2; h = 2; mask = new int[h][w]; mask[0][0] = 1; mask[0][1] = 1; mask[1][0] = 1; mask[1][1] = 1; } if (id == 5) { r = 0; c = bW/2; w = 3; h = 2; mask = new int[h][w]; mask[0][0] = 0; mask[0][1] = 1; mask[0][2] = 1; mask[1][0] = 1; mask[1][1] = 1; mask[1][2] = 0; } if (id == 6) { r = 0; c = bW/2; w = 3; h = 2; mask = new int[h][w]; mask[0][0] = 0; mask[0][1] = 1; mask[0][2] = 0; mask[1][0] = 1; mask[1][1] = 1; mask[1][2] = 1; } if (id == 7) { r = 0; c = bW/2; w = 3; h = 2; mask = new int[h][w]; mask[0][0] = 1; mask[0][1] = 1; mask[0][2] = 0; mask[1][0] = 0; mask[1][1] = 1; mask[1][2] = 1; } } private boolean isMoveable(int r, int c) { for (int i = 0; i < h; i ++) for (int j = 0; j < w; j ++) { int row = r + i; int col = c + j; if (mask[i][j] == 1 && (col < 0 || col >= bW || row >= bH) ) return false; if ( row >= 0 && row < bH && col >= 0 && col < bW) { if (board[row][col] <= 7 && board[row][col] >= 1 && mask[i][j] == 1) return false; } } return true; } private boolean isRotateable(int r, int c) { int centerR = r + h/2; int centerC = c + w/2; int rNew = centerR - w/2; int cNew = centerC - h/2; for (int i = 0; i < w; i ++) for (int j = 0; j < h; j ++) { int row = rNew + i; int col = cNew + j; if (mask[j][w-1-i] == 1 && (col < 0 || col >= bW || row >= bH) ) return false; if ( row >= 0 && row < bH && col >= 0 && col < bW) { if (board[row][col] <= 7 && board[row][col] >= 1 && mask[j][w-1-i] == 1) return false; } } return true; } public boolean canMoveDown() { return isMoveable(r+1, c); } public void mark(int v) { for (int i = 0; i < h; i ++) for (int j = 0; j < w; j ++) { int row = r + i; int col = c + j; if ( row >= 0 && row < bH && col >= 0 && col < bW) { if (mask[i][j] == 1) board[row][col] = v; } } } public void move() { mark(id + 7); } public void stay() { mark(id); } public void moveLeft() { if (isMoveable(r, c-1)) c = c - 1; } public void moveRight() { if (isMoveable(r, c + 1)) c = c + 1; } public void moveDown() { if ( isMoveable(r+1, c) ) { r = r + 1; if (!isMoveable(r+1, c)) atBottom = true; } } public void rotate() { if (!isRotateable(r, c)) return; int[][] maskNew = new int[w][h]; for(int i = 0; i < w; i ++) for(int j = 0; j < h; j ++) maskNew[i][j] = mask[j][w-1-i]; mask = maskNew; int centerR = r + h/2; int centerC = c + w/2; int t = w; w = h; h = t; r = centerR - h/2; c = centerC - w/2; } }