U E D R , A S I H C RSS

Random Walk2/Insu

No older revisions available

No older revisions available




1. 오리지날 ver 1.0

1.1. 헤더

~cpp 
#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

1.2. 소스

~cpp 
#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;
}

1.3. 메인


~cpp 
#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();
}


2. Array는 모두 Vector로, char* 은 String으로 바꾼 버전 version 1.5

2.1. 헤더

~cpp 
#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 

2.2. 소스

~cpp 
#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; 
} 

2.3. 메인

~cpp 
#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(); 
} 

3. 살짝 OOP 너무 잘게 나누면 관리하기 힘들것 같아서 일단 보드, 바퀴벌레 두개의 클래스로만 나눴음 version 2.0

3.1. 헤더

3.1.1. Board.h

~cpp 
#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 

3.1.2. Roach.h

~cpp 
#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

3.2. 소스

3.2.1. Board.cpp

~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; 
}

3.2.2. Roach.cpp

~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; 
} 

3.3. 메인


~cpp 
#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(); 
} 

4. 변경1 을 만족하는 코드

  • 흠.. 하면서 좀 많이 뜯어 고쳤습니다. 역시 디자인이 구린거였음..;;
  • 하면서 static 사용법을 확실히 익혔습니다. 상규 땡쓰~^^

4.1. 헤더

4.1.1. Board

~cpp 
#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 

4.1.2. Roach

~cpp 
#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

4.2. 소스

4.2.1. Board

~cpp 
#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; 
}

4.2.2. 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; 
} 

4.3. 메인

~cpp 
#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; 
} 

  • 왜 자꾸 탭이 씹히는지..--;

5. 변경 2 를 만족하는 코드

  • 변경 1에서 거의 변경된게 없더군요. 혼자 좋아했다는..--; 변경1을 하면서 리팩토링 쬐금 공부한걸 써봤습니다. 메소드 옮기기 get 이런거 마니 나오면 그것도 옮기기 정도?
  • 변수 이름도 살짝 바꾸고.. 하다 보니까 뭔가 깨달을것도 같다는 생각이.. ^^;
  • 쉽게 생각할라고 일단 999는 지워버렸습니다.

5.1. 헤더

5.1.1. Board.h

~cpp 
#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 

5.1.2. Roach.h

~cpp 
#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

5.2. 소스

5.2.1. Board.cpp

~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;
}

5.2.2. Roach.cpp

~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; 
} 

5.3. 메인

~cpp 
#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; 
} 

6. 요구3을 만족하는 코드

  • 아.. 이번에도 별로 고치지 않았다는 것에 위안을.. ^^;
  • STL 컨테이너는 복사본을 사용한다는 걸 알고 포인터로 바꿨습니다.
  • 어째 점점 더러워져가는것 같다는..--;
  • ... 지금 보니까 SuperRoach 클래스는 왜 만들었는지 의문이 간다.

6.1. 헤더

6.1.1. Board

~cpp 
#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 

6.1.2. Roach

~cpp 
#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

6.1.3. SuperRoach

~cpp 
#include "roach.h"

class SuperRoach : public Roach
{
public:
	SuperRoach();
	~SuperRoach();
	SuperRoach(int nCurRow, int nCurCol, string szCourse);
	int GetDelta() { return 2; }
};

6.2. 소스

6.2.1. 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() 
{ 
	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;
}

6.2.2. 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()
{
}

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; 
} 

6.2.3. SuperRoach

~cpp 
#include "superroach.h"

SuperRoach::SuperRoach()
{
}

SuperRoach::~SuperRoach()
{
}

SuperRoach::SuperRoach(int nCurRow, int nCurCol, string szCourse)
{
	SetRoach(nCurRow, nCurCol, szCourse);
}

6.3. 메인

~cpp 
#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; 
} 
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:50
Processing time 0.0700 sec