#include "all.h"


char Tableau[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
char Tour = 0;
float Ligne[5] = {0, 0, 0, 0, 0};



void init(void)
{
	glClearColor(0.0, 0.0, 0.5, 0.0);
	glEnable(GL_POINT_SMOOTH);
	glEnable(GL_LINE_SMOOTH);
}

void mouse(int button, int state, int x, int y)
{
	switch (button)
	{
	case GLUT_LEFT_BUTTON:
		if ((state == GLUT_DOWN) ^ CheckTTT())
		{
			char Coordx = 0, Coordy = 0;
			if (Verifie(x, y, Coordx, Coordy))
				if (Tableau[Coordx][Coordy] == 0)
				{
					if (Tour % 2 == 0)
						Tableau[Coordx][Coordy] = 'x';
					else
						Tableau[Coordx][Coordy] = 'o';
					Tour++;
					glutPostRedisplay();
				}
		if(CheckTTT())
			glutMouseFunc(NULL);
		}
		break;
	}
}



void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);


	Table(20);

	if (fabs(1.0 - Ligne[4]) < 0.01)
	{
		glLineWidth(5.0);
		glBegin(GL_LINES);
			glColor3f(1.0, 0.0, 0.0);
			glVertex2f(Ligne[0], Ligne[1]);
			glVertex2f(Ligne[2], Ligne[3]);
		glEnd();
	}

	
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			Draw(Tableau[i][j], (i-1)*24.0, (j-1)*-24.0);

	glutSwapBuffers();
}

void reshape(int w, int h)
{
	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-38.0, 38.0, -38.0, 38.0, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(250,250);
	glutInitWindowPosition(100,100);
	glutCreateWindow("MART'S TIC-TAC-TOE");
	init();
	glutDisplayFunc(display);
	glutIdleFunc(NULL);
	glutReshapeFunc(reshape);
	glutMouseFunc(mouse);
	glutMainLoop();
	return 0;
}
