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. else if (chekTie(spaces) == 1) {
  33. cout << "droo\n";
  34. }
  35. computerMove(spaces, computer);
  36. drawBoard(spaces);
  37. if (chekWinner(spaces, computer, player) == 0)
  38. {
  39. running = 0;
  40. cout << "player 2 is winner ";
  41. break;
  42. }
  43. }
  44. return 0;
  45. }
  46. void drawBoard(char spaces[]) {
  47. cout << "\n";
  48. cout << " | | " << "\n";
  49. cout << " " << spaces[0] << " | " << spaces[1] << " | " << spaces[2] << "\n";
  50. cout << "_____|_____|_____" << "\n";
  51. cout << " | | " << "\n";
  52. cout << " " << spaces[3] << " | " << spaces[4] << " | " << spaces[5] << "\n";
  53. cout << "_____|_____|_____" << "\n";
  54. cout << " | | " << "\n";
  55. cout << " " << spaces[6] << " | " << spaces[7] << " | " << spaces[8] << "\n";
  56. cout << " | | " << "\n";
  57. cout << "\n";
  58.  
  59. }
  60. void playerMove(char* spaces, char player) {
  61. int num;
  62. do{
  63. cout << "Enter a spot , player 1 (1-9)\n";
  64. cin >> num;
  65. num--;
  66. if (spaces[num] == ' ') {
  67. spaces[num] = player;
  68. break;
  69. }
  70.  
  71. } while (!num > 0 || !num < 8);
  72. }
  73. void computerMove(char* spaces, char computer) {
  74. int num;
  75. do {
  76. cout << "The turn of player 2 : (0-9) \n";
  77. cin >> num;
  78. num--;
  79. if (spaces[num] == ' ') {
  80. spaces[num] = computer;
  81. break;
  82. }
  83. } while (num <= 8 || num >= 0);
  84. }
  85. bool chekWinner(char* spaces, char computer, char player) {
  86. if ((spaces[0] == spaces[1] && spaces[1] == spaces[2] && spaces[1] == player) ||
  87. (spaces[3] == spaces[4] && spaces[4] == spaces[5] && spaces[4] == player) ||
  88. (spaces[6] == spaces[7] && spaces[7] == spaces[8] && spaces[7] == player) ||
  89. (spaces[0] == spaces[3] && spaces[3] == spaces[6] && spaces[3] == player) ||
  90. (spaces[1] == spaces[4] && spaces[4] == spaces[7] && spaces[4] == player) ||
  91. (spaces[2] == spaces[5] && spaces[5] == spaces[8] && spaces[5] == player) ||
  92. (spaces[0] == spaces[4] && spaces[4] == spaces[8] && spaces[4] == player) ||
  93. (spaces[2] == spaces[4] && spaces[4] == spaces[6] && spaces[4] == player)) {
  94. return 1;
  95. }
  96. else if ((spaces[0] == spaces[1] && spaces[1] == spaces[2] && spaces[1] == computer) ||
  97. (spaces[3] == spaces[4] && spaces[4] == spaces[5] && spaces[4] == computer) ||
  98. (spaces[6] == spaces[7] && spaces[7] == spaces[8] && spaces[7] == computer) ||
  99. (spaces[0] == spaces[3] && spaces[3] == spaces[6] && spaces[3] == computer) ||
  100. (spaces[1] == spaces[4] && spaces[4] == spaces[7] && spaces[4] == computer) ||
  101. (spaces[2] == spaces[5] && spaces[5] == spaces[8] && spaces[5] == computer) ||
  102. (spaces[0] == spaces[4] && spaces[4] == spaces[8] && spaces[4] == computer) ||
  103. (spaces[2] == spaces[4] && spaces[4] == spaces[6] && spaces[4] == computer))
  104. {
  105.  
  106. return 0;
  107. }
  108.  
  109. }
  110. bool chekTie(char* spaces) {
  111. if (spaces[0] != ' ' && spaces[1] != ' ' && spaces[2] != ' ' &&
  112. spaces[3] != ' ' && spaces[4] != ' ' && spaces[5] != ' ' &&
  113. spaces[6] != ' ' && spaces[7] != ' ' && spaces[8] != ' ')
  114. {
  115. return 1;
  116. }
  117. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
welcome to Tic_Tac_Toe

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

Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)
Enter a spot , player 1 (1-9)

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

droo
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) 
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 2 is winner