import java.lang.*;
import java.awt.*;
import scoreboard;
import constants;

public class board extends constants{
	int life_here = 0;
	int score = 0;
	int level = 1;
	int lines = 0;

	box grid[][];

	int xoff = 2; 
	int yoff = 2;

	boolean emboss = true;
	boolean drawgrid;
	boolean playsample = false;

	int width, height;

	board(int h, int w, int xoffset, int yoffset, boolean d, int scale){
		drawgrid = d;
		width = w;
		height = h;

		grid = new box[height][width];
		for (int i = 0; i < height; i++){
			for (int j = 0; j < width; j++){
				grid[i][j] = new box(j,i,xoffset,yoffset,scale);
				grid[i][j].setBox(EMPTY);	
			}
		}
	}

	public void drawBoard(Graphics g){
		for (int i = 0; i < height - 1; i++){
			for (int j = 1; j < width - 1; j++){
				grid[i][j].paintBox(emboss,drawgrid, g);
			}
		}
	}

	public int getScore(){
		return score;
	}

	public int getLevel(){
		return level;
	}

	public int getLines(){
		return lines;
	}

	int deleteRows(piece p){
		int tmplines = 0;
		playsample = false;

		for (int i = 0; i < height - 1; i++){
			if (checkRow(i)){
				killRow(i);
				tmplines++;
			}
		}

		lines += tmplines; 

		if ((lines - level * 10) >= 10){
			level += 1;
		}

		switch (tmplines){
			case 0:
			case 1:
				return 0;
			case 2:
				playsample = true;
				return 25;
			case 3:
				playsample = true;
				return 100;
			case 4:
				playsample = true;
				return 400;
		}
		return 0;
	}

	boolean checkRow(int y){

		for (int i = 1; i < width - 1; i ++){
			if (!grid[y][i].solid) return false;
		} 
		return true;
	}

	void killRow(int y){
		for (int i = y; i > 0; i --){
			for (int j = 1; j < width - 1; j ++){
				grid[i][j].solid = grid [i-1][j].solid;
				grid[i][j].hilight = grid [i-1][j].hilight;
				grid[i][j].shadow = grid [i-1][j].shadow;
				grid[i][j].normal = grid [i-1][j].normal;
				grid[i][j].boxcolor = grid[i-1][j].boxcolor;
			}
		}

		for (int j = 1; j < width - 1; j ++){
			grid[0][j].setBox(EMPTY);			
		}
	}

	public void doLife(){
		int newgrid[][];
		int counter = 0;
		int colortotal = 0;
		newgrid = new int[height][width];
		int upoff, downoff, leftoff, rightoff;
		life_here = 1;  /* take this out to initialize board to a blinker */
		if (life_here == 0) {
			System.out.println("Initting");
			for (int i = 0; i < height; i++) {
				for (int j = 1; j < width; j++) {
					grid[i][j].setBox(0);
				}
			}
			grid[10][5].solid = true;
			grid[10][5].setBox(1);
			grid[11][5].solid = true;
			grid[11][5].setBox(1);
			grid[12][5].solid = true;
			grid[12][5].setBox(1);
			life_here = 1;
			return;
		}
 
		for (int i = 0; i < height - 1; i ++){
			if (i == 0) upoff = height - 1;
			else upoff = i - 1;
			if (i == height - 1) downoff = 0;
			else downoff = i + 1;

			for (int j = 1; j < width -1; j ++){
				counter = 0; colortotal = 0;
				if (j == 1) leftoff = width - 2;
				else leftoff = j - 1;
				if (j == width - 2) rightoff = 1;
				else rightoff = j + 1;

				if (grid[upoff][leftoff].solid){
					counter ++;
					colortotal += grid[upoff][leftoff].boxcolor;
				}
				if (grid[upoff][j].solid){
					counter ++;
					colortotal += grid[upoff][j].boxcolor;
				}
				if (grid[upoff][rightoff].solid){
					counter ++;
					colortotal += grid[upoff][rightoff].boxcolor;
				}
				if (grid[i][leftoff].solid){
					counter ++;
					colortotal += grid[i][leftoff].boxcolor;
				}
				if (grid[i][rightoff].solid){
					counter ++;
					colortotal += grid[i][rightoff].boxcolor;
				}
				if (grid[downoff][leftoff].solid){
					counter ++;
					colortotal += grid[downoff][leftoff].boxcolor;
				}
				if (grid[downoff][j].solid){
					counter ++;
					colortotal += grid[downoff][j].boxcolor;
				}
				if (grid[downoff][rightoff].solid){
					counter ++;
					colortotal += grid[downoff][rightoff].boxcolor;
				}
				if (grid[i][j].solid){
					counter ++;
					colortotal += grid[i][j].boxcolor;
				}
//				System.out.print(counter+"  ");
				if (counter > 2 && counter < 5){
					newgrid[i][j] = grid[i][j].boxcolor+1;
					if (newgrid[i][j] > 7) newgrid[i][j] = 7; 	
				}
				else newgrid[i][j] = 0;
			}
//			System.out.println();
		}

		for (int i = 0; i < height - 1; i ++){
			for (int j = 1; j < width - 1; j ++){
				grid[i][j].solid = true;
				grid[i][j].setBox(newgrid[i][j]);
			}
		}
			
	}
/*
	public void dumpboard() {
		String dump = new String();
		for (int i = 0; i < height; i++) {
			for (int j = 0; j< width; j++) {
				dump = dump + (grid[i][j].solid ? "1" : "0") + "-"+ grid[i][j].boxcolor + " ";
			}
			System.out.println(dump);
			dump = "";
		}
		System.out.println();
	}
*/
}

