Advertisement
Md_hosen_zisad

TicTacToe

Oct 1st, 2019
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.09 KB | None | 0 0
  1. #include<stdio.h>
  2. int board [4] [4];
  3. void initailize()
  4. {
  5. int i,j;
  6. for(i=1;i<=3;i++)
  7. for(j=1;j<=3;j++)
  8. board [i][j]=2;
  9. printf("\nThe board is initialized");
  10. }
  11.  
  12. void print_board(){
  13. printf("\n\n");
  14. int i,j;
  15. for(i=1;i<=3;i++)
  16. {
  17. for(j=1;j<=3;j++)
  18. if(j!=3)
  19. {
  20. if (board [i][j]==2)
  21. printf(" |");
  22. else if( board [i][j]==3)
  23. printf(" X |");
  24. else
  25. printf(" 0 |");
  26. }
  27. else{
  28. if (board [i][j]==2)
  29. printf(" ");
  30. else if( board [i][j]==3)
  31. printf(" X ");
  32. else
  33. printf(" 0 ");
  34. }
  35. if(i!=3){
  36. printf("\n-----------\n");
  37. }
  38.  
  39. }
  40. printf("\n\n");
  41. }
  42.  
  43. int Make2(){
  44. if (board[2][2]==2)
  45. return 5;
  46. else if (board[1][2]==2)
  47. return 2;
  48. else if (board[2][1]==2)
  49. return 4;
  50. else if (board[2][3]==2)
  51. return 6;
  52. else
  53. return 8;
  54. }
  55. void Go(int player, int position){
  56.  
  57. if(position==1)
  58. board[1][1]=player;
  59. else if(position==2)
  60. board[1][2]=player;
  61. else if(position==3)
  62. board[1][3]=player;
  63. else if(position==4)
  64. board[2][1]=player;
  65. else if(position==5)
  66. board[2][2]=player;
  67. else if(position==6)
  68. board[2][3]=player;
  69. else if(position==7)
  70. board[3][1]=player;
  71. else if(position==8)
  72. board[3][2]=player;
  73. else if(position==9)
  74. board[3][3]=player;
  75. }
  76. int PossWinX()
  77. {
  78. if(board[1][1]*board[1][2]*board[1][3]==18)
  79. {
  80. if(board[1][1]==2)
  81. return 1;
  82. else if(board[1][2]==2)
  83. return 2;
  84. else
  85. return 3;
  86. }
  87. else if(board[2][1]*board[2][2]*board[2][3]==18)
  88. {
  89. if(board[2][1]==2)
  90. return 4;
  91. else if(board[2][2]==2)
  92. return 5;
  93. else
  94. return 6;
  95. }
  96. else if(board[3][1]*board[3][2]*board[3][3]==18)
  97. {
  98. if(board[3][1]==2)
  99. return 7;
  100. else if(board[3][2]==2)
  101. return 8;
  102. else
  103. return 9;
  104. }
  105. else if(board[1][1]*board[2][1]*board[3][1]==18)
  106. {
  107. if(board[1][1]==2)
  108. return 1;
  109. else if(board[2][1]==2)
  110. return 4;
  111. else
  112. return 7;
  113. }
  114. else if(board[1][2]*board[2][2]*board[3][2]==18)
  115. {
  116. if(board[1][2]==2)
  117. return 2;
  118. else if(board[2][2]==2)
  119. return 5;
  120. else
  121. return 8;
  122. }
  123. else if(board[1][3]*board[2][3]*board[3][3]==18)
  124. {
  125. if(board[1][3]==2)
  126. return 3;
  127. else if(board[2][3]==2)
  128. return 6;
  129. else
  130. return 9;
  131. }
  132. else if(board[1][1]*board[2][2]*board[3][3]==18)
  133. {
  134. if(board[1][1])
  135. return 1;
  136. else if(board[2][2]==2)
  137. return 5;
  138. else
  139. return 9;
  140. }
  141. else if(board[1][3]*board[2][2]*board[3][1]==18)
  142. {
  143. if(board[1][1]==2)
  144. return 3;
  145. else if(board[2][2]==2)
  146. return 5;
  147. else
  148. return 7;
  149. }
  150. else
  151. return 0;
  152.  
  153. }
  154.  
  155. int PossWinO()
  156. {
  157. if(board[1][1]*board[1][2]*board[1][3]==50)
  158. {
  159. if(board[1][1]==2)
  160. return 1;
  161. else if(board[1][2]==2)
  162. return 2;
  163. else
  164. return 3;
  165. }
  166. else if(board[2][1]*board[2][2]*board[2][3]==50)
  167. {
  168. if(board[2][1]==2)
  169. return 4;
  170. else if(board[2][2]==2)
  171. return 5;
  172. else
  173. return 6;
  174. }
  175. else if(board[3][1]*board[3][2]*board[3][3]==50)
  176. {
  177. if(board[3][1]==2)
  178. return 7;
  179. else if(board[3][2]==2)
  180. return 8;
  181. else
  182. return 9;
  183. }
  184. else if(board[1][1]*board[2][1]*board[3][1]==50)
  185. {
  186. if(board[1][1]==2)
  187. return 1;
  188. else if(board[2][1]==2)
  189. return 4;
  190. else
  191. return 7;
  192. }
  193. else if(board[1][2]*board[2][2]*board[3][2]==50)
  194. {
  195. if(board[1][2]==2)
  196. return 2;
  197. else if(board[2][2]==2)
  198. return 5;
  199. else
  200. return 8;
  201. }
  202. else if(board[1][3]*board[2][3]*board[3][3]==50)
  203. {
  204. if(board[1][3]==2)
  205. return 3;
  206. else if(board[2][3]==2)
  207. return 6;
  208. else
  209. return 9;
  210. }
  211. else if(board[1][1]*board[2][2]*board[3][3]==50)
  212. {
  213. if(board[1][1]==2)
  214. return 1;
  215. else if(board[2][2]==2)
  216. return 5;
  217. else
  218. return 9;
  219. }
  220. else if(board[1][3]*board[2][2]*board[3][1]==50)
  221. {
  222. if(board[1][3]==2)
  223. return 3;
  224. else if(board[2][2]==2)
  225. return 5;
  226. else
  227. return 7;
  228. }
  229. else
  230. return 0;
  231.  
  232. }
  233.  
  234. int check()
  235. {
  236. if(board[1][1]*board[1][2]*board[1][3]==125)
  237. return 5;
  238. else if(board[2][1]*board[2][2]*board[2][3]==125)
  239. return 5;
  240. else if(board[3][1]*board[3][2]*board[3][3]==125)
  241. return 5;
  242. else if(board[1][1]*board[2][1]*board[3][1]==125)
  243. return 5;
  244. else if(board[1][2]*board[2][2]*board[3][2]==125)
  245. return 5;
  246. else if(board[1][3]*board[2][3]*board[3][3]==125)
  247. return 5;
  248. else if(board[1][1]*board[2][2]*board[3][3]==125)
  249. return 5;
  250. else if(board[1][3]*board[2][2]*board[3][1]==125)
  251. return 5;
  252. else if(board[1][1]*board[1][2]*board[1][3]==27)
  253. return 3;
  254. else if(board[2][1]*board[2][2]*board[2][3]==27)
  255. return 3;
  256. else if(board[3][1]*board[3][2]*board[3][3]==27)
  257. return 3;
  258. else if(board[1][1]*board[2][1]*board[3][1]==27)
  259. return 3;
  260. else if(board[1][2]*board[2][2]*board[3][2]==27)
  261. return 3;
  262. else if(board[1][3]*board[2][3]*board[3][3]==27)
  263. return 3;
  264. else if(board[1][1]*board[2][2]*board[3][3]==27)
  265. return 3;
  266. else if(board[1][3]*board[2][2]*board[3][1]==27)
  267. return 3;
  268. else
  269. return 0;
  270.  
  271.  
  272. }
  273. int anywhere(){
  274. int i , j;
  275. for(i=1; i<=3; i++)
  276. for(j=1;j<=3;j++)
  277. if(board[i][j]==2)
  278. {
  279. return (3*(i-1)+j);
  280. break;
  281. break;
  282.  
  283. }
  284.  
  285. }
  286.  
  287.  
  288. int check_input(int position)
  289. {
  290. if(position==1)
  291. if(board[1][1]==2)
  292. return 0;
  293. else
  294. return 1;
  295.  
  296. else if(position==2)
  297. if(board[1][2]==2)
  298. return 0;
  299. else
  300. return 1;
  301.  
  302. else if(position==3)
  303. if(board[1][3]==2)
  304. return 0;
  305. else
  306. return 1;
  307.  
  308. else if(position==4)
  309. if(board[2][1]==2)
  310. return 0;
  311. else
  312. return 1;
  313. else if(position==5)
  314. if(board[2][2]==2)
  315. return 0;
  316. else
  317. return 1;
  318. else if(position==6)
  319. if(board[2][3]==2)
  320. return 0;
  321. else
  322. return 1;
  323. else if(position==7)
  324. if(board[3][1]==2)
  325. return 0;
  326. else
  327. return 1;
  328. else if(position==8)
  329. if(board[3][2]==2)
  330. return 0;
  331. else
  332. return 1;
  333. else if(position==9)
  334. if(board[3][3]==2)
  335. return 0;
  336. else
  337. return 1;
  338. }
  339. void Play_X(){
  340. int player,position,status;
  341. initailize();
  342. //Turn1
  343. print_board();
  344. printf("\nEnter the position of your move :::");
  345. scanf("%d",&position);
  346. Go(3,position);
  347. printf("\nAfter turn 1 the board is as below :::");
  348. print_board();
  349. //Turn2
  350. if(board[2][2]==2)
  351. Go(5,5);
  352. else
  353. Go(5,1);
  354. printf("\nAfter turn 2 the board is as below ::: \n");
  355. print_board();
  356. //Turn 3
  357. T3:
  358. {
  359.  
  360. printf("\nEnter the position of your move :::");
  361. scanf("%d",&position);
  362. if(check_input(position)==1)
  363. {
  364. printf("\nPosition is previously taken... Enter Another position.\n");
  365. goto T3;
  366. }
  367. }
  368. Go(3,position);
  369. printf("\nAfter turn 3 the board is as below ::: \n");
  370. print_board();
  371. //Turn 4
  372. if(PossWinX()!=0)
  373. Go(5,PossWinX());
  374. else
  375. Go(5,Make2());
  376. printf("\nAfter turn 4 the board is as below :::\n");
  377. print_board();
  378. //Turn 5
  379. T5:
  380. {
  381.  
  382. printf("\nEnter the position of your move :::");
  383. scanf("%d",&position);
  384. if(check_input(position)==1)
  385. {
  386. printf("\nPosition is previously taken... Enter Another position.\n");
  387. goto T5;
  388. }
  389. }
  390. Go(3,position);
  391. printf("\nAfter turn 5 the board is as below :::\n ");
  392. print_board();
  393. if(check()!=0)
  394. {
  395. status=check();
  396. goto result;
  397. }
  398. //Turn 6
  399. if(PossWinO()!=0)
  400. Go(5,PossWinO());
  401. else if(PossWinX()!=0)
  402. Go(5,PossWinX());
  403. else
  404. Go(5,Make2());
  405. printf("\nAfter turn 6 the board is as below ::: \n");
  406. print_board();
  407. if(check()!=0)
  408. {
  409. status=check();
  410. goto result;
  411. }
  412. //Turn 7
  413.  
  414.  
  415. T7:
  416. {
  417.  
  418. printf("\nEnter the position of your move :::");
  419. scanf("%d",&position);
  420. if(check_input(position)==1)
  421. {
  422. printf("\nPosition is previously taken... Enter Another position.\n");
  423. goto T7;
  424. }}
  425.  
  426. Go(3,position);
  427. printf("\nAfter turn 7 the board is as below :::\n ");
  428. print_board();
  429. if(check()!=0)
  430. {
  431. status=check();
  432. goto result;
  433. }
  434. //Turn 8
  435. if(PossWinO()!=0)
  436. Go(5,PossWinO());
  437. else if(PossWinX()!=0)
  438. Go(5,PossWinX());
  439. else
  440. Go(5,anywhere());
  441. printf("\nAfter turn 8 the board is as below :::\n");
  442. print_board();
  443. if(check()!=0)
  444. {
  445. status=check();
  446. goto result;
  447. }
  448. //Turn 9
  449. T9:
  450. {
  451.  
  452. printf("\nEnter the position of your move :::");
  453. scanf("%d",&position);
  454. if(check_input(position)==1)
  455. {
  456. printf("\nPosition is previously taken... Enter Another position.\n");
  457. goto T9;
  458. }
  459. }
  460.  
  461. Go(3,position);
  462. printf("\nAfter Turn 9 the board is as below :::\n");
  463. print_board();
  464. if(check()!=0)
  465. {
  466. status=check();
  467. goto result;
  468. }
  469. result :
  470. if(status==3)
  471. printf("\nHurrey!!!! Player X(Human) Wins the game !!!!!\n");
  472. else if(status==5)
  473. printf("\nAaaahhh!!! Player O(Computer) Wins the Game !!!!!\n");
  474. else
  475. printf("\nThe game is Drawn !!!!!\n");
  476. }
  477.  
  478.  
  479. int main(){
  480.  
  481.  
  482.  
  483. int choice;
  484. while(1){
  485. printf("\nEnter 3 for play as X");
  486. printf("\nEnter 5 for play as O");
  487. printf("\nEnter 100 for exit");
  488. printf("\nEnter your choice :::");
  489. scanf("%d",&choice);
  490.  
  491. if(choice==100)
  492. break;
  493. else if(choice==3)
  494. Play_X();
  495. else
  496. printf("\n\n your choice is wrong!!\n");
  497.  
  498.  
  499.  
  500. }
  501.  
  502. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement