package Puzzle.game;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class EightPuzzle extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    private Button button[] = new Button[9]; //버튼 배열 생성
    String[] buttons = new String[9];
    
    
    
    private int left,right, top ,bottom;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eightpuzzle);
        
        //버튼배열에 버튼대입
        button[0] = (Button)findViewById(R.id.b1);
        button[1] = (Button)findViewById(R.id.b2);
        button[2] = (Button)findViewById(R.id.b3);
        button[3] = (Button)findViewById(R.id.b4);
        button[4] = (Button)findViewById(R.id.b5);
        button[5] = (Button)findViewById(R.id.b6);
        button[6] = (Button)findViewById(R.id.b7);
        button[7] = (Button)findViewById(R.id.b8);
        button[8] = (Button)findViewById(R.id.blank);
        
        int i;
        for(i=0; i<8; i++){
         button[i].setOnClickListener(on_click);
        }
        Button btn = (Button)findViewById(R.id.start);
        btn.setOnClickListener(this);
        
        Button prevbtn = (Button)findViewById(R.id.prev);
        prevbtn.setOnClickListener(prevCiclk);
    }
    
    private void SwapButton(Button A, Button B){
     this.left = A.getLeft();
     this.top = A.getTop();
     this.right = A.getRight();
     this.bottom = A.getBottom();
     
     A.layout(B.getLeft(), B.getTop(), B.getRight(), B.getBottom());
     B.layout(this.left, this.top, this.right, this.bottom);
 
    }
    
 
 public boolean Vert(Button A, Button B){
  return (A.getLeft() == B.getLeft() && (Math.abs(A.getTop() - B.getTop())<110));
 }
 public boolean Hori(Button A, Button B){
  return (A.getTop() == B.getTop() && (Math.abs(A.getLeft() - B.getLeft())<110));
 }
 
 
 private View.OnClickListener on_click = new View.OnClickListener() {
  
  public void onClick(View v) {
   // TODO Auto-generated method stub
   Button A = (Button) v;
   Button blank = (Button)findViewById(R.id.blank);
   if(Vert(A,blank) || Hori(A,blank)){
    
   SwapButton((Button) v, blank);
   
   }
   
      boolean bWinLose = false;
         int num = 3;
          for (int i = 0 ; i < num ; i++)        {
            for (int j = 0 ;  j < num ; j++)
            {
              bWinLose = Vert(button[i*num], button[i*num+j]) && Hori(button[i+num], button[j*num+i]); 
 
              if (!bWinLose) return;
            }
          }
      if (bWinLose){
       TextView tv = (TextView) findViewById(R.id.textView1);
         tv.setText("성공");
      }   
   
  
  }
 };
 
 
 //START 버튼 누를시 발생
 public void onClick(View v) {
  // TODO Auto-generated method stub
  int x,y;
  for(y=0; y<3; y++){
   for(x=0; x<3; x++){
    button[y*3 + x].layout(x*100, y*100,x*100+100,y*100+100);
   }
  }
  
  Random rand = new Random();
  
  int i;
  for(i=0; i<1000; i++){
   on_click.onClick(button[rand.nextInt(9)]); //랜덤으로 숫자를 썪어준다.
  }
 }
 //이전으로 가는 버튼리스너
 public View.OnClickListener prevCiclk = new OnClickListener() {
  
  public void onClick(View v) {
   // TODO Auto-generated method stub
      Intent Eightintent = new Intent();
      Eightintent.setClass(EightPuzzle.this, main.class);
      startActivity(Eightintent);
  }
 };
}

 

아무리해도.. 성공이라는 텍스트뷰가 안떠요 ㅠㅠ  뭐가 문젠지 확인좀 해주세요 ㅠㅠ