- ì�Œ... ìš°ì„ ë�„스창ì—�서 ìž…ë ¥ë°›ì•„ì„œ ì�´ë�™ì‹œí‚¤ëŠ” 것만 빼면 다 했습니다. ê·¸ ìž…ë ¥ì�´ C처럼 만만하지가 않ë�”êµ°ìš”... ìž…ë ¥ 하나 받는ë�° ë˜ ê·¸ë ‡ê²Œ ë§Žì�´ ì�¨ì•¼ 하는지...
- ìš°ì„ ìƒ�민ì�´í˜•ì�´ ì�¨ ì£¼ì‹ ê²ƒì—�서 1번ì�„ í•´ 보았습니다.(버그 ìˆ˜ì • 완료)...ì�´ì œ 2번 í• ì°¨ë¡€...ê·¸ë¦¬ê³ ì–´ì©Œë‹¤ê°€ count변수가 다시 í•„ìš”í•´ì ¸ì„œ 다시 넣었습니다.
3번째ì�˜ 코드는 comment solvingì�„ ë³´ì�¸ê±°ë‹ˆ ê°�안하ì‹ì‹œì˜¤. 3번ì�„ 그대로 한다면, ìž…ë ¥ ë�°ì�´í„°ì™€, Bug 사ì�´ì�˜ ì�¸í„°íŽ˜ì�´ìŠ¤ë¥¼ 맞추는 함수가 í•„ìš”í• ê²ƒìž…ë‹ˆë‹¤.
사소한 것ì�´ì§€ë§Œ ì§€ì �한다면 class main ì�˜ ì�´ë¦„ì�„ Main 으로 바꾸시기를 ê°•ë ¥(?) 추천합니다. Java ì—�는 지켜야하는 규칙ì�¸ 문법외ì—� 코딩 약ì†�ì�„ ì¶”ì²œí•˜ê³ ìžˆì”�니다. 과거 MSë�¼ë©´ í—�가리안표기법 ì�´ê² 지요? 현재ì�˜ .net ì—�서 í—�가리안표기법ì�€ 없어졌습니다. --neocoin
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ì—� 붙여서, ë�ê°€ 바뀌었는지 보세요.
ArrayList 나, HashMap ì�€ 보통ì�˜ ìž�ë°” 책들ì—�서 나오는 Vector 와 Hashtable ê³¼ ë�™ì�¼í•œ ì—í• ì�„ 합니다. 1.3ì—�서 추가ë�œ collection frameworkì—�서 위ì�˜ ë‘�가지를 ë�” 추천해서 ì�´ìš©í–ˆìŠµë‹ˆë‹¤.
collection framework를 ì•Œê³ ì‹¶ìœ¼ì‹œë©´ 여기 ì—�서 보세요. ê·¸ë¦¬ê³ ë³´ì…¨ìœ¼ë©´ ì €ì—�게 세미나 시켜주세요. 쿨ëŸ.. --neocoin










