/***********************************************
	Wanderer.h - A man who wanders in a maze
************************************************/

class WANDERER {
private:
	//メンバー変数
	int m_x;	//x position in a maze
	int m_y;	//y position in a maze
	int m_dir;	//MAZEクラスでUp - 0, Right - 1, Down - 2, Left - 3が定義済の前提
	int m_gx;	//Goal's x position in a maze
	int m_gy;	//Goal's y position in a maze
	//メンバー関数
public:
	WANDERER() {m_x = m_y = m_dir = m_gx = m_gy = 0;}
	WANDERER(int X, int Y) {m_x = X; m_y = Y; m_dir = m_gx = m_gy = 0;}
	int GetX() {return m_x;}
	int GetY() {return m_y;}
	int GetDir() {return m_dir;}
	int GetGoalX() {return m_gx;}
	int GetGoalY() {return m_gy;}
	void SetPos(int x, int y) {m_x = x; m_y = y;}
	void SetGoal(int x, int y) {m_gx = x; m_gy = y;}
	void GoOn(int, int, int*);
	bool IsGoal() {return (m_x == m_gx && m_y == m_gy);}
};

void WANDERER::GoOn(int w, int h, int* data) {
	//絶対座標（外周は壁なので左上=(1, 1)、右下=(w - 2, h - 2)）ベース
	int dirs[4] = {0};
	if(!data[m_x + h * (m_y - 1)]) {
		dirs[0] = 1;
	}
	if(!data[m_x + 1 + h * m_y]) {
		dirs[1] = 1;
	}
	if(!data[m_x + h * (m_y + 1)]) {
		dirs[2] = 1;
	}
	if(!data[m_x - 1 + h * m_y]) {
		dirs[3] = 1;
	}
	//左手法
	switch(m_dir) {
		case 0:	//Up to Left
				if(dirs[3]) m_dir = 3;
				else if (dirs[0]) m_dir = 0;
				else if (dirs[1]) m_dir = 1;
				else if (dirs[2]) m_dir = 2;
				break;
		case 1:	//Right to Up
				if(dirs[0]) m_dir = 0;
				else if (dirs[1]) m_dir = 1;
				else if (dirs[2]) m_dir = 2;
				else if (dirs[3]) m_dir = 3;
				break;
		case 2:	//Down to Right
				if(dirs[1]) m_dir = 1;
				else if (dirs[2]) m_dir = 2;
				else if (dirs[3]) m_dir = 3;
				else if (dirs[0]) m_dir = 0;
				break;
		case 3:	//Left to Down
				if(dirs[2]) m_dir = 2;
				else if (dirs[3]) m_dir = 3;
				else if (dirs[0]) m_dir = 0;
				else if (dirs[1]) m_dir = 1;
				break;
	}
	//決定方向へ一コマ進む
	switch(m_dir) {
		case 0:	//Up
				m_y--;
				break;
		case 1:	//Right
				m_x++;
				break;
		case 2:	//Down
				m_y++;
				break;
		case 3:	//Left
				m_x--;
				break;
	}
}
