I am working on the AI for this game, I just have to get the computer O's to not draw over the themselves. here is my code so far.
#include <freeglut.h>
#include <iostream>
using namespace std;
char board[9];
int index;
void drawBoard()
{
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
glVertex3f(-18.75f, 6.25f, 0.0f);
glVertex3f(18.75f, 6.25f, 0.0f);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(-18.75f, -6.25f, 0.0f);
glVertex3f(18.75f, -6.25f, 0.0f);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(-6.25f, 18.75f, 0.0f);
glVertex3f(-6.25f, -18.75f, 0.0f);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(6.25f, 18.75f, 0.0f);
glVertex3f(6.25f, -18.75f, 0.0f);
glEnd();
}
void drawText()
{
glColor3f(0.0f, 1.0f, 1.0f);
for (int i = 0; i < 9; i++)
{
float x = -15 + (i % 3) * 13;
float y = -15 + (i / 3) * 13;
glRasterPos2f(x, y);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, board[i]);
}
}
void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT);
drawBoard();
drawText();
glutSwapBuffers();
}
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat aspectRatio;
if (h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
else
glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouseClicks(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if (x >= 340 && x <= 380 && y >= 240 && y <= 280)
{
index = 6;
}
if (x >= 380 && x <= 420 && y >= 240 && y <= 280)
{
index = 7;
}
if (x >= 420 && x <= 460 && y >= 240 && y <= 280)
{
index = 8;
}
if (x >= 340 && x <= 380 && y >= 280 && y <= 320)
{
index = 3;
}
if (x >= 380 && x <= 420 && y >= 280 && y <= 320)
{
index = 4;
}
if (x >= 420 && x <= 460 && y >= 280 && y <= 320)
{
index = 5;
}
if (x >= 340 && x <= 380 && y >= 320 && y <= 360)
{
index = 0;
}
if (x >= 380 && x <= 420 && y >= 320 && y <= 360)
{
index = 1;
}
if (x >= 420 && x <= 460 && y >= 320 && y <= 360)
{
index = 2;
}
board[index] = 'X';
board[rand() % 9] = 'O';
glutPostRedisplay();
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GL_DEPTH);
glutInitWindowPosition(600, 400);
glutInitWindowSize(800, 600);
glutCreateWindow("Tic Tac Toe");
glutDisplayFunc(renderScene);
glutReshapeFunc(ChangeSize);
glutMouseFunc(mouseClicks);
glutMainLoop();
}