안드로이드 개발 질문/답변
(글 수 45,052)
일정한 좌표를 지정해 둔 다음에 랜덤값을 돌려서 이미지를 뿌려주는데요.
액티비티가 새로 시작될 때마다 퍼즐맞추듯이 뿌려지는 이미지의 순서를 랜덤으로 하려고 합니다.
일렬로 이미지를 뿌리는데 이미지 수를 num_more에 맞춰서 했더니..
3장까지는 무리없이 돌아가는데요.
4장부터 java.lang.StackOverflowError 라고 떠버리네요..;;;
함수 자체를 다시 돌리고 다시 돌리고 해서 그런거 같은데요..
어떤 형식으로 바꿔서 해야 할까요? ㅠ
public void setpos(){
int XY[][] = {{41,360},{81,360},{121,360},{161,360},{201,360},{241,360}};
Random r = new Random();
int index = 0;
int num_more = 0;
if(stage>=1&&stage<=2){num_more = 2;}
if(stage>=3&&stage<=5){num_more = 3;}
if(stage>=6&&stage<=9){num_more = 4;}
if(stage>=10&&stage<=14){num_more = 5;}
if(stage>=15&&stage<=20){num_more = 6;}
int ran = 0, ran1 = 0, ran2 = 0, ran3 = 0, ran4 = 0;
for(int i = 0; i<num_more;i++){
index = r.nextInt(num_more);
if(num_more==2){
if(i==0){
Log.d("test","1 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x1 = XY[index][0];
bottom_y = XY[index][1];
ran = index;
}
else if(i==1){
if(ran==index){setpos();}
else {
Log.d("test","2 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x2 = XY[index][0];
bottom_y = XY[index][1];
}
}
}
if(num_more==3){
Log.d("test","i = "+i);
if(i==0){
Log.d("test","3_1 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x1 = XY[index][0];
bottom_y = XY[index][1];
ran = index;
}
else if(i==1){
if(ran==index){setpos();}
else {
Log.d("test","3_2 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x2 = XY[index][0];
bottom_y = XY[index][1];
ran1 = index;
}
}
else if(i==2){
if(ran==index || ran1==index){setpos();}
else {
Log.d("test","3_3 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x3 = XY[index][0];
bottom_y = XY[index][1];
}
}
}
if(num_more==4){
Log.d("test","i = "+i);
if(i==0){
Log.d("test","4_1 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x1 = XY[index][0];
bottom_y = XY[index][1];
ran = index;
}
else if(i==1){
if(ran==index){setpos();}
else {
Log.d("test","4_2 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x2 = XY[index][0];
bottom_y = XY[index][1];
ran1 = index;
}
}
else if(i==2){
if(ran==index || ran1==index){setpos();}
else {
Log.d("test","4_3 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x3 = XY[index][0];
bottom_y = XY[index][1];
ran2 = index;
}
}
else if(i==3){
if(ran==index || ran1==index || ran2==index){setpos();}
else {
Log.d("test","4_4 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x4 = XY[index][0];
bottom_y = XY[index][1];
}
}
}
if(num_more==5){
if(i==0){
Log.d("test","5_1 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x1 = XY[index][0];
bottom_y = XY[index][1];
ran = index;
}
else if(i==1){
if(ran==index){setpos();}
else {
Log.d("test","5_2 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x2 = XY[index][0];
bottom_y = XY[index][1];
ran1 = index;
}
}
else if(i==2){
if(ran==index || ran1==index){setpos();}
else {
Log.d("test","5_3 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x3 = XY[index][0];
bottom_y = XY[index][1];
ran2 = index;
}
}
else if(i==3){
if(ran==index || ran1==index || ran2==index){setpos();}
else {
Log.d("test","5_4 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x4 = XY[index][0];
bottom_y = XY[index][1];
ran3 = index;
}
}
else if(i==4){
if(ran==index || ran1==index || ran2==index || ran3==index){setpos();}
else {
Log.d("test","5_5 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x5 = XY[index][0];
bottom_y = XY[index][1];
}
}
}
if(num_more==6){
Log.d("test","i = "+i);
if(i==0){
Log.d("test","6_1 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x1 = XY[index][0];
bottom_y = XY[index][1];
ran = index;
}
else if(i==1){
if(ran==index){setpos();}
else {
Log.d("test","6_2 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x2 = XY[index][0];
bottom_y = XY[index][1];
ran1 = index;
}
}
else if(i==2){
if(ran==index || ran1==index){setpos();}
else {
Log.d("test","6_3 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x3 = XY[index][0];
bottom_y = XY[index][1];
ran2 = index;
}
}
else if(i==3){
if(ran==index || ran1==index || ran2==index){setpos();}
else {
Log.d("test","6_4 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x4 = XY[index][0];
bottom_y = XY[index][1];
ran3 = index;
}
}
else if(i==4){
if(ran==index || ran1==index || ran2==index || ran3==index){setpos();}
else {
Log.d("test","6_5 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x5 = XY[index][0];
bottom_y = XY[index][1];
ran4 = index;
}
}
else if(i==5){
if(ran==index || ran1==index || ran2==index || ran3==index || ran4==index){setpos();}
else {
Log.d("test","6_6 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x6 = XY[index][0];
bottom_y = XY[index][1];
}
}
}
}//for
}
액티비티가 새로 시작될 때마다 퍼즐맞추듯이 뿌려지는 이미지의 순서를 랜덤으로 하려고 합니다.
일렬로 이미지를 뿌리는데 이미지 수를 num_more에 맞춰서 했더니..
3장까지는 무리없이 돌아가는데요.
4장부터 java.lang.StackOverflowError 라고 떠버리네요..;;;
함수 자체를 다시 돌리고 다시 돌리고 해서 그런거 같은데요..
어떤 형식으로 바꿔서 해야 할까요? ㅠ
public void setpos(){
int XY[][] = {{41,360},{81,360},{121,360},{161,360},{201,360},{241,360}};
Random r = new Random();
int index = 0;
int num_more = 0;
if(stage>=1&&stage<=2){num_more = 2;}
if(stage>=3&&stage<=5){num_more = 3;}
if(stage>=6&&stage<=9){num_more = 4;}
if(stage>=10&&stage<=14){num_more = 5;}
if(stage>=15&&stage<=20){num_more = 6;}
int ran = 0, ran1 = 0, ran2 = 0, ran3 = 0, ran4 = 0;
for(int i = 0; i<num_more;i++){
index = r.nextInt(num_more);
if(num_more==2){
if(i==0){
Log.d("test","1 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x1 = XY[index][0];
bottom_y = XY[index][1];
ran = index;
}
else if(i==1){
if(ran==index){setpos();}
else {
Log.d("test","2 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x2 = XY[index][0];
bottom_y = XY[index][1];
}
}
}
if(num_more==3){
Log.d("test","i = "+i);
if(i==0){
Log.d("test","3_1 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x1 = XY[index][0];
bottom_y = XY[index][1];
ran = index;
}
else if(i==1){
if(ran==index){setpos();}
else {
Log.d("test","3_2 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x2 = XY[index][0];
bottom_y = XY[index][1];
ran1 = index;
}
}
else if(i==2){
if(ran==index || ran1==index){setpos();}
else {
Log.d("test","3_3 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x3 = XY[index][0];
bottom_y = XY[index][1];
}
}
}
if(num_more==4){
Log.d("test","i = "+i);
if(i==0){
Log.d("test","4_1 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x1 = XY[index][0];
bottom_y = XY[index][1];
ran = index;
}
else if(i==1){
if(ran==index){setpos();}
else {
Log.d("test","4_2 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x2 = XY[index][0];
bottom_y = XY[index][1];
ran1 = index;
}
}
else if(i==2){
if(ran==index || ran1==index){setpos();}
else {
Log.d("test","4_3 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x3 = XY[index][0];
bottom_y = XY[index][1];
ran2 = index;
}
}
else if(i==3){
if(ran==index || ran1==index || ran2==index){setpos();}
else {
Log.d("test","4_4 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x4 = XY[index][0];
bottom_y = XY[index][1];
}
}
}
if(num_more==5){
if(i==0){
Log.d("test","5_1 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x1 = XY[index][0];
bottom_y = XY[index][1];
ran = index;
}
else if(i==1){
if(ran==index){setpos();}
else {
Log.d("test","5_2 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x2 = XY[index][0];
bottom_y = XY[index][1];
ran1 = index;
}
}
else if(i==2){
if(ran==index || ran1==index){setpos();}
else {
Log.d("test","5_3 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x3 = XY[index][0];
bottom_y = XY[index][1];
ran2 = index;
}
}
else if(i==3){
if(ran==index || ran1==index || ran2==index){setpos();}
else {
Log.d("test","5_4 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x4 = XY[index][0];
bottom_y = XY[index][1];
ran3 = index;
}
}
else if(i==4){
if(ran==index || ran1==index || ran2==index || ran3==index){setpos();}
else {
Log.d("test","5_5 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x5 = XY[index][0];
bottom_y = XY[index][1];
}
}
}
if(num_more==6){
Log.d("test","i = "+i);
if(i==0){
Log.d("test","6_1 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x1 = XY[index][0];
bottom_y = XY[index][1];
ran = index;
}
else if(i==1){
if(ran==index){setpos();}
else {
Log.d("test","6_2 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x2 = XY[index][0];
bottom_y = XY[index][1];
ran1 = index;
}
}
else if(i==2){
if(ran==index || ran1==index){setpos();}
else {
Log.d("test","6_3 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x3 = XY[index][0];
bottom_y = XY[index][1];
ran2 = index;
}
}
else if(i==3){
if(ran==index || ran1==index || ran2==index){setpos();}
else {
Log.d("test","6_4 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x4 = XY[index][0];
bottom_y = XY[index][1];
ran3 = index;
}
}
else if(i==4){
if(ran==index || ran1==index || ran2==index || ran3==index){setpos();}
else {
Log.d("test","6_5 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x5 = XY[index][0];
bottom_y = XY[index][1];
ran4 = index;
}
}
else if(i==5){
if(ran==index || ran1==index || ran2==index || ran3==index || ran4==index){setpos();}
else {
Log.d("test","6_6 : XY[][] = "+XY[index][0]+", XY[][] = "+XY[index][1]);
bottom_x6 = XY[index][0];
bottom_y = XY[index][1];
}
}
}
}//for
}
setpos() 안에 for문에서setpost()를 계속해서 호출하고 있는데요... break를 걸어주면 되지 않을까요? 소스가.. 길어서.. 자세히는 못봤는데 혹시나 해서요 ㅋ;;