Może tak:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
struct spaceship_prop {
int x = 0;
int y = 0;
};
int main() {
spaceship_prop ship[5];
srand(time(NULL));
for (int i = 0; i<5; i++) {
ship[i].x = rand() % 1024 + 1;
ship[i].y = rand() % 768 + 1;
}
int new_position_x[5];
int new_position_y[5];
while (true)
{
for (int i = 0; i < 5; i++) {
new_position_x[i] = rand() % 4;
new_position_y[i] = rand() % 4;
ship[i].x += new_position_x[i];
ship[i].y += new_position_y[i];
}
int ile_poza_zasiegiem = 0;
for (int j = 0; j<5; j++) {
if (ship[j].x < 1024 && ship[j].y < 768)
{
cout << "X_S" << j + 1 << " : " << ship[j].x << " | " << " Y_S" << j + 1 << " : " << ship[j].y << endl;
}
else
{
cout << "X_S" << j + 1 << " : " << "out of screen" << " | " << " Y_S" << j + 1 << " : " << "out of screen" << endl;
ile_poza_zasiegiem++;
}
}
if (ile_poza_zasiegiem == 5) {
break;
}
}
cout << "Wszystkie statki gdzieś poleciały sir ;/\n";
}
Albo tak:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
struct spaceship_prop {
int x = 0;
int y = 0;
};
bool in_range(const spaceship_prop& ship)
{
return ship.x < 1024 && ship.y < 768;
}
int main() {
spaceship_prop ship[5];
srand(time(NULL));
for (int i = 0; i<5; i++) {
ship[i].x = rand() % 1024 + 1;
ship[i].y = rand() % 768 + 1;
}
do
{
for (int i = 0; i < 5; i++) {
ship[i].x += rand() % 4;
ship[i].y += rand() % 4;
if (in_range(ship[i]))
{
cout << "X_S" << i + 1 << " : " << ship[i].x << " | " << " Y_S" << i + 1 << " : " << ship[i].y << endl;
}
else
{
cout << "X_S" << i + 1 << " : " << "out of screen" << " | " << " Y_S" << i + 1 << " : " << "out of screen" << endl;
}
}
} while (any_of(begin(ship), end(ship), in_range));
cout << "Wszystkie statki gdzieś poleciały sir ;/\n";
}