Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Aekae/RandomWalk: Difference between revisions

From ZeroWiki
imported>Unknown
No edit summary
 
(Repair batch-0008 pages from live compare)
 
Line 4: Line 4:
  #include <ctime>  
  #include <ctime>  
   
   
  int table[5][5] = {0,};  
  int table[5][5] = {0,};  
  bool IsExistZero();  
  bool IsExistZero();  
   
   
Line 14: Line 14:
 
 
  cin >> x >> y;  
  cin >> x >> y;  
  table[y][x]++;
  table[y][x]++;
   
   
  while( IsExistZero() )  
  while( IsExistZero() )  
Line 32: Line 32:
  y = 4;  
  y = 4;  
 
 
  table[y][x]++;
  table[y][x]++;
  }  
  }  
   
   
Line 38: Line 38:
  {  
  {  
  for (int j=0; j<5; j++)  
  for (int j=0; j<5; j++)  
  cout << table[i][j] << "\t";  
  cout << table[i][j] << "\t";  
  cout << endl;  
  cout << endl;  
  }                         
  }                         
Line 51: Line 51:
  for (int j=0; j<5; j++)  
  for (int j=0; j<5; j++)  
  {  
  {  
  if(table[i][j] == 0)  
  if(table[i][j] == 0)  
  return true;  
  return true;  
  }  
  }  
Line 62: Line 62:
  #include <ctime>
  #include <ctime>
   
   
  bool IsExistZero(int table[5][5]);
  bool IsExistZero(int table[5][5]);
  void ShowTable(int table[5][5]);
  void ShowTable(int table[5][5]);
  void SetCoordinate(int& coord);
  void SetCoordinate(int& coord);
   
   
Line 69: Line 69:
  {
  {
  srand(time(0)); // 그냥 쓴다.
  srand(time(0)); // 그냥 쓴다.
  int table[5][5] = {0,};
  int table[5][5] = {0,};
   
   
  int x, y;
  int x, y;
Line 77: Line 77:
  while( IsExistZero(table) )
  while( IsExistZero(table) )
  {
  {
  table[y][x]++;
  table[y][x]++;
  SetCoordinate(x);
  SetCoordinate(x);
  SetCoordinate(y);
  SetCoordinate(y);
Line 86: Line 86:
  }
  }
   
   
  bool IsExistZero(int table[5][5])
  bool IsExistZero(int table[5][5])
  {
  {
  for (int i=0; i<5; i++)
  for (int i=0; i<5; i++)
Line 92: Line 92:
  for (int j=0; j<5; j++)
  for (int j=0; j<5; j++)
  {
  {
  if(table[i][j] == 0)
  if(table[i][j] == 0)
  return true;
  return true;
  }
  }
Line 99: Line 99:
  }
  }
   
   
  void ShowTable(int table[5][5])
  void ShowTable(int table[5][5])
  {
  {
  for (int i=0; i<5; i++)
  for (int i=0; i<5; i++)
  {
  {
  for (int j=0; j<5; j++)
  for (int j=0; j<5; j++)
  cout << table[i][j] << "\t";
  cout << table[i][j] << "\t";
  cout << endl;
  cout << endl;
  }
  }
Line 121: Line 121:
----
----
[aekae]
[aekae]

Latest revision as of 01:40, 27 March 2026

리팩토링 전

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

리팩토링 후

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

[aekae]