package alarm.puzzle;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class PuzzleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_ImageButtons[0]= (ImageButton)findViewById(R.id.Button01);
_ImageButtons[1]= (ImageButton)findViewById(R.id.Button02);
_ImageButtons[2]= (ImageButton)findViewById(R.id.Button03);
_ImageButtons[3]= (ImageButton)findViewById(R.id.Button04);
_ImageButtons[4]= (ImageButton)findViewById(R.id.Button05);
_ImageButtons[5]= (ImageButton)findViewById(R.id.Button06);
_ImageButtons[6]= (ImageButton)findViewById(R.id.Button07);
_ImageButtons[7]= (ImageButton)findViewById(R.id.Button08);
_ImageButtons[8]= (ImageButton)findViewById(R.id.Blank);
int i;
for (i=0;i<8;i++) _ImageButtons[i].setOnClickListener(on_Click);
((Button) findViewById(R.id.btStart)).setOnClickListener(on_Start);
}
private ImageButton _ImageButtons[]=new ImageButton[9];
private void do_SwapButtons(ImageButton A,ImageButton B){
int Left=A.getLeft();
int Top=A.getTop();
int Right=A.getRight();
int Buttom=A.getBottom();
A.layout(B.getLeft(), B.getTop(), B.getRight(), B.getBottom());
B.layout(Left,Top,Right,Buttom);
}
private boolean is_Vert(ImageButton A,ImageButton B){
return(A.getLeft()==B.getLeft())&&(Math.abs(A.getTop()-B.getTop())<50);
}
private boolean is_Hori(ImageButton A,ImageButton B){
return (A.getTop() == B.getTop()) && (Math.abs(A.getLeft() - B.getLeft()) < 50);
}
private View.OnClickListener on_Click = new View.OnClickListener() {
public void onClick(View v) {
ImageButton A=(ImageButton)v;
ImageButton Blank=(ImageButton)findViewById(R.id.Blank);
if(is_Vert(A,Blank)||is_Hori(A,Blank)) do_SwapButtons((ImageButton) A,Blank);
}
};
private View.OnClickListener on_Start = new View.OnClickListener() {
public void onClick(View v){
int x, y;
for(y = 0 ; y < 3 ; y++){
for(x = 0 ; x < 3 ; x++){
_ImageButtons[y * 3 + x].layout(x * 48, y * 48, x * 48 + 48, y * 48 + 48);
}
}
Random _Random = new Random();
int i;
for(i = 0 ; i < 1000 ; i++) on_Click.onClick(_ImageButtons[_Random.nextInt(8)]);
}
};
}
=================================================================================
안드로이드를 배우고있는학생입니다
류종택님 강좌에있는 동영상강의를 보고만든 퍼즐을 만들었는데요
궁금한점이있습니다
퍼즐을 맟춘후에 다완료되었다는 메시지를 출력하고싶은데 어떤식으로 해야 가능한가요?
처음에 원래의 맞는 위치를 지정한다음 다맞춘다음에 비교를해서 빠져나오는식으로하면 될듯한데 어렵네요