TICTACTOE
TICTACTOE
CHAPTER-1
INTRODUCTION
Also in 1961 another student at MIT, Steve Russell, created the first video game, Space
war. E. E. Zajac, a scientist at Bell Telephone Laboratory (BTL), created a film called
“Simulation of a two-giro gravity attitude control system” in 1963. In this computer generated
film, Zajac showed how the attitude of a satellite could be altered as it orbits the Earth. Many of
the most important early breakthroughs in computer graphics research occurred at the University
of Utah in the 1970s.
The first major advance in 3D computer graphics was created at UU by these early
pioneers, the hidden-surface algorithm. In order to draw a representation of a 3D object on the
screen, the computer must determine which surfaces are “behind” the object from the viewer’s
perspective, and thus should be “hidden” when the computer creates (or renders) the image.
Graphics and application processing were increasingly migrated to the intelligence in the
workstation, rather than continuing to rely on central mainframe and mini-computers. 3D
graphics became more popular in the 1990s in gaming, multimedia and animation. Computer
graphics used in films and video games gradually began to be realistic to the point of entering the
uncanny valley. Examples include the later Final Fantasy games and animated films like The
Polar Express.
The OpenGL specification describes an abstract API for drawing 2D and 3D graphics.
Although it's possible for the API to be implemented entirely in software, it's designed to be
implemented mostly or entirely in hardware.
OpenGL is an evolving API. New versions of the OpenGL specification are regularly
released by the Khronos Group, each of which extends the API to support various new features.
In addition to the features required by the core API, GPU vendors may provide additional
functionality in the form of extensions.
Extensions may introduce new functions and new constants, and may relax or remove
restrictions on existing OpenGL functions. Vendors can use extensions to expose custom APIs
without needing support from other vendors or the Khronos Group as a whole, which greatly
increases the flexibility of OpenGL. All extensions are collected in, and defined by, the OpenGL
Registry.
DISPLAY OF INFORMATION: More than 4000 years ago, the Babylonians developed
floor. plans of buildings on stones. Today, the same type of information is generated by
architects using computers. Over the past 150 years, workers in the field of statistics have
explored techniques for generating plots. Now, we have computer plotting packages.
Supercomputers now allow researchers in many areas to solve previously intractable
problems. Thus, Computer Graphics has innumerable applications.
DESIGN: Professions such as engineering and architecture are concerned with design.
Today, the use of interactive graphical tools in CAD, in VLSI circuits, characters for
animation have developed in a great way.
SIMULATION AND ANIMATION: One of the most important uses has been in pilots‟
training. Graphical flight simulators have proved to increase safety and reduce expenses.
Simulators can be used for designing robots, plan it’s path, etc. Video games and
animated movies can now be made with low expenses.
USER INTERFACES: Our interaction with computers has become dominated by a visual
paradigm. The users’ access to internet is through graphical network browsers. Thus
Computer Graphics plays a major role in all fields.
The API is typically used to interact with a GPU, to achieve hardware accelerated
rendering.
i. Geometric Primitives.
ii. Image(Raster) Primitives.
Geometric primitives are specified in the problem domain and include points, line segments,
polygons, curves and surfaces.
Raster primitives, such as arrays of pixels pass through a separate parallel pipeline on their
way to the frame buffer.
CHAPTER-2
REQUIREMENT SPECIFICATION
The graphics editor has been programmed in C. It makes use of Turbo C Graphics library
package for creating the user interface. This is a subroutine library for terminal independent
screen painting and input event handling.
CHAPTER-3
ABOUT THE PROJECT
It is the same noughts and crosses or the Xs and Os, the other names for Tic-Tac-Toe,
you’ve played with paper and pencil. This mini game project is written in C language in a very
simple manner; it is complete and totally error-free.
You have probably played the Tic-Tac-Toe game to pass time during school hours. It’s fun when
you play with paper and pencil. Here, we have developed a mini project in C Tic-Tac-Toe game-
a simple console application with graphics.
This Tic-Tac-Toe game in C is compiled in Code::Blocks with gcc compiler. The source code is
not that long; it is about 400 lines.
While making a Tic-Tac-Toe game using C language, it is important to make use of arrays. The
Xs and Os are kept in different arrays, and they are passed between several functions in the code
to keep track of how the game goes. With the code here you can play the game choosing either X
or O against the computer.
This Tic-Tac-Toe C game is such that you will have to input a numerical character, from 1 to 9,
to select a position for X or O into the space you want. For example: if you are playing with O
and you input 2, the O will go to first row – second column. If you want to place O in third row –
first column, you have to enter 7. And, it is similar for the other positions.
CHAPTER-4
DESIGN
Design of the project includes the initialization and the flow in which the project works.
The initialization involves the use of mouse and keyboard functions. Flow of control includes the
working pattern of the project.
INITIALIZATION:
1. The game is to be played between two people (in this program between HUMAN and
COMPUTER).
2. One of the player chooses “O” and the other “X” to mark their respective cells.
3. The game starts with one of the players and the game ends when one of the players has
one whole row/ column/ diagonal filled with his/her respective character (“O” or “X”).
FLOW OF CONTROL:
A board game (such as Tic-tac-toe) is typically programmed as a state machine.
Depending on the current-state and the player's move, the game goes into the next-state. In this
example, I use a variable currentState to keep track of the current-state of the game, and define
named-constants to denote the various states of the game (PLAYING, DRAW,
CROSS_WON, and NOUGHT_WON). A method called updateGame() is defined, which will be
called after every move to update this currentState, by checking the status of the game-board.
Two methods are defined for printing the game board, printBoard() and
printCell(). The printBoard() shall call printCell() to print each of the 9 cells. This seems trivial
here, but will be useful in the object-oriented design to separate the board and cells into separate
classes.
FLOWCHART:
CHAPTER-5
IMPLEMENTATION
5.1 BUILT-IN FUNCTIONS:
1) glColor3f() - Used to display color.
11) glutCreateWindow() - Creates a top level window with the window name as specified.
12) glutDisplayFunc(display) - Sets the display call back for the current window.
13) glutReshapeFunc(reshape) - Sets the reshape call back for the current window.
14) glutMainLoop() - Enters the GLUT event processing loop. This routine should be called
atmost once in a GLUT program. Once called, this routine will never return. It will calls
necessary any callbacks that have been registered.
{
int i;
// Clear map for new game
for( i = 0; i < 9; i++)
{
box_map[i] = 0;
}
// Set 0 for no winner
win = 0;
start_game = 1;
}
// Check for three in a row/colunm/diagnal
// returns 1 if there is a winner
int check_move( void )
{
int i, t = 0;
//Check for three in a row
for( i = 0; i < 8; i++)
{
t = box_map[box[i][0]] + box_map[box[i][1]] + box_map[box[i][2]];
if ( (t == 3) || ( t == -3) )
{
spinboxes = i;
return( 1 );
}
}
t = 0;
// check for tie
for( i = 0; i < 8; i++)
{
t = t + abs(box_map[box[i][0]]) + abs( box_map[box[i][1]]) + abs( box_map[box[i][2]]);
}
if ( t == 24 ) return( 2 );
return( 0 );
}
// Do we need to block other player?
int blocking_win(void){
int i, t;
for( i = 0; i < 8; i++)
{
t = box_map[box[i][0]] + box_map[box[i][1]] + box_map[box[i][2]];
if ( (t == 2) || ( t == -2) )
{
// Find empty
if (box_map[box[i][0]] == 0) box_map[box[i][0]] = computer;
if (box_map[box[i][1]] == 0) box_map[box[i][1]] = computer;
return( 1 );
}
if ( box_map[3] == 0)
{
box_map[3] = computer;
return( 1 );
}
if ( box_map[5] == 0)
{
box_map[5] = computer;
return( 1 );
}
if ( box_map[7] == 0)
{
box_map[7] = computer;
return( 1 );
}
return( 0 );
}
// 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 );
}
// I use this to put text on the screen
void Sprint( int x, int y, char *st)
{
int l,i;
l=strlen( st ); // see how many characters are in text string.glRasterPos2i( x, y); // location to start
printing text
for( i=0; i < l; i++) // loop until i is greater then l
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]); // Print a character on the
screen
}
}
// 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.
}
// Setup our Opengl world, called once at startup.
void init(void)
{
glClearColor (0.6,0.6,0.4,0.0); // When screen cleared, use black.
glShadeModel (GL_SMOOTH); // How the object color will be rendered smooth or flat
glEnable(GL_DEPTH_TEST); // Check depth when rendering
// Lighting is added to scene
glLightfv(GL_LIGHT1 ,GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1 ,GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1 ,GL_POSITION, LightPosition);
glEnable(GL_LIGHTING); // Turn on lighting
glEnable(GL_LIGHT1); // Turn on light 1
start_game = 0;
win = 0;
// Create a new quadric
Cylinder = gluNewQuadric();
gluQuadricDrawStyle( Cylinder, GLU_FILL );
gluQuadricNormals( Cylinder, GLU_SMOOTH );
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);
//glutSolidCone( 2.5, 3.0, 16, 8 );
glPopMatrix();
glPushMatrix();
glRotatef(a, 1, 0, 0);
glRotatef(90, 0, 1, 0);
glRotatef(315, 1, 0, 0);
glTranslatef( 0, 0, -3);
gluCylinder( Cylinder, 0.5, 0.5, 6, 16, 16);
{
//char txt[30];
int ix, iy;
int i;
int j;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen
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(1.0, 0.0, 0.0);
/*if ( start_game == 0 )
{
Sprint(-2, 0, "ggb and kittu");
Sprint(-3, -1, "To Start press");
Sprint(-3, -2, "right button for X's");
Sprint(-3, -3, "and left for O's"); }
*/
if (win == 1) Sprint( -2, 1, "congratulations you win");
if (win == -1) Sprint( -2, 1, "Computer win");
if (win == 2) Sprint( -2, 1, "Tie");
// Setup view, and print view state on screen
if (view_state == 1)
{
glColor3f( 0.0, 0.0, 1.0);
Sprint(-3, 8, "Perspective view");
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 30);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}else
{
glColor3f( 1.0, 0.0, 0.0);
Sprint(-2, 8, "Ortho view");
}
// Lighting on/off
if (light_state == 1)
{
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
}else
{
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
}
gluLookAt( 0, 0, 20, 0, 0, 0, 0, 1, 0);
// Draw Grid
for( ix = 0; ix < 4; ix++)
{
glPushMatrix();
glColor3f(1,1,1);
glBegin(GL_LINES);
glVertex2i(-9 , -9 + ix * 6);
glVertex2i(9 , -9 + ix * 6 );
glEnd();
glPopMatrix();
}
for( iy = 0; iy < 4; iy++ )
{
glPushMatrix();
glColor3f(1,1,1); glBegin(GL_LINES);
glVertex2i(-9 + iy * 6, 9 );
glVertex2i(-9 + iy * 6, -9 );
glEnd();
glPopMatrix();
}
glColorMaterial(GL_FRONT, GL_AMBIENT);
glColor4f(0.5, 0.5, 0.5, 1.0);
glColorMaterial(GL_FRONT, GL_EMISSION);
glColor4f(0.0, 0.0, 0.0, 1.0 );
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.35, 0.35, 0.35, 1.0);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.69, 0.69, 0.69, 1.0);
//glDisable(GL_COLOR_MATERIAL);
glColor3f( 0.0, 0.0, 0.0); // Cube color
//glEnable(GL_COLOR_MATERIAL);
// Draw object in box's
for( i = 0; i < 9; i++)
{
j = 0;
if (abs( win ) == 1 )
{
if ( (i == box[spinboxes][0]) || (i == box[spinboxes][1]) || (i == box[spinboxes][2]))
{
j = spin;
}else j = 0;
}
if(box_map[i] == 1) Draw_X( object_map[i][0], object_map[i][1], -1, j);
if(box_map[i] == -1) Draw_O( object_map[i][0], object_map[i][1], -1, j);
}
//glDisable(GL_COLOR_MATERIAL);
glutSwapBuffers();
}
}
// This is called when the window has been resized.
void reshape (int w, int h)
{
Win_x = w;
Win_y = h;
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}// Read the keyboard
void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 'v':
case 'V':
view_state = abs(view_state -1);
break;
case 'b':
case 'B':
light_state = abs(light_state -1);
break;
case 27:
exit(0); // exit program when [ESC] key presseed
break;
default:
break;
}
}
void mouse(int button, int state, int x, int y)
{
// We convert windows mouse coords to out openGL coords
mouse_x = (18 * (float) ((float)x/(float)Win_x))/6;
mouse_y = (18 * (float) ((float)y/(float)Win_y))/6;
// What square have they clicked in?
object_select = mouse_x + mouse_y * 3;
if ( start_game == 0)
{
if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN))
{
player = 1;
computer = -1;
init_game();
computer_move();
return;
}
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{
player = -1;
computer = 1;
init_game();
return; }
}
if ( start_game == 1)
{
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{
if (win == 0)
{
if (box_map[ object_select ] == 0)
{
box_map[ object_select ] = player;
win = check_move();
if (win == 1)
{
start_game = 0;
return;
}
computer_move();
win = check_move();
if (win == 1)
{
win = -1;
start_game = 0;
}
}
}
}
}
if ( win == 2 )start_game = 0;
}
void menu(int choice)
{
switch(choice)
{
case 1: abc=1;
glutMouseFunc(mouse);
break;
case 2:
view_state = abs(view_state -1);
break;
case 3: abc=3;
glutMouseFunc(mouse);
break;
case 4:
exit(0);
break;}
}
// 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 O'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;
}
CHAPTER-6
RESULT
SNAPSHOTS:
CONCLUSION
In conclusion, our computer graphics mini project on tic-tac-toe successfully applied
computer graphics principles to create a visually appealing and interactive game. We developed
a responsive game board using rendering techniques and implemented smooth animations to
enhance the user experience. The intuitive user interface allowed for easy navigation and
gameplay. Additionally, the incorporation of an AI opponent added a challenging element to the
game. Through this project, we gained valuable knowledge and hands-on experience in
implementing computer graphics concepts in the context of a popular game.
FUTURE ENHANCEMENT
This project has been designed such that it works on the windows platform. The project
can be designed using different languages and better graphical interfaces. The following features
can be incorporated;
o This project may be useful to follow the architecture of fountain and its further
development.
o We can show that the water color of the fountain can be changed by the interaction of the
mouse.
o We can add some more graphics which show the simulation of other objects including the
sound effects which can give the project an attractive look.
REFERENCES
[1] Edward Angel, “Interactive Computer Graphics”, 5th edition, Pearson Education,2005.
[2] Jackie L. Neider, Mark Warhol, Tom R. Davies,” OpenGL Red Book”, 2 nd Revised Edition,
2005.
[3] Donald D Hearn and M. Pauline Baker, “Computer Graphics with OpenGL”, 3rd Edition.
[4] F.S. Hill and Stephen M. Kelly, “Computer Graphics using OpenGL”, 3rd Edition.
WEBSITES:
http://www.opengl.org
http://www.wikipedia.com
http://basic4gl.wikispaces.com
http://openglprojects.in