fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. char a[9];
  6.  
  7. // Print the Tic Tac Toe field
  8. void print_the_field() {
  9. for (int i = 0; i < 9; i++) {
  10. if (a[i] == ' ') printf("%d", i + 1);
  11. else printf("%c", a[i]);
  12.  
  13. if (i % 3 == 2) {
  14. if (i != 8) printf("\n-+-+-\n");
  15. else printf("\n");
  16. } else {
  17. printf("|");
  18. }
  19. }
  20. printf("\n");
  21. }
  22.  
  23. // Check for win, loss, draw, or ongoing
  24. int check() {
  25. int wins[8][3] = {
  26. {0, 1, 2},
  27. {3, 4, 5},
  28. {6, 7, 8},
  29. {0, 3, 6},
  30. {1, 4, 7},
  31. {2, 5, 8},
  32. {0, 4, 8},
  33. {2, 4, 6}
  34. };
  35.  
  36. for (int i = 0; i < 8; i++) {
  37. if (a[wins[i][0]] != ' ' &&
  38. a[wins[i][0]] == a[wins[i][1]] &&
  39. a[wins[i][1]] == a[wins[i][2]]) {
  40. if(a[wins[i][0]] == 'X') return 1;
  41. else return 2;
  42. }
  43. }
  44.  
  45. for (int i = 0; i < 9; i++) {
  46. if (a[i] == ' ') return 0;
  47. }
  48.  
  49. return 3; // Draw
  50. }
  51.  
  52. int main() {
  53. srand(time(0));
  54.  
  55. for (int i = 0; i < 9; i++) a[i] = ' ';
  56.  
  57. int turn = 0;
  58.  
  59. while (1) {
  60.  
  61. if (turn == 0) {
  62. printf("Enter the number of the cell: ");
  63. int x;
  64. scanf("%d", &x);
  65. x--;
  66.  
  67. if (x < 0 || x >= 9 || a[x] != ' ') {
  68. printf("Invalid cell number. Try again.\n");
  69. system("pause");
  70. continue;
  71. }
  72.  
  73. a[x] = 'X';
  74.  
  75. int res = check();
  76. if (res != 0) {
  77. print_the_field();
  78. if (res == 1) printf("You Win!\n");
  79. else if (res == 2) printf("Computer Wins!\n");
  80. else printf("It's a Draw!\n");
  81. break;
  82. }
  83.  
  84. turn = 1;
  85. } else {
  86. int x;
  87. while (1) {
  88. x = rand() % 9;
  89. if (a[x] == ' ') {
  90. a[x] = 'O';
  91. break;
  92. }
  93. }
  94.  
  95. printf("Computer chose cell %d\n", x + 1);
  96.  
  97. int res = check();
  98. if (res != 0) {
  99. print_the_field();
  100. if (res == 1) printf("You Win!\n");
  101. else if (res == 2) printf("Computer Wins!\n");
  102. else printf("It's a Draw!\n");
  103. break;
  104. }
  105.  
  106. turn = 0;
  107. }
  108. system("cls");
  109. }
  110.  
  111. return 0;
  112. }
  113.  
Success #stdin #stdout #stderr 0.01s 5324KB
stdin
1
2
3
4
5
6
7
8
9
stdout
Enter the number of the cell: Computer chose cell 2
Enter the number of the cell: Invalid cell number. Try again.
Enter the number of the cell: Computer chose cell 4
Enter the number of the cell: Invalid cell number. Try again.
Enter the number of the cell: Computer chose cell 7
Enter the number of the cell: Computer chose cell 9
Enter the number of the cell: Invalid cell number. Try again.
Enter the number of the cell: X|O|X
-+-+-
O|X|X
-+-+-
O|X|O

It's a Draw!
stderr
sh: 1: cls: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: cls: not found
sh: 1: cls: not found
sh: 1: cls: not found
sh: 1: pause: not found