#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <sstream>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

enum Direction { None, Left, Right, Up, Down };
enum Controller_t {PC , User};

//Global Variables:
char*board;
int Height, Width;

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Classes - Prototypes
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class board_t;
class peg_t;

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Classes Decleratios
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class peg_t
{
private:
	bool Moving;
	Controller_t controller;
public:
	char Peg;
	peg_t (char,bool,Controller_t);
	Direction Move;
};

class board_t
{
private:
	void SetBoard(int,int);
public:
	void SetValues(int, int);
	void PrintBoard();
	friend class peg_t;
}Board;


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Class Functions:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//PEG_T_FUNCTIONS:

peg_t::peg_t(const char a, bool b, Controller_t c)
{
	Peg = a;
	Moving = b;
	controller = c;
}

//BOARD_T_FUNCTIONS:

void board_t::SetValues(int a, int b)
{
	SetBoard(a,b);
}

void board_t::SetBoard(int a, int b)
{
	board = new char [a*b];

	for (int i = 0; i < a; i++)
	{
		for (int j = 0;j<b;j++)
		{
			board[i*b + j] = '.';
		}
	}
}

void board_t::PrintBoard()
{
	for (int i = 0; i < Height; i++)
	{
		cout << "\t";
		for (int j = 0;j < Width ;j++)
		{
			cout << board[i*Width + j];
		}
		cout << endl;
	}
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Function:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void DrawLine (char x)
{
	cout << endl;
	for (int i = 0; i < 80;i++)
	{
		cout << x;
	}
	cout << endl;
}


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Main Function:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

int main ()
{	
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//Variables:
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	peg_t Hero ('H',true, User);
	peg_t Enemy ('X', true, PC);
	peg_t Trap ('O', false, PC);
	peg_t Treasure ('T', false, PC);
	char Move;


	cout << "Please enter the Dimensions of the Board you want to play on[Min - 5*5 | Max - 8*8]: " << endl;
	
	cout << "Height:  ";
	cin >> Height;
		if (Height > 8)
			Height = 8;
		if (Height < 5)
			Height = 5;
			
	cout << "Width: ";
	cin >> Width;
		if (Width > 8)
			Width = 8;
		if (Width < 5)
			Width = 5;

	Board.SetValues(Height,Width);
	
	int playh = 1, playw = 1;

	board [ (Height*Width) - 1 ] = Treasure.Peg;
	
	DrawLine('*');
	cout << "The target of the game is to reach the Treasure [T] without coming in contact with a Trap [O] or an Enemy[X].";
	cout << "The character is controlled by W,S,A & D, that alllow the character to move Up, Down, Left and Right respectively\n";
	DrawLine ('*');

	cout << endl;
	while (true)
	{	
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//board[ (Row-1)   *   Width +   (Column-1)  ] = CHARACTER
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		board[ (playh-1)  * (Width) + (playw-1)] = Hero.Peg;
		Board.PrintBoard();
		
		if (board [(Height*Width)-1] != Treasure.Peg)
		{
			cout << "Congratulations. You have won the Game!!!";
			break;
		}

		cout << "In What direction do you want to move:  ";
		cin >> Move;
		cin.sync();
		switch (Move)
		{
		case 'W':
		case 'w':
			Hero.Move = Up;
			break;
		case 'S':
		case 's':
			Hero.Move = Down;
			break;
		case 'A':
		case 'a':
			Hero.Move = Left;
			break;
		case 'D':
		case 'd':
			Hero.Move = Right;
			break;
		}

		switch (Hero.Move)
		{
		case Up:
			if (playh != 1)
			{
				board[ (playh-1) * Width + (playw-1)] = '.';
				playh -= 1;
			}
			break;

		case Down:
			if (playh != Height)
			{
				board[ (playh-1) * Width + (playw-1)] = '.';
				playh += 1;
			}
			break;

		case Left:
			if (playw != 1)
			{
				board[ (playh-1) * Width + (playw-1)] = '.';
				playw -= 1;
			}
			break;

		case Right:
			if (playw != Width)
			{
				board[ (playh-1) * Width + (playw-1)] = '.';
				playw += 1;
			}
			break;
		}

		DrawLine('-');
	}
	cout << endl << endl;
	DrawLine ('=');
	cout << endl;
	cout << "Press ENTER/RETURN to Exit . . .";
	cin.sync();
	cin.get();
	return 0;
}