Post on 05-Apr-2018
7/31/2019 Tic Modified
1/33
K L University
GREEN FIELDS VADDESWARAM
GUNTUR DISTRICT
FRESHMAN ENGINEERING DEPARTMENT
I/IV B.tech
TIC TAC TOE GAMEUSING C-PROGRAMMING
7/31/2019 Tic Modified
2/33
DECLARATION
We declare that project work entitled TIC TAC TOE was
carried out by us during 2012 and this work is not the same
as that of any other and has not been submitted for the
award of any other degree/diploma.
Place:
Date:
7/31/2019 Tic Modified
3/33
CERTIFICATE
This is to certify that this project work TIC TAC TOE is a
bonified work carried out in the department of freshmenengineering during the year 2011-2012 by
T.Sravan Kumar (11003081),
Ch.Chaitanya (11003333),
V.Harish (11003344),
D.Satish (11003391),
T.Sundeep Babu (11003414)
HEAD OF DEPARTMENT STAFF IN-CHARGE
PROJECT GUIDE EXTERNAL EXAMINER
7/31/2019 Tic Modified
4/33
ACKNOWLEDGEMENT
First we would like to convey our heart full thanks to
almighty for his blessings on us to carry out mini projectwork
without any disruption.
We are very much obliged to Dr.K.Rama Krishna,
Dean FED for permitting us to carry out our mini project work
and for providing us all support required.
We are greatly indebted to Dr. A.Srinivas Rao Head Of
Department for giving us moral support and also permitting
us to do this mini project.
We would like to convey our heart full thanks to our
Mini Project Guide D.Bala Krishna Kamesh for his guidance
and support in every step of this project.We convey our
sincere thanks to all our faculty and friends who directly or
indirectly helped us for the successful completion of ourproject.
7/31/2019 Tic Modified
5/33
INDEX
1.ABSTRACT2.INTRODUCION3.SOFTWARE AND HARDWARE REQUIREMENTS4.CONTENT
5.FLOW CHART
6.SOURCE CODE7.SCREEN SHOTS
8.CONCLUSION9.BIBLOGRAPHY
7/31/2019 Tic Modified
6/33
ABSTRACT
This Mini Project is to create a Tic-Tac-Toe game and
to generate it using C-Program Language. Tic Tac Toe is a very
old game, nobody knows its origin but its so simple there iseven a probability that early man had its own version of the
game. All you have to do is to get your three symbols in
straight line. Tic Tac Toe game using C is relatively easy to
program and beginners can give it a shot if they understand.
INTRODUCTION
About the Tic Tac Toe game. It is also called as Wick
Wack Woe (in some Asian countries), Noughts and
Crosses (in the British Commonwealth countries), X's and
O's in the Republic of Ireland, is a Pencil and Paper game for
two players.
X and O, who take turns marking the spaces in a 33
grid. The X player usually goes first. The player who succeeds
in placing three respective marks in a horizontal, vertical, or
diagonal row wins the game.
7/31/2019 Tic Modified
7/33
Software And Hardware Requirements
Dos Box 0.74 or Turbo C++ 3.0 IDE.Intel ProcessorRAM - 2 GB (Minimum)System Type - 32 or 64 BitGraphics Card 128 MB (Minimum)
Content
1. Getting Started
2. Coding Main
3. Initializing Graphics4. Game Data
http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#2http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#2http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#3http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#3http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#4http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#4http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#5http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#5http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#5http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#4http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#3http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#27/31/2019 Tic Modified
8/33
5. Board Initialization6. Mapping Board7. Game Engine8. Taking Input9. Checking Winner
1. Getting Started:
Go to C:\TC\Bin there you will find a executable file called TC
execute it and IDE will load itself.Now go to File Menu and choose New.Now Press F2 to save the file. Give it name tictacto.c (only 8
character filename is allowed).Now you are all set to rock. The screen should be something
like this -
http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#6http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#6http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#7http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#7http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#8http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#8http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#9http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#9http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#10http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#10http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#10http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#9http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#8http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#7http://www.cprogrammingreference.com/Tutorials/Games_Programming/TicTacToe.php#67/31/2019 Tic Modified
9/33
2. Coding Main:First Thing a program should do is to include header files and
code main only header we will need is graphics.h and
conio.h. Including unnecessary header files increase the size
of the code and compile time. Next we create an parameter
less int returning main with no code at present. Your Code
should look like this#include #include int main ()
{return 0;
7/31/2019 Tic Modified
10/33
}3. Initializing Graphics:
Now we initialise graphics in main, stop for a key press and
then close the graphics. When we stop you should see a
blank page and not any error. If you face an error make sure
your installation path is correct. All our game activity occurs
in this opening and closing of graphics. It will generally give
you a 640*480 mode with VGA graphics. These graphics are
good enough to code any descent looking games. If you getdifferent settings try to set them as above. Your code should
be now -
4. Game Data:Now we create data your game needs. In this game we need
following data -
We need 2D 3 X 3 Matrix to Store the board. Each member of
the board contains one the three values -'O' if there is a zero at that place.'X' if there is a cross at that place'*' if there is no element at that place i.e. placed has not yet
been filled.
5. Board Initialization:
Now we create a function which initializes the entire board to
'*'.
This indicates that no block is filled.
7/31/2019 Tic Modified
11/33
We use a function called initiateboard called in main to do
this job for us.6. Mapping Board:
This is a very important function in our program. Its job is to
create a graphical image of the game form the data array
board.It first clears all the graphics on the present screen.Then its initiate the graphics and re draw the entire screen.First the function draws the border lines. Then it draws a
cross, a circle or nothing depending upon the values in the
array.It uses function drawcircle and drawcross to draw circles and
cross respectively. These functions take input value 0 to 9
depending upon the array position to decide what to draw.This function is first called in initialisation but will be called
several times
Here some screenshot of the game developed till now -
7/31/2019 Tic Modified
12/33
7. Game Engine:Now we design a game engine for this game. This Game
engine will contain a which end after 5 Users moves
assuming user moves first. Now our game engine must do
the following jobs in one sequence.1. Take user input.2. Place user move on board.3. Calculate computer move.4. Place computer move on board.5. Refresh Screen for next round.6. Check whether there is a winner.
We use function play to be name of our game engine and
game exits when the function exits. This function is called
once in main.
Now we have designed mapboard function in such a way that
it can be used to refresh screen and can do the 5th job. We
discuss the rest next.
8. Taking Input:We create a function name input which does the first two
jobs of our game engine. It takes user input and if valid place
that on to the data.
7/31/2019 Tic Modified
13/33
User is asked to input X coordinate (1 - 3) and Y coordinate (1
- 3).If in our data board [X-1] and [Y-1] position is '*' then weplace a 'X' There otherwise reprompt for input.Here is a screenshot of the Game Developed Till Now
Coding Now Becomes:
#include#include
#include
#include
char board [3] [3];
void drawcircle (int position)
{
int centrex;int centrey;
centrex = 245 + (50*(position%3)) + 25;
centrey = 165 + (50*(position/3)) + 25;
fillellipse (centrex,centrey, 10,10);
}
void drawcross (int position)
{
7/31/2019 Tic Modified
14/33
int centrex;
int centrey;
centrex = 245 + (50*(position%3)) + 25;
centrey = 165 + (50*(position/3)) + 25;line (centrex-5,centrey+5,centrex+5,centrey-5);
line (centrex+5,centrey+5,centrex-5,centrey-5);
}
void mapboard ()
{
int gdriver = DETECT, gmode;
int i,j;
closegraph ();
initgraph (&gdriver, &gmode, "c:\\bgi");
// Draw 4 Lines to Form Board
line (295,165,295,315);
line (345,165,345,315);
line (245,215,395,215);
line (245,265,395,265);
for (i = 0; i
7/31/2019 Tic Modified
15/33
for (i=0;i
7/31/2019 Tic Modified
16/33
printf ("\n Enter X Coordinate - ");
scanf ("%d",&x);
printf ("\n Enter Y Coordinate - ");
scanf ("%d", &y);if (board [x-1] [y-1] == '*')
{
board [x-1] [y-1] = 'O';
}
else
{
mapboard ();
printf ("\n draw");
}
}
void computer ()
{
int i,j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
if (board [i] [j] == '*')
{
board [i] [j] = 'O';
return;}
}
}
void check ()
{
int i;
char key = '*';
7/31/2019 Tic Modified
17/33
// Check Rows
for (i=0; i
7/31/2019 Tic Modified
18/33
// Check Rows
for (i=0; i
7/31/2019 Tic Modified
19/33
{
input ();
computer ();
mapboard ();check ();
}
printf ("\n GAME DRAW");
}
void play2 ()
{
int i,j;
for (i = 0; i
7/31/2019 Tic Modified
20/33
switch(ch)
{
case 1:initiateboard ();
play ();break;
case 2:initiateboard ();
play2();
break;
}
getch ();
closegraph ();
return 0;
}
9. Checking Winner:
Now last thing our gameengine must do is to see whether wehave a winner or not.
This function use a flag variable key (arbitary name) which is
initialise to '*'
Then our function checks all possible combination of winning
and if it finds a character other than '*' in series of 3 it setskey value to it.
Then we calculate whether player wins, computer wins or
nobody wins and game is still on.
Now here is the final code of this version -
void mapboard ()
7/31/2019 Tic Modified
21/33
{
int gdriver = DETECT, gmode;
int i,j;
closegraph ();initgraph (&gdriver, &gmode, "c:\\bgi");
// Draw 4 Lines to Form Board
line (295,165,295,315);
line (345,165,345,315);
line (245,215,395,215);
line (245,265,395,265);
for (i = 0; i
7/31/2019 Tic Modified
22/33
mapboard ();
}
void input ()
{int x,y;
printf ("nEnter X Coordinate - ");
scanf ("%d",&x);
printf ("nEnter Y Coordinate - ");
scanf ("%d",&y);
if (board [x-1] [y-1] == '*')
{
board [x-1] [y-1] = 'X';
}
else
{
mapboard ();
printf ("\n Wrong Coordinates");
input ();
}
}
void input2 ()
{
int x,y;
printf ("\n Enter X Coordinate - ");scanf ("%d",&x);
printf ("\n Enter Y Coordinate - ");
scanf ("%d", &y);
if (board [x-1] [y-1] == '*')
{
board [x-1] [y-1] = 'O';
}
7/31/2019 Tic Modified
23/33
else
{
mapboard ();
printf ("\n draw");}
}
void computer ()
{
int i,j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
if (board [i] [j] == '*')
{
board [i] [j] = 'O';
return;
}
}
}
void check ()
{
int i;
char key = '*';
// Check Rowsfor (i=0; i
7/31/2019 Tic Modified
24/33
// Check Diagonals
if (board [0][0] == board [1] [1] && board [1][1] == board [2]
[2] && board [1] [1] != '*') key = board [1] [1];
if (board [0][2] == board [1] [1] && board [1][1] == board [2][0] && board [1] [1] != '*') key = board [1] [1];
//Declare Winner if any
if (key == 'X')
{
printf ("You Win");
getch ();
exit (0);
}
if (key == 'O')
{
printf ("Computer Win");
getch ();
exit (0);
}
}
void check2 ()
{
int i;
char key = '*';
// Check Rowsfor (i=0; i
7/31/2019 Tic Modified
25/33
// Check Diagonals
if (board [0][0] == board [1] [1] && board [1][1] == board [2]
[2] && board [1] [1] != '*') key = board [1] [1];
if (board [0][2] == board [1] [1] && board [1][1] == board [2][0] && board [1] [1] != '*') key = board [1] [1];
//Declare Winner if any
if (key == 'X')
{
printf ("player1 Win");
getch ();
exit (0);
}
if (key == 'O')
{
printf ("player2 Win");
getch ();
exit (0);
}
}
void play ()
{
int i;
for (i = 0; i
7/31/2019 Tic Modified
26/33
void play2 ()
{
int i,j;
for (i = 0; i
7/31/2019 Tic Modified
27/33
}
getch ();
closegraph ();
return 0;}
Flow Chart
7/31/2019 Tic Modified
28/33
SCREEN SHOTS
Player selection
Player must enter the co-ordinates
7/31/2019 Tic Modified
29/33
After entering the co-ordinates, its computers turn to place
its position. Again its players turn to place a position.
7/31/2019 Tic Modified
30/33
Next its computers turn and after its players turn.
7/31/2019 Tic Modified
31/33
Now its a chance for the player to win, if he places the
symbol in the respective 3rd
X and Y co-ordinates.
7/31/2019 Tic Modified
32/33
Player WINS !!!
7/31/2019 Tic Modified
33/33
CONCLUSION
Tic-tac-toe is a trivial game, a fact which cannot be changed
by playing it on more dimensions or a bigger playing field.Given a reasonable heuristic and sufficient search depth,
minimax will converge to perfect play that is, games result
either in a draw or in the first player winning. It can be
concluded that the first player has a clear advantage,
because each move will improve its position. Higher search
depths improve the chances of winning, provided a properheuristic is used.
BIBLIOGRAPHY
ANSI C Bala Guruswamy
www.wikipedia.com