Advertisement
Tucancitto

X și O

Feb 4th, 2021 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int check(int n, int* a[])
  5. {
  6.     //verificare orizontala
  7.     for (int i = 0; i < n; i++)
  8.     {
  9.         bool flag = true;
  10.         for (int j = 0; j < n; j++)
  11.             if (a[i][j] != a[i][0])
  12.             {
  13.                 flag = false;
  14.                 break;
  15.             }
  16.  
  17.         if (flag && a[i][0] != 0)
  18.             return a[i][0];
  19.     }
  20.  
  21.     //verificare verticala
  22.     for (int i = 0; i < n; i++)
  23.     {
  24.         bool flag = true;
  25.         for (int j = 0; j < n; j++)
  26.             if (a[j][i] != a[0][i])
  27.             {
  28.                 flag = false;
  29.                 break;
  30.             }
  31.  
  32.         if (flag && a[0][i] != 0)
  33.             return a[0][i];
  34.     }
  35.  
  36.  
  37.     //verificare diagonala principala
  38.     bool flag = true;
  39.     for (int i = 0; i < n; i++)
  40.         if (a[i][i] != a[0][0])
  41.         {
  42.             flag = false;
  43.             break;
  44.         }
  45.  
  46.     if (flag && a[0][0] != 0)
  47.         return a[0][0];
  48.  
  49.     //verificare diagonala secundara
  50.     flag = true;
  51.     for (int i = 0; i < n; i++)
  52.         if (a[i][n - i - 1] != a[0][n - 1])
  53.         {
  54.             flag = false;
  55.             break;
  56.         }
  57.  
  58.     if (flag && a[0][n - 1] != 0)
  59.         return a[0][n - 1];
  60.  
  61.     int c = 0;
  62.     for (int i = 0; i < n; i++)
  63.         for (int j = 0; j < n; j++)
  64.             if (a[i][j] == 0)
  65.                 c++;
  66.     if (c == 0)
  67.         return -1;
  68.     return 0;
  69. }
  70.  
  71. int main()
  72. {
  73.     system("CLS");
  74.     setlocale(LC_ALL, "Romanian");
  75.  
  76.     int n = 3;
  77.     int** game_field = new int* [n];
  78.     for (int i = 0; i < n; i++)
  79.         game_field[i] = new int[n];
  80.     for (int i = 0; i < n; i++)
  81.         for (int j = 0; j < n; j++)
  82.             game_field[i][j] = 0;
  83.  
  84.     int next_turn = 1;
  85.     while (true)
  86.     {
  87.         for (int i = 0; i < n; i++)
  88.         {
  89.             for (int j = 0; j < n; j++)
  90.                 switch (game_field[i][j])
  91.                 {
  92.                 case 0:
  93.                     cout << ". ";
  94.                     break;
  95.                 case 1:
  96.                     cout << "X ";
  97.                     break;
  98.                 case 2:
  99.                     cout << "O ";
  100.                     break;
  101.                 }
  102.             cout << endl;
  103.         }
  104.  
  105.         if (check(n, game_field) == -1)
  106.         {
  107.             cout << endl << "Niciun castigator. " << endl;
  108.             break;
  109.         }
  110.  
  111.         if (check(n, game_field) != 0)
  112.         {
  113.             cout << endl << "Jucatorul " << check(n, game_field) << " a castigat" << endl;
  114.             break;
  115.         }
  116.  
  117.         int x, y;
  118.         cout << endl;
  119.         cout << "Introduceti coordonatele verticale si orizontale ";
  120.         cout << "(" << ((next_turn == 1) ? "Jucatorul 1 pune X" : "Jucatorul 2 pune O") << "): ";
  121.         cin >> y >> x;
  122.         if (0 <= x && x < n && 0 <= y && y < n)
  123.             if (game_field[y][x] == 0)
  124.             {
  125.                 game_field[y][x] = next_turn;
  126.                 next_turn = (next_turn == 1) ? 2 : 1;
  127.             }
  128.         system("CLS");
  129.     }
  130.  
  131.     return 0;
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement