Advertisement
myloyo

krestiki noliki)

Dec 26th, 2022 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. string board[9] = { " ", " ", " ", " ", " ", " ", " ", " ", " " };
  5. void desk(string s = "What's your move, first player: ") {
  6.     system("cls");
  7.     cout << "~~KRESTIKI NOLIKI~~" << endl;
  8.     cout << "Make your move, enter number of cell" << endl;
  9.     cout << "~~~~~~~~~~~~~~~~~" << endl;
  10.     cout << "  " << board[0] << "  |  " << board[1] << "  |  " << board[2] << endl;
  11.     cout << "~~~~~~~~~~~~~~~~~" << endl;
  12.     cout << "  " << board[3] << "  |  " << board[4] << "  |  " << board[5] << endl;
  13.     cout << "~~~~~~~~~~~~~~~~~" << endl;
  14.     cout << "  " << board[6] << "  |  " << board[7] << "  |  " << board[8] << endl;
  15.     cout << "~~~~~~~~~~~~~~~~~" << endl;
  16.     cout << s;
  17. }
  18.                    
  19. int pobeda() {
  20.     if ((board[0] == board[1] && board[1] == board[2] && board[0] != " ") ||
  21.         (board[3] == board[4] && board[4] == board[5] && board[3] != " ") ||
  22.         (board[6] == board[7] && board[7] == board[8] && board[6] != " ") ||
  23.         (board[0] == board[3] && board[3] == board[6] && board[0] != " ") ||
  24.         (board[1] == board[4] && board[4] == board[7] && board[1] != " ") ||
  25.         (board[2] == board[5] && board[5] == board[8] && board[2] != " ") ||
  26.         (board[0] == board[4] && board[4] == board[8] && board[0] != " ") ||
  27.         (board[6] == board[4] && board[4] == board[2] && board[6] != " ")) {
  28.         return 1;
  29.     }
  30.     if (board[0] != " " && board[1] != " " && board[2] != " " && board[3] != " " && board[4] != " " && board[5] != " " && board[6] != " " && board[7] != " " && board[8] != " ") {
  31.         return -1;
  32.     }
  33.     else {
  34.         return 0;
  35.     }
  36. }
  37.  
  38. int main() {
  39.     int a, n = 0, v = 0;
  40.     desk();
  41.     while (n != 9) {
  42.         cin >> a;
  43.         if (n % 2 == 0 && board[a - 1] == " " && a <= 9 && a >= 0) {
  44.             board[a - 1] = "X";
  45.             n++;
  46.         }
  47.         else if (n%2 != 0 && board[a-1] == " " && a <= 9 && a >= 0) {
  48.             board[a - 1] = "O";
  49.             n++;
  50.         }
  51.         else {
  52.             desk("error, enter again: ");
  53.             continue;
  54.         }
  55.         v++;
  56.         if (v % 2 == 0) {
  57.             desk("What's your move, first player: ");
  58.         }
  59.         if (v % 2 != 0) {
  60.             desk("What's your move, second player: ");
  61.         }
  62.         int g = pobeda();
  63.         if (g != 0) {
  64.             if (g == 1 && v%2 != 0) {
  65.                 desk("WIN 1 PLAYER");
  66.             }
  67.             if (g == 1 && v % 2 == 0) {
  68.                 desk("WIN 2 PLAYER");
  69.             }
  70.             if (g == -1) {
  71.                 desk("TIE");
  72.             }
  73.             break;
  74.         }
  75.     }
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement