U E D R , A S I H C RSS

Scheduled Walk/창섭&상규

프로그램 디자인

  • 여정(Journey)
    • 전체 여정이 있다.(JourneyArray, Length)
    • 다음에 가야 할 방향을 얻을 수 있다.(CurrentPosition, GetNextDirection)
    • 여정이 끝났는지 확인할 수 있다.(IsFinished)
  • 판(Board)
    • 크기가 정해져 있다.(BoardSize)
    • 자취를 남길 수 있다.(BoardArray, TraceCount, LeaveTrace)
    • 판의 모든 곳에 자취가 남았는지 확인할 수 있다.(IsAllCellsPassed)
  • 바퀴벌레(Roach)
    • 판 위에 올라갈 수 있다.(MyBoard, GoOnBoard) | 판(Board)
    • 여정이 끝나거나 판의 모든 곳에 자취가 남을때까지 여정에 따라 판 위를 움직일 수 있다.(CurrentLocation, Walk) | 여정(Journey), 판(Board)
  • 사용자(User) -> 프로그램 사용자와 대응되는 객체
    • 판의 크기를 말해줄 수 있다.(GetBoardSize)
    • 바퀴벌레의 시작위치를 말해줄 수 있다.(GetRoachStartLocation)
    • 원하는 여정을 만들어 줄 수 있다.(GetJourney) | 여정(Journey)
    • 판을 볼 수 있다.(PutBoard) | 판(Board)
  • 진행자(Executor)
    • 모든것을 진행한다.(Execute) | 사용자(User), 판(Board), 바퀴벌레(Roach), 여정(Journey)

진행자는 사용자에게 진행에 필요한 정보를 요청한다. 사용자는 판 크기, 바퀴벌레의 시작위치를 알려주고 여정을 만들어 준다. 진행자는 정보에 따라 판을 만들고, 바퀴벌레를 만든다. 그리고 나서 바퀴벌레에게 여정을 주며 판 위에 올라가서 판 위를 움직이도록 명령한다. 바퀴벌레는 여정을 참고하여 자취를 남기면서 판 위를 움직이고 여정이 끝나거나 판의 모든 곳에 자취가 남으면 움직이는것을 멈춘다. 바퀴벌레가 멈추면 진행자는 판을 사용자에게 보여준다.

프로그램 소스

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

struct Size
{
	int width;
	int height;
};

struct Location
{
	int x;
	int y;
};

class Journey
{
public:
	int *JourneyArray;
	int Length;
	int CurrentPosition;

	Journey(char *str)
	{
		Length=strlen(str);

		JourneyArray=new int[Length];
		for(int i=0;i<Length;i++)
			JourneyArray[i]=str[i]-'0';

		CurrentPosition=0;
	}
	~Journey()
	{
		delete[] JourneyArray;
	}

	int GetNextDirection()
	{
		if(IsFinished())
			return -1;

		return JourneyArray[CurrentPosition++];
	}
	bool IsFinished()
	{
		return (CurrentPosition==Length);
	}
};

class Board
{
public:
	int **BoardArray;
	Size BoardSize;
	int TraceCount;

	Board(Size size)
	{
		BoardSize = size;
		TraceCount = 0;
		
		BoardArray = new int* [size.height];
		for (int i = 0; i < size.height; i++)
		{
			BoardArray[i] = new int [size.width];
			for (int j = 0; j < size.width; j++)
				BoardArray[i][j] = 0;
		}
	}
	~Board()
	{
		for (int i = 0; i < BoardSize.height; i++)
			delete[] BoardArray[i];
		delete[] BoardArray;
	}

	void LeaveTrace(Location location)
	{
		BoardArray[location.y][location.x]++;
		TraceCount++;
	}
	bool IsAllCellsPassed()
	{
		for (int i = 0; i < BoardSize.height; i++)
		{
			for (int j = 0; j < BoardSize.width; j++)
			{
				if (BoardArray[i][j] == 0)
					return false;
			}
		}

		return true;
	}
};

class Roach
{
public:
	Board *MyBoard;
	Location CurrentLocation;

	void GoOnBoard(Board *board, Location startlocation)
	{
		MyBoard=board;
		CurrentLocation=startlocation;

		MyBoard->LeaveTrace(startlocation);
	}
	void Walk(Journey* journey)
	{
		int move[8][2]={ { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 }, { -1, -1 } };
		int direction;

		while(MyBoard->IsAllCellsPassed()==false || journey->IsFinished()==false)
		{
			direction=journey->GetNextDirection();
			CurrentLocation.x+=move[direction][0];
			CurrentLocation.y+=move[direction][1];

			if(CurrentLocation.x==-1) CurrentLocation.x=MyBoard->BoardSize.height-1;
			if(CurrentLocation.x==MyBoard->BoardSize.height) CurrentLocation.x=0;
			if(CurrentLocation.y==-1) CurrentLocation.y=MyBoard->BoardSize.width-1;
			if(CurrentLocation.y==MyBoard->BoardSize.width) CurrentLocation.y=0;

			MyBoard->LeaveTrace(CurrentLocation);
		}
	}
};

class User
{
public:
	Size GetBoardSize()
	{
		Size size;
		cin >> size.width >> size.height;
		return size;
	}
	Location GetRoachStartLocation()
	{
		Location location;
		cin >> location.x >> location.y;
		return location;
	}
	Journey * GetJourney()
	{
		char str[1024];
		cin >> str;
		return new Journey(str);
	}

	void PutBoard(Board *board)
	{
		cout << "움직인 횟수: " << board->TraceCount-1 <<endl;
		for (int h = 0; h < board->BoardSize.height; h++)
		{
			for (int w = 0; w < board->BoardSize.width; w++)
				cout << board->BoardArray[h][w] << " ";
			cout << endl;
		}
	}
};

class Executor
{
public:
	void Execute(User *user)
	{
		Size boardsize=user->GetBoardSize();
		Location startlocation=user->GetRoachStartLocation();
		Journey *journey=user->GetJourney();

		Board board(boardsize);
		Roach roach;

		roach.GoOnBoard(&board,startlocation);
		roach.Walk(journey);

		user->PutBoard(&board);

		delete journey;
	}
};

void main()
{
	Executor executor;
	User user;

	executor.Execute(&user);
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:59
Processing time 0.0194 sec