구 ¶
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(); // 결과값
}
게... 고 ... ...
공 ㅡ_ㅡ^
공 ㅡ_ㅡ^










