Advertisement
Md_hosen_zisad

Untitled

Sep 24th, 2019
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #include<stdio.h>
  2. int board[4][4];
  3.  
  4. void initialize()
  5. {
  6. int i,j;
  7. for(i=1;i<=3;i++)
  8. for(j=1;j<=3;j++)
  9. board[i][j]=2;
  10. printf("\nThe board is initialized.\n");
  11. }
  12.  
  13. void print_board()
  14. {
  15. printf("\n\n");
  16. int i,j;
  17. for(i=1;i<=3;i++)
  18. {
  19. for(j=1;j<=3;j++)
  20. if(j!=3)
  21. {
  22. if(board[i][j] == 2)
  23. printf(" |");
  24. else if (board[i][j]==3)
  25. printf(" X |");
  26. else
  27. printf(" O |");
  28. }
  29. else
  30. {
  31. if(board[i][j] == 2)
  32. printf(" ");
  33. else if (board[i][j]==3)
  34. printf(" X ");
  35. else
  36. printf(" O ");
  37. }
  38. if(i!=3)
  39. {
  40. printf("\n-----------\n");
  41. }
  42. }
  43. printf("\n\n");
  44. }
  45.  
  46. int Make2()
  47. {
  48. if(board[2][2]==2)
  49. return 5;
  50. else if(board[1][2]==2)
  51. return 2;
  52. else if(board[2][1]==2)
  53. return 4;
  54. else if(board[2][3]==2)
  55. return 6;
  56. else
  57. return 8;
  58. }
  59.  
  60. void Go()
  61. {
  62. int player,position;
  63. printf("\nEnter 3 for player X");
  64. printf(" Enter 5 for player O ::: ");
  65. scanf("%d",&player);
  66. printf("\nEnter the position ::: ");
  67. scanf("%d",&position);
  68. if(position==1)
  69. board[1][1]=player;
  70. else if(position==2)
  71. board[1][2]=player;
  72. else if(position==3)
  73. board[1][3]=player;
  74. else if(position==4)
  75. board[2][1]=player;
  76. else if(position==5)
  77. board[2][2]=player;
  78. else if(position==6)
  79. board[2][3]=player;
  80. else if(position==7)
  81. board[3][1]=player;
  82. else if(position==8)
  83. board[3][2]=player;
  84. else if(position==9)
  85. board[3][3]=player;
  86. }
  87.  
  88. int PossWinX()
  89. {
  90. if(board[1][1]*board[1][2]*board[1][3]==18)
  91. {
  92. if(board[1][1]==2)
  93. return 1;
  94. else if(board[1][2]==2)
  95. return 2;
  96. else
  97. return 3;
  98. }
  99. else if(board[2][1]*board[2][2]*board[2][3]==18)
  100. {
  101. if(board[2][1]==2)
  102. return 4;
  103. else if(board[2][2]==2)
  104. return 5;
  105. else
  106. return 6;
  107. }
  108. else if(board[3][1]*board[3][2]*board[3][3]==18)
  109. {
  110. if(board[3][1]==2)
  111. return 7;
  112. else if(board[3][2]==2)
  113. return 8;
  114. else
  115. return 9;
  116. }
  117. else if(board[1][1]*board[2][1]*board[3][1]==18)
  118. {
  119. if(board[1][1]==2)
  120. return 1;
  121. else if(board[2][1]==2)
  122. return 4;
  123. else
  124. return 7;
  125. }
  126. else if(board[1][2]*board[2][2]*board[3][2]==18)
  127. {
  128. if(board[1][2]==2)
  129. return 2;
  130. else if(board[2][2]==2)
  131. return 5;
  132. else
  133. return 8;
  134. }
  135. else if(board[1][3]*board[2][3]*board[3][3]==18)
  136. {
  137. if(board[1][3]==2)
  138. return 3;
  139. else if(board[2][3]==2)
  140. return 6;
  141. else
  142. return 9;
  143. }
  144. else if(board[1][1]*board[2][2]*board[3][3]==18)
  145. {
  146. if(board[1][1]==2)
  147. return 1;
  148. else if(board[2][2]==2)
  149. return 5;
  150. else
  151. return 9;
  152. }
  153. else if(board[1][3]*board[2][2]*board[3][1]==18)
  154. {
  155. if(board[1][3]==2)
  156. return 3;
  157. else if(board[2][2]==2)
  158. return 5;
  159. else
  160. return 7;
  161. }
  162. else
  163. return 0;
  164. }
  165.  
  166. main()
  167. {
  168. int choice,position;
  169. while(1)
  170. {
  171. printf("\nEnter 1 for initialization");
  172. printf("\nEnter 2 for Make2");
  173. printf("\nEnter 3 for Go");
  174. printf("\nEnter 4 for PossWinX");
  175. printf("\nEnter 10 for Print Board");
  176. printf("\nEnter 100 for Exit");
  177. printf("\nEnter your Choice ::: ");
  178. scanf("%d",&choice);
  179. if(choice==100)
  180. break;
  181. switch(choice)
  182. {
  183. case 1: initialize(); break;
  184. case 2: position=Make2();
  185. printf("\nMake2 Returns %d\n",position);
  186. break;
  187. case 3: Go();break;
  188. case 4: position=PossWinX();
  189. printf("\nPossWinX() Returns %d\n",position);
  190. break;
  191. case 10: print_board();break;
  192. default: printf("\nYour choice is wrong !!!\n");
  193. }
  194. }
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement