fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ctime>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <string>
  7. #define ll long long
  8. using namespace std;
  9. void drawBoard(char* spaces);
  10. void playerMove(char* spaces, char player);
  11. void computerMove(char* spaces, char computer);
  12. bool chekWinner(char* spaces, char computer, char player);
  13. bool chekTie(char* spaces);
  14. int main() {
  15. cout << "welcome to Tic_Tac_Toe" << "\n";
  16. char spaces[9] =
  17. { ' ',' ',' ',
  18. ' ',' ',' ',
  19. ' ',' ',' '};
  20. char player = 'X';
  21. char computer = 'O';
  22. bool running = 1;
  23. drawBoard(spaces);
  24. while (running == 1) {
  25. playerMove(spaces, player);
  26. drawBoard(spaces);
  27. if (chekWinner(spaces, computer, player) == 1) {
  28. running = 0;
  29. cout << "player 1 is winner ";
  30. break;
  31. }
  32. computerMove(spaces, computer);
  33. drawBoard(spaces);
  34. if (chekWinner(spaces, computer, player) == 0)
  35. {
  36. running = 0;
  37. cout << "player 1 is winner ";
  38. break;
  39. }
  40. }
  41. return 0;
  42. }
  43. void drawBoard(char spaces[]) {
  44. cout << "\n";
  45. cout << " | | " << "\n";
  46. cout << " " << spaces[0] << " | " << spaces[1] << " | " << spaces[2] << "\n";
  47. cout << "_____|_____|_____" << "\n";
  48. cout << " | | " << "\n";
  49. cout << " " << spaces[3] << " | " << spaces[4] << " | " << spaces[5] << "\n";
  50. cout << "_____|_____|_____" << "\n";
  51. cout << " | | " << "\n";
  52. cout << " " << spaces[6] << " | " << spaces[7] << " | " << spaces[8] << "\n";
  53. cout << " | | " << "\n";
  54. cout << "\n";
  55.  
  56. }
  57. void playerMove(char* spaces, char player) {
  58. int num;
  59. do{
  60. cout << "Enter a spot to place a marker (1-9)\n";
  61. cin >> num;
  62. num--;
  63. if (spaces[num] == ' ') {
  64. spaces[num] = player;
  65. break;
  66. }
  67.  
  68. } while (!num > 0 || !num < 8);
  69. }
  70. void computerMove(char* spaces, char computer) {
  71. int num;
  72. do {
  73. cout << "The turn of player 2 : (0-9) \n";
  74. cin >> num;
  75. num--;
  76. if (spaces[num] == ' ') {
  77. spaces[num] = computer;
  78. break;
  79. }
  80. } while (num <= 8 || num >= 0);
  81. }
  82. bool chekWinner(char* spaces, char computer, char player) {
  83. if ((spaces[0] == spaces[1] && spaces[1] == spaces[2] && spaces[1] == player) ||
  84. (spaces[3] == spaces[4] && spaces[4] == spaces[5] && spaces[4] == player) ||
  85. (spaces[6] == spaces[7] && spaces[7] == spaces[8] && spaces[7] == player) ||
  86. (spaces[0] == spaces[3] && spaces[3] == spaces[6] && spaces[3] == player) ||
  87. (spaces[1] == spaces[4] && spaces[4] == spaces[7] && spaces[4] == player) ||
  88. (spaces[2] == spaces[5] && spaces[5] == spaces[8] && spaces[5] == player) ||
  89. (spaces[0] == spaces[4] && spaces[4] == spaces[8] && spaces[4] == player) ||
  90. (spaces[2] == spaces[4] && spaces[4] == spaces[6] && spaces[4] == player)) {
  91. return 1;
  92. }
  93. else if ((spaces[0] == spaces[1] && spaces[1] == spaces[2] && spaces[1] == computer) ||
  94. (spaces[3] == spaces[4] && spaces[4] == spaces[5] && spaces[4] == computer) ||
  95. (spaces[6] == spaces[7] && spaces[7] == spaces[8] && spaces[7] == computer) ||
  96. (spaces[0] == spaces[3] && spaces[3] == spaces[6] && spaces[3] == computer) ||
  97. (spaces[1] == spaces[4] && spaces[4] == spaces[7] && spaces[4] == computer) ||
  98. (spaces[2] == spaces[5] && spaces[5] == spaces[8] && spaces[5] == computer) ||
  99. (spaces[0] == spaces[4] && spaces[4] == spaces[8] && spaces[4] == computer) ||
  100. (spaces[2] == spaces[4] && spaces[4] == spaces[6] && spaces[4] == computer))
  101. {
  102. return 0;
  103. }
  104.  
  105. }
Success #stdin #stdout 0s 5248KB
stdin
Standard input is empty
stdout
welcome to Tic_Tac_Toe

     |     |     
     |     |   
_____|_____|_____
     |     |     
     |     |   
_____|_____|_____
     |     |     
     |     |   
     |     |     

Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)
Enter a spot to place a marker (1-9)

     |     |     
     |     |   
_____|_____|_____
     |     |     
     |     |   
_____|_____|_____
     |     |     
     |     |   
     |     |     

The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 
The turn of player 2 : (0-9) 

     |     |     
     |     |   
_____|_____|_____
     |     |     
     |     |   
_____|_____|_____
     |     |     
     |     |   
     |     |     

player 1 is winner