public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button)findViewById(R.id.start)).setOnClickListener(aa);
Boutts[0] =(Button)findViewById(R.id.ab1);
Boutts[1] =(Button)findViewById(R.id.ab2);
Boutts[2] =(Button)findViewById(R.id.ab3);
Boutts[3] =(Button)findViewById(R.id.ab4);
Boutts[4] =(Button)findViewById(R.id.ab5);
Boutts[5] =(Button)findViewById(R.id.ab6);
Boutts[6] =(Button)findViewById(R.id.ab7);
Boutts[7] =(Button)findViewById(R.id.ab8);
Boutts[8] =(Button)findViewById(R.id.ab9);
int i;
for(i=0;i<8;i++)
Boutts[i].setOnClickListener(vd);
Boutts[i].setOnClickListener(sg);
}
public Button Boutts[] = new Button[9];
public View.OnClickListener sg = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button A = (Button) v;
Button abcd = (Button)findViewById(R.id.ab9);
do_SwapButtons(A,abcd);
}
};
public void do_SwapButtons(Button A, Button B){
int Left = A.getLeft();
int Top = A.getTop();
int Right= A.getRight();
int Bottom= A.getBottom();
A.layout(B.getLeft(),B.getTop(),B.getRight(),B.getBottom());
B.layout(Left,Top,Right,Bottom);
}
public boolean vert(Button A, Button B){
return (A.getTop() == B.getTop()) && (Math.abs(A.getLeft()- B.getLeft())<140) ;
}
public boolean hori(Button A,Button B){
return (A.getLeft()== B.getLeft()) && (Math.abs(A.getTop()- B.getTop())<140) ;
}
public View.OnClickListener vd = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button A = (Button) v;
Button abcd = (Button)findViewById(R.id.ab9);
if(vert(A,abcd) || hori(A,abcd))
do_SwapButtons(A,abcd);
}
};
public View.OnClickListener aa = new View.OnClickListener() {
@Override
public void onClick(View v) {
int x,y;
Random aaa = new Random();
for(y=0;y<3;y++){
for(x=0;x<3;x++){
Boutts[y*3+x].layout(x*130,y*130,x*130+130,y*130+130);
}
}
int i;
for(i=0;i<10;i++) sg.onClick(Boutts[aaa.nextInt(8)]);
}
};
}
소스 전체인데요
퍼즐이
1 2 3
4 5 6
7 8 " " <---- 로 맞춰졌을때 다 맞춰 졌다는 토스트를 띄우고 싶은데 어떻게해야할지 도저히 모르겟어요...ㅠㅠ
도와주세요 ㅠㅠ