// This class stores information about settled blocks. import java.awt.*; public class TetrisMatrix { // Two-dimensional matrix for holding the settled tetris pieces. TetrisBlock[][] matrix; public TetrisMatrix() { matrix = new TetrisBlock[20][10]; } // Check to see if the position located at x, y is holding a block. public boolean positionEmpty(int x, int y) { if (x > 9 || x < 0 || y < 0 || y > 19) return false; return matrix[y][x] != null; } // Add a tetris piece to the matrix and return how many lines were cleared. public int addTetroid (TetrisPiece n) { TetrisBlock[] b = n.getBlocks(); // Add the tetroid to the matrix. for (int i = 0; i < 4; i++) matrix[b[i].getY()][b[i].getX()] = b[i]; int numLines = 0; int[] linesToClear = new int[4]; // Find out which, if any, lines need clearing for (int i = 0; i < 20; i++) { for (int j = 0; j < 10; j++) if (matrix[i][j] == null) break; else if (j == 9) linesToClear[numLines++] = i; } // Clear any lines that need it if (numLines > 0) { for (int i = 0; i < numLines; i++) { for (int j = linesToClear[i]; j > 0; j--) { for (int k = 0; k < 10; k++) { matrix[j][k] = matrix[j-1][k]; if (matrix[j][k] != null) matrix[j][k].modifyPosition(0,1); // move blocks drawing position down one } } for (int k = 0; k < 10; k++) matrix[0][k] = null; } } return numLines; } // For game over, fill in a line of the matrix with gray blocks. public void grayOutLine(int line) { for (int i = 0; i < 10; i++) matrix[line][i] = new TetrisBlock(0, i, line); } // Draw each block of the matrix. public void draw(Graphics g) { for (int i = 0; i < 20; i++) for(int j = 0; j < 10; j++) if (matrix[i][j] != null) matrix[i][j].draw(g); } }