* 벽에 부딪히면 통과하여 반대쪽으로 나오는 것이 아니라 그냥 그자리에 있는 걸로 해놨습니다. * 상욱이의 지적에 따라 하나의 클래스에서 모든 걸 처리하지 않고 클래스를 여러개로 분리했습니다. Class main--메인함수 클래스 {{{~cpp import java.util.*; public class main{ public static void main(String[] args) { //Board aboard=new Board(); Bug bug1=new Bug(); System.out.println("RandomWalk"); //aboard.board[0][0]++; bug1.move(); } } }}} Class Bug--이동하는 벌레 {{{~cpp import java.util.*; public class Bug{ public int x=0; public int y=0; public int way; public int count=0; public void move() { Board aboard=new Board(); aboard.board[0][0]++; while(count<24) { Random rand=new Random(); way=rand.nextInt()%8; if(way==0)//북 { if(x>0) { x=x-1; } } else if(way==1)//북동 { if(x>0 && y<4) { x=x-1; y=y+1; } } else if(way==2)//동 { if(y<4) { y=y+1; } } else if(way==3)//남동 { if(y<4 && x<4) { x=x+1; y=y+1; } } else if(way==4)//남 { if(x<4) { x=x+1; } } else if(way==5)//남서 { if(y>0 && x<4) { x=x+1; y=y-1; } } else if(way==6)//서 { if(y>0) { y=y-1; } } else if(way==7)//북서 { if(y>0 && x>0) { x=x-1; y=y-1; } } if(aboard.board[x][y]==0) { count++; } aboard.board[x][y]++; aboard.exhibit(); System.out.print("\n"); } } } }}} Class Board--벌레의 발자국이 남는 판. 발자국을 통해 바퀴벌레의 이동 상황을 보고한다. {{{~cpp import java.util.*; public class Board{ public int board[][]={ {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0} }; public void exhibit(){ for(int i=0;i<5;i++) { for(int j=0;j<5;j++){ System.out.print(board[i][j]); System.out.print("\t"); } System.out.print("\n"); } } } }}} ---- 작성자: ["Yggdrasil"] ---- * 영동아 while 문안에 중복이 넘 심하다. 조금만 고민해보면 중복 왕창 줄일 수 있을꺼야...^^ --재동[[BR]] --->확실히 빨리 배열로 테이블(?) 만들어 놓는 걸 해봐야겠습니다. --영동 * 데블스 캠프할때 저런거 해결하는 팁 나왔었던 거 같은데. LookUpTable이라고.. -- 인수 ---- ["JavaStudy2002"]