adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Tic Tac Toe game program in C language

Tic Tac Toe game program in C language



//Developed by Programming.OM
// Tic Tac Toe Game
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<windows.h>
#include<ctype.h>
 void show(int m[][3])
     {

         printf("\t Tic Tac Toe Game\n");
         printf("\t _________________\n");
         printf("\t|     |     |     |\n");
         printf("\t|  %c  |  %c  |  %c  |\n",m[0][0],m[0][1],m[0][2]);
         printf("\t|_____|_____|_____|\n");
         printf("\t|     |     |     |\n");
         printf("\t|  %c  |  %c  |  %c  |\n",m[1][0],m[1][1],m[1][2]);
         printf("\t|_____|_____|_____|\n");
         printf("\t|     |     |     |\n");
         printf("\t|  %c  |  %c  |  %c  |\n",m[2][0],m[2][1],m[2][2]);
         printf("\t|_____|_____|_____|\n");
     }
 int main()
  {
      int m[3][3]={
       0,0,0,
       0,0,0,
     0,0,0
     };
      int p,s,k,ss,wins=0,full=0,fs;
      char fstn[6],sndn[6];;
      printf("Enter name of first player:-");
      scanf("%s",fstn);
      fflush(stdin);
      printf("and symbol:-");
      scanf("%c",&fs);
      printf("Enter name of second player:-");
      scanf("%s",sndn);
      fflush(stdin);
      printf("and symbol:-");
      scanf("%c",&ss);
      k=1;
      while(k<=9)
      {
          if(k%2==0)
          {
              printf("%s enter your position:-",fstn);
              scanf("%d",&p);
              s=toupper(fs);
          }
          else
          {
              printf("%s enter your position:-",sndn);
              scanf("%d",&p);
              s=toupper(ss);
          }
         switch(p)
         {
            case 1:if(m[0][0]==fs||m[0][0]==ss)
         k--;
      else{
                full++;
         m[0][0]=s;
        }
                    break;
            case 2:if(m[0][1]==fs||m[0][1]==ss)
          k--;
     else
        {
                 full++;
          m[0][1]=s;
         }
                    break;
            case 3:if(m[0][2]==fs||m[0][2]==ss)
    k--;
      else
          {
                   full++;
     m[0][2]=s;
                      }
                    break;
            case 4:if(m[1][0]==fs||m[1][0]==ss)
           k--;
      else
          {
                  full++;
    m[1][0]=s;
         }
                    break;
            case 5:if(m[1][1]==fs||m[1][1]==ss)
        k--;
     else
         {
                  full++;
    m[1][1]=s;
        }
                    break;
            case 6:if(m[1][2]==fs||m[1][2]==ss)
        k--;
      else
        {
                 full++;
   m[1][2]=s;
        }
                    break;
            case 7:if(m[2][0]==fs||m[2][0]==ss)
         k--;
     else
       {
                 full++;
          m[2][0]=s;
        }
                    break;
            case 8:if(m[2][1]==fs||m[2][1]==ss)
    k--;
     else
          {
                 full++;
   m[2][1]=s;
         }
     break;
            case 9:if(m[2][2]==fs||m[2][2]==ss)
        k--;
     else
       {
                full++;
         m[2][2]=s;
        }
                    break;
            default:printf("Wrong choice!");
         }
         //winner
         if(m[0][0]==m[1][1]&&m[0][0]==m[2][2])
           {
             if(m[1][1]!=0&&m[0][0]!=0&&m[2][2]!=0)
               wins=m[1][1];
     }
         if(m[0][2]==m[1][1]&&m[0][2]==m[2][0])
             {
             if(m[0][2]!=0&&m[1][1]!=0&&m[2][0]!=0)
               wins=m[1][1];
     }
        if(m[0][0]==m[0][1]&&m[0][0]==m[0][2])
            {
               if(m[0][0]!=0&&m[0][1]!=0&&m[0][2]!=0)
                  wins=m[0][0];
              }
        if(m[0][0]==m[1][0]&&m[0][0]==m[2][0])
            {
               if(m[1][0]!=0&&m[0][0]!=0&&m[2][0]!=0)
                  wins=m[0][0];
              }
         if(m[1][0]==m[1][1]&&m[1][0]==m[1][2])
             {
             if(m[1][1]!=0&&m[1][0]!=0&&m[1][2]!=0)
                  wins=m[1][1];
     }
        if(m[0][1]==m[1][1]&&m[0][1]==m[2][1])
             {
             if(m[1][1]!=0&&m[0][1]!=0&&m[2][1]!=0)
               wins=m[1][1];
     }
         if(m[0][2]==m[1][2]&&m[0][2]==m[2][2])
              {
             if(m[0][2]!=0&&m[1][2]!=0&&m[2][2]!=0)
               wins=m[2][2];
     }
          if(m[2][0]==m[2][1]&&m[2][0]==m[2][2])
              {
             if(m[2][0]!=0&&m[2][1]!=0&&m[2][2]!=0)
               wins=m[2][2];
    }

  system("cls");
 if(full==9) {
            show(m);
            printf("Match is draw!");
            getch();
            system("cls");
            printf("\nPress any key to exit game!");
            getch();
            return 0;
        }
        if(wins==fs)
          {
            show(m);
            printf("%s is winner!",fstn);
            getch();
            system("cls");
            printf("\nPress any key to exit game!");
            getch();
            return 0;
    }
  else if(wins==ss)
    {
  show(m);
           printf("%s is winner!",sndn);
           getch();
                system("cls");
                printf("\nPress any key to exit game!");
                getch();
                return 0;
   }
         show(m);
        k++;
       }
    getch();
  }
Output :-






















Related programs
  

  • Tic Tac Toe game program in C language ( computer vs human )

Post a Comment

0 Comments