Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // stara werja strzelajacej funkcji
- bool Board::ComputerShoot()
- {
- srand(time(NULL));
- int x, y; // koordynaty strzalu
- if(prevX == 11 && prevY == 11) // poprzedni strzal spudlowany
- {
- do
- {
- x = rand() % 10; // 0-9
- y = rand() % 10; // 0-9
- } while (privateBoard[x][y] == 2 || privateBoard[x][y] == 3 || privateBoard[x][y] == 4);
- if (privateBoard[x][y] == 0)
- {
- std::cout << "\nKomputer strzela w: " << char(x + 65) << y+1 <<"\nPudlo";
- privateBoard[x][y] = 3;
- PrintCoords();
- return false;
- }
- else if (privateBoard[x][y] == 1)
- {
- std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nTrafiony";
- privateBoard[x][y] = 2;
- PrintCoords();
- iteration++;
- prevX = x;
- prevY = y;
- return true;
- }
- }
- else
- {
- bool var1 = false;
- bool alreadyShot[4] = { false, false, false, false };
- do
- {
- int newShoot = rand() % 4;
- if (newShoot == 0 && alreadyShot[0] == false)
- {
- x = prevX + 1;
- y = prevY;
- if (x + 1 <= 9) // x+1, y
- {
- if (privateBoard[x][y] == 1)
- {
- std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nTrafiony";
- privateBoard[x][y] = 2;
- PrintCoords();
- prevX = x;
- prevY = y;
- var1 = true;
- iteration++;
- return true;
- }
- else if (privateBoard[x][y] == 0)
- {
- std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nPudlo";
- privateBoard[x][y] = 3;
- PrintCoords();
- var1 = true;
- return false;
- }
- }
- alreadyShot[0] = true;
- }
- else if (newShoot == 1 && alreadyShot[1] == false)
- {
- x = prevX;
- y = prevY - 1;
- if (y - 1 >= 0) // x, y - 1
- {
- if (privateBoard[x][y] == 1)
- {
- std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nTrafiony";
- privateBoard[x][y] = 2;
- PrintCoords();
- prevX = x;
- prevY = y;
- var1 = true;
- iteration++;
- return true;
- }
- else if (privateBoard[x][y] == 0)
- {
- std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nPudlo";
- privateBoard[x][y] = 3;
- PrintCoords();
- var1 = true;
- return false;
- }
- }
- alreadyShot[1] = true;
- }
- else if (newShoot == 2 && alreadyShot[2] == false)
- {
- x = prevX - 1;
- y = prevY;
- if (x - 1 >= 0) // x-1, y
- {
- if (privateBoard[x][y] == 1)
- {
- std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nTrafiony";
- privateBoard[x][y] = 2;
- PrintCoords();
- prevX = x;
- prevY = y;
- var1 = true;
- iteration++;
- return true;
- }
- else if (privateBoard[x][y] == 0)
- {
- std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nPudlo";
- privateBoard[x][y] = 3;
- PrintCoords();
- var1 = true;
- return false;
- }
- alreadyShot[2] = true;
- }
- }
- else if (newShoot == 3 && alreadyShot[3] == false)
- {
- x = prevX;
- y = prevY + 1;
- if (y + 1 <= 9) // x, y +1
- {
- if (privateBoard[x][y] == 1)
- {
- std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nTrafiony";
- privateBoard[x][y] = 2;
- PrintCoords();
- prevX = x;
- prevY = y;
- var1 = true;
- iteration++;
- return true;
- }
- else if (privateBoard[x][y] == 0)
- {
- std::cout << "\nKomputer strzela w: " << char(x + 65) << y + 1 << "\nPudlo";
- privateBoard[x][y] = 3;
- PrintCoords();
- var1 = true;
- return false;
- }
- }
- alreadyShot[3] = true;
- }
- } while (var1 == false && iteration < 1);
- }
- if (iteration == 1)
- {
- Ship::en1 smartShootingPosition; // komputer sprawdza w jakim polozeniu znajduje sie statek przeciwnika
- if(prevX + 1 <= 9 && prevX - 1 >= 0) // zabezpieczenie przed dostepem do nieuzytkowanej pamieci (wycieki pamieci)
- if(privateBoard[prevX + 1][prevY] == 4 || privateBoard[prevX - 1][prevY] == 4 )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment