More actions
imported>Unknown No edit summary |
(Repair batch-0003 pages from live compare) |
||
| Line 17: | Line 17: | ||
char *_szCourse; | char *_szCourse; | ||
int **_arVisitFrequency; | int **_arVisitFrequency; | ||
int _arDirectionX | int _arDirectionX[8]; | ||
int _arDirectionY | int _arDirectionY[8]; | ||
public: | public: | ||
RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, char *szCourse, int DirectX | RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, char *szCourse, int DirectX[], int DirectY[]); | ||
~RandomWalkBoard(); | ~RandomWalkBoard(); | ||
void CourseAllocate(char *szCourse); | void CourseAllocate(char *szCourse); | ||
| Line 34: | Line 34: | ||
bool CheckEndCourse(); | bool CheckEndCourse(); | ||
void CheckDirectionAndMove(int direction); | void CheckDirectionAndMove(int direction); | ||
void DirectionAllocate(int x | void DirectionAllocate(int x[], int y[]); | ||
}; | }; | ||
| Line 44: | Line 44: | ||
using namespace std; | using namespace std; | ||
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, char *szCourse, int DirectX | RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, char *szCourse, int DirectX[], int DirectY[]) | ||
{ | { | ||
_nRow = nRow; | _nRow = nRow; | ||
| Line 57: | Line 57: | ||
DirectionAllocate(DirectX, DirectY); | DirectionAllocate(DirectX, DirectY); | ||
_arVisitFrequency | _arVisitFrequency[_nCurRow][_nCurCol] ++; | ||
} | } | ||
void RandomWalkBoard::CourseAllocate(char *szCourse) | void RandomWalkBoard::CourseAllocate(char *szCourse) | ||
{ | { | ||
_szCourse = new char | _szCourse = new char[ strlen(szCourse) + 1 ]; | ||
strcpy(_szCourse, szCourse); | strcpy(_szCourse, szCourse); | ||
} | } | ||
| Line 68: | Line 68: | ||
void RandomWalkBoard::BoardAllocate() | void RandomWalkBoard::BoardAllocate() | ||
{ | { | ||
_arVisitFrequency = new int* | _arVisitFrequency = new int*[_nRow]; | ||
for(int i = 0 ; i < _nRow ; i++) | for(int i = 0 ; i < _nRow ; i++) | ||
{ | { | ||
_arVisitFrequency | _arVisitFrequency[i] = new int[_nCol]; | ||
} | } | ||
} | } | ||
| Line 77: | Line 77: | ||
void RandomWalkBoard::CourseFree() | void RandomWalkBoard::CourseFree() | ||
{ | { | ||
delete | delete [] _szCourse; | ||
} | } | ||
void RandomWalkBoard::DirectionAllocate(int x | void RandomWalkBoard::DirectionAllocate(int x[], int y[]) | ||
{ | { | ||
for(int i = 0 ; i < 8 ; i ++) | for(int i = 0 ; i < 8 ; i ++) | ||
{ | { | ||
_arDirectionX | _arDirectionX[i] = x[i]; | ||
_arDirectionY | _arDirectionY[i] = y[i]; | ||
} | } | ||
} | } | ||
| Line 95: | Line 95: | ||
for(int i = 0 ; i < _nRow ; i++) | for(int i = 0 ; i < _nRow ; i++) | ||
{ | { | ||
if(_arVisitFrequency | if(_arVisitFrequency[i]) | ||
delete | delete [] _arVisitFrequency[i]; | ||
} | } | ||
delete | delete [] _arVisitFrequency; | ||
} | } | ||
} | } | ||
| Line 116: | Line 116: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
cout << _arVisitFrequency | cout << _arVisitFrequency[i][j] << " "; | ||
} | } | ||
cout << endl; | cout << endl; | ||
| Line 128: | Line 128: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
_arVisitFrequency | _arVisitFrequency[i][j] = 0; | ||
} | } | ||
} | } | ||
| Line 139: | Line 139: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
if(_arVisitFrequency | if(_arVisitFrequency[i][j] == 0) | ||
{ | { | ||
return false; | return false; | ||
| Line 150: | Line 150: | ||
void RandomWalkBoard::IncreaseVisitCount() | void RandomWalkBoard::IncreaseVisitCount() | ||
{ | { | ||
_arVisitFrequency | _arVisitFrequency[_nCurRow][_nCurCol] ++; | ||
} | } | ||
| Line 163: | Line 163: | ||
while( !CheckEndCourse() && !CheckCompletelyPatrol() ) | while( !CheckEndCourse() && !CheckCompletelyPatrol() ) | ||
{ | { | ||
direction = _szCourse | direction = _szCourse[ _nTotalVisitCount ] - 48; | ||
CheckDirectionAndMove(direction); | CheckDirectionAndMove(direction); | ||
IncreaseVisitCount(); | IncreaseVisitCount(); | ||
| Line 181: | Line 181: | ||
void RandomWalkBoard::CheckDirectionAndMove(int direction) | void RandomWalkBoard::CheckDirectionAndMove(int direction) | ||
{ | { | ||
_nCurRow += _arDirectionY | _nCurRow += _arDirectionY[direction]; | ||
_nCurCol += _arDirectionX | _nCurCol += _arDirectionX[direction]; | ||
if(_nCurRow == -1) | if(_nCurRow == -1) | ||
_nCurRow = _nRow - 1; | _nCurRow = _nRow - 1; | ||
| Line 204: | Line 204: | ||
const int MAXCOURSE = 100; | const int MAXCOURSE = 100; | ||
void InputInitData(int &row, int &col, int &currow, int &curcol, char course | void InputInitData(int &row, int &col, int &currow, int &curcol, char course[]); | ||
fstream in("test.txt"); | fstream in("test.txt"); | ||
| Line 211: | Line 211: | ||
{ | { | ||
int row, col, currow, curcol; | int row, col, currow, curcol; | ||
char course | char course[MAXCOURSE]; | ||
InputInitData(row, col, currow, curcol, course); | InputInitData(row, col, currow, curcol, course); | ||
int directX | int directX[8] = {0,1,1,1,0,-1,-1,-1}; | ||
int directY | int directY[8] = {-1,-1,0,1,1,1,0,-1}; | ||
RandomWalkBoard test(row, col, currow, curcol, course, directX, directY); | RandomWalkBoard test(row, col, currow, curcol, course, directX, directY); | ||
| Line 223: | Line 223: | ||
} | } | ||
void InputInitData(int &row, int &col, int &currow, int &curcol, char course | void InputInitData(int &row, int &col, int &currow, int &curcol, char course[]) | ||
{ | { | ||
in >> row; | in >> row; | ||
| Line 258: | Line 258: | ||
vector<int> _arDirectionY; | vector<int> _arDirectionY; | ||
public: | public: | ||
RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, string& szCourse, int DirectX | RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, string& szCourse, int DirectX[], int DirectY[]); | ||
~RandomWalkBoard(); | ~RandomWalkBoard(); | ||
void BoardAllocate(); | void BoardAllocate(); | ||
| Line 269: | Line 269: | ||
bool CheckEndCourse(); | bool CheckEndCourse(); | ||
void CheckDirectionAndMove(int direction); | void CheckDirectionAndMove(int direction); | ||
void DirectionAllocate(int x | void DirectionAllocate(int x[], int y[]); | ||
}; | }; | ||
| Line 279: | Line 279: | ||
using namespace std; | using namespace std; | ||
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, string& szCourse, int DirectX | RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, string& szCourse, int DirectX[], int DirectY[]) | ||
{ | { | ||
_nRow = nRow; | _nRow = nRow; | ||
| Line 291: | Line 291: | ||
DirectionAllocate(DirectX, DirectY); | DirectionAllocate(DirectX, DirectY); | ||
_arVisitFrequency | _arVisitFrequency[_nCurRow][_nCurCol] ++; | ||
} | } | ||
| Line 298: | Line 298: | ||
_arVisitFrequency.resize(_nRow); | _arVisitFrequency.resize(_nRow); | ||
for(int i = 0 ; i < _nRow ; i++) | for(int i = 0 ; i < _nRow ; i++) | ||
_arVisitFrequency | _arVisitFrequency[i].resize(_nCol); | ||
SetArrayAsZero(); | SetArrayAsZero(); | ||
} | } | ||
void RandomWalkBoard::DirectionAllocate(int X | void RandomWalkBoard::DirectionAllocate(int X[], int Y[]) | ||
{ | { | ||
_arDirectionX.resize(8); | _arDirectionX.resize(8); | ||
_arDirectionY.resize(8); | _arDirectionY.resize(8); | ||
_arDirectionX.assign(&X | _arDirectionX.assign(&X[0], &X[7]); | ||
_arDirectionY.assign(&Y | _arDirectionY.assign(&Y[0], &Y[7]); | ||
} | } | ||
| Line 323: | Line 323: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
cout << _arVisitFrequency | cout << _arVisitFrequency[i][j] << " "; | ||
} | } | ||
cout << endl; | cout << endl; | ||
| Line 334: | Line 334: | ||
{ | { | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
_arVisitFrequency | _arVisitFrequency[i][j] = 0; | ||
} | } | ||
} | } | ||
| Line 344: | Line 344: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
if(_arVisitFrequency | if(_arVisitFrequency[i][j] == 0) | ||
return false; | return false; | ||
} | } | ||
| Line 353: | Line 353: | ||
void RandomWalkBoard::IncreaseVisitCount() | void RandomWalkBoard::IncreaseVisitCount() | ||
{ | { | ||
_arVisitFrequency | _arVisitFrequency[_nCurRow][_nCurCol] ++; | ||
} | } | ||
| Line 366: | Line 366: | ||
while( !CheckEndCourse() && !CheckCompletelyPatrol() ) | while( !CheckEndCourse() && !CheckCompletelyPatrol() ) | ||
{ | { | ||
direction = _szCourse | direction = _szCourse[ _nTotalVisitCount ] - 48; | ||
CheckDirectionAndMove(direction); | CheckDirectionAndMove(direction); | ||
IncreaseVisitCount(); | IncreaseVisitCount(); | ||
| Line 382: | Line 382: | ||
void RandomWalkBoard::CheckDirectionAndMove(int direction) | void RandomWalkBoard::CheckDirectionAndMove(int direction) | ||
{ | { | ||
_nCurRow += _arDirectionY | _nCurRow += _arDirectionY[direction]; | ||
_nCurCol += _arDirectionX | _nCurCol += _arDirectionX[direction]; | ||
if(_nCurRow == -1) | if(_nCurRow == -1) | ||
_nCurRow = _nRow - 1; | _nCurRow = _nRow - 1; | ||
| Line 414: | Line 414: | ||
InputInitData(row, col, currow, curcol, course); | InputInitData(row, col, currow, curcol, course); | ||
int directX | int directX[8] = {0,1,1,1,0,-1,-1,-1}; | ||
int directY | int directY[8] = {-1,-1,0,1,1,1,0,-1}; | ||
RandomWalkBoard test(row, col, currow, curcol, course, directX, directY); | RandomWalkBoard test(row, col, currow, curcol, course, directX, directY); | ||
| Line 457: | Line 457: | ||
vector<int> _arDirectionY; | vector<int> _arDirectionY; | ||
public: | public: | ||
RandomWalkBoard(int nRow, int nCol, int DirectX | RandomWalkBoard(int nRow, int nCol, int DirectX[], int DirectY[], const Roach& roach); | ||
~RandomWalkBoard(); | ~RandomWalkBoard(); | ||
void BoardAllocate(); | void BoardAllocate(); | ||
| Line 466: | Line 466: | ||
void StartProcedure(); | void StartProcedure(); | ||
bool CheckEndCourse() const; | bool CheckEndCourse() const; | ||
void DirectionAllocate(int x | void DirectionAllocate(int x[], int y[]); | ||
}; | }; | ||
| Line 503: | Line 503: | ||
using namespace std; | using namespace std; | ||
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int DirectX | RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int DirectX[], int DirectY[], const Roach& roach) : _Roach(roach) | ||
{ | { | ||
_nRow = nRow; | _nRow = nRow; | ||
| Line 511: | Line 511: | ||
DirectionAllocate(DirectX, DirectY); | DirectionAllocate(DirectX, DirectY); | ||
_arVisitFrequency | _arVisitFrequency[_Roach.GetCurRow()][_Roach.GetCurCol()] ++; | ||
} | } | ||
| Line 518: | Line 518: | ||
_arVisitFrequency.resize(_nRow); | _arVisitFrequency.resize(_nRow); | ||
for(int i = 0 ; i < _nRow ; i++) | for(int i = 0 ; i < _nRow ; i++) | ||
_arVisitFrequency | _arVisitFrequency[i].resize(_nCol); | ||
SetArrayAsZero(); | SetArrayAsZero(); | ||
} | } | ||
void RandomWalkBoard::DirectionAllocate(int X | void RandomWalkBoard::DirectionAllocate(int X[], int Y[]) | ||
{ | { | ||
_arDirectionX.resize(8); | _arDirectionX.resize(8); | ||
_arDirectionY.resize(8); | _arDirectionY.resize(8); | ||
_arDirectionX.assign(&X | _arDirectionX.assign(&X[0], &X[7]); | ||
_arDirectionY.assign(&Y | _arDirectionY.assign(&Y[0], &Y[7]); | ||
} | } | ||
| Line 543: | Line 543: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
cout << _arVisitFrequency | cout << _arVisitFrequency[i][j] << " "; | ||
} | } | ||
cout << endl; | cout << endl; | ||
| Line 554: | Line 554: | ||
{ | { | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
_arVisitFrequency | _arVisitFrequency[i][j] = 0; | ||
} | } | ||
} | } | ||
| Line 564: | Line 564: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
if(_arVisitFrequency | if(_arVisitFrequency[i][j] == 0) | ||
return false; | return false; | ||
} | } | ||
| Line 573: | Line 573: | ||
void RandomWalkBoard::IncreaseVisitCount() | void RandomWalkBoard::IncreaseVisitCount() | ||
{ | { | ||
_arVisitFrequency | _arVisitFrequency[ _Roach.GetCurRow() ][ _Roach.GetCurCol() ] ++; | ||
} | } | ||
| Line 581: | Line 581: | ||
while( !CheckEndCourse() && !CheckCompletelyPatrol() ) | while( !CheckEndCourse() && !CheckCompletelyPatrol() ) | ||
{ | { | ||
direction = _Roach.GetCourse() | direction = _Roach.GetCourse()[ _Roach.GetTotalVisitCount() ] - 48; | ||
_Roach.CheckDirectionAndMove(_arDirectionX, _arDirectionY, _nRow, _nCol, direction); | _Roach.CheckDirectionAndMove(_arDirectionX, _arDirectionY, _nRow, _nCol, direction); | ||
IncreaseVisitCount(); | IncreaseVisitCount(); | ||
| Line 633: | Line 633: | ||
void Roach::CheckDirectionAndMove(const vector<int>& X, const vector<int>& Y, int nRow, int nCol, int direction) | void Roach::CheckDirectionAndMove(const vector<int>& X, const vector<int>& Y, int nRow, int nCol, int direction) | ||
{ | { | ||
_nCurRow += Y | _nCurRow += Y[direction]; | ||
_nCurCol += X | _nCurCol += X[direction]; | ||
if(_nCurRow == -1) | if(_nCurRow == -1) | ||
_nCurRow = nRow - 1; | _nCurRow = nRow - 1; | ||
| Line 667: | Line 667: | ||
InputInitData(row, col, currow, curcol, course); | InputInitData(row, col, currow, curcol, course); | ||
int directX | int directX[8] = {0,1,1,1,0,-1,-1,-1}; | ||
int directY | int directY[8] = {-1,-1,0,1,1,1,0,-1}; | ||
Roach roach(currow, curcol, course); | Roach roach(currow, curcol, course); | ||
| Line 710: | Line 710: | ||
int _nCol; | int _nCol; | ||
vector< vector<int> > _arVisitFrequency; | vector< vector<int> > _arVisitFrequency; | ||
Roach _Roach | Roach _Roach[2]; | ||
void BoardAllocate(); | void BoardAllocate(); | ||
| Line 718: | Line 718: | ||
public: | public: | ||
RandomWalkBoard(int nRow, int nCol, const Roach roach | RandomWalkBoard(int nRow, int nCol, const Roach roach[]); | ||
~RandomWalkBoard(); | ~RandomWalkBoard(); | ||
void ShowStatus() const; | void ShowStatus() const; | ||
| Line 753: | Line 753: | ||
int GetTotalVisitCount() const { return _nTotalVisitCount; } | int GetTotalVisitCount() const { return _nTotalVisitCount; } | ||
void CheckDirectionAndMove(int nRow, int nCol, int direction); | void CheckDirectionAndMove(int nRow, int nCol, int direction); | ||
static void DirectionAllocate(int X | static void DirectionAllocate(int X[], int Y[]); | ||
}; | }; | ||
| Line 763: | Line 763: | ||
using namespace std; | using namespace std; | ||
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, const Roach roach | RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, const Roach roach[]) | ||
{ | { | ||
_nRow = nRow; | _nRow = nRow; | ||
| Line 772: | Line 772: | ||
for(int i = 0 ; i < MAX_ROACH ; i++) | for(int i = 0 ; i < MAX_ROACH ; i++) | ||
{ | { | ||
_Roach | _Roach[i] = roach[i]; | ||
_arVisitFrequency | _arVisitFrequency[_Roach[i].GetCurRow()][_Roach[i].GetCurCol()] ++; | ||
} | } | ||
} | } | ||
| Line 781: | Line 781: | ||
_arVisitFrequency.resize(_nRow); | _arVisitFrequency.resize(_nRow); | ||
for(int i = 0 ; i < _nRow ; i++) | for(int i = 0 ; i < _nRow ; i++) | ||
_arVisitFrequency | _arVisitFrequency[i].resize(_nCol); | ||
SetArrayAsZero(); | SetArrayAsZero(); | ||
} | } | ||
| Line 792: | Line 792: | ||
{ | { | ||
for(int i = 0 ; i < MAX_ROACH ; i ++) | for(int i = 0 ; i < MAX_ROACH ; i ++) | ||
cout << _Roach | cout << _Roach[i].GetTotalVisitCount() << endl; | ||
cout << endl; | cout << endl; | ||
| Line 800: | Line 800: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
cout << _arVisitFrequency | cout << _arVisitFrequency[i][j] << " "; | ||
} | } | ||
cout << endl; | cout << endl; | ||
| Line 811: | Line 811: | ||
{ | { | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
_arVisitFrequency | _arVisitFrequency[i][j] = 0; | ||
} | } | ||
} | } | ||
| Line 821: | Line 821: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
if(_arVisitFrequency | if(_arVisitFrequency[i][j] == 0) | ||
return false; | return false; | ||
} | } | ||
| Line 830: | Line 830: | ||
void RandomWalkBoard::IncreaseVisitCount(int nSequence) | void RandomWalkBoard::IncreaseVisitCount(int nSequence) | ||
{ | { | ||
_arVisitFrequency | _arVisitFrequency[ _Roach[nSequence].GetCurRow() ][ _Roach[nSequence].GetCurCol() ] ++; | ||
} | } | ||
| Line 840: | Line 840: | ||
for(int i = 0 ; i < MAX_ROACH ; i ++) | for(int i = 0 ; i < MAX_ROACH ; i ++) | ||
{ | { | ||
if( _Roach | if( _Roach[i].GetCourse().size() > _Roach[i].GetTotalVisitCount() ) | ||
{ | { | ||
direction = _Roach | direction = _Roach[i].GetCourse()[ _Roach[i].GetTotalVisitCount() ] - 48; | ||
_Roach | _Roach[i].CheckDirectionAndMove(_nRow, _nCol, direction); | ||
IncreaseVisitCount(i); | IncreaseVisitCount(i); | ||
_Roach | _Roach[i].IncreaseTotalVisitCount(); | ||
} | } | ||
} | } | ||
| Line 853: | Line 853: | ||
bool RandomWalkBoard::CheckEndCourse() const | bool RandomWalkBoard::CheckEndCourse() const | ||
{ | { | ||
if( _Roach | if( _Roach[0].GetTotalVisitCount() == _Roach[0].GetCourse().size() && _Roach[1].GetTotalVisitCount() == _Roach[1].GetCourse().size()) | ||
return true; | return true; | ||
return false; | return false; | ||
| Line 863: | Line 863: | ||
vector<int> Roach::_arDirectionY; | vector<int> Roach::_arDirectionY; | ||
void Roach::DirectionAllocate(int X | void Roach::DirectionAllocate(int X[], int Y[]) | ||
{ | { | ||
_arDirectionX.resize(8); | _arDirectionX.resize(8); | ||
_arDirectionY.resize(8); | _arDirectionY.resize(8); | ||
_arDirectionX.assign(&X | _arDirectionX.assign(&X[0], &X[7]); | ||
_arDirectionY.assign(&Y | _arDirectionY.assign(&Y[0], &Y[7]); | ||
} | } | ||
| Line 891: | Line 891: | ||
void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction) | void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction) | ||
{ | { | ||
_nCurRow += _arDirectionY | _nCurRow += _arDirectionY[direction]; | ||
_nCurCol += _arDirectionX | _nCurCol += _arDirectionX[direction]; | ||
if(_nCurRow == -1) | if(_nCurRow == -1) | ||
_nCurRow = nRow - 1; | _nCurRow = nRow - 1; | ||
| Line 920: | Line 920: | ||
int main() | int main() | ||
{ | { | ||
int directX | int directX[8] = {0,1,1,1,0,-1,-1,-1}; | ||
int directY | int directY[8] = {-1,-1,0,1,1,1,0,-1}; | ||
Roach::DirectionAllocate(directX, directY); | Roach::DirectionAllocate(directX, directY); | ||
| Line 928: | Line 928: | ||
string course; | string course; | ||
Roach roach | Roach roach[2]; | ||
in >> row; | in >> row; | ||
| Line 936: | Line 936: | ||
{ | { | ||
InputInitData(currow, curcol, course); | InputInitData(currow, curcol, course); | ||
roach | roach[i].SetRoach(currow, curcol, course); | ||
} | } | ||
| Line 1,022: | Line 1,022: | ||
int GetTotalVisitCount() const { return _nTotalVisitCount; } | int GetTotalVisitCount() const { return _nTotalVisitCount; } | ||
void CheckDirectionAndMove(int nRow, int nCol, int direction); | void CheckDirectionAndMove(int nRow, int nCol, int direction); | ||
static void DirectionAllocate(int X | static void DirectionAllocate(int X[], int Y[]); | ||
}; | }; | ||
| Line 1,045: | Line 1,045: | ||
for(int i = 0 ; i < _nRoachCount ; i++) | for(int i = 0 ; i < _nRoachCount ; i++) | ||
{ | { | ||
_Roaches | _Roaches[i] = roach[i]; | ||
_arVisitFrequency | _arVisitFrequency[_Roaches[i].GetCurRow()][_Roaches[i].GetCurCol()] ++; | ||
} | } | ||
} | } | ||
| Line 1,054: | Line 1,054: | ||
_arVisitFrequency.resize(_nRow); | _arVisitFrequency.resize(_nRow); | ||
for(int i = 0 ; i < _nRow ; i++) | for(int i = 0 ; i < _nRow ; i++) | ||
_arVisitFrequency | _arVisitFrequency[i].resize(_nCol); | ||
SetArrayAsZero(); | SetArrayAsZero(); | ||
} | } | ||
| Line 1,065: | Line 1,065: | ||
{ | { | ||
for(int i = 0 ; i < _nRoachCount ; i ++) | for(int i = 0 ; i < _nRoachCount ; i ++) | ||
cout << _Roaches | cout << _Roaches[i].GetTotalVisitCount() << endl; | ||
cout << endl; | cout << endl; | ||
| Line 1,073: | Line 1,073: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
cout << _arVisitFrequency | cout << _arVisitFrequency[i][j] << " "; | ||
} | } | ||
cout << endl; | cout << endl; | ||
| Line 1,085: | Line 1,085: | ||
{ | { | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
_arVisitFrequency | _arVisitFrequency[i][j] = 0; | ||
} | } | ||
} | } | ||
| Line 1,095: | Line 1,095: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
if(_arVisitFrequency | if(_arVisitFrequency[i][j] == 0) | ||
return false; | return false; | ||
} | } | ||
| Line 1,104: | Line 1,104: | ||
void RandomWalkBoard::IncreaseVisitCount(int nSequence) | void RandomWalkBoard::IncreaseVisitCount(int nSequence) | ||
{ | { | ||
_arVisitFrequency | _arVisitFrequency[ _Roaches[nSequence].GetCurRow() ][ _Roaches[nSequence].GetCurCol() ] ++; | ||
} | } | ||
| Line 1,114: | Line 1,114: | ||
for(int i = 0 ; i < _nRoachCount ; i ++) | for(int i = 0 ; i < _nRoachCount ; i ++) | ||
{ | { | ||
if( _Roaches | if( _Roaches[i].GetCourse().size() > _Roaches[i].GetTotalVisitCount() ) | ||
{ | { | ||
direction = _Roaches | direction = _Roaches[i].GetCourse()[ _Roaches[i].GetTotalVisitCount() ] - 48; | ||
_Roaches | _Roaches[i].CheckDirectionAndMove(_nRow, _nCol, direction); | ||
IncreaseVisitCount(i); | IncreaseVisitCount(i); | ||
_Roaches | _Roaches[i].IncreaseTotalVisitCount(); | ||
} | } | ||
} | } | ||
| Line 1,136: | Line 1,136: | ||
for(int i = 0 ; i < _nRoachCount ; i ++) | for(int i = 0 ; i < _nRoachCount ; i ++) | ||
{ | { | ||
if( _Roaches | if( _Roaches[i].GetTotalVisitCount() != _Roaches[i].GetCourse().size() ) | ||
return false; | return false; | ||
} | } | ||
| Line 1,147: | Line 1,147: | ||
vector<int> Roach::_arDirectionY; | vector<int> Roach::_arDirectionY; | ||
void Roach::DirectionAllocate(int X | void Roach::DirectionAllocate(int X[], int Y[]) | ||
{ | { | ||
_arDirectionX.resize(8); | _arDirectionX.resize(8); | ||
_arDirectionY.resize(8); | _arDirectionY.resize(8); | ||
_arDirectionX.assign(&X | _arDirectionX.assign(&X[0], &X[7]); | ||
_arDirectionY.assign(&Y | _arDirectionY.assign(&Y[0], &Y[7]); | ||
} | } | ||
| Line 1,175: | Line 1,175: | ||
void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction) | void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction) | ||
{ | { | ||
_nCurRow += _arDirectionY | _nCurRow += _arDirectionY[direction]; | ||
_nCurCol += _arDirectionX | _nCurCol += _arDirectionX[direction]; | ||
if(_nCurRow == -1) | if(_nCurRow == -1) | ||
_nCurRow = nRow - 1; | _nCurRow = nRow - 1; | ||
| Line 1,202: | Line 1,202: | ||
int main() | int main() | ||
{ | { | ||
int directX | int directX[8] = {0,1,1,1,0,-1,-1,-1}; | ||
int directY | int directY[8] = {-1,-1,0,1,1,1,0,-1}; | ||
Roach::DirectionAllocate(directX, directY); | Roach::DirectionAllocate(directX, directY); | ||
| Line 1,221: | Line 1,221: | ||
roaches.resize( roaches.size() + 10 ); | roaches.resize( roaches.size() + 10 ); | ||
InputInitData(currow, curcol, course); | InputInitData(currow, curcol, course); | ||
roaches | roaches[roachcount].SetRoach(currow, curcol, course); | ||
roachcount ++; | roachcount ++; | ||
} | } | ||
| Line 1,306: | Line 1,306: | ||
int GetTotalVisitCount() const { return _nTotalVisitCount; } | int GetTotalVisitCount() const { return _nTotalVisitCount; } | ||
virtual void CheckDirectionAndMove(int nRow, int nCol, int direction); | virtual void CheckDirectionAndMove(int nRow, int nCol, int direction); | ||
static void DirectionAllocate(int X | static void DirectionAllocate(int X[], int Y[]); | ||
virtual ~Roach(); | virtual ~Roach(); | ||
virtual int GetDelta() { return 1; } | virtual int GetDelta() { return 1; } | ||
| Line 1,341: | Line 1,341: | ||
for(int i = 0 ; i < _nRoachCount ; i++) | for(int i = 0 ; i < _nRoachCount ; i++) | ||
{ | { | ||
_Roaches | _Roaches[i] = roach[i]; | ||
_arVisitFrequency | _arVisitFrequency[_Roaches[i]->GetCurRow()][_Roaches[i]->GetCurCol()] ++; | ||
} | } | ||
} | } | ||
| Line 1,350: | Line 1,350: | ||
_arVisitFrequency.resize(_nRow); | _arVisitFrequency.resize(_nRow); | ||
for(int i = 0 ; i < _nRow ; i++) | for(int i = 0 ; i < _nRow ; i++) | ||
_arVisitFrequency | _arVisitFrequency[i].resize(_nCol); | ||
SetArrayAsZero(); | SetArrayAsZero(); | ||
} | } | ||
| Line 1,358: | Line 1,358: | ||
for(int i = 0 ; i < _nRoachCount ; i++) | for(int i = 0 ; i < _nRoachCount ; i++) | ||
{ | { | ||
delete _Roaches | delete _Roaches[i]; | ||
} | } | ||
| Line 1,366: | Line 1,366: | ||
{ | { | ||
for(int i = 0 ; i < _nRoachCount ; i ++) | for(int i = 0 ; i < _nRoachCount ; i ++) | ||
cout << _Roaches | cout << _Roaches[i]->GetTotalVisitCount() << endl; | ||
cout << endl; | cout << endl; | ||
| Line 1,374: | Line 1,374: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
cout << _arVisitFrequency | cout << _arVisitFrequency[i][j] << " "; | ||
} | } | ||
cout << endl; | cout << endl; | ||
| Line 1,386: | Line 1,386: | ||
{ | { | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
_arVisitFrequency | _arVisitFrequency[i][j] = 0; | ||
} | } | ||
} | } | ||
| Line 1,396: | Line 1,396: | ||
for(int j = 0 ; j < _nCol ; j ++) | for(int j = 0 ; j < _nCol ; j ++) | ||
{ | { | ||
if(_arVisitFrequency | if(_arVisitFrequency[i][j] == 0) | ||
return false; | return false; | ||
} | } | ||
| Line 1,405: | Line 1,405: | ||
void RandomWalkBoard::IncreaseVisitCount(int nSequence) | void RandomWalkBoard::IncreaseVisitCount(int nSequence) | ||
{ | { | ||
_arVisitFrequency | _arVisitFrequency[ _Roaches[nSequence]->GetCurRow() ][ _Roaches[nSequence]->GetCurCol() ] ++; | ||
} | } | ||
| Line 1,415: | Line 1,415: | ||
for(int i = 0 ; i < _nRoachCount ; i ++) | for(int i = 0 ; i < _nRoachCount ; i ++) | ||
{ | { | ||
if( _Roaches | if( _Roaches[i]->GetCourse().size() > _Roaches[i]->GetTotalVisitCount() ) | ||
{ | { | ||
direction = _Roaches | direction = _Roaches[i]->GetCourse()[ _Roaches[i]->GetTotalVisitCount() ] - 48; | ||
for( int j = 0 ; j < _Roaches | for( int j = 0 ; j < _Roaches[i]->GetDelta() ; j++) | ||
{ | { | ||
_Roaches | _Roaches[i]->CheckDirectionAndMove(_nRow, _nCol, direction); | ||
IncreaseVisitCount(i); | IncreaseVisitCount(i); | ||
} | } | ||
_Roaches | _Roaches[i]->IncreaseTotalVisitCount(); | ||
} | } | ||
} | } | ||
| Line 1,434: | Line 1,434: | ||
for(int i = 0 ; i < _nRoachCount ; i ++) | for(int i = 0 ; i < _nRoachCount ; i ++) | ||
{ | { | ||
if( _Roaches | if( _Roaches[i]->GetTotalVisitCount() != _Roaches[i]->GetCourse().size() ) | ||
return false; | return false; | ||
} | } | ||
| Line 1,445: | Line 1,445: | ||
vector<int> Roach::_arDirectionY; | vector<int> Roach::_arDirectionY; | ||
void Roach::DirectionAllocate(int X | void Roach::DirectionAllocate(int X[], int Y[]) | ||
{ | { | ||
_arDirectionX.resize(8); | _arDirectionX.resize(8); | ||
_arDirectionY.resize(8); | _arDirectionY.resize(8); | ||
_arDirectionX.assign(&X | _arDirectionX.assign(&X[0], &X[7]); | ||
_arDirectionY.assign(&Y | _arDirectionY.assign(&Y[0], &Y[7]); | ||
} | } | ||
| Line 1,477: | Line 1,477: | ||
void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction) | void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction) | ||
{ | { | ||
_nCurRow += _arDirectionY | _nCurRow += _arDirectionY[direction]; | ||
_nCurCol += _arDirectionX | _nCurCol += _arDirectionX[direction]; | ||
if(_nCurRow == -1) | if(_nCurRow == -1) | ||
_nCurRow = nRow - 1; | _nCurRow = nRow - 1; | ||
| Line 1,519: | Line 1,519: | ||
int main() | int main() | ||
{ | { | ||
int directX | int directX[8] = {0,1,1,1,0,-1,-1,-1}; | ||
int directY | int directY[8] = {-1,-1,0,1,1,1,0,-1}; | ||
Roach::DirectionAllocate(directX, directY); | Roach::DirectionAllocate(directX, directY); | ||
| Line 1,544: | Line 1,544: | ||
else if(isSuper == 'S') | else if(isSuper == 'S') | ||
temp = new SuperRoach(currow, curcol, course); | temp = new SuperRoach(currow, curcol, course); | ||
roaches | roaches[roachcount] = temp; | ||
roachcount ++; | roachcount ++; | ||
} | } | ||
| Line 1,566: | Line 1,566: | ||
---- | ---- | ||
[[RandomWalk2]] | [[RandomWalk2]] | ||
Latest revision as of 00:29, 27 March 2026
오리지날 ver 1.0
헤더
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
class RandomWalkBoard
{
private:
enum { MAXCOURSE = 100 };
int _nRow;
int _nCol;
int _nCurRow;
int _nCurCol;
int _nTotalVisitCount;
char *_szCourse;
int **_arVisitFrequency;
int _arDirectionX[8];
int _arDirectionY[8];
public:
RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, char *szCourse, int DirectX[], int DirectY[]);
~RandomWalkBoard();
void CourseAllocate(char *szCourse);
void BoardAllocate();
void BoardFree();
void CourseFree();
void ShowStatus();
void SetArrayAsZero();
bool CheckCompletelyPatrol();
void IncreaseVisitCount();
void IncreaseTotalVisitCount();
void StartProcedure();
bool CheckEndCourse();
void CheckDirectionAndMove(int direction);
void DirectionAllocate(int x[], int y[]);
};
#endif
소스
#include "RandomWalkBoard.h"
#include <cstring>
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, char *szCourse, int DirectX[], int DirectY[])
{
_nRow = nRow;
_nCol = nCol;
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
CourseAllocate(szCourse);
BoardAllocate();
SetArrayAsZero();
DirectionAllocate(DirectX, DirectY);
_arVisitFrequency[_nCurRow][_nCurCol] ++;
}
void RandomWalkBoard::CourseAllocate(char *szCourse)
{
_szCourse = new char[ strlen(szCourse) + 1 ];
strcpy(_szCourse, szCourse);
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency = new int*[_nRow];
for(int i = 0 ; i < _nRow ; i++)
{
_arVisitFrequency[i] = new int[_nCol];
}
}
void RandomWalkBoard::CourseFree()
{
delete [] _szCourse;
}
void RandomWalkBoard::DirectionAllocate(int x[], int y[])
{
for(int i = 0 ; i < 8 ; i ++)
{
_arDirectionX[i] = x[i];
_arDirectionY[i] = y[i];
}
}
void RandomWalkBoard::BoardFree()
{
if(_arVisitFrequency)
{
for(int i = 0 ; i < _nRow ; i++)
{
if(_arVisitFrequency[i])
delete [] _arVisitFrequency[i];
}
delete [] _arVisitFrequency;
}
}
RandomWalkBoard::~RandomWalkBoard()
{
CourseFree();
BoardFree();
}
void RandomWalkBoard::ShowStatus()
{
cout << _nTotalVisitCount << endl << endl;
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
_arVisitFrequency[i][j] = 0;
}
}
}
bool RandomWalkBoard::CheckCompletelyPatrol()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
{
return false;
}
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount()
{
_arVisitFrequency[_nCurRow][_nCurCol] ++;
}
void RandomWalkBoard::IncreaseTotalVisitCount()
{
_nTotalVisitCount ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
direction = _szCourse[ _nTotalVisitCount ] - 48;
CheckDirectionAndMove(direction);
IncreaseVisitCount();
IncreaseTotalVisitCount();
}
}
bool RandomWalkBoard::CheckEndCourse()
{
if( _nTotalVisitCount == strlen( _szCourse ) )
{
return true;
}
return false;
}
void RandomWalkBoard::CheckDirectionAndMove(int direction)
{
_nCurRow += _arDirectionY[direction];
_nCurCol += _arDirectionX[direction];
if(_nCurRow == -1)
_nCurRow = _nRow - 1;
else if(_nCurRow == _nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = _nCol - 1;
else if(_nCurCol == _nCol)
_nCurCol = 0;
}
메인
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
#include "RandomWalkBoard.h"
const int MAXCOURSE = 100;
void InputInitData(int &row, int &col, int &currow, int &curcol, char course[]);
fstream in("test.txt");
int main()
{
int row, col, currow, curcol;
char course[MAXCOURSE];
InputInitData(row, col, currow, curcol, course);
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
RandomWalkBoard test(row, col, currow, curcol, course, directX, directY);
test.StartProcedure();
test.ShowStatus();
return 0;
}
void InputInitData(int &row, int &col, int &currow, int &curcol, char course[])
{
in >> row;
in >> col;
in >> currow;
in >> curcol;
in >> course;
in.close();
}
Array는 모두 Vector로, char* 은 String으로 바꾼 버전 version 1.5
헤더
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
#include <vector>
#include <string>
using namespace std;
class RandomWalkBoard
{
private:
enum { MAXCOURSE = 100 };
int _nRow;
int _nCol;
int _nCurRow;
int _nCurCol;
int _nTotalVisitCount;
string _szCourse;
vector< vector<int> > _arVisitFrequency;
vector<int> _arDirectionX;
vector<int> _arDirectionY;
public:
RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, string& szCourse, int DirectX[], int DirectY[]);
~RandomWalkBoard();
void BoardAllocate();
void ShowStatus();
void SetArrayAsZero();
bool CheckCompletelyPatrol();
void IncreaseVisitCount();
void IncreaseTotalVisitCount();
void StartProcedure();
bool CheckEndCourse();
void CheckDirectionAndMove(int direction);
void DirectionAllocate(int x[], int y[]);
};
#endif
소스
#include "RandomWalkBoard.h"
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, string& szCourse, int DirectX[], int DirectY[])
{
_nRow = nRow;
_nCol = nCol;
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
_szCourse = szCourse;
BoardAllocate();
DirectionAllocate(DirectX, DirectY);
_arVisitFrequency[_nCurRow][_nCurCol] ++;
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency.resize(_nRow);
for(int i = 0 ; i < _nRow ; i++)
_arVisitFrequency[i].resize(_nCol);
SetArrayAsZero();
}
void RandomWalkBoard::DirectionAllocate(int X[], int Y[])
{
_arDirectionX.resize(8);
_arDirectionY.resize(8);
_arDirectionX.assign(&X[0], &X[7]);
_arDirectionY.assign(&Y[0], &Y[7]);
}
RandomWalkBoard::~RandomWalkBoard()
{
}
void RandomWalkBoard::ShowStatus()
{
cout << _nTotalVisitCount << endl << endl;
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
_arVisitFrequency[i][j] = 0;
}
}
bool RandomWalkBoard::CheckCompletelyPatrol()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount()
{
_arVisitFrequency[_nCurRow][_nCurCol] ++;
}
void RandomWalkBoard::IncreaseTotalVisitCount()
{
_nTotalVisitCount ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
direction = _szCourse[ _nTotalVisitCount ] - 48;
CheckDirectionAndMove(direction);
IncreaseVisitCount();
IncreaseTotalVisitCount();
}
}
bool RandomWalkBoard::CheckEndCourse()
{
if( _nTotalVisitCount == _szCourse.size() )
return true;
return false;
}
void RandomWalkBoard::CheckDirectionAndMove(int direction)
{
_nCurRow += _arDirectionY[direction];
_nCurCol += _arDirectionX[direction];
if(_nCurRow == -1)
_nCurRow = _nRow - 1;
else if(_nCurRow == _nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = _nCol - 1;
else if(_nCurCol == _nCol)
_nCurCol = 0;
}
메인
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "RandomWalkBoard.h"
const int MAXCOURSE = 100;
void InputInitData(int &row, int &col, int &currow, int &curcol, string &course);
fstream in("test.txt");
int main()
{
int row, col, currow, curcol;
string course;
InputInitData(row, col, currow, curcol, course);
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
RandomWalkBoard test(row, col, currow, curcol, course, directX, directY);
test.StartProcedure();
test.ShowStatus();
return 0;
}
void InputInitData(int &row, int &col, int &currow, int &curcol, string &course)
{
in >> row;
in >> col;
in >> currow;
in >> curcol;
in >> course;
in.close();
}
살짝 OOP 너무 잘게 나누면 관리하기 힘들것 같아서 일단 보드, 바퀴벌레 두개의 클래스로만 나눴음 version 2.0
헤더
Board.h
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
#include <vector>
#include <string>
#include "Roach.h"
using namespace std;
class RandomWalkBoard
{
private:
enum { MAXCOURSE = 100 };
int _nRow;
int _nCol;
vector< vector<int> > _arVisitFrequency;
Roach _Roach;
vector<int> _arDirectionX;
vector<int> _arDirectionY;
public:
RandomWalkBoard(int nRow, int nCol, int DirectX[], int DirectY[], const Roach& roach);
~RandomWalkBoard();
void BoardAllocate();
void ShowStatus() const;
void SetArrayAsZero();
bool CheckCompletelyPatrol() const;
void IncreaseVisitCount();
void StartProcedure();
bool CheckEndCourse() const;
void DirectionAllocate(int x[], int y[]);
};
#endif
Roach.h
#ifndef _ROACH_H_
#define _ROACH_H_
#include <vector>
#include <string>
using namespace std;
class Roach
{
private:
int _nCurRow;
int _nCurCol;
int _nTotalVisitCount;
string _szCourse;
public:
Roach(int nCurRow, int nCurCol, string szCourse);
const string& GetCourse() const;
int GetCurRow() const;
int GetCurCol() const;
void IncreaseTotalVisitCount();
void CheckDirectionAndMove(const vector<int>& X, const vector<int>& Y, int nRow, int nCol, int direction);
int GetTotalVisitCount() const;
};
#endif
소스
Board.cpp
#include "RandomWalkBoard.h"
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int DirectX[], int DirectY[], const Roach& roach) : _Roach(roach)
{
_nRow = nRow;
_nCol = nCol;
BoardAllocate();
DirectionAllocate(DirectX, DirectY);
_arVisitFrequency[_Roach.GetCurRow()][_Roach.GetCurCol()] ++;
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency.resize(_nRow);
for(int i = 0 ; i < _nRow ; i++)
_arVisitFrequency[i].resize(_nCol);
SetArrayAsZero();
}
void RandomWalkBoard::DirectionAllocate(int X[], int Y[])
{
_arDirectionX.resize(8);
_arDirectionY.resize(8);
_arDirectionX.assign(&X[0], &X[7]);
_arDirectionY.assign(&Y[0], &Y[7]);
}
RandomWalkBoard::~RandomWalkBoard()
{
}
void RandomWalkBoard::ShowStatus() const
{
cout << _Roach.GetTotalVisitCount() << endl << endl;
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
_arVisitFrequency[i][j] = 0;
}
}
bool RandomWalkBoard::CheckCompletelyPatrol() const
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount()
{
_arVisitFrequency[ _Roach.GetCurRow() ][ _Roach.GetCurCol() ] ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
direction = _Roach.GetCourse()[ _Roach.GetTotalVisitCount() ] - 48;
_Roach.CheckDirectionAndMove(_arDirectionX, _arDirectionY, _nRow, _nCol, direction);
IncreaseVisitCount();
_Roach.IncreaseTotalVisitCount();
}
}
bool RandomWalkBoard::CheckEndCourse() const
{
if( _Roach.GetTotalVisitCount() == _Roach.GetCourse().size() )
return true;
return false;
}
Roach.cpp
#include "Roach.h"
Roach::Roach(int nCurRow, int nCurCol, string szCourse)
{
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
_szCourse = szCourse;
}
const string& Roach::GetCourse() const
{
return _szCourse;
}
int Roach::GetCurRow() const
{
return _nCurRow;
}
int Roach::GetCurCol() const
{
return _nCurCol;
}
int Roach::GetTotalVisitCount() const
{
return _nTotalVisitCount;
}
void Roach::IncreaseTotalVisitCount()
{
_nTotalVisitCount ++;
}
void Roach::CheckDirectionAndMove(const vector<int>& X, const vector<int>& Y, int nRow, int nCol, int direction)
{
_nCurRow += Y[direction];
_nCurCol += X[direction];
if(_nCurRow == -1)
_nCurRow = nRow - 1;
else if(_nCurRow == nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = nCol - 1;
else if(_nCurCol == nCol)
_nCurCol = 0;
}
메인
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "RandomWalkBoard.h"
#include "Roach.h"
const int MAXCOURSE = 100;
void InputInitData(int &row, int &col, int &currow, int &curcol, string &course);
fstream in("test.txt");
int main()
{
int row, col, currow, curcol;
string course;
InputInitData(row, col, currow, curcol, course);
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
Roach roach(currow, curcol, course);
RandomWalkBoard test(row, col, directX, directY, roach);
test.StartProcedure();
test.ShowStatus();
return 0;
}
void InputInitData(int &row, int &col, int &currow, int &curcol, string &course)
{
in >> row;
in >> col;
in >> currow;
in >> curcol;
in >> course;
in.close();
}
변경1 을 만족하는 코드
- 흠.. 하면서 좀 많이 뜯어 고쳤습니다. 역시 디자인이 구린거였음..;;
- 하면서 static 사용법을 확실히 익혔습니다. 상규 땡쓰~^^
헤더
Board
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
#include <vector>
#include <string>
#include "Roach.h"
using namespace std;
class RandomWalkBoard
{
private:
enum { MAX_ROACH = 2 };
int _nRow;
int _nCol;
vector< vector<int> > _arVisitFrequency;
Roach _Roach[2];
void BoardAllocate();
void SetArrayAsZero();
bool CheckEndCourse() const;
bool CheckCompletelyPatrol() const;
public:
RandomWalkBoard(int nRow, int nCol, const Roach roach[]);
~RandomWalkBoard();
void ShowStatus() const;
void IncreaseVisitCount(int nSequence);
void StartProcedure();
};
#endif
Roach
#ifndef _ROACH_H_
#define _ROACH_H_
#include <vector>
#include <string>
using namespace std;
class Roach
{
private:
int _nCurRow;
int _nCurCol;
int _nTotalVisitCount;
string _szCourse;
static vector<int> _arDirectionX;
static vector<int> _arDirectionY;
public:
Roach();
Roach(int nCurRow, int nCurCol, string szCourse);
void SetRoach(int nCurRow, int nCurCol, string szCourse);
const string& GetCourse() const { return _szCourse; }
int GetCurRow() const { return _nCurRow; }
int GetCurCol() const { return _nCurCol; }
void IncreaseTotalVisitCount() { _nTotalVisitCount ++; }
int GetTotalVisitCount() const { return _nTotalVisitCount; }
void CheckDirectionAndMove(int nRow, int nCol, int direction);
static void DirectionAllocate(int X[], int Y[]);
};
#endif
소스
Board
#include "RandomWalkBoard.h"
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, const Roach roach[])
{
_nRow = nRow;
_nCol = nCol;
BoardAllocate();
for(int i = 0 ; i < MAX_ROACH ; i++)
{
_Roach[i] = roach[i];
_arVisitFrequency[_Roach[i].GetCurRow()][_Roach[i].GetCurCol()] ++;
}
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency.resize(_nRow);
for(int i = 0 ; i < _nRow ; i++)
_arVisitFrequency[i].resize(_nCol);
SetArrayAsZero();
}
RandomWalkBoard::~RandomWalkBoard()
{
}
void RandomWalkBoard::ShowStatus() const
{
for(int i = 0 ; i < MAX_ROACH ; i ++)
cout << _Roach[i].GetTotalVisitCount() << endl;
cout << endl;
for(i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
_arVisitFrequency[i][j] = 0;
}
}
bool RandomWalkBoard::CheckCompletelyPatrol() const
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount(int nSequence)
{
_arVisitFrequency[ _Roach[nSequence].GetCurRow() ][ _Roach[nSequence].GetCurCol() ] ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
for(int i = 0 ; i < MAX_ROACH ; i ++)
{
if( _Roach[i].GetCourse().size() > _Roach[i].GetTotalVisitCount() )
{
direction = _Roach[i].GetCourse()[ _Roach[i].GetTotalVisitCount() ] - 48;
_Roach[i].CheckDirectionAndMove(_nRow, _nCol, direction);
IncreaseVisitCount(i);
_Roach[i].IncreaseTotalVisitCount();
}
}
}
}
bool RandomWalkBoard::CheckEndCourse() const
{
if( _Roach[0].GetTotalVisitCount() == _Roach[0].GetCourse().size() && _Roach[1].GetTotalVisitCount() == _Roach[1].GetCourse().size())
return true;
return false;
}
Roach
#include "Roach.h"
vector<int> Roach::_arDirectionX;
vector<int> Roach::_arDirectionY;
void Roach::DirectionAllocate(int X[], int Y[])
{
_arDirectionX.resize(8);
_arDirectionY.resize(8);
_arDirectionX.assign(&X[0], &X[7]);
_arDirectionY.assign(&Y[0], &Y[7]);
}
Roach::Roach()
{
}
Roach::Roach(int nCurRow, int nCurCol, string szCourse)
{
SetRoach(nCurRow, nCurCol, szCourse);
}
void Roach::SetRoach(int nCurRow, int nCurCol, string szCourse)
{
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
_szCourse = szCourse;
}
void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction)
{
_nCurRow += _arDirectionY[direction];
_nCurCol += _arDirectionX[direction];
if(_nCurRow == -1)
_nCurRow = nRow - 1;
else if(_nCurRow == nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = nCol - 1;
else if(_nCurCol == nCol)
_nCurCol = 0;
}
메인
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "RandomWalkBoard.h"
#include "Roach.h"
const int MAXCOURSE = 100;
void InputInitData(int &currow, int &curcol, string &course);
fstream in("test.txt");
int main()
{
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
Roach::DirectionAllocate(directX, directY);
int row, col, currow, curcol;
string course;
Roach roach[2];
in >> row;
in >> col;
for(int i = 0 ; i < 2 ; i ++)
{
InputInitData(currow, curcol, course);
roach[i].SetRoach(currow, curcol, course);
}
RandomWalkBoard test(row, col, roach);
test.StartProcedure();
test.ShowStatus();
in.close();
return 0;
}
void InputInitData(int &currow, int &curcol, string &course)
{
in >> currow;
in >> curcol;
in >> course;
}
- 왜 자꾸 탭이 씹히는지..--;
변경 2 를 만족하는 코드
- 변경 1에서 거의 변경된게 없더군요. 혼자 좋아했다는..--; 변경1을 하면서 리팩토링 쬐금 공부한걸 써봤습니다. 메소드 옮기기 get 이런거 마니 나오면 그것도 옮기기 정도?
- 변수 이름도 살짝 바꾸고.. 하다 보니까 뭔가 깨달을것도 같다는 생각이.. ^^;
- 쉽게 생각할라고 일단 999는 지워버렸습니다.
헤더
Board.h
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
#include <vector>
#include <string>
#include "Roach.h"
using namespace std;
class RandomWalkBoard
{
private:
int _nRoachCount;
int _nRow;
int _nCol;
vector< vector<int> > _arVisitFrequency;
vector< Roach > _Roaches;
void BoardAllocate();
void SetArrayAsZero();
bool CheckEndCourse() const;
bool CheckCompletelyPatrol() const;
bool CheckAllRoachesEndCourse() const;
public:
RandomWalkBoard(int nRow, int nCol, vector<Roach>& roach, int nRoachCount);
~RandomWalkBoard();
void ShowStatus() const;
void IncreaseVisitCount(int nSequence);
void StartProcedure();
};
#endif
Roach.h
#ifndef _ROACH_H_
#define _ROACH_H_
#include <vector>
#include <string>
using namespace std;
class Roach
{
private:
int _nCurRow;
int _nCurCol;
int _nTotalVisitCount;
string _szCourse;
static vector<int> _arDirectionX;
static vector<int> _arDirectionY;
public:
Roach();
Roach(int nCurRow, int nCurCol, string szCourse);
void SetRoach(int nCurRow, int nCurCol, string szCourse);
const string& GetCourse() const { return _szCourse; }
int GetCurRow() const { return _nCurRow; }
int GetCurCol() const { return _nCurCol; }
void IncreaseTotalVisitCount() { _nTotalVisitCount ++; }
int GetTotalVisitCount() const { return _nTotalVisitCount; }
void CheckDirectionAndMove(int nRow, int nCol, int direction);
static void DirectionAllocate(int X[], int Y[]);
};
#endif
소스
Board.cpp
#include "RandomWalkBoard.h"
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, vector<Roach>& roach, int nRoachCount)
{
_nRow = nRow;
_nCol = nCol;
_nRoachCount = nRoachCount;
_Roaches.resize(_nRoachCount);
BoardAllocate();
for(int i = 0 ; i < _nRoachCount ; i++)
{
_Roaches[i] = roach[i];
_arVisitFrequency[_Roaches[i].GetCurRow()][_Roaches[i].GetCurCol()] ++;
}
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency.resize(_nRow);
for(int i = 0 ; i < _nRow ; i++)
_arVisitFrequency[i].resize(_nCol);
SetArrayAsZero();
}
RandomWalkBoard::~RandomWalkBoard()
{
}
void RandomWalkBoard::ShowStatus() const
{
for(int i = 0 ; i < _nRoachCount ; i ++)
cout << _Roaches[i].GetTotalVisitCount() << endl;
cout << endl;
for(i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
_arVisitFrequency[i][j] = 0;
}
}
bool RandomWalkBoard::CheckCompletelyPatrol() const
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount(int nSequence)
{
_arVisitFrequency[ _Roaches[nSequence].GetCurRow() ][ _Roaches[nSequence].GetCurCol() ] ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
for(int i = 0 ; i < _nRoachCount ; i ++)
{
if( _Roaches[i].GetCourse().size() > _Roaches[i].GetTotalVisitCount() )
{
direction = _Roaches[i].GetCourse()[ _Roaches[i].GetTotalVisitCount() ] - 48;
_Roaches[i].CheckDirectionAndMove(_nRow, _nCol, direction);
IncreaseVisitCount(i);
_Roaches[i].IncreaseTotalVisitCount();
}
}
}
}
bool RandomWalkBoard::CheckEndCourse() const
{
if( CheckAllRoachesEndCourse() )
return true;
return false;
}
bool RandomWalkBoard::CheckAllRoachesEndCourse() const
{
for(int i = 0 ; i < _nRoachCount ; i ++)
{
if( _Roaches[i].GetTotalVisitCount() != _Roaches[i].GetCourse().size() )
return false;
}
return true;
}
Roach.cpp
#include "Roach.h"
vector<int> Roach::_arDirectionX;
vector<int> Roach::_arDirectionY;
void Roach::DirectionAllocate(int X[], int Y[])
{
_arDirectionX.resize(8);
_arDirectionY.resize(8);
_arDirectionX.assign(&X[0], &X[7]);
_arDirectionY.assign(&Y[0], &Y[7]);
}
Roach::Roach()
{
}
Roach::Roach(int nCurRow, int nCurCol, string szCourse)
{
SetRoach(nCurRow, nCurCol, szCourse);
}
void Roach::SetRoach(int nCurRow, int nCurCol, string szCourse)
{
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
_szCourse = szCourse;
}
void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction)
{
_nCurRow += _arDirectionY[direction];
_nCurCol += _arDirectionX[direction];
if(_nCurRow == -1)
_nCurRow = nRow - 1;
else if(_nCurRow == nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = nCol - 1;
else if(_nCurCol == nCol)
_nCurCol = 0;
}
메인
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#include "RandomWalkBoard.h"
#include "Roach.h"
void InputInitData(int &currow, int &curcol, string &course);
fstream in("test.txt");
int main()
{
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
Roach::DirectionAllocate(directX, directY);
int row, col, currow, curcol;
int roachcount = 0;
string course;
vector<Roach> roaches(10);
in >> row;
in >> col;
while(!in.eof())
{
if(roaches.size() <= roachcount)
roaches.resize( roaches.size() + 10 );
InputInitData(currow, curcol, course);
roaches[roachcount].SetRoach(currow, curcol, course);
roachcount ++;
}
RandomWalkBoard test(row, col, roaches, roachcount);
test.StartProcedure();
test.ShowStatus();
in.close();
return 0;
}
void InputInitData(int &currow, int &curcol, string &course)
{
in >> currow;
in >> curcol;
in >> course;
}
요구3을 만족하는 코드
- 아.. 이번에도 별로 고치지 않았다는 것에 위안을.. ^^;
- STL 컨테이너는 복사본을 사용한다는 걸 알고 포인터로 바꿨습니다.
- 어째 점점 더러워져가는것 같다는..--;
- ... 지금 보니까 SuperRoach 클래스는 왜 만들었는지 의문이 간다.
헤더
Board
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
#include <vector>
#include <string>
#include "Roach.h"
using namespace std;
class RandomWalkBoard
{
private:
int _nRoachCount;
int _nRow;
int _nCol;
vector< vector<int> > _arVisitFrequency;
vector< Roach* > _Roaches;
void BoardAllocate();
void SetArrayAsZero();
bool CheckEndCourse() const;
bool CheckCompletelyPatrol() const;
public:
RandomWalkBoard(int nRow, int nCol, vector< Roach* >& roach, int nRoachCount);
~RandomWalkBoard();
void ShowStatus() const;
void IncreaseVisitCount(int nSequence);
void StartProcedure();
};
#endif
Roach
#ifndef _ROACH_H_
#define _ROACH_H_
#include <vector>
#include <string>
using namespace std;
class Roach
{
protected:
int _nCurRow;
int _nCurCol;
static vector<int> _arDirectionX;
static vector<int> _arDirectionY;
int _nTotalVisitCount;
string _szCourse;
public:
Roach();
Roach(int nCurRow, int nCurCol, string szCourse);
void SetRoach(int nCurRow, int nCurCol, string szCourse);
const string& GetCourse() const { return _szCourse; }
int GetCurRow() const { return _nCurRow; }
int GetCurCol() const { return _nCurCol; }
void IncreaseTotalVisitCount() { _nTotalVisitCount ++; }
int GetTotalVisitCount() const { return _nTotalVisitCount; }
virtual void CheckDirectionAndMove(int nRow, int nCol, int direction);
static void DirectionAllocate(int X[], int Y[]);
virtual ~Roach();
virtual int GetDelta() { return 1; }
};
#endif
SuperRoach
#include "roach.h"
class SuperRoach : public Roach
{
public:
SuperRoach();
~SuperRoach();
SuperRoach(int nCurRow, int nCurCol, string szCourse);
int GetDelta() { return 2; }
};
소스
Board
#include "RandomWalkBoard.h"
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, vector< Roach* >& roach, int nRoachCount)
{
_nRow = nRow;
_nCol = nCol;
_nRoachCount = nRoachCount;
_Roaches.resize(_nRoachCount);
BoardAllocate();
for(int i = 0 ; i < _nRoachCount ; i++)
{
_Roaches[i] = roach[i];
_arVisitFrequency[_Roaches[i]->GetCurRow()][_Roaches[i]->GetCurCol()] ++;
}
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency.resize(_nRow);
for(int i = 0 ; i < _nRow ; i++)
_arVisitFrequency[i].resize(_nCol);
SetArrayAsZero();
}
RandomWalkBoard::~RandomWalkBoard()
{
for(int i = 0 ; i < _nRoachCount ; i++)
{
delete _Roaches[i];
}
}
void RandomWalkBoard::ShowStatus() const
{
for(int i = 0 ; i < _nRoachCount ; i ++)
cout << _Roaches[i]->GetTotalVisitCount() << endl;
cout << endl;
for(i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
_arVisitFrequency[i][j] = 0;
}
}
bool RandomWalkBoard::CheckCompletelyPatrol() const
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount(int nSequence)
{
_arVisitFrequency[ _Roaches[nSequence]->GetCurRow() ][ _Roaches[nSequence]->GetCurCol() ] ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
for(int i = 0 ; i < _nRoachCount ; i ++)
{
if( _Roaches[i]->GetCourse().size() > _Roaches[i]->GetTotalVisitCount() )
{
direction = _Roaches[i]->GetCourse()[ _Roaches[i]->GetTotalVisitCount() ] - 48;
for( int j = 0 ; j < _Roaches[i]->GetDelta() ; j++)
{
_Roaches[i]->CheckDirectionAndMove(_nRow, _nCol, direction);
IncreaseVisitCount(i);
}
_Roaches[i]->IncreaseTotalVisitCount();
}
}
}
}
bool RandomWalkBoard::CheckEndCourse() const
{
for(int i = 0 ; i < _nRoachCount ; i ++)
{
if( _Roaches[i]->GetTotalVisitCount() != _Roaches[i]->GetCourse().size() )
return false;
}
return true;
}
Roach
#include "Roach.h"
vector<int> Roach::_arDirectionX;
vector<int> Roach::_arDirectionY;
void Roach::DirectionAllocate(int X[], int Y[])
{
_arDirectionX.resize(8);
_arDirectionY.resize(8);
_arDirectionX.assign(&X[0], &X[7]);
_arDirectionY.assign(&Y[0], &Y[7]);
}
Roach::Roach()
{
}
Roach::~Roach()
{
}
Roach::Roach(int nCurRow, int nCurCol, string szCourse)
{
SetRoach(nCurRow, nCurCol, szCourse);
}
void Roach::SetRoach(int nCurRow, int nCurCol, string szCourse)
{
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
_szCourse = szCourse;
}
void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction)
{
_nCurRow += _arDirectionY[direction];
_nCurCol += _arDirectionX[direction];
if(_nCurRow == -1)
_nCurRow = nRow - 1;
else if(_nCurRow == nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = nCol - 1;
else if(_nCurCol == nCol)
_nCurCol = 0;
}
SuperRoach
#include "superroach.h"
SuperRoach::SuperRoach()
{
}
SuperRoach::~SuperRoach()
{
}
SuperRoach::SuperRoach(int nCurRow, int nCurCol, string szCourse)
{
SetRoach(nCurRow, nCurCol, szCourse);
}
메인
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#include "RandomWalkBoard.h"
#include "SuperRoach.h"
void InputInitData(int &currow, int &curcol, string &course, char &isSuper);
fstream in("test.txt");
int main()
{
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
Roach::DirectionAllocate(directX, directY);
int row, col, currow, curcol;
int roachcount = 0;
char isSuper;
string course;
vector< Roach* > roaches(10);
Roach* temp;
in >> row;
in >> col;
while(!in.eof())
{
if(roaches.size() <= roachcount)
roaches.resize( roaches.size() + 10 );
InputInitData(currow, curcol, course, isSuper);
if(isSuper == 'N')
temp = new Roach(currow, curcol, course);
else if(isSuper == 'S')
temp = new SuperRoach(currow, curcol, course);
roaches[roachcount] = temp;
roachcount ++;
}
RandomWalkBoard test(row, col, roaches, roachcount);
test.StartProcedure();
test.ShowStatus();
in.close();
return 0;
}
void InputInitData(int &currow, int &curcol, string &course, char &isSuper)
{
in >> currow;
in >> curcol;
in >> isSuper;
in >> course;
}