Computer Graphics Report

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

SIMULATION OF TIC-TAC-TOE

Chapter 1

INTRODUCTION

1.1 Computer Graphics

• Graphics provides one of the most natural means of communicating with a computer, since our

highly developed 2D Or 3D pattern-recognition abilities allow us to perceive and process pictorial

data rapidly.

• Computers have become a powerful medium for the rapid and economical production of

pictures.

• There is virtually no area in which graphical displays cannot be used to some advantage.

• Graphics provide a so natural means of communicating with the computer that they have

become widespread.

• Interactive graphics is the most important means of producing pictures since the invention of

photography and television .

• We can make pictures of not only the real world objects but also of abstract objects such as

mathematical surfaces on 4D and of data that have no inherent geometry.

DEPT OF CSE ,REC HULKOTI 1


SIMULATION OF TIC-TAC-TOE

1.2 OpenGL Technology

OpenGL is the premier environment for developing portable, interactive 2D and 3D

graphics applications. Since its introduction in 1992, OpenGL has become the industry's most

widely used and supported 2D and 3D graphics application programming interface (API),

bringing thousands of applications to a wide variety of computer platforms.

OpenGL fosters innovation and speeds application development by incorporating a

broad set of rendering, texture mapping, special effects, and other powerful visualization

functions. Developers can leverage the power of OpenGL across all popular desktop and

workstation platforms, ensuring wide application deployment. OpenGL Available Everywhere:

Supported on all UNIX® workstations, and shipped standard with every Windows 95/98/2000/NT

and MacOS PC, no other graphics API operates on a wider range of hardware platforms and

software environments.

OpenGL runs on every major operating system including Mac OS, OS/2, UNIX,

Windows 95/98, Windows 2000, Windows NT, Linux, OPENStep, and BeOS; it also works with

every major windowing system, including Win32, MacOS, Presentation Manager, and X-Window

System. OpenGL is callable from Ada, C, C++, Fortran, Python, Perl and Java and offers complete

independence from network protocols and topologies.

DEPT OF CSE ,REC HULKOTI 2


SIMULATION OF TIC-TAC-TOE

Chapter 2
SOFTWARE AND HARDWARE REQUIREMENTS
2.1 Hardware Requirements
The standard output device is assumed to be a Color Monitor. It is quite essential for
any graphics package to have this, as provision of color options to the user is a must. The mouse,
the main input device, has to be functional i.e. used to give input in the game. A keyboard is used
for controlling and inputting data in the form of characters, numbers i.e. to change the user views
. Apart from these hardware requirements there should be sufficient hard disk space and primary
memory available for proper working of the package to execute the program. Pentium III or higher
processor, 16MB or more RAM. A functional display card.

Minimum Requirements expected are cursor movement, creating objects like lines,
squares, rectangles, polygons, etc. Transformations on objects/selected area should be possible.
Filling of area with the specified color should be possible.

2.2 Software Requirements


The editor has been implemented on the OpenGL platform and mainly requires an
appropriate version of eclipse compiler to be installed and functional in ubuntu. Though it is
implemented in OpenGL, it is very much performed and independent with the restriction, that there
is support for the execution of C and C++ files. Text Modes is recommended.

Developed Platform

Windows 10

Language Used In Coding

C-language

Tool Used in Coding

Developer C++

DEPT OF CSE ,REC HULKOTI 3


SIMULATION OF TIC-TAC-TOE

Chapter 3

DESIGN OF PROPOSED SYSTEM

PROPOSED SYSTEM :

To achieve three dimensional effects, OpenGL software is proposed . It is software which

provides a graphical interface. It is a interface between application program and graphics

hardware. the advantages are:

1. OpenGL is designed as a streamlined.

2. It is a hardware independent interface i.e. it can be implemented on many different hardware

platforms.

3. With OpenGL, we can draw a small set of geometric primitives such as points, lines and

polygons etc.

4. Its provides double buffering which is vital in providing transformations.

5. It is event driven software.

6. It provides call back function.

SYSTEM DESIGN :

