Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int check(int n, int* a[])
- {
- //verificare orizontala
- for (int i = 0; i < n; i++)
- {
- bool flag = true;
- for (int j = 0; j < n; j++)
- if (a[i][j] != a[i][0])
- {
- flag = false;
- break;
- }
- if (flag && a[i][0] != 0)
- return a[i][0];
- }
- //verificare verticala
- for (int i = 0; i < n; i++)
- {
- bool flag = true;
- for (int j = 0; j < n; j++)
- if (a[j][i] != a[0][i])
- {
- flag = false;
- break;
- }
- if (flag && a[0][i] != 0)
- return a[0][i];
- }
- //verificare diagonala principala
- bool flag = true;
- for (int i = 0; i < n; i++)
- if (a[i][i] != a[0][0])
- {
- flag = false;
- break;
- }
- if (flag && a[0][0] != 0)
- return a[0][0];
- //verificare diagonala secundara
- flag = true;
- for (int i = 0; i < n; i++)
- if (a[i][n - i - 1] != a[0][n - 1])
- {
- flag = false;
- break;
- }
- if (flag && a[0][n - 1] != 0)
- return a[0][n - 1];
- int c = 0;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (a[i][j] == 0)
- c++;
- if (c == 0)
- return -1;
- return 0;
- }
- int main()
- {
- system("CLS");
- setlocale(LC_ALL, "Romanian");
- int n = 3;
- int** game_field = new int* [n];
- for (int i = 0; i < n; i++)
- game_field[i] = new int[n];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- game_field[i][j] = 0;
- int next_turn = 1;
- while (true)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- switch (game_field[i][j])
- {
- case 0:
- cout << ". ";
- break;
- case 1:
- cout << "X ";
- break;
- case 2:
- cout << "O ";
- break;
- }
- cout << endl;
- }
- if (check(n, game_field) == -1)
- {
- cout << endl << "Niciun castigator. " << endl;
- break;
- }
- if (check(n, game_field) != 0)
- {
- cout << endl << "Jucatorul " << check(n, game_field) << " a castigat" << endl;
- break;
- }
- int x, y;
- cout << endl;
- cout << "Introduceti coordonatele verticale si orizontale ";
- cout << "(" << ((next_turn == 1) ? "Jucatorul 1 pune X" : "Jucatorul 2 pune O") << "): ";
- cin >> y >> x;
- if (0 <= x && x < n && 0 <= y && y < n)
- if (game_field[y][x] == 0)
- {
- game_field[y][x] = next_turn;
- next_turn = (next_turn == 1) ? 2 : 1;
- }
- system("CLS");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement