Randomwalk Problem ¶
~cpp
== Roach.h ==
//20041004 홍
const int Direction = 8; // 8향
const int imove[8] = {-1,0,1,1,1,0,-1,-1}; // 향 x 표
const int jmove[8] = {1,1,1,0,-1,-1,-1,0}; // 향 y 표
int Tile[40][20] = {0,}; // 화
int size_x, size_y; // x, y 표
int count,t,R_count=0; // 횟, , 하
class Roach
{
private:
int Pos_x, Pos_y; //
public:
void Initiation(); // 화
void Input(); // 함
void Walk(); //
void show(); // 함
};
void Roach :: Initiation() // 화
{
cout << " 크 하. ( 2 < x <=40 ) \n";
while(!(cin >> size_x) || size_x<3 || size_x > 40)
cerr << " 하. 해.\n";
cout << " 크 하. ( 2 < y <=20 ) \n";
while(!(cin >> size_y) || size_y < 3 || size_y >20)
cerr << " 하. 해.\n";
};
void Roach :: Input() // 함
{
int i,j;
cout << "x 표 하 \n";
cin >> i;
cout << "y 표 하 \n";
cin >> j;
Pos_x = i;
Pos_y = j;
};
void Roach :: Walk()
{
t=rand()%Direction; // 향 한
if(Pos_x + imove[t] >= 0 && Pos_x + imove[t] < size_x) // 하
Pos_x += imove[t]; // x 표
if(Pos_y + jmove[t] >= 0 && Pos_y + jmove[t] < size_y)
Pos_y += jmove[t]; // y 표
count++; // 횟 하 한
if(Tile[Pos_x][Pos_y]==0)
R_count++; // 하 하 한
Tile[Pos_x][Pos_y]++; // 할 하 횟 한.
};
void Roach :: show()
{
cout << " 횟 " << count << " .\n";
cout << " " << Pos_x << " , " << Pos_y << ".\n";
cout << " 한 횟 . \n";
for(int m=0; m < size_x; m++){
for(int n=0; n < size_y; n++)
cout << Tile[m][n] << "\t";
cout << endl;
}
}
//20041004 홍
#include <iostream.h>
#include <cstdlib>
#include <ctime>
#include "Roach.h"
void main()
{
srand((unsigned)time(NULL)); // 해
Roach Hong; // Hong Roach 클
Hong.Initiation(); // 화
Hong.Input(); //
while(R_count!=size_x*size_y || count < 50000) // 횟 50000 한
Hong.Walk(); //
Hong.show(); //
}
한... 할... 하...
합 ㅡ_ㅡ^
합 ㅡ_ㅡ^










