Thursday, October 11, 2012

Games – Two Users Tic-Tac-Toe


Not a great one but lost bit patients while writing. I wanted one using graphics routine but may later I will come up with it. Two weeks Puja breaks and then times for C++.







#include <stdio.h>
#include <conio.h>
#include<stdlib.h>

  char gm[3][3];
  void checks(int q)
      {
           if(q==65 || q==33)
              {
                   printf("\n\n\tPress Any key to Quit.....");
                   getch();
                   exit(0);
              }
      }
  void design()
      {
           int i,j;
           for(i=0;i<3;i++)
              {
                   printf("\n\t\t___________________________\n");
                   printf("\t");
                   for(j=0;j<3;j++)
                        {
                            printf("\t|  %c ",gm[i][j]);
                        }
                   printf("\t |");
                   printf("");
              }
           printf("\n\t\t___________________________\n");
      }
  void initz()
      {
           int i,j;
           for(i=0;i<3; i++)
              {
                   for(j=0;j<3;j++)
                        {
                            gm[i][j]=' ';
                        }
              }
      }
  void input()
      {
           int r,c,r1,c1;
           int i=0,j,chk=0,chk1=0,count=0;
           while(i<10)
              {
           printf("\n\t\tPress 'Q' to Quit\n");
                   while(1)
                        {
                            if(count>=9)
                                {
                                      break;
                                }
                            printf("\n\tUser-1 Press Row No. (1-3) : ");
                            r=(int)(getche()-48);
                            checks(r);
                            printf("\n\tUser-1 Press Column No. (1-3) : ");
                            c=(int)(getche()-48);
                            checks(c);
                            if((r>=1 && r<=3) && (c>=1 && c<=3))
                                {
                                      r=r-1;
                                      c=c-1;
                                      if(gm[r][c]==' ')
                                                {
                                                          gm[r][c]='X';
                                                          chk=check();
                                                          count++;
                                                          i++;
                                                          break;
                                                }
                                      else
                                                {
                                                          printf("\n\t* * Given Cell is Already Used");

                                                }
                                }
                            else
                                {
                                      printf("\n\t* * Invalid Input * *");
                                }
                        }
           if(chk!=1)
              {
                   while(1)
                        {
                            if(count>=9)
                                {
                                      break;
                                }
                            printf("\n\t\tPress 'Q' to Quit\n");
                            printf("\n\tUser-2 Press Row No. (1-3) : ");
                            r1=(int)(getche()-48);
                            checks(r1);
                            printf("\n\tUser-2 Press Column No. (1-3) : ");
                            c1=(int)(getche()-48);
                            checks(c1);
                            if((r1>=1 && r1<=3) && (c1>=1 && c1<=3))
                                {
                                      r1=r1-1;
                                      c1=c1-1;
                                      if(gm[r1][c1]==' ')
                                                {
                                                          gm[r1][c1]='O';
                                                          chk1=check1();
                                                          i++;
                                                          count++;
                                                          break;
                                                }
                                      else
                                                {
                                                          printf("\n\t* * Invalid Input * *");
                                                }
                                }
                            else
                                {
                                      printf("\n\t* * Invalid Input * *");
                                }
                            }
                        }
                   if(chk==1)
                        {
                            printf("\n\t* * User-1 Won * *");
                            printf("\n\n\tPress Any Key to Cont.. ");
                            getch();
                            break;
                        }
                   else if(chk1==1)
                        {
                            printf("\n\t* * User-2 Won * *");
                            printf("\n\n\tPress Any Key to Cont.. ");
                            getch();
                            break;
                        }
                   if(count>=9)
                        {
                            printf("\n\t* * Draw * *");
                            printf("\n\n\tPress Any Key to Cont.. ");
                            getch();
                            break;
                        }
              }
  }
  int check()
      {
           int i,j,sr,sc,ld=0,rd=0,flag=0;
           design();
           for(i=0;i<3;i++)
              {
                   sr=sc=0;
                   for(j=0;j<3;j++)
                        {
                            if(gm[i][j]=='X')
                                {
                                      sr++;
                                }
                            if(gm[j][i]=='X')
                                {
                                      sc++;
                                }
                            if(i==j && gm[j][i]=='X')
                                {
                                      ld++;
                                 }
                            if(i+j==2 && gm[j][i]=='X')
                                {
                                      rd++;
                                }
                        }
                   if(sr==3 || sc==3)
                        {
                            flag=1;
                            break;
                        }
                   if(flag==1 || ld==3 || rd==3)
                        {
                            flag=1;
                            break;
                        }
              }
           return flag;
      }

  int check1()
      {
           int i,j,sr,sc,ld=0,rd=0,flag=0;
           design();
           for(i=0;i<3;i++)
              {
                   sr=sc=0;
                   for(j=0;j<3;j++)
                        {
                            if(gm[i][j]=='O')
                                {
                                      sr++;
                                }
                            if(gm[j][i]=='O')
                                {
                                      sc++;
                                }
                            if(i==j && gm[j][i]=='O')
                                {
                                      ld++;
                                }
                            if(i+j==2 && gm[j][i]=='O')
                                {
                                      rd++;
                                }
                        }
                   if(sr==3 || sc==3)
                        {
                            flag=1;
                            break;
                        }
                   if(flag==1 || ld==3 || rd==3)
                        {
                            flag=1;
                            break;
                        }
              }
           return flag;
      }
  int main()
      {
           initz();
           input();
           printf("\n\n\tPress Any Key to Cont.. ");

           return 0;
      }





No comments: