/**    The Col class 

  *    by Linh Pham. 
  *
  *    This class represents the actual columns that fall down the field
  *    It knows how to paint itself, how to move left and right, how to fall down
  *    the playing field, and so on. 

*/



import java.lang.Math;   // For random numbers
import java.awt.*;


public class Col {

	int posX, posY;    // The column's position corresponds to its bottom block 
	byte block[] = new byte[3];   // These are the colors of the block BOTTOM TO TOP!

	public Col() {   //The constructor
		posX = 4;
		posY = 0;

		for (int i = 0; i < 3; i++) {   // Fill 'em up with random colors


			// Colors are labeled from 1 to 8, not 0 to 7, 'cuz 0 signifies an empty space on the
			// playing field.


			block[i] = (byte)((8.0 * Math.random())+1);  
		}
	}
	// This function translates the numbers of our blocks to an appropriate color
	public Color findColor (byte colornumber) {
			Color fillcolor;
			int r, g, b;
			switch (colornumber) {
				case 0:
					r=0; g=0; b=0;
					break;
				case 1:
					r=238; g=154; b=21;
					break;
				case 2:
					r=54; g=193; b=26;
					break;
				case 3:
					r=194; g=28; b=209;
					break;
				case 4:
					r=51; g=32; b=195;
					break;
				case 5:
					r=210; g=24; b=24;
					break;
				case 6:
					r=240; g=242; b=43;
					break;
				case 7:
					r=55; g=232; b=196;
					break;
				case 8:
					r=172;g=172;b=172;
					break;
				case 9:
					r=0; g=0; b=0;
					break;
				default:
					r=0; g=0; b=0;
					break;
			}
			fillcolor = new Color(r,g,b);
			return fillcolor;
	}

	public void paint (Graphics g) {   // Painting the column normally in the field
		int drawX, drawY;

		for (int i =0; i < 3; i++) {
			drawX = posX * 25;
			drawY = (posY - i)*25;
			if (drawY >=0) {
				g.setColor(findColor(block[i]));
				g.fillRect(drawX,drawY,25,25);
				g.fill3DRect(drawX+4,drawY+4,17,17,true);

			}
		}
	}

	public void paintNext (Graphics g) { // Sneak preview of the next column
		int drawX, drawY;
		for (int i =0; i < 3; i++) {
			drawX = 210;
			drawY = 190 - (i*25);
			g.setColor(findColor(block[i]));
			g.fillRect(drawX,drawY,25,25);
			g.fill3DRect(drawX+4, drawY+4, 17, 17, true);
		}
	}


	public void moveLeft(byte[][] field) {
		if (posX == 0) return;   // Can't move when we're already as far left as we can go!

		for (int i = 0; i < 3; i++) {
			if ((posY - i) > -1) {  // Don't want to check when out of bounds of array
				if ((field[posX-1][posY-i]) != 0)  // Something in the way
					return;
			}
		}

		// Okay, everything checks out. Move the shape!

		posX--;
	}


	public void moveRight(byte[][] field) {
		if (posX == 7) return;   // Can't move when we're already as far left as we can go!

		for (int i = 0; i < 3; i++) {
			if ((posY - i) > -1) {  // Don't want to check when out of bounds of array
				if ((field[posX+1][posY-i]) != 0)  // Something in the way
					return;
			}
		}
		// Okay, everything checks out. Move the shape!
		posX++;
	}


    public void rotate () {
	// Stupid Programming Tricks I learned from Prof. Blake
	// How to swap three variables without using a temp variable
	
	block[1] ^= block[0];
	block[2] ^= block[1];
	block[0] ^= block[2];
	block[1] ^= block[0];
	block[2] ^= block[1];
	block[0] ^= block[2];
	block[1] ^= block[0];
	
	return;
	
	

    }
    
    public boolean haveRoom(byte[][] field) {
	if (posY == 17) 
	    return false;
	if (field[posX][posY+1] == 0)
	    return true;
	return false;
    }
        
    public void drop() {
	posY++;
	}

    public boolean checkGameOver() {   // Checks to see if any part of the current column is drawn
	// off the screen
	return (posY < 2);
    }

    public void freeze (byte[][] field) {   // Adds the current block to the field.
	for (int i = 0; i < 3; i++) {
	    field[posX][posY - i] = block[i];
	}
	
    }
    
    
}

