Advertisement
Bertran_rz

bigShipsWar

Sep 8th, 2021
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     srand(time(0));
  8.  
  9.     int const size = 10;
  10.  
  11.     int arr[size][size];
  12.  
  13.     for (int i = 0; i < size; i++)
  14.     {
  15.         for (int j = 0; j < size; j++)
  16.         {
  17.             arr[i][j] = 0;
  18.             cout << arr[i][j] << " ";
  19.         }
  20.         cout << endl;
  21.     }
  22.     cout << endl;
  23.  
  24.     //Draw 4 lvl boat
  25.     int x = 1, y = 1;
  26.     bool acces = false;
  27.  
  28.     do
  29.     {
  30.         acces = false;
  31.         x = rand() % 10;
  32.         y = rand() % 10;
  33.  
  34.         if (x > size - 4)
  35.         {
  36.             acces = true;
  37.         }
  38.         else
  39.         {
  40.             for (int i = x; i < x + 4; i++)
  41.                 if (arr[y][i] != 0)
  42.                     acces = true;
  43.         }
  44.     } while (acces);
  45.  
  46.     if (!acces)
  47.     {
  48.         for (int i = y - 1; i < y + 2; i++)
  49.         {
  50.             for (int j = x - 1; j < x + 5; j++)
  51.             {
  52.                 if (i >= 0 && j >= 0 && i < size && j < size)
  53.                     arr[i][j] = 2;
  54.             }
  55.         }
  56.     }
  57.  
  58.     for (int i = x; i < x + 4; i++)
  59.     {
  60.         arr[y][i] = 1;
  61.     }
  62.  
  63.     //draw 1 lvl boat
  64.  
  65.     for (int n = 0; n < 4; n++)
  66.     {
  67.         int x, y;
  68.         do
  69.         {
  70.             x = rand() % 10;
  71.             y = rand() % 10;
  72.  
  73.         } while (arr[x][y] != 0);
  74.  
  75.         for (int i = y - 1; i < y + 2; i++)
  76.         {
  77.             for (int j = x - 1; j < x + 2; j++)
  78.             {
  79.                 if (i >= 0 && j >= 0 && i < size && j < size)
  80.                     arr[j][i] = 2;
  81.             }
  82.         }
  83.  
  84.         arr[x][y] = 1;
  85.     }
  86.  
  87.     //shown all place
  88.     for (int i = 0; i < size; i++)
  89.     {
  90.         for (int j = 0; j < size; j++)
  91.             cout << arr[i][j] << " ";
  92.  
  93.         cout << endl;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement