U E D R , A S I H C RSS

Random Walk/임민수

~cpp 
#include <iostream> 
using namespace std;
#include <ctime> 
int const arsize = 11; 
void main() 
{ 
	int num,x,y,cnt=0,total=0,end=1,temp;
	cout << "원하는 칸의 크기를 입력하세요 : ";
	cin >> num;
	int square[arsize][arsize] ={0,};
	cout << "바퀴벌레의 처음 좌표를 입력하세요 (x,y): ";
	cin >> x;
	cin >> y;
	square[x][y]=1;
	srand(0);
	while(end < num*num)
	{
			temp = rand()%8;
			switch (temp)
				{
				case 1 :
						if(x-1>=0 && y-1>=0)
						{
							y--;
							x--;
						}
						break;
				case 2 : 
						if(x-1>=0)
							x--;
						break;
				case 3 : 
						if(x-1>=0 && y+1<num)
						{
							x--;
							y++;
						}
						break;
				case 4 : if(y-1>=0)
							 y--;
						break;
				case 5 : if(y+1<num)
							 y++;
						 break;
				case 6 : if(x+1<num && y-1>=0)
						 {
							x++;
							y--;
						 }
						 break;
				case 7 : if(x+1<num)
							 x++;
						 break;
				case 0 : if(x+1<num && y+1<num)
						 {
							 x++;
							 y++;
						 }
						 break;
				}
		if (square[x][y]==0)
			end++;
		square[x][y]++;
		total++;

	}

	for (int i = 0 ; i<num ; i++)
		{
			for (int j=0 ; j<num ; j++)
				cout << square[i][j] << "\t";
			cout << endl;
		}
	
	cout << endl << "총 이동횟수는 " <<total<<endl;

}

최근에 C++ 로 짠것..

Board.cpp

~cpp 
#include <iostream>
using namespace std;
#include"Board.h"
#include"Bug.h"
Board::Board()
{
	for (int i = 0; i < MAX_BOARD ; i++)
	{
		for (int j = 0; j < MAX_BOARD ; j++)
			board[i][j]=0;
	}
	EndCount=0;
}

void Board::Show_Board()
{
	for (int i = 0; i < MAX_BOARD ; i++)
	{
		for (int j = 0; j < MAX_BOARD ; j++)
			cout << board[i][j] << "\t" ;
		cout << endl;
	} 
	cout << endl;
}

void Board::Make_Footprint(int a_x, int a_y)
{
	board[a_x][a_y]++;
	if(board[a_x][a_y]==1)
		EndCount++;
}

bool Board::IsNotEnd()
{
	if (EndCount < 25)
		return true;
	else 
		return false;
}

Bug.cpp

~cpp 

#include <iostream>
using namespace std;
#include"Bug.h"
#include <ctime>

Bug::Bug()
{
	x=0; y=0;
}

void Bug::move()
{
	int way;
	way = rand()%8;
	if (x+MOVE_X[way]> -1 && x+MOVE_X[way]<5 && y+MOVE_Y[way] > -1 && y+MOVE_Y[way] < 5)
	{
		x+=MOVE_X[way];
		y+=MOVE_Y[way];
	}
}


RandomWalk.cpp

~cpp 

#include <iostream>
using namespace std;
#include "Board.h"
#include "Bug.h"
#include <ctime>


void main()
{
	srand((unsigned)time(NULL));
	Bug bug1;
	Board board;
	board.Make_Footprint(bug1.return_x(), bug1.return_y());
	board.Show_Board();
	do{
		bug1.move();
		board.Make_Footprint(bug1.return_x(), bug1.return_y());
		board.Show_Board();
	}while(board.IsNotEnd());
	
}

Board.h


~cpp 

#include <iostream>
using namespace std;
#include "Board.h"
#include "Bug.h"
#include <ctime>


void main()
{
	srand((unsigned)time(NULL));
	Bug bug1;
	Board board;
	board.Make_Footprint(bug1.return_x(), bug1.return_y());
	board.Show_Board();
	do{
		bug1.move();
		board.Make_Footprint(bug1.return_x(), bug1.return_y());
		board.Show_Board();
	}while(board.IsNotEnd());
	
}


Bug.h

~cpp 
#include <iostream>
using namespace std;
#include "Board.h"
#include "Bug.h"
#include <ctime>


void main()
{
	srand((unsigned)time(NULL));
	Bug bug1;
	Board board;
	board.Make_Footprint(bug1.return_x(), bug1.return_y());
	board.Show_Board();
	do{
		bug1.move();
		board.Make_Footprint(bug1.return_x(), bug1.return_y());
		board.Show_Board();
	}while(board.IsNotEnd());
	
}

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