- ... 키 했. C 하 ... 하 하...
- 형 1 해 .( )... 2 할 ... count 해 .
3 comment solving 하. 3 한, , Bug 페 함 할.
Class main
~cpp import java.util.*; public class main{ public static void main(String[] args) { Bug bug1=new Bug(); System.out.println("RandomWalk"); bug1.journey[0]='0'; bug1.journey[1]='0'; bug1.journey[2]='4'; for(int i=0;bug1.journey[i]!='\0';i++) bug1.move(bug1.journey[i]); } }Class Bug
~cpp public class Bug{ public int x=0; public int y=0; public int count=0; public char journey[]={'\0','\0','\0','\0','\0'}; Board aboard=new Board(); public void move(char way) { if(count==0) aboard.board[0][0]++; if(way=='0')// x=x-1; else if(way=='1')// { x=x-1; y=y+1; } else if(way=='2')// y=y+1; else if(way=='3')// { x=x+1; y=y+1; } else if(way=='4')// x=x+1; else if(way=='5')// { x=x+1; y=y-1; } else if(way=='6')// y=y-1; else if(way=='7')// { x=x-1; y=y-1; } if(x>=aboard.board.length) x=0; if(x<0) x=aboard.board.length-1; if(y>=aboard.board[0].length) y=0; if(y<0) y=aboard.board[0].length-1; aboard.board[x][y]++; aboard.exhibit(); System.out.print("\n"); count++; } }Class Board
~cpp 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<board.length;i++) { for(int j=0;j<board.length;j++){ System.out.print(board[i][j]); System.out.print("\t"); } System.out.print("\n"); } } }
1 ¶
- 항 합.
- word rap 하 했.
- Board.count
~cpp public class Board { public char journey[] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; public int x = 0; public int y = 0; 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 move(char way) { if (way == '0') // { x = x - 1; } else if (way == '1') // { x = x - 1; y = y + 1; } else if (way == '2') // { y = y + 1; } else if (way == '3') // { y = y + 1; x = x + 1; } else if (way == '4') // { x = x + 1; } else if (way == '5') // { y = y - 1; x = x + 1; } else if (way == '6') // { y = y - 1; } else if (way == '7') // { y = y - 1; x = x - 1; } // WordRap 행 if (x >= board.length) x = 0; if (y >= board[0].length) y = 0; if (x < 0) x = board.length - 1; if (y < 0) y = board[0].length - 1; board[x][y]++; exhibit(); System.out.println(); } public void exhibit() { // Magic Number for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { System.out.print(board[i][j] + "\t"); } System.out.println(); } } public static void main(String[] args) { Board aboard = new Board(); System.out.println("RandomWalk"); aboard.board[0][0]++; aboard.journey[0] = '0'; aboard.journey[1] = '3'; for (int i = 0; aboard.journey[i] != '\0'; i++) aboard.move(aboard.journey[i]); } }
2 ¶
- HashMap 해 향 한 화 . 하 하 .
~cpp import java.util.HashMap; public class Board { public char journey[] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; public int x = 0; public int y = 0; 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 } }; class 향 { 향(int x, int y) { this.x = x; this.y = y; } int x; int y; } public void move(char way) { // HashMap 해 화 HashMap directionMap = new HashMap(); directionMap.put("0", new 향(-1, 0)); // directionMap.put("1", new 향(-1, 1)); // directionMap.put("2", new 향(0, 1)); // directionMap.put("3", new 향(1, 1)); // directionMap.put("4", new 향(1, 0)); // directionMap.put("5", new 향(1, -1)); // directionMap.put("6", new 향(0, -1)); // directionMap.put("7", new 향(-1, -1)); // // primitive type String(Object) 하 // HashMap 향 x,y 함 향 delta = (향) directionMap.get("" + way); x += delta.x; y += delta.y; // Wordrap x = (x >= board.length) ? 0 : x; y = (y >= board[0].length) ? 0 : y; x = (x < 0) ? board.length - 1 : x; y = (y < 0) ? board[0].length - 1 : y; board[x][y]++; // exhibit -> printBoard rename printBoard(); System.out.println(); } public void printBoard() { // Magic Number for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { System.out.print(board[i][j] + "\t"); } System.out.println(); } } public static void main(String[] args) { Board aboard = new Board(); System.out.println("RandomWalk"); aboard.board[0][0]++; aboard.journey[0] = '0'; aboard.journey[1] = '3'; for (int i = 0; aboard.journey[i] != '\0'; i++) aboard.move(aboard.journey[i]); } }
3 ¶
- char jouney -> ArrayList
- x, y -> nowX, nowY rename : currentXPos 하.
- 향 표하 Magic number , Board.jouney ArrayList , String . comment mixing .
~cpp import java.util.ArrayList; import java.util.HashMap; public class Board { // char jouney -> ArrayList protected ArrayList jouney = new ArrayList(); // x -> nowX public int nowX = 0; // y -> nowY public int nowY = 0; 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 } }; class 향 { 향(int x, int y) { this.x = x; this.y = y; } int x; int y; } public void move(String way) { // HashMap 해 화 HashMap directionMap = new HashMap(); // 향 Magic number directionMap.put("", new 향(-1, 0)); directionMap.put("", new 향(-1, 1)); directionMap.put("", new 향(0, 1)); directionMap.put("", new 향(1, 1)); directionMap.put("", new 향(1, 0)); directionMap.put("", new 향(1, -1)); directionMap.put("", new 향(0, -1)); directionMap.put("", new 향(-1, -1)); // primitive type String(Object) 하 // HashMap 향 . 향 delta = (향) directionMap.get("" + way); nowX += delta.x; nowY += delta.y; // Wordrap nowX = (nowX >= board.length) ? 0 : nowX; nowY = (nowY >= board[0].length) ? 0 : nowY; nowX = (nowX < 0) ? board.length - 1 : nowX; nowY = (nowY < 0) ? board[0].length - 1 : nowY; board[nowX][nowY]++; printBoard(); System.out.println(); } public void printBoard() { // Magic Number for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { System.out.print(board[i][j] + "\t"); } System.out.println(); } } public ArrayList getJouney() { return jouney; } public static void main(String[] args) { Board aboard = new Board(); System.out.println("RandomWalk"); aboard.board[0][0]++; // jouney getter jouney . // jouney aboard . ArrayList jouney = aboard.getJouney(); jouney.add(""); jouney.add(""); // ArrayList for (int i = 0; i < jouney.size(); i++) aboard.move((String) jouney.get(i)); } }
Thread ¶
한 . 화 하 판해 하 , IDE , .