import java.awt.*; import java.util.*; abstract class Shape { boolean moveDown() { Map future = this.createMap(row + 1, column); if (true) { // world.validates(future)) { this.squares = future; // also a few things // System.out.println( "I am going down." ); this.row += 1; return true; } else { return false; } } boolean moveLeft() { Map future = this.createMap(row, column-1); if (true) { // world.validates(future)) { this.squares = future; this.column -= 1; return true; } else { return false; } } boolean moveRight() { Map future = this.createMap(row, column+1); if (true) { // world.validates(future)) { this.squares = future; this.column += 1; return true; } else { return false; } } boolean moveUp() { Map future = this.createMap(row-1, column); if (true) { // world.validates(future)) { this.squares = future; this.row -= 1; return true; } else { return false; } } void draw(Graphics g) { for (String key : squares.keySet()) { Square square = squares.get(key); if (square != null) square.draw(g); } } Map squares; Map createMap(int row, int column) { Map squares = new HashMap(); int[][] matrix = this.rotations[this.rotation]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { Square square = matrix[i][j] == 1 ? new Square(world, color, row + i, column + j) : null; squares.put(i + ", " + j, square); } } return squares; } World world; int row, column; Color color; Shape(int row, int column, World world) { this.row = row; this.column = column; this.world = world; this.squares = new HashMap(); } int rotation; int[][][] rotations; static Shape randomShape(World world) { int which = 1; // (int)(Math.random() * 7 + 1); switch (which) { case 1 : return new SquareShape (0, world.COLS/2, world); //case 2 : return new LShape (0, world.COLS/2, world); //case 3 : return new SShape (0, world.COLS/2, world); //case 4 : return new ZShape (0, world.COLS/2, world); //case 5 : return new MirroredLShape(0, world.COLS/2, world); //case 6 : return new LineShape (0, world.COLS/2, world); default: return new TShape (0, world.COLS/2, world); } } public String toString() { return this.squares + " at (" + row + ", " + column + ")"; } }