Existing System Existing system for a graphics is the TC++ . This system will support
only the 2D graphics. 2D graphics package being designed should be easy to use and understand.
It should provide various option such as free hand drawing , line drawing , polygon drawing , filled
polygons, flood fill, translation , rotation , scaling , clipping etc. Even though these properties were
supported, it was difficult to render 2D graphics cannot be . Very difficult to get a 3Dimensional
object. Even the effects like lighting , shading cannot be provided. So we go for Eclipse software.

DEPT OF CSE ,REC HULKOTI 4


SIMULATION OF TIC-TAC-TOE

Chapter 4

IMPLEMENTATION

4.1 FUNCTIONS USED

Void glColor3f(float red, float green, float blue);

This function is used to mention the color in which the pixel should appear. The number

3 specifies the number of arguments that the function would take. ‘f ’ gives the type that is float.

The arguments are in the order RGB(Red, Green, Blue). The color of the pixel can be specified

as the combination of these 3 primary colors.

Void glClearColor(int red, int green, int blue, int alpha);

This function is used to clear the color of the screen. The 4 values that are passed as

arguments for this function are (RED, GREEN, BLUE, ALPHA) where the red green and blue

components are taken to set the background color and alpha is a value that specifies depth of the

window. It is used for 3D images.

Void glutKeyboardFunc();

void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y));

where func is the new keyboard callback function. glutKeyboardFunc sets the keyboard callback

for the current window. When a user types into the window, each key press generating an ASCII

character will generate a keyboard callback. The key callback parameter is the generated ASCII

character. The x and y callback parameters indicate the mouse location in window relative

coordinates when the key was pressed. When a new window is created, no keyboard

callbacinitially registered, and ASCII key strokes in the window are ignored. Passing NULL to

glutKeyboardFunc disables the generation of keyboard callbacks.

DEPT OF CSE ,REC HULKOTI 5


SIMULATION OF TIC-TAC-TOE

Void glFlush();

Different GL implementations buffer commands in several different locations, including


network buffers and the graphics accelerator itself. glFlush empties all of these buffers, causing all
issued commands to be executed as quickly as they are accepted by the actual rendering engine.
Though this execution may not be completed in any particular time period, it does complete in
finite time.

Void glMatrixMode(GLenum mode);

where mode Specifies which matrix stack is the target for subsequent matrix operations.
Three values are accepted: GL_MODELVIEW, GL_PROJECTION, and GL_TEXTURE. The
initial value is GL_MODELVIEW.

glMatrixMode sets the current matrix mode. mode can assume one of three values:

GL_MODELVIEW Applies subsequent matrix operations to the modelview matrix stack.


GL_PROJECTION Applies subsequent matrix operations to the projection matrix stack.

void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)

where x, y Specify the lower left corner of the viewport rectangle, in pixels. The initial value is
(0, 0).

width, height Specify the width and height of the viewport. When a GL context is first attached to
a surface (e.g. window), width and height are set to the dimensions of that surface. glViewport
specifies the affine transformation of x and y from normalized device coordinates to window
coordinates. Let (xnd, ynd) be normalized device coordinates. Then the window coordinates (xw,
yw) are computed as follows:

xw = ( xnd + 1 ) width/2 + x

yw = ( ynd + 1 ) height/2 + y

Viewport width and height are silently clamped to a range that depends on the implementation. To
query this range, call glGetInteger with argument GL_MAX_VIEWPORT_DIMS.

DEPT OF CSE ,REC HULKOTI 6


SIMULATION OF TIC-TAC-TOE

void glutInit(int *argcp, char **argv);

glutInit will initialize the GLUT library and negotiate a session with the window system.
During this process, glutInit may cause the termination of the GLUT program with an error
message to the user if GLUT cannot be properly initialized. Examples of this situation include the
failure to connect to the window system, the lack of window system support for OpenGL, and
invalid command line options.glutInit also processes command line options, but the specific
options parse are window system dependent.

void glutReshapeFunc(void (*func)(int width, int height));

glutReshapeFunc sets the reshape callback for the current window. The reshape callback
is triggered when a window is reshaped. A reshape callback is also triggered immediately before
a window's first display callback after a window is created or whenever an overlay for the window
is established. The width andheight parameters of the callback specify the new window size in
pixels. Before the callback, the current window is set to the window that has been reshaped.

