Advertisement
EconomicSerg

Tic Tac Toe

Feb 23rd, 2021
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
  5.  
  6. int checkwin();
  7. void board();
  8.  
  9. int main()
  10. {
  11.     int player = 1,i,choice;
  12.  
  13.     char mark;
  14.     do
  15.     {
  16.         board();
  17.         player=(player%2)?1:2;
  18.  
  19.         cout << "Player " << player << ", enter a number:  ";
  20.         cin >> choice;
  21.  
  22.         mark=(player == 1) ? 'X' : 'O';
  23.  
  24.         if (choice == 1 && square[1] == '1')
  25.  
  26.             square[1] = mark;
  27.         else if (choice == 2 && square[2] == '2')
  28.  
  29.             square[2] = mark;
  30.         else if (choice == 3 && square[3] == '3')
  31.  
  32.             square[3] = mark;
  33.         else if (choice == 4 && square[4] == '4')
  34.  
  35.             square[4] = mark;
  36.         else if (choice == 5 && square[5] == '5')
  37.  
  38.             square[5] = mark;
  39.         else if (choice == 6 && square[6] == '6')
  40.  
  41.             square[6] = mark;
  42.         else if (choice == 7 && square[7] == '7')
  43.  
  44.             square[7] = mark;
  45.         else if (choice == 8 && square[8] == '8')
  46.  
  47.             square[8] = mark;
  48.         else if (choice == 9 && square[9] == '9')
  49.  
  50.             square[9] = mark;
  51.         else
  52.         {
  53.             cout<<"Invalid move ";
  54.  
  55.             player--;
  56.             cin.ignore();
  57.             cin.get();
  58.         }
  59.         i=checkwin();
  60.  
  61.         player++;
  62.     }while(i==-1);
  63.     board();
  64.     if(i==1)
  65.  
  66.         cout<<"==>\aPlayer "<<--player<<" win ";
  67.     else
  68.         cout<<"==>\aGame draw";
  69.  
  70.     cin.ignore();
  71.     cin.get();
  72.     return 0;
  73. }
  74.  
  75. /*********************************************
  76.     FUNCTION TO RETURN GAME STATUS
  77.     1 FOR GAME IS OVER WITH RESULT
  78.     -1 FOR GAME IS IN PROGRESS
  79.     O GAME IS OVER AND NO RESULT
  80. **********************************************/
  81.  
  82. int checkwin()
  83. {
  84.     if (square[1] == square[2] && square[2] == square[3])
  85.  
  86.         return 1;
  87.     else if (square[4] == square[5] && square[5] == square[6])
  88.  
  89.         return 1;
  90.     else if (square[7] == square[8] && square[8] == square[9])
  91.  
  92.         return 1;
  93.     else if (square[1] == square[4] && square[4] == square[7])
  94.  
  95.         return 1;
  96.     else if (square[2] == square[5] && square[5] == square[8])
  97.  
  98.         return 1;
  99.     else if (square[3] == square[6] && square[6] == square[9])
  100.  
  101.         return 1;
  102.     else if (square[1] == square[5] && square[5] == square[9])
  103.  
  104.         return 1;
  105.     else if (square[3] == square[5] && square[5] == square[7])
  106.  
  107.         return 1;
  108.     else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
  109.                     && square[4] != '4' && square[5] != '5' && square[6] != '6'
  110.                   && square[7] != '7' && square[8] != '8' && square[9] != '9')
  111.  
  112.         return 0;
  113.     else
  114.         return -1;
  115. }
  116.  
  117.  
  118. /*******************************************************************
  119.      FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
  120. ********************************************************************/
  121.  
  122.  
  123. void board()
  124. {
  125.     system("cls");
  126.     cout << "\n\n\tTic Tac Toe\n\n";
  127.  
  128.     cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
  129.     cout << endl;
  130.  
  131.     cout << "     |     |     " << endl;
  132.     cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;
  133.  
  134.     cout << "_____|_____|_____" << endl;
  135.     cout << "     |     |     " << endl;
  136.  
  137.     cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;
  138.  
  139.     cout << "_____|_____|_____" << endl;
  140.     cout << "     |     |     " << endl;
  141.  
  142.     cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;
  143.  
  144.     cout << "     |     |     " << endl << endl;
  145. }
  146.  
  147. /*******************************************************************
  148.                 END OF PROJECT
  149. ********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement