Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. void run();
  5. void display(char sym[9]);
  6. int check(char sym[9],char ch,int count);
  7. struct myData inputValue(char sym[9],int count);
  8.  
  9. struct myData
  10. {
  11. int i;
  12. char ch;
  13. }inputValue();
  14.  
  15. int main()
  16. {
  17. char cont;
  18. while(1)
  19. {
  20. run();
  21. printf("\n want to continue press 1:\n");
  22. scanf("%s",&cont);
  23. if(cont=='1')
  24. {
  25. system("clear");
  26. }
  27.  
  28. else
  29. break;
  30.  
  31. }
  32. return 0;
  33. }
  34.  
  35.  
  36. void run()
  37. {
  38.  
  39. int count=0;
  40. struct myData info;
  41. char symbol[9]={'1','2','3','4','5','6','7','8','9'};
  42. display(symbol);
  43.  
  44. Again:
  45.  
  46. info=inputValue(symbol,count);
  47. symbol[info.i]=info.ch;
  48. system("clear");
  49. display(symbol);
  50.  
  51. if(check(symbol,info.ch,count)==1);
  52.  
  53. else
  54. {
  55. count++;
  56. goto Again;
  57. }
  58.  
  59. }
  60.  
  61. int check(char sym[9],char ch,int count)
  62. {
  63. int i;
  64.  
  65. for(i=0;i<=6;i+=3)//for row
  66. if(sym[i]==ch && sym[i+1]==ch && sym[i+2]==ch)
  67. {printf("\nwinner is : %c",ch);return 1;}
  68.  
  69. for(i=0;i<3;i++)//for column
  70. if(sym[i]==ch && sym[i+3]==ch && sym[i+6]==ch)
  71. {printf("\nwinner is : %c",ch);return 1;}
  72.  
  73. if(sym[0]==ch && sym[4]==ch && sym[8]==ch)
  74. {printf("\nwinner is : %c",ch);return 1;}
  75.  
  76. else if(sym[2]==ch && sym[4]==ch && sym[6]==ch)
  77. {printf("\nwinner :%c",ch);return 1;}
  78.  
  79. else if(count==8)
  80. {printf("\ngame is drawn ");return 1;}
  81.  
  82. else return 0;
  83.  
  84. }
  85.  
  86.  
  87. struct myData inputValue(char sym[9],int count)
  88. {
  89. char value;
  90. int i;
  91. struct myData info;
  92.  
  93. while(1)
  94. {
  95.  
  96. if(count%2==0)
  97. {printf("\nenter your choice X:");}
  98. else
  99. {printf("\nenter your choice 0: ");}
  100.  
  101. scanf("%s",&value);
  102. for(i=0;i<9;i++)
  103. {
  104. if(value==sym[i])
  105. {
  106. info.i=i;
  107. if(count%2==0)
  108. info.ch='X';
  109. else
  110. info.ch='0';
  111. break;
  112. }
  113.  
  114. else
  115. {
  116. info.i=-1;
  117. info.ch=' ';
  118. }
  119.  
  120. }
  121. if(info.i==-1)
  122. {
  123. printf("\ninput not valid");
  124. break;
  125.  
  126. }
  127. return info;
  128. }
  129. }
  130.  
  131. void display(char sym[9])
  132. {
  133. printf("\t\t\t T i c T a c T o e\n");
  134. printf("\nplayer 1 symbol :X");
  135. printf("\nplayer 2 symbol :0\n");
  136. printf("\n\t\t\t %c | %c | %c ",sym[0],sym[1],sym[2]);
  137. printf("\n\t\t\t--------------");
  138. printf("\n\t\t\t %c | %c | %c ",sym[3],sym[4],sym[5]);
  139. printf("\n\t\t\t--------------");
  140. printf("\n\t\t\t %c | %c | %c ",sym[6],sym[7],sym[8]);
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement