/* * Tetris by Eric Idema - idemaeri@cps.msu.edu * * This code is for educational purposes only. */ import java.lang.Math; import java.awt.Color; import java.awt.Graphics; public class Shape { int posX[], posY[]; Color color; boolean isSquare = false; public Shape() { posX = new int[4]; posY = new int[4]; switch( (int)(7.0 * Math.random()) ) { case 0: for( int i = 0; i < 4; i++ ) { // xxxx posX[i] = i+3; posY[i] = 0; } color = Color.red; break; case 1: for( int i = 0; i < 3; i++ ) { // xxx posX[i] = i+3; // x posY[i] = 0; } posX[3] = 3; posY[3] = 1; color = Color.blue; break; case 2: for( int i = 0; i < 3; i++ ) { // xxx posX[i] = i+3; // x posY[i] = 0; } posX[3] = 5; posY[3] = 1; color = Color.orange; break; case 3: for( int i = 0; i < 3; i++ ) { // xxx posX[i] = i+3; // x posY[i] = 0; } posX[3] = 4; posY[3] = 1; color = Color.pink; break; case 4: posX[0] = 3; posY[0] = 0; // xx posX[1] = 4; posY[1] = 0; // xx posX[2] = 4; posY[2] = 1; posX[3] = 5; posY[3] = 1; color = Color.magenta; break; case 5: posX[0] = 3; posY[0] = 1; // xx posX[1] = 4; posY[1] = 1; // xx posX[2] = 4; posY[2] = 0; posX[3] = 5; posY[3] = 0; color = Color.green; break; case 6: posX[0] = 4; posY[0] = 1; posX[1] = 4; posY[1] = 0; posX[2] = 5; posY[2] = 1; posX[3] = 5; posY[3] = 0; isSquare = true; color = Color.cyan; break; } } public void paint( Graphics g ) { for( int i = 0; i < 4; i++ ) { g.setColor( color ); g.fillRect( posX[i]*15, posY[i]*15, 15, 15 ); g.setColor( Color.white ); g.drawRect( posX[i]*15, posY[i]*15, 15, 15 ); g.drawLine( posX[i]*15, posY[i]*15, posX[i]*15+15, posY[i]*15+15 ); g.drawLine( posX[i]*15+15, posY[i]*15, posX[i]*15, posY[i]*15+15 ); } } public void paintNext( Graphics g ) { for( int i = 0; i < 4; i++ ) { int x = posX[i] - posX[0]; g.setColor( color ); g.fillRect( x*15+170, posY[i]*15+30, 15, 15 ); g.setColor( Color.white ); g.drawRect( x*15+170, posY[i]*15+30, 15, 15 ); g.drawLine( x*15+170, posY[i]*15+30, x*15+185, posY[i]*15+45 ); g.drawLine( x*15+185, posY[i]*15+30, x*15+170, posY[i]*15+45 ); } } public void drop() { for( int i = 0; i < 4; i++ ) { posY[i]++; } } public void moveLeft( Color[][] map ) { for( int i = 0; i < 4; i++ ) { if( posX[i] - 1 < 0 ) return; else if( map[posX[i]-1][posY[i]] != Color.white ) return; } for( int i = 0; i < 4; i++ ) { posX[i]--; } } public void moveRight( Color[][] map ) { for( int i = 0; i < 4; i++ ) { if( posX[i] + 1 > 9 ) return; else if( map[posX[i]+1][posY[i]] != Color.white ) return; } for( int i = 0; i < 4; i++ ) { posX[i]++; } } public boolean checkGameOver( Color[][] map ) { for( int i = 0; i < 4; i++ ) { if( map[posX[i]][posY[i]] != Color.white ) return true; } return false; } public void rotate( Color[][] map ) { if( isSquare ) return; int[] backX = new int[4]; int[] backY = new int[4]; for( int i = 0; i < 4; i++ ) { backX[i] = posX[i]; backY[i] = posY[i]; } posX[0] -= posX[1]; //to rotate around ( posX[1], posY[1] ) we posX[2] -= posX[1]; //subtract it out of the other points posX[3] -= posX[1]; posY[0] -= posY[1]; posY[2] -= posY[1]; posY[3] -= posY[1]; int temp; temp = posX[0]; //switch the X and Y of the other points posX[0] = -posY[0]; posY[0] = temp; temp = posX[2]; posX[2] = -posY[2]; posY[2] = temp; temp = posX[3]; posX[3] = -posY[3]; posY[3] = temp; posX[0] += posX[1]; //add ( posX[1], posY[1] ) back into the other posX[2] += posX[1]; //points posX[3] += posX[1]; posY[0] += posY[1]; posY[2] += posY[1]; posY[3] += posY[1]; boolean recover = false; for( int i = 0; i < 4 && !recover; i++ ) { if( posX[i] < 0 || posX[i] > 9 || posY[i] > 19 || posY[i] < 0 ) recover = true; else if( posX[i] < 10 && posX[i] > 0 && posY[i] < 20 && posY[i] > 0 ) if( map[posX[i]][posY[i]] != Color.white ) recover = true; } if( recover ) { for( int i = 0; i < 4; i++ ) { posX[i] = backX[i]; posY[i] = backY[i]; } } } public boolean isFrozen( Color[][] map ) { for( int i = 0; i < 4; i++ ) { if( posY[i] == 19 ) return true; if( map[posX[i]][posY[i]+1] != Color.white ) return true; } return false; } public void frozen( Color[][] map ) { for( int i = 0; i < 4; i++ ) { map[posX[i]][posY[i]] = color; } } }