If a reshape callback is not registered for a window or NULL is passed to glutReshapeFunc (to
deregister a previously registered callback), the default reshape callback is used. This default
callback will simply void

glutMainLoop(void);

glutMainLoop enters the GLUT event processing loop. This routine should be called at
most once in a GLUT program. Once called, this routine will never

glutPostRedisplay(): glutPostRedisplay, glutPostWindowRedisplay — marks the current or


specified window as needing to be redisplayed.

DEPT OF CSE ,REC HULKOTI 7


SIMULATION OF TIC-TAC-TOE

4.2 ALGORITHM
// Tic Tac Toe or X's and 0's.

// Keyboard input

// 'v' = view player/perspective

// 'l' = lighting on/of

#include <GL/glut.h> // glut (gl utility toolkit) basic windows functions, keyboard, mouse.

#include <stdio.h> // standard (I/O library)

#include <stdlib.h> // standard library (set of standard C functions

#include <math.h> // Math library (Higher math functions )

#include<string.h>

// lighting

GLfloat LightAmbient[]= {0.0f, 0.0f, 1.0f };

GLfloat LightDiffuse[]= { 0.0f, 0.0f, 0.0f, 1.0f };

GLfloat LightPosition[]= { 0.0f, 0.0f, 0.0f, 1.0f};

GLfloat mat_specular[] = { 0.0, 0.0, 0.0, 1.0};

int abc=0;

// mouse variables: Win = windows size, mouse = mouse position

int mouse_x, mouse_y, Win_x, Win_y, object_select;

// state variables for player/Perspective view, lighting on/off

static int view_state = 0, light_state = 0;

// Use to spin X's and O's

int spin, spinboxes;

// Win = 1 player wins, -1 computer wins, 2 tie.

// player or computer; 1 = X, -1 = O

// start_game indicates that game is in play.

int player, computer, win, start_game;

DEPT OF CSE ,REC HULKOTI 8


SIMULATION OF TIC-TAC-TOE

// alingment of boxes in which one can win

// We have 8 posiblities, 3 accross, 3 down and 2 diagnally

//

// 0 | 1 | 2

// 3 | 4 | 5

// 6 | 7 | 8

// logic for computer's turn

int computer_move()

if ( blocking_win() == 1) return( 1 );

if ( check_corner() == 1) return( 1 );

if ( check_row() == 1) return( 1);

return( 0 );

// This creates the spinning of the cube.

static void TimeEvent(int te)

spin++; // increase cube rotation by 1

if (spin > 360) spin = 180; // if over 360 degress, start back at zero.

glutPostRedisplay(); // Update screen with new rotation data

glutTimerFunc( 8, TimeEvent, 1); // Reset our timmer.

// Create a new quadric

Cylinder = gluNewQuadric();

gluQuadricDrawStyle( Cylinder, GLU_FILL );

gluQuadricNormals( Cylinder, GLU_SMOOTH );

DEPT OF CSE ,REC HULKOTI 9


SIMULATION OF TIC-TAC-TOE

gluQuadricOrientation( Cylinder, GLU_OUTSIDE );

void Draw_O(int x, int y, int z, int a)

glPushMatrix();

glTranslatef(x, y, z);

glRotatef(a, 1, 0, 0);

glutSolidTorus(0.5, 2.0, 8, 16);

glPopMatrix();

void Draw_X(int x, int y, int z, int a)

glPushMatrix();

glTranslatef(x, y, z);

glPushMatrix();

glRotatef(a, 1, 0, 0);

glRotatef(90, 0, 1, 0);

glRotatef(45, 1, 0, 0);

glTranslatef( 0, 0, -3);

gluCylinder( Cylinder, 0.5, 0.5, 6, 16, 16);

// Draw our world

void display(void)

if(abc==3)

//int mk=0;

DEPT OF CSE ,REC HULKOTI 10


SIMULATION OF TIC-TAC-TOE

// glColor3f(0.0,1.0,0.0);

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen

