SET OF PROGRAMS

This blog provided "C" programs for beginners. Providing c programs for students.enjoy this blog.this blog include many c programs for engineering students, IT engineering students and programmers this blog is very useful.this blog post daily 1-5 programs for you.this programs use full for devlop games devlops application and much more. This blog programs useful for projects because all programs post with explanations.

Search Bar

Ads Here

Saturday, 5 May 2018

TIC_TAC_TOE GAME C PROGRAM

Tic tac toe game
#include<stdio.h>
#include<string.h>
main()
{
char choice1,choice2;
int p1,p2,i,j,position,move=1,x=0,y=0,count=0,c=1,d=0,a[8],k=0,flag=1;
for(i=0;i<9;i++)
a[i]=3;
printf("Welcome to play TIC-TAC-TOE world\n");
printf("for reference to players\n");
printf(" ______________\n");
printf("|a[0]|a[1]|a[2]|\n");
printf("|____|____|____|\n");
printf("|a[3]|a[4]|a[5]|\n");
printf("|____|____|____|\n");
printf("|a[6]|a[7]|a[8]|\n");
printf("|____|____|____|\n");
while(flag==1)
{
printf("Enter X or O for Player1\n");
scanf("%c",&choice1);
if(choice1=='X'||choice1=='x'||choice1=='O'||choice1=='o')
{
    flag=0;
}
else
{
    flag=1;
    printf("Invalid choice\n");
}
}

if(choice1=='X'||choice1=='x')
{
choice2='O';
}
else
{
choice2='X';
}

printf("Player2 choice is %c\n",choice2);
if(choice1=='X'||choice1=='x')
{
p1=1;
p2=0;
}
else
{
p1=0;
p2=1;
}
while(move<3)
{
    count=0;
    if(move==1)
    {
        printf("Player1 Enter the position\n");
         scanf("%d",&position);

         if(a[position]==3)
           {
               a[position]=p1;
               k=0;
               for(i=0;i<3;i++)
               {
                   for(j=0;j<3;j++)
                   {
                       if(a[k]==3)
                       printf("\t-");
                       if(a[k]==1)
                       printf("\tX");
                        if(a[k]==0)
                       printf("\tO");
                       k++;
                   }
                   printf("\n");
               }

               if((a[0]==p1&&a[1]==p1&&a[2]==p1)
||(a[3]==p1&&a[4]==p1&&a[5]==p1)||(a[6]==p1&&a[7]==p1&&a[8]==p1)||
(a[0]==p1&&a[3]==p1&&a[6]==p1)||(a[1]==p1&&a[4]==p1&&a[7]==p1)||
(a[2]==p1&&a[5]==p1&&a[8]==p1)||(a[0]==p1&&a[4]==p1&&a[8]==p1)||
(a[2]==p1&&a[4]==p1&&a[6]==p1))
                  {
                     printf("Player1 wins\n");
                      x=1;
                     printf("Thank you for playing this game\n");
                      move=3;
                    break;
                   }
                else
                {
                   move=2;
                }
           }
           else
           {
               printf("This place is not empty.Please
 give another position\n");
                   move=1;
                 }

      }
      for(i=0;i<9;i++)
      {

        if(a[i]==3)
        count++;
      }
      if(count==0)
      {
          move=3;
          break;
      }
      if(move==2)
      {
              printf("Player2 Enter position\n");
         scanf("%d",&position);

         if(a[position]==3)
           {
              a[position]=p2;
              k=0;
               for(i=0;i<3;i++)
               {
                   for(j=0;j<3;j++)
                   {
                       if(a[k]==3)
                       printf("\t-");
                       else if(a[k]==1)
                       printf("\tX");
                       else
                       printf("\tO");
                       k++;
                   }
                   printf("\n");
               }

              if((a[0]==p2&&a[1]==p2&&a[2]==p2)||
(a[3]==p2&&a[4]==p2&&a[5]==p2)||(a[6]==p2&&a[7]==p2&&a[8]==p2)||
(a[0]==p2&&a[3]==p2&&a[6]==p2)||(a[1]==p2&&a[4]==p2&&a[7]==p2)||(a[2]==p2&&a[5]==p2&&a[8]==p2)||
(a[0]==p2&&a[4]==p2&&a[8]==p2)||
(a[2]==p2&&a[4]==p2&&a[6]==p2))
                  {
                     printf("Player2 wins\n");
                      y=1;
                     printf("Thank you for playing this game\n");
                      move=3;
                    break;
                   }
                else
                {
                   move=1;
                }
           }
           else
           {
           printf("This place is not empty.Please 
give another position\n");
                   move=2;
                 }
      }


}
if(x!=1&&y!=1)
{
printf("Draw Match\n");
printf("Thank you for playing this game\n");
}

}
Output:
Welcome to play TIC-TAC-TOE world
for reference to players
 ______________
|a[0]|a[1]|a[2]|
|____|____|____|
|a[3]|a[4]|a[5]|
|____|____|____|
|a[6]|a[7]|a[8]|
|____|____|____|
Enter X or O for Player1
O
Player2 choice is X
Player1 Enter the position
4
        -       -       -
        -       O       -
        -       -       -
Player2 Enter position
2
        -       -       X
        -       O       -
        -       -       -
Player1 Enter the position
0
        O       -       X
        -       O       -
        -       -       -
Player2 Enter position
5
        O       -       X
        -       O       X
        -       -       -
Player1 Enter the position
8
        O       -       X
        -       O       X
        -       -       O
Player1 wins
Thank you for playing this game
        

Explanation:
//Coming Soon..

No comments:

Post a Comment