#include "stdafx.h"
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
struct spaceship { // create the ship
int x, y;
char callsign[51];
};
void shiprandloc(spaceship *ship, int maxrange) { //randomize location
ship->x = rand() % maxrange;
ship->y = rand() % maxrange;
}
int shipdetcol(spaceship *ship1, spaceship *ship2, float colrange) { //if they collide return a 1
colrange < 10;
return 1;
}
int main()
{
int maxloc = 100, maxcol = 10;
int numloops;
cout << "Enter the Number of Collisions to Simulate: ";
cin >> numloops;
for (int i = 0; i < numloops; i++) {
int loopcnt = 0;
spaceship *ship1, *ship2;
ship1 = new spaceship;
ship2 = new spaceship;
strcpy_s(ship1->callsign, "Red1");
strcpy_s(ship2->callsign, "Blue1");
shiprandloc(ship1, maxloc);
shiprandloc(ship2, maxloc);
d = sqrt((ship1->x - ship2->x)*(ship1->y - ship2->y)); //find distance between the two ships.
while (!shipdetcol(ship1, ship2, maxcol)) {
++loopcnt;
}
delete ship1, ship2;
}
return 0;
}
The square root function to check the distance isn't working also the collide returning a 1 if it hits and a 0 if it misses. What am I missing? .