#include "Square.h" #include "Circle.h" #include #include #include using namespace std; const int NUMBER_OF_SHAPES = 20; const int GRID_SIZE_HEIGHT = 500; const int GRID_SIZE_WIDTH = 500; const float MIN_SHAPE_WIDTH = 2.5f; const float MAX_SHAPE_WIDTH = 10.0f; const float SHAPE_SPEED = 0.25f; int calculateRandomInt(int min, int max); float calculateRandomFloat(float min, float max); void collisionInformation(Shape* shapeToCompareOne, Shape* shapeToCompareTwo); vector shapes; /*Create shapes, check for collision until vector is empty of has only one shape left in the vector*/ int main() { void createShapeVector(); void updateShapes(); void updateCollisionDetection(); // read booleans cout.setf(ios::boolalpha); // generate random seed srand(time(0)); createShapeVector(); while (true) { //Program terminates if vector size is <= 1 if (shapes.size() <= 1) { break; } updateShapes(); updateCollisionDetection(); } cout << "GAME OVER" << endl; cout << "There are: " << shapes.size() << " shape(s) left in the Game" << endl; cout << "-------------" << endl; cout << endl; // Delete the shapes that collided for (int i = 0; i < shapes.size(); i++) { delete shapes[i]; } std::cin.get(); return 0; } /*Create random shapes*/ void createShapeVector() { for (int i = 0; i < NUMBER_OF_SHAPES; i++) { // Randomly choose square or circle int chooseShape = calculateRandomInt(0, 2); float x = calculateRandomFloat(0, GRID_SIZE_WIDTH); float y = calculateRandomFloat(0, GRID_SIZE_HEIGHT); float width = calculateRandomFloat(MIN_SHAPE_WIDTH, MAX_SHAPE_WIDTH); // Create a random shape with random values and add it to the vector if (chooseShape == 0) { shapes.push_back(new Circle(x, y, width)); } else { shapes.push_back(new Square(x, y, width)); } } } /*Update the position of the shapes. If the shape is outside of the grid, move it, so that it fits inside the specified grid*/ void updateShapes() { for (int i = 0; i < shapes.size(); i++) { if (shapes[i]->getTop() > GRID_SIZE_HEIGHT) { shapes[i]->setIncreaseY(false); } else if (shapes[i]->getBottom() < 0) { shapes[i]->setIncreaseY(true); } if (shapes[i]->getLeft() < 0) { shapes[i]->setIncreaseX(true); } else if (shapes[i]->getRight() > GRID_SIZE_WIDTH) { shapes[i]->setIncreaseX(false); } float x = (shapes[i]->getIncreaseX()) ? calculateRandomFloat(0, SHAPE_SPEED) : calculateRandomFloat(0, -SHAPE_SPEED); float y = (shapes[i]->getIncreaseY()) ? calculateRandomFloat(0, SHAPE_SPEED) : calculateRandomFloat(0, -SHAPE_SPEED); shapes[i]->moveShape(x, y); } } /*Collision detection - for each shape, compare each shape and check if we have a collision, skip comparing to itself*/ void updateCollisionDetection() { // For each shape... for (int i = 0; i < shapes.size(); i++) { for (int j = 0; j < shapes.size(); j++) { if (shapes[i] != shapes[j]) { if (shapes[i]->isDetectCollision(*shapes[j])) { collisionInformation(shapes[i], shapes[j]); if (i < j) { delete shapes[i]; shapes.erase(shapes.begin() + i); delete shapes[j - 1]; shapes.erase(shapes.begin() + (j - 1)); } else { delete shapes[i]; shapes.erase(shapes.begin() + i); delete shapes[j]; shapes.erase(shapes.begin() + j); } cout << "Number of Shapes Left in Vector: " << shapes.size() << endl; cout << "+++++++++++++++++++" << endl; cout << endl; } } } } } /*Print the collision*/ void collisionInformation(Shape* shapeOne, Shape* shapeTwo) { cout << endl; cout << "+++++++++++++++++++" << endl; cout << "*COLLISON*" << endl; cout << endl; shapeOne->result(cout) << endl; shapeTwo->result(cout) << endl; } /*Generate random int*/ int calculateRandomInt(int min, int max) { return (rand() % (max - min)) + min; } /*Generate random float*/ float calculateRandomFloat(float min, float max) { float random = ((float)rand()) / (float)RAND_MAX; float difference = max - min; float result = random * difference; return min + result; }