fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
  5. int checkwin();
  6. void board();
  7.  
  8. int main()
  9. {
  10. int player = 1,i,choice;
  11. char mark;
  12. do
  13. {
  14. board();
  15. player=(player%2)?1:2;
  16. cout << "Player " << player << ", enter a number: ";
  17. cin >> choice;
  18. mark=(player == 1) ? 'X' : 'O';
  19. if (choice == 1 && square[1] == '1')
  20. square[1] = mark;
  21. else if (choice == 2 && square[2] == '2')
  22. square[2] = mark;
  23. else if (choice == 3 && square[3] == '3')
  24. square[3] = mark;
  25. else if (choice == 4 && square[4] == '4')
  26. square[4] = mark;
  27. else if (choice == 5 && square[5] == '5')
  28. square[5] = mark;
  29. else if (choice == 6 && square[6] == '6')
  30. square[6] = mark;
  31. else if (choice == 7 && square[7] == '7')
  32. square[7] = mark;
  33. else if (choice == 8 && square[8] == '8')
  34. square[8] = mark;
  35. else if (choice == 9 && square[9] == '9')
  36. square[9] = mark;
  37. else
  38. {
  39. cout<<"Invalid move ";
  40. player--;
  41. cin.ignore();
  42. cin.get();
  43. }
  44. i=checkwin();
  45. player++;
  46. }while(i==-1);
  47. board();
  48. if(i==1)
  49. cout<<"==>\aPlayer "<<--player<<" win ";
  50. else
  51. cout<<"==>\aGame draw";
  52. cin.ignore();
  53. cin.get();
  54. return 0;
  55. }
  56. /*********************************************
  57. FUNCTION TO RETURN GAME STATUS
  58. 1 FOR GAME IS OVER WITH RESULT
  59. -1 FOR GAME IS IN PROGRESS
  60. O GAME IS OVER AND NO RESULT
  61. **********************************************/
  62.  
  63. int checkwin()
  64. {
  65. if (square[1] == square[2] && square[2] == square[3])
  66. return 1;
  67. else if (square[4] == square[5] && square[5] == square[6])
  68. return 1;
  69. else if (square[7] == square[8] && square[8] == square[9])
  70. return 1;
  71. else if (square[1] == square[4] && square[4] == square[7])
  72. return 1;
  73. else if (square[2] == square[5] && square[5] == square[8])
  74. return 1;
  75. else if (square[3] == square[6] && square[6] == square[9])
  76. return 1;
  77. else if (square[1] == square[5] && square[5] == square[9])
  78. return 1;
  79. else if (square[3] == square[5] && square[5] == square[7])
  80. return 1;
  81. else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')
  82. return 0;
  83. else
  84. return -1;
  85. }
  86.  
  87.  
  88. /*******************************************************************
  89.   FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
  90. ********************************************************************/
  91.  
  92.  
  93. void board()
  94. {
  95. system("cls");
  96. cout << "\n\n\tTic Tac Toe\n\n";
  97. cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
  98. cout << endl;
  99. cout << " | | " << endl;
  100. cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
  101. cout << "_____|_____|_____" << endl;
  102. cout << " | | " << endl;
  103. cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
  104. cout << "_____|_____|_____" << endl;
  105. cout << " | | " << endl;
  106. cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
  107. cout << " | | " << endl << endl;
  108. }
  109.  
  110. /*******************************************************************
  111. END OF PROJECT
  112. ********************************************************************/
Success #stdin #stdout 0.02s 2280KB
stdin
Standard input is empty
stdout
Standard output is empty