package com.puzzle;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class puzzle extends Activity {
/*Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_buttons[0]=(Button)findViewById(R.id.Button01);
_buttons[1]=(Button)findViewById(R.id.Button02);
_buttons[2]=(Button)findViewById(R.id.Button03);
_buttons[3]=(Button)findViewById(R.id.Button04);
_buttons[4]=(Button)findViewById(R.id.Button05);
_buttons[5]=(Button)findViewById(R.id.Button06);
_buttons[6]=(Button)findViewById(R.id.Button07);
_buttons[7]=(Button)findViewById(R.id.Button08);
_buttons[8]=(Button)findViewById(R.id.Blank);
for(int i=0; i<8; i++) _buttons[i].setOnClickListener(On_Click); //각 버튼에 대한 리스너 등록
((Button)findViewById(R.id.btstart)).setOnClickListener(On_Start); //start 버튼에 대한 리스너 등록
}
private Button[] _buttons = new Button[10];
private void do_swap(Button A, Button B){ //버튼의 대한 위치 바꾸기
int Aleft = A.getLeft();
int Atop = A.getTop();
int Aright = A.getRight();
int Abottom = A.getBottom();
A.layout(B.getLeft(), B.getTop(), B.getRight(), B.getBottom());
B.layout(Aleft, Atop, Aright, Abottom);
}
private boolean do_width(Button A, Button B){ //해당 버튼(A)의 대한 x값과 Blank 버튼(B)의 대한 x값을 비교하여 결과를 리턴한다.
return (A.getLeft()==B.getLeft())&&(Math.abs(A.getTop()-B.getTop())<50); //Math.ads() 절대값을 반환 한다.
}
private boolean do_high(Button A, Button B){ //해당 버튼(A)의 대한 y값과 Blank 버튼(B)의 대한 y값을 비교하여 결과를 리턴한다.
return (A.getTop()==B.getTop())&&(Math.abs(A.getLeft()-B.getLeft())<50);
}
private View.OnClickListener On_Click = new View.OnClickListener() { //해당 버튼 클릭에 대한 action
public void onClick(View v) {
Button Blank = (Button)findViewById(R.id.Blank);
if(do_high((Button) v,Blank) || do_width((Button) v,Blank))do_swap((Button)v,Blank);
}
};
private View.OnClickListener On_Start = new View.OnClickListener(){ //start 버튼 클릭시 random 으로 섞기
public void onClick(View v){
int y,x;
for(y=0;y<3;y++){
for(x=0;x<3;x++){
_buttons[y*3+x].layout(x*48, y*48, x*48+48, y*48+48);
}
}
Random _Random= new Random();
for(int i=0; i<1000; i++) On_Click.onClick(_buttons[_Random.nextInt(8)]);
}
};
}
퍼즐게임 을 만들엇는데 클리어시 클리어라고 뜨고 다시 스타트버튼을 누르면 재시작되게 만들고 싶은데 어떻게 해야하는지 모르겟네요
조언좀 부탁드립니다
현재는 사진 첨부한것과 같이 원래대로 퍼즐을 마추어 클리어되도 그냥 무한반복으로 계속 할수있습니다 이걸 수정하고 싶네요




클리어 조건은 퍼즈리 완성 되었을 경우의 좌표를 기준으로
각 퍼즐 조각의 좌표를 저장합니다.
그리고 사용자가 퍼즐을 움직일 경우 각 뷰마다 boolean값으로 완성된 위치에 왔는지 값을 설정해주고
모든 뷰의 boolean값을 체크하여 클리어 조건을 달성합니다.