U E D R , A S I H C RSS

Random Walk/Extreme Slayer

  • 인수군의 Random Walk - 아 심심해--;

  • 헤더

~cpp 
#ifndef _BOARD_H_
#define _BOARD_H_

class RandomWalkBoard
{
private:
	int _nRow;
	int _nCol;
	int** _nBlockFrequency;
	int _nCurRow;
	int _nCurCol;
	int _nTotalMovement;
public:
	RandomWalkBoard(int& nRow, int& nCol, int& nCurRow, int& nCurCol);
	~RandomWalkBoard();
	void BoardAllocate();
	void BoardFree();
	void AddVisitCount();
	void ShowBoardStatus() const;
	bool CheckCompletelyPatrol() const;
	void SetArrayAsZero(int& nCurRow);
	void StartMovement();
	int GetRandomDirection() const; 
	bool CheckCorrectCoordinate(int& nDelRow, int& nDelCol) const;
	void AddTotalCount();
};

#endif

  • 소스

~cpp 
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
#include "RandomWalkBoard.h"

//// 생성자와 소멸자 ////

RandomWalkBoard::RandomWalkBoard(int& nRow, int& nCol, int& nCurRow, int& nCurCol)
{
	_nRow = nRow;
	_nCol = nCol;
	_nCurRow = nCurRow;
	_nCurCol = nCurCol;
	_nTotalMovement = 0;
	srand(time(0));
	BoardAllocate();
}

void RandomWalkBoard::BoardAllocate()
{
	_nBlockFrequency = new int*[_nRow];

	for(int i = 0 ; i < _nRow ; i ++)
	{
		_nBlockFrequency[i] = new int[_nCol];
		SetArrayAsZero(i);
	}
}

void RandomWalkBoard::SetArrayAsZero(int& nCurRow)
{
	for(int i = 0 ; i < _nCol ; i ++)
	{
		_nBlockFrequency[nCurRow][i] = 0;
	}	
}

RandomWalkBoard::~RandomWalkBoard()
{
	BoardFree();
}

void RandomWalkBoard::BoardFree()
{
	if(_nBlockFrequency)
	{
		for(int i = 0 ; i < _nRow ; i ++)
		{
			if(_nBlockFrequency[i])
				delete [] _nBlockFrequency[i];
		}
		delete [] _nBlockFrequency;
	}
}

////////////////////////

//// 본 프로그램 ////

bool RandomWalkBoard::CheckCompletelyPatrol() const
{
	for(int i = 0 ; i < _nRow ; i ++)
	{
		for(int j = 0 ; j < _nCol ; j++)
		{
			if(_nBlockFrequency[i][j] == 0)
				return false;
		}
	}	
	return true;
}

void RandomWalkBoard::ShowBoardStatus() const
{
	for(int i = 0 ; i < _nRow ; i ++)
	{
		for(int j = 0 ; j < _nCol ; j++)
		{
			cout << _nBlockFrequency[i][j] << " ";
		}
		cout << endl;
	}
	cout << "현재 행 : " << _nCurRow << endl;
	cout << "현재 열 : " << _nCurCol << endl;
	cout << "총 이동량 : " << _nTotalMovement << endl;
}

void RandomWalkBoard::AddVisitCount()
{
	_nBlockFrequency[_nCurRow][_nCurCol]++;
}

void RandomWalkBoard::StartMovement()
{
	int DelRow = 0;
	int DelCol = 0;
	while(!CheckCompletelyPatrol())	
	{
		DelRow = GetRandomDirection();
		DelCol = GetRandomDirection();
		if(CheckCorrectCoordinate(DelRow, DelCol))
		{
			_nCurRow += DelRow;
			_nCurCol += DelCol;
			AddVisitCount();
			AddTotalCount();
		}
	}
}

int RandomWalkBoard::GetRandomDirection() const
{
	return (rand()%3) - 1;	
}

bool RandomWalkBoard::CheckCorrectCoordinate(int& nDelRow, int& nDelCol) const
{
	if(_nCurRow + nDelRow < 0 || _nCurRow + nDelRow > _nRow - 1)
		return false;
	if(_nCurCol + nDelCol < 0 || _nCurCol + nDelCol > _nCol - 1)
		return false;
	return true;
}

void RandomWalkBoard::AddTotalCount()
{
	_nTotalMovement++;
}

  • 메인

~cpp 
#include <iostream>
using namespace std;
#include "RandomWalkBoard.h"

void AskInitData(int& nRow, int& nCol, int& nCurRow, int& nCurCol);

int main()
{
	int nRow, nCol, nCurRow, nCurCol;
	AskInitData(nRow, nCol, nCurRow, nCurCol);
	RandomWalkBoard Test(nRow, nCol, nCurRow, nCurCol);
	Test.StartMovement();
	Test.ShowBoardStatus();

	return 0;
}

void AskInitData(int& nRow, int& nCol, int& nCurRow, int& nCurCol)
{
	cout << "\n행을 입력하세요 : ";
	cin >> nRow;
	cout << "\n열을 입력하세요 : ";
	cin >> nCol;
	cout << "\n현재 바퀴벌레의 행을 입력하세요(0 ~ 행-1) : ";
	cin >> nCurRow;
	cout << "\n현재 바퀴벌레의 열을 입력하세요(0 ~ 열-1) : ";
	cin >> nCurCol;
}

  • 간단한걸 너무 복잡하게 짠건 아닌가 하는 생각이 든다.. 제길 -- 인수
  • 근데 리팩토링 책 보면 메소드는 최대한 짧고 간결하게 끊으라 그래서--; -- 인수
  • 소스가 좀 길어지긴 해도 확실히 알아보긴 편한것 같다. -- 인수
  • 이걸 왜 했냐면, 그냥 심심해서..--; 나두 데블스 캠프 가고 싶당.. 쩝.. -- 인수

DeleteMe ) 누가 작성했나요?

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:50
Processing time 0.0285 sec