// // tetris game // written by Tim Budd, January 1997 // import java.awt.*; class Piece { // data fields private int x[]; private int y[]; private Color color; // moves public static final int Down = 1; public static final int Left = 2; public static final int Rotate = 3; public static final int Right = 4; // constructor public Piece (int type) { switch(type) { case 1: // x = new int[4]; y = new int[4]; x[0] = 7; y[0] = 29; x[1] = 7; y[1] = 28; x[2] = 8; y[2] = 28; x[3] = 8; y[3] = 27; color = Color.green; break; case 2: // x = new int[4]; y = new int[4]; x[0] = 7; y[0] = 29; x[1] = 8; y[1] = 29; x[2] = 8; y[2] = 28; x[3] = 9; y[3] = 28; color = Color.blue; break; case 3: // x = new int[4]; y = new int[4]; x[0] = 7; y[0] = 29; x[1] = 7; y[1] = 28; x[2] = 8; y[2] = 28; x[3] = 9; y[3] = 28; color = Color.red; break; case 4: // x = new int[4]; y = new int[4]; x[0] = 7; y[0] = 28; x[1] = 8; y[1] = 28; x[2] = 9; y[2] = 28; x[3] = 9; y[3] = 29; color = Color.yellow; break; case 5: // x = new int[4]; y = new int[4]; x[0] = 7; y[0] = 29; x[1] = 8; y[1] = 29; x[2] = 7; y[2] = 28; x[3] = 8; y[3] = 28; color = Color.orange; break; case 6: // x = new int[5]; y = new int[5]; x[0] = 6; y[0] = 29; x[1] = 7; y[1] = 29; x[2] = 8; y[2] = 29; x[3] = 9; y[3] = 29; x[4] = 10; y[4] = 29; color = Color.green; break; case 7: // x = new int[4]; y = new int[4]; x[0] = 7; y[0] = 28; x[1] = 8; y[1] = 28; x[2] = 9; y[2] = 28; x[3] = 8; y[3] = 29; color = Color.green; break; default: System.out.println("Tried to create object value " + type); } } private void erase (Color [][] table) { for (int i = 0; i < x.length; i++) table[x[i]][y[i]] = Color.white; } private void draw (Color [][] table) { for (int i = 0; i < x.length; i++) table[x[i]][y[i]] = color; } private boolean testPosition (int x, int y, Color[][] table) { if ((x < 0) || (x > 14)) return false; if ((y < 0) || (y > 29)) return false; if (table[x][y] != Color.white) return false; return true; } private boolean testDown (Color [][] table) { for (int i = 0; i < x.length; i++) if (! testPosition(x[i], y[i]-1, table)) return false; return true; } private boolean testMoveRight (Color [][] table) { for (int i = 0; i < x.length; i++) if (! testPosition(x[i]+1, y[i], table)) return false; return true; } private boolean testMoveLeft (Color [][] table) { for (int i = 0; i < x.length; i++) if (! testPosition(x[i]-1, y[i], table)) return false; return true; } private boolean testRotate (Color [][] table) { for (int i = 0; i < x.length; i++) { int dx = x[i] - x[1]; int dy = y[i] - y[1]; int nx = x[1] + dy; int ny = y[1] - dx; if (! testPosition(nx, ny, table)) return false; } return true; } private void moveDown () { for (int i = 0; i < x.length; i++) y[i] = y[i] - 1; } private void moveRight () { for (int i = 0; i < x.length; i++) x[i] = x[i] + 1; } private void moveLeft () { for (int i = 0; i < x.length; i++) x[i] = x[i] - 1; } private void moveRotate () { for (int i = 0; i < x.length; i++) { int dx = x[i] - x[1]; int dy = y[i] - y[1]; x[i] = x[1] + dy; y[i] = y[1] - dx; } } public boolean move (int command, Color [][] table) { erase(table); boolean canDoIt = false; switch (command) { case Down: canDoIt = testDown(table); break; case Right: canDoIt = testMoveRight(table); break; case Left: canDoIt = testMoveLeft(table); break; case Rotate: canDoIt = testRotate(table); break; } if (canDoIt) switch (command) { case Down: moveDown(); break; case Right: moveRight(); break; case Left: moveLeft(); break; case Rotate: moveRotate(); break; } draw(table); if (command == Down) return canDoIt; return true; } }