[[TableOfContents]] == 1학년 데블스캠프 때 짠 것 == {{{~cpp //랜덤 워크 #include #include #include int main() { int i, j; int a, b; int way;//이동 방향 int count=1;//이동 횟수 int not_go=1;//아직 가지 않은 곳을 셀 때 쓰는 수 int input; //숫자 입력받고 동적할당 cout<<"Random Walk"<>input; int **square=new int *[input]; for(i=0;i #include using namespace std; const int MAX_X=5; const int MAX_Y=5; const int DIRECTION=8; const int MOVE_X[DIRECTION]={0, 1, 1, 1, 0, -1, -1, -1}; const int MOVE_Y[DIRECTION]={-1, -1, 0, 1, 1, 1, 0, -1}; struct Bug { int x; int y; int way; }; void showBoard(int a_board[][MAX_X], int length_x, int length_y); void move(Bug & a_bug); void askLocationOfBug(Bug & a_bug); void increaseEndCount(int & a_count, Bug & a_bug, int a_board[][MAX_X]); void makeFootprint(Bug & a_bug, int a_board[][MAX_X]); bool isEnd(int a_count); bool isInBoard(Bug & a_bug0); void main() { int board[MAX_Y][MAX_X]={{0,}}; // 판 int count=0; // 종료 조건 Bug bug; // 바퀴벌레의 위치 askLocationOfBug(bug); makeFootprint(bug, board); //시작점에서 발자국을 찍고 시작. do{ move(bug); increaseEndCount(count, bug, board); makeFootprint(bug, board); }while(isEnd(count)); showBoard(board, MAX_X, MAX_Y); } void showBoard(int a_board[][MAX_X], int length_x, int length_y) { for(int i=0;i-1 && a_bug0.x+MOVE_X[a_bug0.way]<5 && a_bug0.y+MOVE_Y[a_bug0.way]>-1 && a_bug0.y+MOVE_Y[a_bug0.way]<5) return true; else return false; } void askLocationOfBug(Bug & a_bug) { cout<<"바퀴벌레의 x좌표와 y좌표를 입력하시오. "; cin>>a_bug.x; cin>>a_bug.y; srand((unsigned)time(NULL)); } bool isEnd(int a_count) { if(a_count<(MAX_X*MAX_Y-1)) return true; else return false; } void makeFootprint(Bug & a_bug, int a_board[][MAX_X]) { a_board[a_bug.y][a_bug.x]++; } }}} == 최근에 객체지향으로 짠 것(2003/08/20) == === Board.h === {{{~cpp const int MAX_X=5; const int MAX_Y=5; class Board { private: int board[MAX_X][MAX_Y]; int count; public: Board(); void showBoard(); void makeFootprint(int a_x, int a_y); void increaseEndCount(int a_x, int a_y); int returnCount(){return count;} }; }}} === Bug.h === {{{~cpp const int DIRECTION=8; const int MOVE_X[DIRECTION]={0, 1, 1, 1, 0, -1, -1, -1}; const int MOVE_Y[DIRECTION]={-1, -1, 0, 1, 1, 1, 0, -1}; class Bug { private: int x; int y; int way; public: Bug(){} Bug(int a_x, int a_y); int returnX(){return x;} int returnY(){return y;} void move(); void askLocationOfBug(); bool isInBoard(); bool isEnd(int a_count); }; }}} === Board.cpp === {{{~cpp #include #include"Board.h" Board::Board() { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { board[i][j]=0; } } count=0; } void Board::showBoard() { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { cout< #include #include"Bug.h" using namespace std; const int MAX_X=5; const int MAX_Y=5; Bug::Bug(int a_x, int a_y) { x=a_x; y=a_y; } void Bug::askLocationOfBug() { cout<<"바퀴벌레의 초기 위치를 입력하세요. "<>x>>y; cout<<"바퀴벌레의 초기 위치는 "<-1 && x+MOVE_X[way]-1 && y+MOVE_Y[way] #include"Board.h" #include"Bug.h" using namespace std; void main() { Board board; Bug bug; bug.askLocationOfBug(); board.makeFootprint(bug.returnX(), bug.returnY()); do{ bug.move(); board.increaseEndCount(bug.returnX(), bug.returnY()); board.makeFootprint(bug.returnX(), bug.returnY()); }while(bug.isEnd(board.returnCount())); board.showBoard(); } }}} ---- 작성자: ["Yggdrasil"] ---- ["RandomWalk"]