A Project Report ON " ": Tic Tac Toe Game
A Project Report ON " ": Tic Tac Toe Game
PROJECT REPORT
ON
“TIC TAC TOE GAME”
Submitted in partial fulfillment of the requirement for the IV semester
of
BACHELOR OF TECHNOLOGY
IN
INFORMATION TECHNOLOGY
Submitted By:
CERTIFICATE
(Asst. Professor)
DECLARATION
2. Syed Abdul
Quddus
Project Supervisor :
Approved By:
Mr. Agnivesh Pandey
Dr. Amit Khaskalam
HEAD (I.T.)
ACKNOWLEDGEMENTS
PREFACE
This report is an introduction to the Tic Tac Toe game in
C programming. Anybody, who doesn’t know even the
basics of Tic Tac Toe in C ,will be certainly able to
understand and gain the great knowledge from this report.
The core theme of the report focuses on the development
of Tic Tac Toe game in C language.
The report also contains the strategy of making Tic Tac
Toe game which serve a good idea to make a Tic Tac Toe
game program in C language to the programmer .
The most of the idea of making this game and report is
taken from “ Let Us C - by Yashwant Kanetkar’’ ,
Schaum’s Outline-C(TMH publications), and Internet
(Wikipedia ,Google,Yahoo ,etc.) .
APRIL 2013
Ahteshem Ahamad Khan
Syed
Abdul Quddus
CONTENTS
1. PREFACE………………………………………………
………….. 1
2. ACKNOWLEDGEMENTS….…………………………
………… 2
4. STRATEGY……………………………………………
………...... 4
6. CONCLUSION…………………………………………
……….. 19
7. REFERENCE…………………………………………
…………... 20
WHAT IS TIC TAC TOE?
Tic-tac-toe is not a very challenging game for human
beings. If you’re an enthusiast, you’ve probably moved
from the basic game to some variant like three
dimensional tic-tac-toe on a larger grid. If you sit down
right now to play ordinary three-by-three tic-tac-toe with
a friend, what will probably happen is that every game
will come out a tie. Both you and your friend can
probably play perfectly, never making a mistake that
would allow your opponent to win. But can you describe
how you know where to move each turn? Most of the
time, you probably aren’t even aware of alternative
possibilities; you just look at the board and instantly know
where you want to move. That kind of instant knowledge
is great for human beings, because it makes you a fast
player. But it isn’t much help in writing a computer
program. For that, you have to know very explicitly what
your strategy is.
STRATEGY / ALGORITHM USED IN CODING
The highest-priority and the lowest-priority rules seemed
obvious to me right away.The highest-priority are these:
1. If I can win on this move,do it.
2. If the other player can win on the next move,block that
winning square.
Here are the lowest-priority rules,used only if there is
nothing suggested more strongly by the board position:
n-2. Take the center square if it’s free.
n-1. Take a corner square if one is free.
n. Take whatever is available.
The highest priority rules are the ones dealing with the
most urgent situations:either I or my opponent can win on
the next move.The lowest priority ones deal with the least
urgent situations,in which there is nothing special about
the moves already made to guide me.
What was harder was to find the rules in between.I knew
that the goal of my own tic-tac-toe strategy was to set up
a fork, a board position in which I have two winning
moves,
so my opponent can only block one of them.
Here is an example:
x o
x
x o
x
o
x
o o
x x
x o x
o o
x x x x o
o x o x
x
o
x
Again, the computer is playing O. After the third grid, it is
looking for a possible winning combination for itself.
There are three possibilities: 258,357 and 456. So far we
have not given the computer any reason to prefer one over
another. But here is what happens if the program happens
to choose 357:
x x
o
x x o
o o
x x
x o
o
x x
By this choice, the computer has forced its opponent into
a fork that will win the game for the opponent. If the
computer chooses either of the other two possible winning
combinations, the game ends in a tie. (All moves after this
choice turn out to be forced.)
This particular game sequence was very troublesome for
me because it goes against most of the rules I had chosen
earlier. For one thing, the correct choice for the program
is any edge square, while the corner squares must be
avoided. This is the opposite of the usual priority.
Another point is that this situation contradicts rule 4a
(prevent forks for the other player) even more sharply
than the example we considered earlier. In that example,
rule 4a wasn’t enough guidance to ensure a correct
choice, but the correct choice was at least with the rule.
That is, just blocking a fork isn’t enough, but threatening
a win and blocking a fork is better than just threatening a
win alone. This is the meaning of rule 4. But in this new
situation, the corner square (the move we have to avoid)
block a fork, while the edge square (the correct move)
block a fork!
When I discovered this anomalous case, I was ready to
give up on the idea of beautiful, general rules. I almost
decided to build into the program a special check for this
precise board configuration. That would have been pretty
ugly, I think. But a shift in viewpoint makes this case
easier to understand: What the program must do is force
the other player’s move, and force it in a way that helps
the computer win. If one possible winning combination
doesn’t allow us to meet these conditions, the program
should try another combination. My mistake was to think
either about forcing alone (rule 4b) or about the
opponent’s forks alone (rule 4a).
As it turns out, the board situation we’ve been considering
is the only one in which a possible winning combination
could include two possible forks for the opponent. What’s
more, in this board situation, it’s a diagonal combination
that gets us in trouble, while a horizontal or vertical
combination is always okay. Therefore, I was able to
implement rule 4 in a way that only considers one
possible winning combination by setting up the
program’s data structures so that diagonal combinations
are the last to be chosen. This trick makes the program’s
design less than obvious from reading the actual program,
but it does save the program some effort.
PROGRAM FOR TIC TAC TOE GAME:
#include <stdio.h>
#include <stdlib.h>
char matrix[3][3];
char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);
int main(void)
{
char done;
printf("This is the game of Tic Tac Toe.\n");
printf("You will be playing against the computer.\n");
done = ' ';
init_matrix();
do {
disp_matrix();
get_player_move();
done = check();
if(done!= ' ') break;
get_computer_move();
done = check();
}
while(done== ' ');
if(done=='X')
printf("You won!\n");
else printf("I won!!!!\n");
disp_matrix();
return 0;
}
void init_matrix(void)
{
int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = ' ';
}
void get_player_move(void)
{
int x, y;
printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);
x--; y--;
if(matrix[x][y]!= ' ')
{ printf("Invalid move, try again.\
n"); get_player_move();
}
else matrix[x][y] = 'X';
}
void get_computer_move(void)
{
int i, j;
for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
if(matrix[i][j]==' ') break;
if(matrix[i][j]==' ') break;
}
if(i*j==9) {
printf("draw\n");
exit(0);
}
else
matrix[i][j] = 'O';
}
void disp_matrix(void)
{
int t;
for(t=0; t<3; t++) {
printf(" %c | %c | %c ",matrix[t][0],
matrix[t][1], matrix [t][2]);
if(t!=2) printf("\n---|---|---\n");
}
printf("\n");
}
char check(void)
{
int i;
for(i=0; i<3; i++) if(matrix[i]
[0]==matrix[i][1] &&
matrix[i][0]==matrix[i][2]) return matrix[i][0];
for(i=0; i<3; i++) /* check columns */ if(matrix[0]
[i]==matrix[1][i] && matrix[0][i]==matrix[2][i])
return matrix[0][i]; if(matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])
return matrix[0][0]; if(matrix[0]
[2]==matrix[1][1] && matrix[1]
[1]==matrix[2][0]) return
matrix[0][2];
getch();
return ' ';}
REFERENCE
1. GOOGLE
2. WIKIPEDIA
4. www.indiabix.com
CONCLUSION