Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. char bd[3][3] = { { '1','2','3' },{ '4','5','6' },{ '7','8','9' } };
  5. static int temp1 = 0, temp2 = 0;
  6. void board(char bd[][3],int size)
  7. {
  8. for (int i = 0; i < 3; i++)
  9. {
  10. for (int j = 0; j < 3; j++)
  11. {
  12. printf("%c ", bd[i][j]);
  13. if (j < 2)
  14. printf(" | ");
  15. }
  16. printf("\n");
  17. if (i < 2)
  18. printf("--------------------\n");
  19. }
  20. }
  21. int player(int a,int c, char bd[][3])
  22. {
  23. if (bd[a][c] == 'x' || bd[a][c] == 'o')
  24. {
  25. printf("Incorrect move...Try again\n");
  26. scanf_s("%d %d", &a, &c);
  27. return player(a, c, bd);
  28. }
  29. else
  30. bd[a][c] = 'x';
  31. return 0;
  32. }
  33. void computer(int a,int c, char bd[][3])
  34. {
  35. srand(time(NULL));
  36. if (bd[a][c] == 'x' || bd[a][c] == 'o')
  37. {
  38. while (bd[a][c] == 'x' || bd[a][c] == 'o')
  39. {
  40. a = rand() % 3;
  41. c = rand() % 3;
  42.  
  43. }if (bd[a][c] != 'x' || bd[a][c] != 'o')
  44. bd[a][c] = 'o';
  45. }
  46. else
  47. bd[a][c] = 'o';
  48. }
  49. int result(char bd[][3])
  50. {
  51. int w = 0;
  52. // Diagnoal
  53. if (bd[0][0] == bd[1][1] && bd[0][0] == bd[2][2])
  54. w++;
  55. if (bd[0][2] == bd[1][1] && bd[0][2] == bd[2][0])
  56. w++;
  57. //Lines
  58. for (int line = 0; line <= 2; line++)
  59. {
  60. if (bd[line][0] == bd[line][1] && bd[line][0] == bd[line][2])
  61. w++;
  62. if (bd[0][line] == bd[1][line] && bd[0][line] == bd[2][line])
  63. w++;
  64. }
  65. return w;
  66. }
  67. void final(int temp1, int temp2)
  68. {
  69. printf("%d wins and %d draws\n", temp1, temp2);
  70. }
  71. void moves(char bd[][3],char *name)
  72. {
  73.  
  74. int w = 3, s = 3;
  75. for (int i = 0; i<3; i++)
  76. {
  77. for (int j = 2; j >= 0; j--)
  78. {
  79. if ('x' == bd[i][i] || 'x' == bd[i][j])
  80. w--;
  81. }
  82.  
  83. }
  84. for (int line = 0; line < 3; line++)
  85. {
  86. for (int k = 0; k < 3; k++)
  87. if ('x' == bd[k][line] || 'x' == bd[line][k])
  88. s--;
  89. }
  90. if (w == 0 || s == 0)
  91. {
  92. printf("Congrats %s !\n", name);
  93. temp1++;
  94.  
  95. }
  96. else
  97. {
  98. printf("It's a draw %s !\n", name);
  99. temp2++;
  100. }
  101.  
  102. }
  103. void main()
  104. {
  105. char name[26];
  106. printf("What's your name?\n");
  107. scanf_s("%s", name, 26);
  108. char *p = name;
  109. printf("Ok %s you will play a game of Tic-Tac-Toe with the computer.\n", name);
  110. board(bd, 3);
  111. int a, c;
  112. do
  113. {
  114. printf("Choose the coordinates in which you like to input X:\n");
  115. scanf_s("%d%d", &a, &c);
  116. player(a, c, bd);
  117. computer(a, c, bd);
  118. board(bd, 3);
  119.  
  120. } while (result(bd) <= 0);
  121. printf("The game has ended!\n");
  122. moves(bd,name);
  123. printf("Would you like to play again ?\nIf yes enter 'y', else enter 'n'\n");
  124.  
  125. char answer;
  126. scanf_s(" %c", &answer);
  127. int m = 1;
  128. if (answer == 'y')
  129. {
  130. while (answer == 'y')
  131. {
  132. char bd[3][3] = { { '1','2','3' },{ '4','5','6' },{ '7','8','9' } };
  133. board(bd,3);
  134. m++;
  135. do
  136. {
  137. printf("Choose the coordinates in which you like to input X:\n");
  138. scanf_s("%d%d", &a, &c);
  139. player(a, c, bd);
  140. computer(a, c, bd);
  141. board(bd, 3);
  142.  
  143. } while (result(bd) <= 0);
  144. printf("The game has ended!\n");
  145. moves(bd,name);
  146. printf("Would you like to play again ?\nIf yes enter 'y', else enter 'n'\n");
  147. scanf_s(" %c", &answer);
  148. }
  149. if (answer == 'n')
  150. {
  151. printf("Thank you %s for playing\n", name);
  152. printf("You played %d times\n", m);
  153. final(temp1,temp2);
  154. }
  155. }
  156. else
  157. {
  158. printf("Thank you %s for playing\n", name);
  159. printf("You played %d times\n", m);
  160.  
  161. final(temp1,temp2);
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement