#include <iostream>

//Grid to ne display for the users to see
char   grid[8][8];
// Grid to store player ones values
bool  tGrid[8][8];

//lives of the second palyer
int lives = 3;
//number of player twos correct answers
int corretGuess = 0;

void setupGrid() {

	//set default values to 0
	unsigned int treasureLocations[6] = {0,0,0,0,0,0};

	//get player ones values for the treasure 3 times
	std::cout << "Player 1 place the treasure! " << std::endl;

	std::cout << "x: "; std::cin >> treasureLocations[0];
	std::cout << "y: "; std::cin >> treasureLocations[1];
	std::cout << "Treasure 1 placed" << std::endl;

	std::cout << "x: "; std::cin >> treasureLocations[2];
	std::cout << "y: "; std::cin >> treasureLocations[3];
	//check the user hasnt put the treasure in the same place
	if (treasureLocations[0] && treasureLocations[1] == treasureLocations[2] && treasureLocations[3]) {
		std::cout << "You can't enter the same place twice! try agian" << std::endl;
		setupGrid();
	} else {
		std::cout << "Treasure 2 placed" << std::endl;
	}


	std::cout << "x: "; std::cin >> treasureLocations[4];
	std::cout << "y: "; std::cin >> treasureLocations[5];
	//check the user hasnt put the treasure in the same place again
	if (treasureLocations[0] && treasureLocations[1] == treasureLocations[4] && treasureLocations[5]) {
		std::cout << "You can't enter the same place twice! try agian" << std::endl;
		setupGrid();
	}else if (treasureLocations[2] && treasureLocations[3] == treasureLocations[4] && treasureLocations[5]) {
		std::cout << "You can't enter the same place twice! try agian" << std::endl;
		setupGrid();
	} else {
		std::cout << "Treasure 3 placed" << std::endl;
	}

	//check that player one hasnt entered a number bigger then the size of the grid
	for (size_t i = 0; i < 6; i++) {
		if (treasureLocations[i] > 7) {
			std::cout << "You entered a number bigger then the size of the grid! try again.." << std::endl;
			setupGrid();
		}
	}

	//if everything is good store the values so the game can start
	tGrid[treasureLocations[0]][treasureLocations[1]] = true;
	tGrid[treasureLocations[2]][treasureLocations[3]] = true;
	tGrid[treasureLocations[4]][treasureLocations[5]] = true;

	//initialize display grid to the default char ' - '
	for (size_t x = 0; x < 8; x++) {
		for (size_t y = 0; y < 8; y++) {
			grid[x][y] = '-';
		}
	}

}

void displayGrid() {
	//push the first line over by one for formatting reasons 
	std::cout << "  ";

	//print out 0 to 7 for index row at the top
	for (size_t i = 0; i < 8; i++) {
		std::cout << i << " ";
		if (i >= 7) {
			std::cout << std::endl;
		}
	}

	// display col number + the grid values 
	for (size_t x = 0; x < 8; x++) {
		std::cout << x << " ";
		for (size_t y = 0; y < 8; y++) {
			std::cout << grid[x][y] << " ";
		}
		std::cout << std::endl;
	}
}

void checkForCorretGuesses() {

	// check if player 2 has guessed correctly 3 time if so they are the winner
	if (corretGuess == 3) {
		//print that player 2 has one and end the program
		std::cout << "Player 2 wins!" << std::endl;
		system("pause");
		exit(0);
	}

}

void step() {

	// if lives are grater then 0 player 2 can still try to win
	if (lives > 0) {
		//get palyer 2 input
		struct guessPos
		{
			int x;
		    int y;
		};
		guessPos player2Guess;
		std::cout << "Player 2 Guess!" << std::endl;
	    std::cout << "x: ";
	    std::cin >> player2Guess.x;
	    std::cout << "y: ";
	    std::cin >> player2Guess.y;
		//make sure player 2's input isn't bigger then the grid size
		if (player2Guess.x <= 7 && player2Guess.y <= 7) {
			//check if the player 2's guess is correct 
			if (tGrid[player2Guess.x][player2Guess.y] == true) {
				//if the guess is right, player should be informed 
				std::cout << "You found some treasure!" << std::endl;
				//grid is marked with an X to show that they found something
				grid[player2Guess.x][player2Guess.y] = 'X';
				//add 1 to correct answers
				corretGuess++;
			}
			else {
				//if they are wrong take a life and mark with a N
				lives--;
				grid[player2Guess.x][player2Guess.y] = 'N';
			}
		}
		else {
			//if they entered a number too high this will be printed and they lose a life
			std::cout << "You entered a number not on the grid! -1 life!" << std::endl;
			lives--;
		}

		//disaply grid
		displayGrid();
		//check to see if player 2 won or not
		checkForCorretGuesses();
		//get next guess;
	    step();
	}
	else {
		//if lives = 0, player 1 won!
		std::cout << "Player 1 wins!" << std::endl;
	}
}

int main() 
{
	//setup grid with player 1's input
	setupGrid();
	//display grid
	displayGrid();
	//let player 2 play
	step();

	//pause game so players get a chance to see who won!
	system("pause");
	return 0;
}