U E D R , A S I H C RSS

Random Walk/대근

~cpp 

// 랜덤 워크 만들기...(마구 더해지는 워크...)

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int test(int);
int main()
{
	srand ( (unsigned int) time (NULL) );
	cout << "랜덤 프로그램입니다..\n";
	cout << "몇 줄을 만들까요?\n";
	int line;
	cin >> line;
	cout<<endl<<endl;
	int ** rands = new int *[line+2]; 
    
	for(int i=0;i<line+2;i++) 
		rands[i] = new int [line+2]; 
	
	for(i=0;i<line+2;i++)
	{
		for(int j=0;j<line+2;j++)
			rands[i][j] = 0;
	}

	for(i=0;i<line+2;i++)
	{
		rands[i][line+1] = -1;
		rands[i][0] = -1;
		rands[line+1][i] = -1;
		rands[0][i] = -1;
	}
	int x = rand() % line + 1;
	int y = rand() % line + 1;
	rands[x][y]++;
	int bang;
	int count;
	int sum=0;
	do
	{
		cin.get();
		system("cls");
		bang = rand() % 8;
		
		if(bang==0)
		{
			x--;
			y--;
			
			if(rands[x][y]==-1)
			{
				x++;
				y++;
			}		
	
		}
		
		else if(bang==1)
		{
			x--;
			
			if(rands[x][y]==-1)
				x++;
			
		}
		else if(bang==2)
		{
			x--;
			y++;
			
			if(rands[x][y]==-1)
			{
				x++;
				y--;
			}
			
		}
		
		else if(bang==3)
		{
			y--;
			
			if(rands[x][y]==-1)
				y++;
			
		}
		
		else if(bang==4)
		{
			y++;
			
			if(rands[x][y]==-1)
				y--;
			
		}
		else if(bang==5)
		{
			x++;
			y++;
			
			if(rands[x][y]==-1)
			{
				x--;
				y--;
			}
			
		}
		
		else if(bang==6)
		{
			x++;
			y--;
			
			if(rands[x][y]==-1)
			{
				x--;
				y++;
			}
			
		}	
	
		else if(bang==7)
		{
			x++;		

			if(rands[x][y]==-1)
				x--;
			
		}

		rands[x][y]++;
		sum++;

		count=0;

		for(i=1;i<line+1;i++)
		{
			for(int j=1;j<line+1;j++)
			{
				if(rands[i][j]==0)
					count++;
			}
		}

		for(i=1;i<line+1;i++)
		{
			for(int j=1;j<line+1;j++)
				cout<<rands[i][j]<<"\t";
			cout<<endl;
		}

	}while(count!=0);
	cin.get();
	cout<<"\n\n총 움직인 횟수는 " << sum << " 번 입니다.\n";

	for(i=0;i<line+2;i++) 
		delete [] rands [i]; 

	delete [] rands;
	return 0;
}

/*
재동형의 말대로...
두 가지 고쳐보기...
1. 방화벽 없이 방향대로 움직이기 전에 그것이 칸 안인지 확인하고 점 찍기..
2. 방향을 x와 y의 배열로 나타내어 움직이기..
*/
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:52
Processing time 0.0103 sec