TIC-TAC-TOE THROUGH C

IMPLEMENTATION OF TIC-TAC-TOE THROUGH C :-


From Wikipedia, the free encyclopedia.

Tic-tac-toe, originally called noughts and crosses (and still known as this in Britain and Australia), Xs and Os (in Ireland) and X and 0 (in India) is a pencil-and-paper game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game.
The following example game is won by the first player, X:
Players soon discover that best play from both parties leads to a draw (often referred to as cat or cat's game). Hence, tic-tac-toe is most often played by young children.
The friendliness of tic-tac-toe games makes them ideal as a pedagogical tool for teaching the concepts of good sportsmanship and the branch of artificial intelligence that deals with the searching of game trees. It is straightforward to write a computer program to play tic-tac-toe perfectly, to enumerate the 765 essentially different positions (the state space complexity), or the 26,830 possible games up to rotations and reflections (the game tree complexity) on this space.
http://en.wikipedia.org/wiki/Tic-tac-toe
---------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
int check(int i,int j);
int winner(void);
void printmatrix(void);
int a[3][3];
void main()
{
int  win,choice,i,posi,posj;
char pl1[30],pl2[30],str[30];
while(1)
{
clrscr();
printf("\nEnter 0 to Exit");
printf("\nEnter Any Other Number to Play \n");
scanf("%d",&choice);
if(!choice)
break;
win=100;
for(posi=0;posi<=2;posi++)
for(posj=0;posj<=2;posj++)
a[posi][posj]=win++;
printmatrix();
printf("\nWELCOME TO TIC-TAC-TOE");
printf("\nENTER PLAYER 1 NAME WHO WILL PLAY X AND STARTS GAME ");
fflush(stdin);
gets(pl1);
printf("\nENTER PLAYER 2 NAME WHO WILL PLAY O ");
gets(pl2);
for(i=1;i<=9;i++)
{
if(i%2!=0)
strcpy(str,pl1);
else
strcpy(str,pl2);
printf("\n%s ENTER ROW NUMBER ",str);
scanf("%d",&posi);
printf("\n%s ENTER COLUMN NUMBER ",str);
scanf("%d",&posj);
if(check((posi-1),(posj-1)))
{
while(check(posi-1,posj-1))
{
printf("\nINVALID CHOICE , RE-ENTER");
printf("\nENTER ROW NUMBER ");
scanf("%d",&posi);
printf("\nENTER COLUMN NUMBER ");
scanf("%d",&posj);
}//while
}//if
if(i%2!=0)
a[posi-1][posj-1]=1;
else
a[posi-1][posj-1]=0;
clrscr();
printmatrix();
win=winner();
if(win==1)
{
printf("\nPLAYER 1 WINS ");
break;
}
else if(win==2)
{
printf("\nPLAYER 2 WINS ");
break;
}
}//for
clrscr();
printf("\n\n\tHERE IS THE RESULT\n");
for(i=1;i<=80;i++)
printf("-");
printmatrix();
for(i=1;i<=80;i++)
printf("-");
win=winner();
if(win==0)
printf("\nOOPS IT'S A DRAW");
else if(win==1)
printf("\n%s IS WINNER",pl1);
else
printf("\n%s IS WINNER",pl2);
delay(5000);
}
}//main
int check(int i ,int j)
{
if(a[i][j]==0||a[i][j]==1||i<0||i>2||j<0||j>2)
return 1;
return 0;
}
void printmatrix(void)
{
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(a[i][j]==0)
printf("\t\tO\t|");
else if(a[i][j]==1)
printf("\t\tX\t|");
else
printf("\t\t \t|");
}
printf("\t\t\n");
}
}
int winner()
{
int i;
for(i=0;i<=2;i++)
{
if((a[i][0]==a[i][1]&&a[i][0]==a[i][2])&&(a[i][0]!=0||a[i][0]!=1))
{
if(a[i][0]==1)
return 1;
return 2;
}//if
if((a[0][i]==a[1][i]&&a[0][i]==a[2][i])&&(a[i][0]!=0||a[i][0]!=1))
{
if(a[0][i]==1)
return 1;
return 2;
}//if
}//for
//CHECK FOR DIAGONAL ELEMENT'S
if((a[1][1]==a[2][2]&&a[2][2]==a[0][0])&&(a[1][1]!=0||a[1][1]!=1))
{
if(a[1][1]==1)
return 1;
return 2;
}
if((a[0][2]==a[1][1]&&a[1][1]==a[2][0])&&(a[2][0]!=0||a[2][0]!=1))
{
if(a[1][3]==1)
return 1;
return 2;
}//if
return 0;
}
---------------------------------------------------------------------------------
                                                           !!! HOPE U LIKE IT !!!
                                                   !!!  THANKS FOR READING !!!
                                                  !!!!!HAPPY PROGRAMMING !!!!!

Comments

Popular posts from this blog

QUEUE IMPLEMENTATION USING LINKED LIST

DATA STRUCTURES INTERVIEW QUESTIONS

Program To Evaluate Square Root Of A Number