public class Two_Thinking_Tactics implements Tactics{
	int [][] hyouka = {
					  {300, -20, 10,  5,  5, 10, -20, 300},
					  {-20, -50, -2, -2, -2, -2, -50, -20},
					  { 10,  -2, -1, -1, -1, -1,  -2,  10},
					  {  5,  -2, -1, -1, -1, -1,  -2,   5},
 					  {  5,  -2, -1, -1, -1, -1,  -2,   5},
					  { 10,  -2, -1, -1, -1, -1,  -2,  10},
					  {-20, -50, -2, -2, -2, -2, -50, -20},
					  {300, -20, 10,  5,  5, 10, -20, 300}
					  };
	private int [] spx = new int[60];
	private int [] spy = new int[60];
	private int [] sscore = new int[60];
	private boolean [] sflg = new boolean[60];
	private int [] px = new int[1000];
	private int [] py = new int[1000];
	private int [] tesu = new int[1000];
	private int [] c_turn = new int[1000];
	private int [] oyano = new int[1000];
	private boolean final_check = false;
	public int where(Kyokumen kyokumen){
		int myTurn = kyokumen.getTurn();
		int zibun_stone = kyokumen.stoneCount(myTurn);
		int aite_stone = kyokumen.stoneCount(kyokumen.getothersideTurn());
		if(zibun_stone + aite_stone >= 58){
			final_check = true;
		}
		int p_x = -1;
		int p_y = -1;
		int cnt = 0;
		for(int i = 0;i < 8;i++){
			for(int j = 0;j < 8;j++){
				if(kyokumen.masuPlaceCheck(i,j,kyokumen.getTurn())){
					px[cnt] = i;
					spx[cnt] = i;
					py[cnt] = spy[cnt] = j;
					tesu[cnt] = 0;
					c_turn[cnt] = myTurn;
					oyano[cnt] = -1;
					sscore[cnt] = 9999;
					sflg[cnt] = false;
					cnt++;
				}
			}
		}
		int scnt = cnt;
		if(cnt == 0){
			return -1;
		}else{
			int [][][] hp = new int[2][64][3];
			int [] hcnt = {0,0};
			cnt = cnt - 1;
			int current_read_tesu = 0;
			while(cnt >= 0){
				cornerCheck(kyokumen);
				//とりあえず置く。
				if(tesu[cnt] < current_read_tesu){
					for(int i = 0;i < hcnt[0];i++){
						kyokumen.specialsetMasu(hp[0][i][0],hp[0][i][1],0,hp[0][i][2]);
					}
					hcnt[0] = 0;
					kyokumen.othersidesetTurn();
				}
				for(int k = 0;k < 9;k++){
					if(k != 4){
						int [] check = kyokumen.placeAbleCheck(px[cnt],py[cnt],k / 3 - 1,k % 3 - 1,kyokumen.getTurn());
						if(check[0] == 1){
							int fx = check[1] / 10;
							int fy = check[1] % 10;
							int dx = k / 3 - 1;
							int dy = k % 3 - 1;
							while(px[cnt]!= fx  || py[cnt]!= fy ){
								hp[tesu[cnt]][hcnt[tesu[cnt]]][0] = fx;
								hp[tesu[cnt]][hcnt[tesu[cnt]]][1] = fy;
								hp[tesu[cnt]][hcnt[tesu[cnt]]][2] = kyokumen.specialgetMasu(fx,fy,0);
								fx = fx - dx;
								fy = fy - dy;
								hcnt[tesu[cnt]] = hcnt[tesu[cnt]] + 1;
							}
							kyokumen.henkan(px[cnt] * 10 + py[cnt],check[1],k / 3 - 1,k % 3 - 1,kyokumen.getTurn());
						}
					}
				}
				hp[tesu[cnt]][hcnt[tesu[cnt]]][0] = px[cnt];
				hp[tesu[cnt]][hcnt[tesu[cnt]]][1] = py[cnt];
				hp[tesu[cnt]][hcnt[tesu[cnt]]][2] = kyokumen.specialgetMasu(px[cnt],py[cnt],0);
				hcnt[tesu[cnt]] = hcnt[tesu[cnt]] + 1;
				kyokumen.placeSyori(px[cnt],py[cnt]);
				current_read_tesu = tesu[cnt];
				//評価するか判定
				if(tesu[cnt] == 1){
					if(!final_check){
						System.out.println(1);
						int zibun_tensu = 0;
						int aite_tensu = 0;
						for(int i = 0;i < 8;i++){
							for(int j = 0;j < 8;j++){
								if(kyokumen.specialgetMasu(i,j,0) == myTurn){
									zibun_tensu = zibun_tensu + hyouka[i][j];
								}else{
									if(kyokumen.specialgetMasu(i,j,0) == kyokumen.getothersideTurn()){
										aite_tensu = aite_tensu + hyouka[i][j];
									}
								}
							}
						}
						int tensu = zibun_tensu - aite_tensu;
						if(sscore[oyano[cnt]] > tensu){
							sscore[oyano[cnt]] = tensu;
						}
						for(int i = 0;i < hcnt[1];i++){
							kyokumen.specialsetMasu(hp[1][i][0],hp[1][i][1],0,hp[1][i][2]);
						}
						hcnt[1] = 0;
						cnt = cnt - 1;
					}else{
						zibun_stone = kyokumen.stoneCount(myTurn);
						if(sscore[oyano[cnt]] > zibun_stone){
							sscore[oyano[cnt]] = zibun_stone;
						}
					}
				}else{
					
					int place_x = px[cnt];
					int place_y = py[cnt];
					int re_tesu = tesu[cnt];
					boolean no_henkan = true;
					//着手可能マスを探す。
					kyokumen.othersidesetTurn();
					for(int i = 0;i < 8;i++){
						for(int j = 0;j < 8;j++){
							if(kyokumen.masuPlaceCheck(i,j,kyokumen.getTurn())){
								px[cnt] = i;
								py[cnt] = j;
								tesu[cnt] = re_tesu + 1;
								if(no_henkan){
									oyano[cnt] = cnt;
								}else{
									oyano[cnt] = oyano[cnt - 1];
								}
								cnt++;
								no_henkan = false;
							}
						}
					}
					if(no_henkan){
						for(int i = 0;i < hcnt[0];i++){
							kyokumen.specialsetMasu(hp[0][i][0],hp[0][i][1],0,hp[0][i][2]);
						}
						kyokumen.setTurn(myTurn);
						return place_x * 10 + place_y;
					}
					System.out.println(cnt);
					cnt = cnt - 1;
				}
			}
			int max_score = -9999;
			int max_point[] = new int[64];
			max_point[0] = -1;
			int max_point_cnt = 1;
			for(int i = 0;i < scnt;i++){
				if(max_score <= sscore[i]){
					if(max_score < sscore[i]){
						max_score = sscore[i];
						max_point_cnt = 0;
					}
					max_point[max_point_cnt] = spx[i] * 10 + spy[i];
					max_point_cnt++;
				}
			}
			for(int i = 0;i < hcnt[0];i++){
				kyokumen.specialsetMasu(hp[0][i][0],hp[0][i][1],0,hp[0][i][2]);
			}
			hcnt[0] = 0;
			kyokumen.setTurn(myTurn);
			return max_point[(int)(Math.random()* max_point_cnt)];
		}
	}
	public void cornerCheck(Kyokumen kyokumen){
		if(kyokumen.specialgetMasu(0,0,0) != 0){
			hyouka[0][1] = hyouka[1][0] = hyouka[1][1] = 30;
		}else{
			hyouka[0][1] = hyouka[1][0] = -20;
			hyouka[1][1] = -50;
		}
		if(kyokumen.specialgetMasu(0,7,0) != 0){
			hyouka[0][6] = hyouka[1][6] = hyouka[1][7] = 30;
		}else{
			hyouka[0][6] = hyouka[1][7] = -20;
			hyouka[1][6] = -50;
		}
		if(kyokumen.specialgetMasu(7,0,0) != 0){
			hyouka[6][0] = hyouka[6][1] = hyouka[7][1] = 30;
		}else{
			hyouka[7][1] = hyouka[6][0] = -20;
			hyouka[6][1] = -50;
		}
		if(kyokumen.specialgetMasu(7,7,0) != 0){
			hyouka[6][6] = hyouka[6][7] = hyouka[7][6] = 30;
		}else{
			hyouka[6][7] = hyouka[7][6] = -20;
			hyouka[6][6] = -50;
		}
	}
}