Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- void run();
- void display(char sym[9]);
- int check(char sym[9],char ch,int count);
- struct myData inputValue(char sym[9],int count);
- struct myData
- {
- int i;
- char ch;
- }inputValue();
- int main()
- {
- char cont;
- while(1)
- {
- run();
- printf("\n want to continue press 1:\n");
- scanf("%s",&cont);
- if(cont=='1')
- {
- system("clear");
- }
- else
- break;
- }
- return 0;
- }
- void run()
- {
- int count=0;
- struct myData info;
- char symbol[9]={'1','2','3','4','5','6','7','8','9'};
- display(symbol);
- Again:
- info=inputValue(symbol,count);
- symbol[info.i]=info.ch;
- system("clear");
- display(symbol);
- if(check(symbol,info.ch,count)==1);
- else
- {
- count++;
- goto Again;
- }
- }
- int check(char sym[9],char ch,int count)
- {
- int i;
- for(i=0;i<=6;i+=3)//for row
- if(sym[i]==ch && sym[i+1]==ch && sym[i+2]==ch)
- {printf("\nwinner is : %c",ch);return 1;}
- for(i=0;i<3;i++)//for column
- if(sym[i]==ch && sym[i+3]==ch && sym[i+6]==ch)
- {printf("\nwinner is : %c",ch);return 1;}
- if(sym[0]==ch && sym[4]==ch && sym[8]==ch)
- {printf("\nwinner is : %c",ch);return 1;}
- else if(sym[2]==ch && sym[4]==ch && sym[6]==ch)
- {printf("\nwinner :%c",ch);return 1;}
- else if(count==8)
- {printf("\ngame is drawn ");return 1;}
- else return 0;
- }
- struct myData inputValue(char sym[9],int count)
- {
- char value;
- int i;
- struct myData info;
- while(1)
- {
- if(count%2==0)
- {printf("\nenter your choice X:");}
- else
- {printf("\nenter your choice 0: ");}
- scanf("%s",&value);
- for(i=0;i<9;i++)
- {
- if(value==sym[i])
- {
- info.i=i;
- if(count%2==0)
- info.ch='X';
- else
- info.ch='0';
- break;
- }
- else
- {
- info.i=-1;
- info.ch=' ';
- }
- }
- if(info.i==-1)
- {
- printf("\ninput not valid");
- break;
- }
- return info;
- }
- }
- void display(char sym[9])
- {
- printf("\t\t\t T i c T a c T o e\n");
- printf("\nplayer 1 symbol :X");
- printf("\nplayer 2 symbol :0\n");
- printf("\n\t\t\t %c | %c | %c ",sym[0],sym[1],sym[2]);
- printf("\n\t\t\t--------------");
- printf("\n\t\t\t %c | %c | %c ",sym[3],sym[4],sym[5]);
- printf("\n\t\t\t--------------");
- printf("\n\t\t\t %c | %c | %c ",sym[6],sym[7],sym[8]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement