tic tac toe

Started by
55 comments, last by 8Observer8 4 years, 5 months ago

Phil, please don't push for more until you've worked through the help you've gotten so far.

-- Tom Sloper -- sloperama.com

Advertisement

ok tom I am working on the problem. when I get it worked out can I post more?

2 hours ago, phil67rpg said:

I am working on this problem do you have any more advice for me?

I can't make it any easier for you than spelling it out, Phil. It's only a couple of lines. 
 


#include <freeglut.h>
#include <iostream>

using namespace std;

char board[9];

void drawBoard()
{
	// don't need push/popmatrix
	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)
	{
		board[rand()%9] = 'A'+rand()%26;
		glutPostRedisplay();
	}
}

int main(int argc, char**argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE);
	glutInitWindowPosition(600, 400);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Tic Tac Toe");
	glutDisplayFunc(renderScene);
	glutReshapeFunc(ChangeSize);
	glutMouseFunc(mouseClicks);
	glutMainLoop();
}


 

@phil67rpg do you know how to work with textures in OpenGL? You can draw images in Paint. I use GIMP.

I created this texture atlas in GIMP:

GameField.png.2c3f4e35970854150e8e1bc28783f9ed.png

I use texture coordinated to draw selected item:


            GL.Begin(PrimitiveType.Triangles);
            {
                // Left top triangle
                GL.TexCoord2(0f, 0f); GL.Vertex2(0f, 0f);
                GL.TexCoord2(0f, 0.33f); GL.Vertex2(0f, 64f);
                GL.TexCoord2(0.33f, 0f); GL.Vertex2(64f, 0f);
                // Right bottom triangle
                GL.TexCoord2(0f, 0.33f); GL.Vertex2(0f, 64f);
                GL.TexCoord2(0.33f, 0.33f); GL.Vertex2(64f, 64f);
                GL.TexCoord2(0.33f, 0f); GL.Vertex2(64f, 0f);
            }
            GL.End();

Source code: https://github.com/8Observer8/TicTacToe_OpenTkOpenGL11CSharp

OneItem.png.f778fbbd3f25e2ab8b380135b3c95e27.png

TicTacToe_OpenTkOpenGL11CSharp_Trello.png.9d0a18db451cec1a7901cf41581a395b.png

14 hours ago, Prototype said:

I can't make it any easier for you than spelling it out, Phil. It's only a couple of lines. 

well I am working on it sorry I am kind of slow. well I got the x's to be drawn now I have to get the computer to draw the o's thanks for all the help when I get this done I will repost my code.

 

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();
}

 

16 hours ago, phil67rpg said:

I am working on the AI for this game

I have not made the AI for the game but I will try. At first I want to make a network version for two players using TCP sockets.

Source code: https://github.com/8Observer8/TicTacToe_OpenTkOpenGL11CSharp

I draw a game field from 2D character array:


        private char[,] _gameField = new char[,]
        {
            { ' ', 'x', ' ' },
            { ' ', 'x', ' ' },
            { '0', ' ', ' ' }
        };

TicTacToe_OpenTkOpenGL11CSharp.png.e76726dc37e49b3000dbd981e76784a5.png

 well I have finished reading the opengl superbible 

46 minutes ago, phil67rpg said:

well I have finished reading the opengl superbible

What version of the book do you have?

This topic is closed to new replies.

Advertisement