U E D R , A S I H C RSS

aekae/Random Walk

리팩토링 전

~cpp 
#include <iostream> 
using namespace std; 
#include <ctime> 

int table[5][5] = {0,}; 
bool IsExistZero(); 

int main() 
{ 
	srand(time(0));         // 그냥 쓴다. 
	
	int x, y; 
	
	cin >> x >> y; 
	table[y][x]++; 	

	while( IsExistZero() ) 
	{                
		int a = rand() % 3 - 1;              
		x += a; 
		if(x == 5) 
			x = 0; 
		else if(x == -1) 
			x = 4; 

		int b = rand() % 3 - 1;              
		y += b; 
		if(y == 5) 
			y = 0; 
		else if(y == -1) 
			y = 4; 
	
		table[y][x]++; 	
	} 

	for (int i=0; i<5; i++) 
	{ 
		for (int j=0; j<5; j++) 
			cout << table[i][j] << "\t"; 
		cout << endl; 
	}                        
	
	return 0; 
} 

bool IsExistZero() 
{ 
	for (int i=0; i<5; i++) 
	{ 
		for (int j=0; j<5; j++) 
		{ 
			if(table[i][j] == 0) 
				return true; 
		} 
	} 
	return false; 
} 

리팩토링 후

~cpp 
#include <iostream>
using namespace std;
#include <ctime>

bool IsExistZero(int table[5][5]);
void ShowTable(int table[5][5]);
void SetCoordinate(int& coord);

int main()
{
	srand(time(0));		// 그냥 쓴다.
	int table[5][5] = {0,};

	int x, y;

	cin >> x >> y;
	
	while( IsExistZero(table) )
	{		
		table[y][x]++;
		SetCoordinate(x);	
		SetCoordinate(y);	
	}
	ShowTable(table);

	return 0;
}

bool IsExistZero(int table[5][5])
{
	for (int i=0; i<5; i++)
	{
		for (int j=0; j<5; j++)
		{
			if(table[i][j] == 0)
				return true;
		}
	}
	return false;
}

void ShowTable(int table[5][5])
{
	for (int i=0; i<5; i++)
	{
		for (int j=0; j<5; j++)
			cout << table[i][j] << "\t";
		cout << endl;
	}			
}

void SetCoordinate(int& coord)
{
	int delta = rand() % 3 - 1;		
	coord += delta;
	if(coord == 5)
		coord = 0;
	else if(coord == -1)
		coord = 4;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:36
Processing time 0.0109 sec