Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <stdlib.h>    
  4. #include <time.h>
  5. using namespace std;
  6. const int SIZE = 9;
  7. void initializingTheBoard(char arr[][SIZE],int SIZE) {
  8.     for (int i = 0; i < SIZE; i++)
  9.     {
  10.         for (int j = 0; j < SIZE; j++)
  11.         {
  12.             arr[i][j] = ' ';
  13.         }
  14.     }
  15. }
  16. void displayingTheBoard(char arr[][SIZE], int n) {
  17.     for (int i = 0; i < n; i++)
  18.     {
  19.         for (int j = 0; j < n; j++)
  20.         {
  21.             cout << '[' << arr[i][j] << ']';
  22.         }
  23.         cout << endl;
  24.     }
  25. }
  26. bool rowWinner(char arr[][SIZE], int n,char symbol) {
  27.     bool flag = false;
  28.     int counter = 0;
  29.     for (int i = 0; i < n; i++)
  30.     {
  31.         for (int j = 0; j < n; j++)
  32.         {
  33.             if (arr[i][j]==symbol)
  34.             {
  35.                 counter++;
  36.             }
  37.         }
  38.         if (counter == n)
  39.         {
  40.             flag = true;
  41.             break;
  42.         }
  43.         else
  44.         {
  45.             counter = 0;
  46.         }
  47.     }
  48.     return flag;
  49. }
  50. bool colWinner(char arr[][SIZE], int n, char symbol) {
  51.     bool flag = false;
  52.     int counter = 0;
  53.     for (int i = 0; i < n; i++)
  54.     {
  55.         for (int j = 0; j < n; j++)
  56.         {
  57.             if (arr[j][i] == symbol)
  58.             {
  59.                 counter++;
  60.             }
  61.         }
  62.         if (counter == n)
  63.         {
  64.             flag = true;
  65.             break;
  66.         }
  67.         else
  68.         {
  69.             counter = 0;
  70.         }
  71.     }
  72.     return flag;
  73. }bool diagonal1Winner(char arr[][SIZE], int n, char symbol) {
  74.     bool flag = false;
  75.     int counter = 0;
  76.     for (int i = 0; i < n; i++)
  77.     {
  78.         for (int j = 0; j < n; j++)
  79.         {
  80.             if (i==j&&arr[i][j] == symbol)
  81.             {
  82.                 counter++;
  83.             }
  84.         }
  85.         if (counter == n)
  86.         {
  87.             flag = true;
  88.             break;
  89.         }
  90.        
  91.     }
  92.     return flag;
  93. }
  94. bool diagonal2Winner(char arr[][SIZE], int n, char symbol) {
  95.     bool flag = false;
  96.     int counter = 0;
  97.     int j;
  98.     for (int i = 0; i <n; i++)
  99.     {
  100.         j= n - i - 1;
  101.             if (arr[i][j] == symbol)
  102.             {
  103.                 counter++;
  104.             }
  105.        
  106.         if (counter == n)
  107.         {
  108.             flag = true;
  109.             break;
  110.         }
  111.  
  112.     }
  113.     return flag;
  114. }
  115. void playing(char arr[][SIZE], int n,int choice) {
  116.     int switcher,row,column;
  117.     char symbol;
  118.     bool isDraw = true;
  119.     srand(time(nullptr));
  120.     for (int i = 0; i < n*n; i++)
  121.     {
  122.         if (i%2==0)
  123.         {
  124.             switcher = 1;
  125.             symbol = 'X';
  126.         }
  127.         else
  128.         {
  129.             switcher = 2;
  130.             symbol = 'O';
  131.         }
  132.         cout << "It's player "<<switcher<<"'s turn: " << endl;
  133.         while (true) {
  134.             if (choice == 1)
  135.             {
  136.                 cin >> row >> column;
  137.             }
  138.             else if (choice == 2)
  139.             {
  140.                 if (switcher == 1)
  141.                 {
  142.                     cin >> row >> column;
  143.                 }
  144.                 else
  145.                 {
  146.                     row = rand() % n + 1;
  147.                     column = rand() % n + 1;
  148.                 }
  149.             }
  150.             else if (choice == 3)
  151.             {
  152.  
  153.                 row = rand() % n + 1;
  154.                 column = rand() % n + 1;
  155.  
  156.             }
  157.             if (row > 0 && row <= n && column > 0 && column <= n && arr[row - 1][column - 1] == ' ')
  158.             {
  159.                 arr[row - 1][column - 1] = symbol;
  160.                 break;
  161.             }
  162.             else
  163.             {
  164.                 cout << "Invalid position. Please, try again." << endl;
  165.             }
  166.         }
  167.         displayingTheBoard(arr, n);
  168.         bool rowResult=rowWinner(arr, n, symbol);
  169.         bool colResult=colWinner(arr, n, symbol);
  170.         bool diagonal1Result=diagonal1Winner(arr, n, symbol);
  171.         bool diagonal2Result=diagonal2Winner(arr, n, symbol);
  172.        
  173.         if (rowResult||colResult||diagonal1Result||diagonal2Result)
  174.         {
  175.             cout << "Player " << switcher << "won!" << endl;
  176.             isDraw = false;
  177.             break;
  178.            
  179.         }
  180.     }
  181.     if (isDraw)
  182.     {
  183.         cout << "The result is draw";
  184.     }
  185. }
  186. int main()
  187. {
  188.    
  189.     char arr[SIZE][SIZE];
  190.     int choice;
  191.     cout << "Choose game mode(1 - PvP, 2 - PvC, 3 - CvC) : ";
  192.     do
  193.     {
  194.         cin >> choice;
  195.     } while (choice<1||choice>3);
  196.     cout << "Enter grid size: ";
  197.     int n;
  198.     do
  199.     {
  200.         cin >> n;
  201.     } while (n < 3 || n>9);
  202.     initializingTheBoard(arr, SIZE);
  203.     displayingTheBoard(arr, n);
  204.     playing(arr, n,choice);
  205.     return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement