SHARE
TWEET

Untitled

a guest Jul 18th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3. char matrix[3][3] = { '1', '2', '3' , '4' , '5' , '6', '7' , '8' , '9' };
  4. char player = 'X';
  5. void print() {
  6.     system("cls");
  7.     for (int i = 0; i < 3; i++) {
  8.         for ( int j = 0; j < 3; j++) {
  9.             cout << matrix[i][j] << " | " ;
  10.         }
  11.         cout << endl <<"__________"<< endl;
  12.          
  13.     }
  14. }
  15.  
  16. void play() {
  17.     char pos;
  18.     cout << "Enter you position player  ( " << player << " ) ?" << endl;
  19.     cin >> pos;
  20.     for (int i = 0; i < 3; i++) {
  21.         for (int j = 0; j < 3; j++) {
  22.             if (matrix[i][j] == pos)
  23.                 matrix[i][j] = player;
  24.         }
  25.     }
  26.     if (player == 'X')
  27.         player = 'O';
  28.     else
  29.         player = 'X';
  30.    
  31. }
  32.  
  33. char whoWin() {
  34.     int counter = 0;
  35.     int Cx = 0, Co = 0;
  36.     for (int i = 0; i < 3; i++) {
  37.         for (int j = 0; j < 3; j++) {
  38.             if (matrix[i][j] != 'X' && matrix[i][j] != 'O') counter++;
  39.             if (matrix[i][j] == 'X') Cx++;
  40.             else if (matrix[i][j] == 'O') Co++;
  41.         }
  42.         if (Cx == 3 || Co == 3) {
  43.             return(Cx > Co) ? 'X' : 'O';
  44.         }
  45.         Cx = 0;
  46.         Co = 0;
  47.     }
  48.  
  49.    
  50.     for (int i = 0; i < 3; i++) {
  51.         for (int j = 0; j < 3; j++) {
  52.             if (matrix[j][i] == 'X') Cx++;
  53.             else if (matrix[j][i] == 'O') Co++;
  54.         }
  55.         if (Cx == 3 || Co == 3) {
  56.             return(Cx > Co) ? 'X' : 'O';
  57.         }
  58.         Cx = 0;
  59.         Co = 0;
  60.     }
  61.  
  62.     if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')return 'X';
  63.     else if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O') return 'O';
  64.  
  65.     if (matrix[0][2] == 'X' && matrix[1][1] == 'X' && matrix[2][0] == 'X')return 'X';
  66.     else if (matrix[0][2] == 'O' && matrix[1][1] == 'O' && matrix[2][0] == 'O') return 'O';
  67.  
  68.     // Z mean no place in the gray
  69.     if (counter == 0) return 'Z';
  70.     // game is still and no one win
  71.     return '.';
  72. }
  73. int main() {
  74.     while (whoWin() == '.') {
  75.         print();
  76.         play();
  77.     }
  78.     if (whoWin() == 'Z')
  79.         cout << "Game is ended without winner *_*";
  80.     else
  81.         cout << "The winner is player (" << whoWin() << ") Congratulations ";
  82.     system("pause");
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top