Total Pageviews

Friday 15 March 2013

Tic Tac Toe Game!!! (C version)



#include<stdio.h>

char ttt[3][3];
int row[3],coln[3];

char check()
{
     int i=1,j=1,count=0,count1=0;
     for(;i<=3;i++)
     {
                   if(row[i]==3)                return ttt[i][1];
                   
                   if(coln[i]==3)               return ttt[1][i];
     }
     
     for(i=1;i<=3;i++)
     {
                      for(j=1;j<=3;j++)
                      {
                                       if(i==j && ttt[i][j]=='x')   count--;
                                       if(i==j && ttt[i][j]=='o')   count++;
                                       if(i+j==4 && ttt[i][j]=='x')   count1--;
                                       if(i+j==4 && ttt[i][j]=='o')   count1++;
                      }
     }
     if(count==3 || count1==3)     return 'o';
     if(count==-3 || count1==-3)    return 'x';
                                                    
     return 'a';
}


void print()
{
     int i,j;
     for(i=1;i<=3;i++)
     {
                      for(j=1;j<=3;j++)
                      {
                                       printf("%c\t",ttt[i][j]);
                      }
                      printf("\n");
     }
}
     
     


int main()
{
    int i,j,dummy=0,count=0;
    char symbol='o',win='a';
    printf("Welcome to Tic Tac Toe!!!\n");
    for(i=1;i<=3;i++)
    {
                    for(j=1;j<=3;j++)
                    {
                                    ttt[i][j]='-';
                                    printf("%c\t",ttt[i][j]);
                    }
                    row[i]=coln[i]=0;
                    printf("\n");             
    }

    while(count<10 && win=='a')
    {
                   while(1)
                   {
                           scanf("%d%d",&i,&j);
                           if(ttt[i][j]=='x' || ttt[i][j]=='o')  printf("enter the correct place\n");
                           else              break;
                   } 
                   
                   
                   if(dummy%2!=0)      symbol='x';
                   else                symbol='o';
                   

                   if(symbol=='x')   
                   {
                                     row[i]--;
                                     coln[j]--;
                   }
                   else
                   {
                       row[i]++;
                       coln[j]++;
                   }
                   ttt[i][j]=symbol;
                   win=check();
                   print(ttt);
                   if(win=='x')     
                   {
                                    printf("x wins!!!\n");
                                    break;
                   }
                   if(win=='o')
                   {
                               printf("o wins!!!\n");
                               break;
                   }
                   dummy++;  
                   count++;
    }
   
    getch();
    return 0;
}

No comments:

Post a Comment