glColor3f(0.0,1.0,0.0);

glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work

glLoadIdentity(); // Clear the matrix

glOrtho(-9.0, 9.0, -9.0, 9.0, 0.0, 30.0); // Setup an Ortho view

glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work.


(drawing)

glLoadIdentity(); // Clear the model matrix

glDisable(GL_COLOR_MATERIAL);

glDisable(GL_LIGHTING);

glColor3f(0.0, 0.0, 1.0);

Sprint(-2, 0, "Project by");

Sprint(-2, -1, "Adarsh malavade and Rahul gumaste");

Sprint(-3, -2, "To Start press right button");

Sprint(-3, -3, "right button for X's");

Sprint(-3, -4, "and left for 0's");

glutSwapBuffers();

if (win == 1) Sprint( -2, 1, "congratulations you win");

if (win == -1) Sprint( -2, 1, "Computer win");

if (win == 2) Sprint( -2, 1, "Tie");

DEPT OF CSE ,REC HULKOTI 11


SIMULATION OF TIC-TAC-TOE

// Main program

int main(int argc, char** argv)

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize (850,600);

glutInitWindowPosition (10, 10);

glutCreateWindow (argv[0]);

glutSetWindowTitle("X's and 0's 3D");

init ();

glutCreateMenu(menu);

glutAddMenuEntry("start game",1);

glutAddMenuEntry("prespective view",2);

glutAddMenuEntry("help",3);

glutAddMenuEntry("Quit",4);

glutAttachMenu(GLUT_RIGHT_BUTTON);

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutKeyboardFunc(keyboard);

//glutMouseFunc(mouse);

glutTimerFunc( 50, TimeEvent, 1);

glutMainLoop();

return 0;

DEPT OF CSE ,REC HULKOTI 12


SIMULATION OF TIC-TAC-TOE

Chapter 5

RESULTS AND DISCUSSION

SNAPSHOTS :

Fig 5.1 : Snapshot Showing Instruction Of Game Before Starting.

Fig 5.2 : Snapshot Showing of player view of tic-tac-toe game with help of X’s and 0’s

DEPT OF CSE ,REC HULKOTI 13


SIMULATION OF TIC-TAC-TOE

Fig 5.3 : Snapshot showing if player wins the game

Fig 5.4 : Snapshot showing if the game wins by computer.

DEPT OF CSE ,REC HULKOTI 14


SIMULATION OF TIC-TAC-TOE

Fig 5.5 : Snapshot showing if the game is tie

DEPT OF CSE ,REC HULKOTI 15


SIMULATION OF TIC-TAC-TOE

Chapter 6
CONCLUSION
The full designing and creating of Tic-Tac-Toe has been executed under windows
operating system using dev c++, this platform provides a and satisfies the basic need of a good
compiler. Using GL/glut.h library and built in functions make it easy to design good graphics
package such as this simple game.

The following example game is won by the first player, X:

A player can play perfect tic-tac-toe (win or draw) given they move according to the highest
possible moves:

1. Win: If the player has two in a row, play the third to get three in a row.

2. Block: If the opponent has two in a row, play the third to block them.

3. Fork: Create an opportunity where you can win in two ways.

4. Block opponent's fork:

Option 1: Create two in a row to force the opponent into defending, as long as it doesn't result in
them creating a fork or winning. For example, if "X" has a corner, "O" has the center, and "X" has
the opposite corner as well, "O" must not play a corner in order to win. (Playing a corner in this
scenario creates a fork for "X" to win.)

Option 2: If there is a configuration where the opponent can fork, block that fork.

5. Center: Play the center.

6. Opposite corner: If the opponent is in the corner, play the opposite corner.

7. Empty corner: Play in a corner square.

8. Empty side: Play in a middle square on any of the 4 sides.

DEPT OF CSE ,REC HULKOTI 16


SIMULATION OF TIC-TAC-TOE

REFERENCES
1. Reference from Interactive Computer Graphics by EDWARD ANGEL

2. Internet source

www.angelfire.com

Wikipedia.org.

Google

Opengl.org

DEPT OF CSE ,REC HULKOTI 17

You might also like