Tuesday, 29 October 2013

TIC TAC TOE game in C language

I have made TIC TAC TOE game using C language. I have also given explanation of the program. If you only have basic knowledge of C language and don't no about graphics than don't worry I have not used any graphics in my program I have only used basic concepts such as switch case ,if-else,for loop etc .


SOURCE CODE 

#include<conio.h>
#include<stdio.h>
int z[10],y[10],i;
char d[10];
int check(int c[10])
{
  int j;
  for(j=1;j<=3;j++)
    if(c[j]&&c[j+3]&&c[j+6])
      return 1;
  for(j=1;j<=7;j+=3)
    if(c[j]&&c[j+1]&&c[j+2])
      return 1;
  if(c[1]&&c[5]&&c[9] || c[3]&&c[5]&&c[7])
    return 1;
  return 0;
}
void print1(int s[10],int flag)
{
  printf("\n\n\t\t\t %c | %c | %c\n\t\t\t___|___|___\n\t\t\t %c | %c | %c\n\t\t\t___|___|___\n\t\t\t %c | %c | %c\n\t\t\t   |   |",d[1],d[2],d[3],d[4],d[5],d[6],d[7],d[8],d[9]);
  if(check(s))
  {
    printf("\n\n\nPlayer %d wins",flag);
    i=11;
  }
}
void toe()
{
  char yo='X',ol='O';
  printf("\n\t\t\tTIC-TAC-TOE\n");
  printf("Player 1 Symbol  %c\nPlayer 2 Symbol  %c",yo,ol);
  printf("\n\n\t\t\t 1 | 2 | 3\n\t\t\t___|___|___\n\t\t\t 4 | 5 | 6\n\t\t\t___|___|___\n\t\t\t 7 | 8 | 9\n\t\t\t   |   |");
}
void condition()
{
  print1(z,1);
  i--;
  printf("\n\nOOps!!! This block is already filled ,please enter another block no.");
}
void design()
{
  int a=0,q;
  for(q=1;q<10;q++)
    z[q]=y[q]=d[q]=0;
  toe();
  for(i=1;i<10;i++)
  {
    phir:
    if(i%2==1)
    printf("\n\nPlayer 1 enter the block no. ");
    else
    printf("\n\nPlayer 2 enter the block no. ");
    a=getch()-48;
    if(a<0||a>10)
    {
      printf("Wrong block no. entered");
      goto phir;
    }
    clrscr();
    toe();
    if(i%2==1)
    {
      if(d[a])
condition();
      else
      {
       z[a]=1,d[a]='X';
       print1(z,1);
      }
    }
    else
    {
      if(d[a])
condition();
      else
      {
y[a]=1,d[a]='O';
print1(y,2);
      }
    }
  }
  if(i==10)
  printf("\n\nGame Drawn");
  printf("\n\npress any key to continue");
}
void main()
{
  char ch;
  int oip;
  clrscr();
  printf("\n\n\n\n\n\t\t\t    MOHAMMED UJJAIN WALA\n\n\t\t\t\t  PRESENTS\n\n\n\t\t\t\tTIC-TAC-TOE\n\n\n\npress any to continue");
  getch();
  while(1)
  {
    clrscr();
    printf("\n\n\t\t\tTIC-TAC-TOE\n\n\n\t1.) START\t\t\tpress 1\n\n\t2.) EXIT\t\t\tpress 2");
    oip=getch()-48;
    switch(oip)
    {
      case 1:
      clrscr();
      design();
      break;
      case 2:
      exit(0);
      break;
      default:
      printf("Sorry wrong choice");
      break;
    }
    getch();
  }
}


EXPLANATION 

When I was making the program .There are two challenges for me ,first I have to make design of tic tac toe and then I have to give such condition in my tic tac toe game using which computer can find whether any one has won the game or not .This all we will discuss one by one .
So let us understand the program from starting and all the things become clear. At first I have declare three arrays z[10] , y[10] and d[10]. In z[] and y[] I will store the entries of player 1 and player 2 and with the help of d[] I will put symbols of player 1 and player 2 at appropriate position there is no need to declare two character arrays. I have declared them globally so that I can use it in any function. When game starts first design() function is called.
Design()
In this function all the three arrays z[] ,y[] and d[] are initialized with zero. Than toe() function is called. The toe() function is use to print upper part of the output as shown in figure which remains printed throughout the program

Condition If(i%2==1) simply give alternate chances to player 1 and player 2 to enter. Now here I have use getch() function to take input from user  so that user don’t have press enter key  after pressing block number.Instead of it we can also use scanf() .Now suppose player 1 enters  1 and ASCII code of 1 is 49 so a=49-48=1,therefore I have minus 48 .Now  again If(i%2==1) is checked if it becomes true than following condition is checked
      if(d[a])
                condition();
   above code fragment simply ensures that none of the player will enter that block number which is already filled. If any player tries to re enter any block than condition() function is called which prints “OOps!!! This block is already filled, please enter another block no.”.In our case player 1 enters first time therefore d[a]=0 and above condition becomes false.
Now d[a]=’x’ and z[1]=1, here it is important to note that I have taken z[a]=1 through which I can track the entries of the players by changing the value of that indices with a non zero value  and with the help of it I can match the pattern and can find whether any player has won the game or not  .After that print1() function is called .
Print1()
This function simply prints the following output .Here d[1]=’X’ and all the other array variables are equal to zero ,therefore a symbol appears at d[1]’s position and blank space at other variables position .After that check1() function is called.

Check1()
This was the most frustrating part of my program .Now what condition should I give in my TIC-TAC-TOE game on the basis of which computer can match the patterns and decide whether any player has won the game or not ,so here it is.
int check1()
{
if(z[3]==1&&z[1]==1&&z[2]==1)
return  1;
 else if(z[6]==1&&z[4]==1&&z[5]==1)
return  1;
else if(z[9]==1&&z[7]==1&&z[8]==1)
return  1;
else if(z[7]==1&&z[4]==1&&z[1]==1)
return  1;
else if(z[9]==1&&z[6]==1&&z[3]==1)
return  1;
else if(z[8]==1&&z[5]==1&&z[2]==1)
return  1;
else if(z[9]==1&&z[1]==1&&z[5]==1)
return  1;
else if(z[7]==1&&z[5]==1&&z[3]==1)
return 1;
return 0;
}
Thus when any of the condition become true than function will return 1 else it will return 0.At first I have use above condition .After that change the above code from the code given below .Both the codes are same but the following code is more compact .
int check(int c[10])
{
  int j;
  for(j=1;j<=3;j++)
    if(c[j]&&c[j+3]&&c[j+6])
      return 1;
  for(j=1;j<=7;j+=3)
    if(c[j]&&c[j+1]&&c[j+2])
      return 1;
  if(c[1]&&c[5]&&c[9] || c[3]&&c[5]&&c[7])
    return 1;
  return 0;
}
Now in our case check1() function will return zero and chance is given to second player .


OUTPUT 


Any questions regarding to program please write in comments.

RELATED POST:-

3 comments:

  1. what if you want to make it 5 games ??

    ReplyDelete
  2. how to make the number is avaiable to choose once by one player? for the example when the player 1 input number 1 and place 'X' sign in number 1. and when the another time player 2 choosee number 1 and give a sign 'O' to that place that player 1 place his sign at there....? can you give me some advice?
    please answer my question at this email limitesoode1190@gmail.com

    ReplyDelete