#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
usingnamespace std;
struct SpaceShip
{
int x_coord;
int y_coord;
int name;
bool onGrid;
};
int randRange( int low, int high );
SpaceShip getNewShip(SpaceShip ship);
SpaceShip newTurn(SpaceShip wanderShip[10]);
void showGrid(SpaceShip wanderShip[10]);
bool anyShipOnGrid(SpaceShip wanderShip[10]);
void populateGrid( SpaceShip wanderShip[10] );
void shipProg();
int randRange( int low, int high )
{
return rand() % ( high - low + 1 ) + low;
}
SpaceShip getNewShip(SpaceShip ship)
{
ship.x_coord = randRange(1,1024);
ship.y_coord = randRange(1,768);
ship.onGrid=true;
return ship;
}
SpaceShip newTurn(SpaceShip wanderShip[10])
{
int x_movement;
int y_movement;
for( int i = 0; i < 10; i++ )
{
x_movement = ( randRange(1,50) );
y_movement = 50 - x_movement;
wanderShip[i].x_coord += x_movement;
wanderShip[i].y_coord += y_movement;
}
}
void showGrid(SpaceShip wanderShip[10])
{
for( int i=0; i < 10; i++ )
{
if( wanderShip[i].onGrid==false ){ cout << "\nShip " << wanderShip[i].name << " is off the grid." << endl; }
else
{
cout << "\nShip " << wanderShip[i].name << " location: ( "
<< wanderShip[i].x_coord << ", " << wanderShip[i].y_coord << " )" << endl;
}
}
}
bool anyShipOnGrid(SpaceShip wanderShip[10])
{
for( int i=0; i < 10; i++)
{
//return wanderShip[i].onGrid==true;
if( wanderShip[i].onGrid==true ){ returntrue; }
}
}
void populateGrid( SpaceShip wanderShip[10] )
{
for( int i=0; i < 10; i++ )
{
SpaceShip ship;
ship=getNewShip(ship);
ship.name=i+1;
wanderShip[i]=ship;
}
}
void shipProg()
{
cout << "Ships have been generated. Their coordinates: " << endl;
SpaceShip wanderShip[10];
populateGrid(wanderShip);
showGrid( wanderShip );
while( anyShipOnGrid(wanderShip) )
{
cout << "\n\nPress enter to start next phase, moving each ship 50 units." << endl;
cin.ignore();
newTurn( wanderShip );
for( int i=0; i < 10; i++ )
{
if( wanderShip[i].x_coord > 1024 || wanderShip[i].y_coord > 768 ){ wanderShip[i].onGrid=false; }
}
showGrid( wanderShip );
}
cout << "\n\nAll ships are off the grid.\n" << endl;
}
int main()
{
srand( time( NULL ) );
cout << "This program generates 10 ships and randomly positions them on a grid.\n""The grid size is 1024 units across (x-axis) and 768 units high (y-axis).\n""Ships always move in a positive direction, away from the point of origin ( 0,0 ).\n""Every ship moves 50 units per phase.\n""Movement across the x-axis is calculated first by \n""generating a random number between 1 and 50.\n""Any remaining amount is applied to movement up the y-axis.\n""Ships are tracked and their positions reported until they move off the grid." << endl;
char again='y';
do
{
cout << "Press enter to generate ships." << endl;
cin.ignore();
shipProg();
cout << "Run the program again?" << endl;
cin >> again;
}while( again!='n' );
}
But this causes the loop to break at seemingly random times.
Because that code snippet tests only first ship.
Lets unroll the loop:
1 2 3 4 5 6 7 8 9 10 11
return wanderShip[0].onGrid==true; //Execution won't get past this line
//↓ Never executed as we returned early
return wanderShip[1].onGrid==true;
return wanderShip[2].onGrid==true;
return wanderShip[3].onGrid==true;
return wanderShip[4].onGrid==true;
return wanderShip[5].onGrid==true;
return wanderShip[6].onGrid==true;
return wanderShip[7].onGrid==true;
return wanderShip[8].onGrid==true;
return wanderShip[9].onGrid==true;