public class Field{
	private Tetoris tetoris;
	private Block block[][] = new Block[7][4];
	private int tokuten;
	private int level;
	private boolean gameover;
	private boolean stealthmode;
	private boolean hidden_mode;
	private boolean baisokumode;
	private int ten[] ={1,3,6,10};
	private boolean next[][] = new boolean[4][4];
	private boolean prev[][] = new boolean[4][4];
	private int prev_x;
	private int prev_y;
	private int prev_block_number_x;
	private int prev_block_number_y;
	private int next_block_number_x;
	private int next_block_number_y;
	public  boolean henkan = false;
	private int blockarray[][] = {
		                         {1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
								 {0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0},
								 {0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0},
								 {0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
								 {1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0},
								 {0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},
								 {1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
								 {0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},
								 {0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
								 {1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
								 {1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0},
								 {0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0},
								 {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
								 {0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0},
								 {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
								 {0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0},
								 {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
								 {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
								 {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
								 {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
								 {0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0},
								 {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
								 {0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0},
								 {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
								 {0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0},
								 {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
								 {0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0},
								 {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
	                             };
	private boolean a[][] = new boolean[30][10];
	public Field(Tetoris tetoris){
		this.tetoris = tetoris;
		this.tokuten = 0;
		this.level = 1;
		this.gameover = false;
		this.baisokumode = false;
		this.stealthmode = false;
		this.hidden_mode = false;
		for(int i = 0;i < 30;i++){
			for(int j = 0;j < 10;j++){
				a[i][j] = false;
			}
		}
	}
	public void rowCompleteCheck(){
		int count = 0;
		for(int i = 0;i < 30;i++){
			if(subRowCompleteCheck(i)){
				count++;
			}
		}
		if(count > 0){
			addTokuten(ten[count - 1]);
			upLevelCheck(ten[count - 1]);
		}
	}
	public boolean subRowCompleteCheck(int retu){
		for(int j = 0; j < 10;j++){
			if(!a[retu][j]){
				return false;
			}
		}
		deleteRow(retu);
		tetoris.repaint();
		return true;
	}
	public void deleteRow(int retu){
		int j = retu - 1;
		for(int i = j;i >= 0;i--){
			for(int k = 0;k < 10;k++){
				a[i + 1][k] = a[i][k];
			}
		}
		for(int i = 0;i < 10;i++){
			a[0][i] = false;
		}
	}
	public boolean masuOutCheck(int x,int y){
		if(topbottomCheck(x,y)){
			return true;
		}
		if(sideCheck(x,y)){
			return true;
		}
		return false;
	}
	public boolean sideCheck(int x,int y){
		if(y < 0 || y >= 10){
			return true;
		}
		return false;
	}
	public boolean topbottomCheck(int x,int y){
		if(x < 0 || x >= 30){
			return true;
		}
		return false;
	}
	public boolean blockPildCheck(){
		for(int i = 0;i < 4;i++){
			for(int j = 0;j < 4;j++){
				if(!masuOutCheck(prev_x + i,prev_y + j)){
					if(prev[i][j] && a[prev_x + i][prev_y + j]){
						return true;
					}
				}
			}
		}
		return false;
	}
	public boolean blockOutCheck(){
		for(int i = 0;i < 4;i++){
			for(int j = 0;j < 4;j++){
				if(masuOutCheck(prev_x + i,prev_y + j)){
					if(prev[i][j]){
						return true;
					}
				}
			}
		}
		return false;
	}
	public void addTokuten(int tensu){
		tokuten = tokuten + tensu;
	}
	public boolean specialGetMasuCheck(int x,int y){
		return a[x][y];
	}
	public void specialSetMasuCheck(int x,int y,boolean atai){
		a[x][y] = atai;
	}
	public boolean getGameOver(){
		return gameover;
	}
	public boolean getStealthmode(){
		return stealthmode;
	}
	public boolean getHidden_mode(){
		return hidden_mode;
	}
	public boolean getBaisokumode(){
		return baisokumode;
	}
	public void setStealthmode(boolean stealthmode){
		this.stealthmode = stealthmode;
	}
	public void setHidden_mode(boolean hidden_mode){
		this.hidden_mode = hidden_mode;
	}
	public void setBaisokumode(boolean baisokumode){
		this.baisokumode = baisokumode;
	}
	public int getTokuten(){
		return tokuten;
	}
	public int getLevel(){
		return level;
	}
	public void blockDecide(){
		for(int i = 0;i < 4;i++){
			for(int j = 0;j < 4;j++){
				if(!masuOutCheck(prev_x + i,prev_y + j)){
					if(prev[i][j]){
						a[prev_x + i][prev_y + j] = true;
					}
				}
			}
		}
		rowCompleteCheck();
		gameOverCheck();
		blockInput();
		prepareBlockFall();
		prepareNextPop();
	}
	public void prepareBlockFall(){
		for(int i = 0;i < 4;i++){
			for(int j = 0;j < 4;j++){
				prev[i][j] = next[i][j];
			}
		}
		prev_x = -4;
		prev_y = 3;
		prev_block_number_x = next_block_number_x;
		prev_block_number_y = next_block_number_y;
		tetoris.repaint();
	}
	public void prepareNextPop(){
		int i = (int)(Math.random() * 7);
		int j = (int)(Math.random() * 4);
		next = block[i][j].getBlock();
		next_block_number_x = i;
		next_block_number_y = j;
		tetoris.repaint();
	}
	public void setPrev_x(int x){
		prev_x = prev_x + x;
	}
	public void setPrev_y(int y){
		prev_y = prev_y + y;
	}
	public void blockInput(){
		for(int i = 0;i < 7;i++){
			for(int j = 0;j < 4;j++){
				block[i][j] = new Block(blockarray[i*4+j]);
			}
		}
	}
	public int getPrev_x(){
		return prev_x;
	}
	public int getPrev_y(){
		return prev_y;
	}
	public boolean getPrev(int i,int j){
		return prev[i][j];
	}
	public boolean getNext(int i,int j){
		return next[i][j];
	}
	public void blockAroundSet(){
		int save_number = prev_block_number_y;
		prev_block_number_y++;
		if(prev_block_number_y == 4){
			prev_block_number_y = 0;
		}
		prev = block[prev_block_number_x][prev_block_number_y].getBlock();
		if(blockOutCheck() || blockPildCheck()){
			prev = block[prev_block_number_x][save_number].getBlock();
			prev_block_number_y = save_number;
		}
		tetoris.repaint();
	}
	public void gameOverCheck(){
		for(int x = 3;x < 7;x++){
			if(a[0][x]){
				gameover = true;
			}
		}
	}
	public void initialize(){
		for(int i = 0;i < 30;i++){
			for(int j = 0;j < 10;j++){
				a[i][j] = false;
			}
		}
	}
	public void upLevelCheck(int ten){
		int b = (tokuten - ten) / 10;
		int c = tokuten / 10;
		if(b != c){
			upLevel();
		}
		tetoris.repaint();
	}
	public void upLevel(){
		level++;
		if(level > 25){
			level = 25;
		}
		tetoris.setSpeed();
		tetoris.repaint();
	}
}
		