#include "all.h"

void Carre(float x, float y, float width)
{
	glBegin(GL_QUADS);
	glColor3f(1.0, 1.0, 1.0);
	glVertex2f(x - width/2, y + width/2);
	glVertex2f(x + width/2, y + width/2);
	glVertex2f(x + width/2, y - width/2);
	glVertex2f(x - width/2, y - width/2);
	glEnd();
}

void Table(float width)
{
	Carre(-24.0, 24.0, width);
	Carre(0.0, 24.0, width);
	Carre(24.0, 24.0, width);
	
	Carre(-24.0, 0.0, width);
	Carre(0.0, 0.0, width);
	Carre(24.0, 0.0, width);

	Carre(-24.0, -24.0, width);
	Carre(0.0, -24.0, width);
	Carre(24.0, -24.0, width);
}

void Draw(char Lettre, float x, float y)
{
	switch (Lettre)
	{
	case ('x') : 
		{
			glLineWidth(3.0);
			glBegin(GL_LINES);
			glColor3f(0.0, 0.0, 0.0);
			glVertex2f(x - LARGEUR, y + LARGEUR);
			glVertex2f(x + LARGEUR, y - LARGEUR);
			glVertex2f(x + LARGEUR, y + LARGEUR);
			glVertex2f(x - LARGEUR, y - LARGEUR);
			glEnd();
			break;

		}
	case ('o') :
		{
			glLineWidth(3.0);
			glBegin(GL_LINE_STRIP);
			int NbPoint = 1000;
			glColor3f(0.0, 0.0, 0.0);
			for (int i = 0; i < NbPoint; i++)
				glVertex2f(x + LARGEUR*cos(360.0*i/NbPoint), y + LARGEUR*sin(360.0*i/NbPoint));
			glEnd();
			break;
		}
	}
}

bool Verifie(int x, int y, char& Coordx, char& Coordy)
{
	return( (CheckIndice(x, Coordx)) && (CheckIndice(y, Coordy)) );
}

bool CheckIndice(int Coord,char& Indice)
{
	Coord = (int) ((Coord - 125)*38/125.0);
	
	if ( (Coord <= -14) && (Coord >= -34) )
		Indice = 0;
	else
		if ( (Coord <= 10) && (Coord >= -10) )
			Indice = 1;
		else
			if ( (Coord <= 34) && (Coord >= 14) )
				Indice = 2;
			else
				return(false);
	return(true);
}

bool CheckTTT(void)
{
	int i;
	for (i = 0; i < 3; i++)
		if ( 
			( (Tableau[i][0] == Tableau[i][1]) && (Tableau[i][1] == Tableau[i][2]) ) 
			&& (Tableau[i][0] != 0)    )
		{
			Ligne[0] = (i-1)*24.0;
			Ligne[1] = 29.0;
			Ligne[2] = (i-1)*24.0;
			Ligne[3] = -29.0;
			Ligne[4] = 1.0;
			return(true);
		}

	for (i = 0; i < 3; i++)
		if ( 
			( (Tableau[0][i] == Tableau[1][i]) && (Tableau[1][i] == Tableau[2][i]) ) 
			&& (Tableau[0][i] != 0)    )
		{
			Ligne[0] = -29.0;
			Ligne[1] = (1-i)*24.0;
			Ligne[2] = 29.0;
			Ligne[3] = (1-i)*24.0;
			Ligne[4] = 1.0;
			return(true);
		}
	
	if ( 
		( (Tableau[0][0] == Tableau[1][1]) && (Tableau[1][1] == Tableau[2][2]) ) 
		&& (Tableau[0][0] != 0)    )
	{
		Ligne[0] = -29.0;
		Ligne[1] = 29.0;
		Ligne[2] = 29.0;
		Ligne[3] = -29.0;
		Ligne[4] = 1.0;
		return(true);
	}

	if ( 
		( (Tableau[0][2] == Tableau[1][1]) && (Tableau[1][1] == Tableau[2][0]) ) 
		&& (Tableau[0][2] != 0)    )
	{
		Ligne[0] = 29.0;
		Ligne[1] = 29.0;
		Ligne[2] = -29.0;
		Ligne[3] = -29.0;
		Ligne[4] = 1.0;
		return(true);
	}

	return(false);
}
