#include <iostream>
#include <conio.h>
using namespace std;

char ntoxy(int coord)
{
	if (coord == 1)
		return 'X';
	else if (coord == 2)
		return 'O';
	else
		return ' ';
}

int game(void)
{
	int round = 9; // Number of possible turns
	int player = 2; // Starting playing will be X
	int pos[3][3] = { {0,0,0}, {0,0,0}, {0,0,0} }; // The matrix array for the board positions

	while (round > 0)
	{
		switch (player)
		{
			case 1: player = 2;
			break;
			case 2: player = 1;
			break;
		}

		char key = ' ';
		int selected = 0;
		
		while (selected != 1)//make sure the user has selected a VALID case
		{
			key = getch();
			
			if(key == 'q') //to quit
				return 0;
			// User select his position
			if (key == '1')	if (pos[0][0] == 0){pos[0][0] = player;selected = 1;}
			if (key == '2')	if (pos[1][0] == 0){pos[1][0] = player;selected = 1;}
			if (key == '3')	if (pos[2][0] == 0){pos[2][0] = player;selected = 1;}
			if (key == '4')	if (pos[0][1] == 0){pos[0][1] = player;selected = 1;}
			if (key == '5')	if (pos[1][1] == 0){pos[1][1] = player;selected = 1;}
			if (key == '6')	if (pos[2][1] == 0){pos[2][1] = player;selected = 1;}
			if (key == '7')	if (pos[0][2] == 0){pos[0][2] = player;selected = 1;}
			if (key == '8')	if (pos[1][2] == 0){pos[1][2] = player;selected = 1;}
			if (key == '9')	if (pos[2][2] == 0){pos[2][2] = player;selected = 1;}
		}
		system("cls");
		// The board is redrawn
		cout << ntoxy(pos[0][2]) << "|" << ntoxy(pos[1][2]) << "|" << ntoxy(pos[2][2]) << endl;
		cout << "-----" << endl;
		cout << ntoxy(pos[0][1]) << "|" << ntoxy(pos[1][1]) << "|" << ntoxy(pos[2][1]) << endl;
		cout << "-----" << endl;
		cout << ntoxy(pos[0][0]) << "|" << ntoxy(pos[1][0]) << "|" << ntoxy(pos[2][0]) << endl;

		// Check for a victory
		if (pos[0][2] == pos[1][2]) if (pos[1][2] == pos[2][2])if(pos[0][2] != 0)// 3-
		{
			cout << "VICTORY BY " << ntoxy(pos[0][2]) << endl;
			return 1;
		}
		if (pos[0][1] == pos[1][1]) if (pos[1][1] == pos[2][1])if(pos[0][1] != 0)// 2-
		{
			cout << "VICTORY BY " << ntoxy(pos[0][1]) << endl;
			return 1;
		}
		if (pos[0][0] == pos[1][0]) if (pos[1][0] == pos[2][0])if(pos[0][0] != 0)// 1-
		{
			cout << "VICTORY BY " << ntoxy(pos[0][0]) << endl;
			return 1;
		}
		if (pos[0][2] == pos[1][1]) if (pos[1][1] == pos[2][0])if(pos[0][2] != 0)/* \ */
		{
			cout << "VICTORY BY " << ntoxy(pos[0][2]) << endl;
			return 1;
		}
		if (pos[0][0] == pos[1][1]) if (pos[1][1] == pos[2][2])if(pos[0][0] != 0)// /
		{
			cout << "VICTORY BY " << ntoxy(pos[0][0]) << endl;
			return 1;
		}
		if (pos[0][0] == pos[0][1]) if (pos[0][1] == pos[0][2])if(pos[0][0] != 0)// 1|
		{
			cout << "VICTORY BY " << ntoxy(pos[0][0]) << endl;
			return 1;
		}
		if (pos[1][0] == pos[1][1]) if (pos[1][1] == pos[1][2])if(pos[1][0] != 0)// 2|
		{
			cout << "VICTORY BY " << ntoxy(pos[1][0]) << endl;
			return 1;
		}
		if (pos[2][0] == pos[2][1]) if (pos[2][1] == pos[2][2])if(pos[2][0] != 0)// 3|
		{
			cout << "VICTORY BY " << ntoxy(pos[2][0]) << endl;
			return 1;
		}
		round--;
	}
	return 1;
}

int main(void)
{
	int run = 1;
	while(run == 1)
	{
		if (game() == 0)
			return 0;
		cout << "PLAY AGAIN? Y or N" << endl;

		char choice = getch();
		while (choice != 'y')
		{
			if (choice == 'n')
			return 0;
			choice = getch();
		}
		cout << "Start!" << endl; //Message telling to start next game
	}
	return 0;
}
