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.0108 sec