Leedwon

Old shooting function

Jul 4th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. // stara werja strzelajacej funkcji
  2.  
  3. bool Board::ComputerShoot()
  4. {
  5.     srand(time(NULL));
  6.     int x, y; // koordynaty strzalu
  7.     if(prevX == 11 && prevY == 11) // poprzedni strzal spudlowany
  8.     {
  9.         do
  10.         {
  11.             x = rand() % 10; // 0-9
  12.             y = rand() % 10; // 0-9
  13.         } while (privateBoard[x][y] == 2 || privateBoard[x][y] == 3 || privateBoard[x][y] == 4);
  14.         if (privateBoard[x][y] == 0)
  15.         {
  16.             std::cout << "\nKomputer strzela w: " << char(x + 65) << y+1 <<"\nPudlo";
  17.             privateBoard[x][y] = 3;
  18.             PrintCoords();
  19.             return false;
  20.         }
  21.         else if (privateBoard[x][y] == 1)
  22.         {
  23.             std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nTrafiony";
  24.             privateBoard[x][y] = 2;
  25.             PrintCoords();
  26.             iteration++;
  27.             prevX = x;
  28.             prevY = y;
  29.             return true;
  30.         }
  31.     }
  32.     else
  33.     {
  34.         bool var1 = false;
  35.         bool alreadyShot[4] = { false, false, false, false };
  36.         do
  37.         {
  38.             int newShoot = rand() % 4;
  39.             if (newShoot == 0 && alreadyShot[0] == false)
  40.             {
  41.                 x = prevX + 1;
  42.                 y = prevY;
  43.                 if (x + 1 <= 9) // x+1, y
  44.                 {
  45.                     if (privateBoard[x][y] == 1)
  46.                     {
  47.                         std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nTrafiony";
  48.                         privateBoard[x][y] = 2;
  49.                         PrintCoords();
  50.                         prevX = x;
  51.                         prevY = y;
  52.                         var1 = true;
  53.                         iteration++;
  54.                         return true;
  55.                     }
  56.                     else if (privateBoard[x][y] == 0)
  57.                     {
  58.                         std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nPudlo";
  59.                         privateBoard[x][y] = 3;
  60.                         PrintCoords();
  61.                         var1 = true;
  62.                         return false;
  63.                     }
  64.                 }
  65.                 alreadyShot[0] = true;
  66.             }
  67.             else if (newShoot == 1 && alreadyShot[1] == false)
  68.             {
  69.                 x = prevX;
  70.                 y = prevY - 1;
  71.                 if (y - 1 >= 0) // x, y - 1
  72.                 {
  73.                     if (privateBoard[x][y] == 1)
  74.                     {
  75.                         std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nTrafiony";
  76.                         privateBoard[x][y] = 2;
  77.                         PrintCoords();
  78.                         prevX = x;
  79.                         prevY = y;
  80.                         var1 = true;
  81.                         iteration++;
  82.                         return true;
  83.                     }
  84.                     else if (privateBoard[x][y] == 0)
  85.                     {
  86.                         std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nPudlo";
  87.                         privateBoard[x][y] = 3;
  88.                         PrintCoords();
  89.                         var1 = true;
  90.                         return false;
  91.                     }
  92.                 }
  93.                 alreadyShot[1] = true;
  94.             }  
  95.             else if (newShoot == 2 && alreadyShot[2] == false)
  96.             {
  97.                 x = prevX - 1;
  98.                 y = prevY;
  99.                 if (x - 1 >= 0) // x-1, y
  100.                 {
  101.                     if (privateBoard[x][y] == 1)
  102.                     {
  103.                         std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nTrafiony";
  104.                         privateBoard[x][y] = 2;
  105.                         PrintCoords();
  106.                         prevX = x;
  107.                         prevY = y;
  108.                         var1 = true;
  109.                         iteration++;
  110.                         return true;
  111.                     }
  112.                     else if (privateBoard[x][y] == 0)
  113.                     {
  114.                         std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nPudlo";
  115.                         privateBoard[x][y] = 3;
  116.                         PrintCoords();
  117.                         var1 = true;
  118.                         return false;
  119.                     }
  120.                     alreadyShot[2] = true;
  121.                 }
  122.             }  
  123.             else if (newShoot == 3 && alreadyShot[3] == false)
  124.             {
  125.                 x = prevX;
  126.                 y = prevY + 1;
  127.                 if (y + 1 <= 9) // x, y +1
  128.                 {
  129.                     if (privateBoard[x][y] == 1)
  130.                     {
  131.                         std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nTrafiony";
  132.                         privateBoard[x][y] = 2;
  133.                         PrintCoords();
  134.                         prevX = x;
  135.                         prevY = y;
  136.                         var1 = true;
  137.                         iteration++;
  138.                         return true;
  139.                     }
  140.                     else if (privateBoard[x][y] == 0)
  141.                     {
  142.                         std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nPudlo";
  143.                         privateBoard[x][y] = 3;
  144.                         PrintCoords();
  145.                         var1 = true;
  146.                         return false;
  147.                     }
  148.                 }
  149.                 alreadyShot[3] = true;
  150.             }
  151.         } while (var1 == false && iteration < 1);
  152.     }
  153.     if (iteration == 1)
  154.     {
  155.         Ship::en1 smartShootingPosition; // komputer sprawdza w jakim polozeniu znajduje sie statek przeciwnika
  156.         if(prevX + 1 <= 9 && prevX - 1 >= 0) // zabezpieczenie przed dostepem do nieuzytkowanej pamieci (wycieki pamieci)
  157.             if(privateBoard[prevX + 1][prevY] == 4 || privateBoard[prevX - 1][prevY] == 4 )
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment