Exemplo n.º 1
0
bool City::integrityCheck() {
	REQUIRE(properlyInitialized(), "Object 'City' was not properly properlyInitializedialized when calling integrityCheck()");

	bool integrity = true;
	std::vector<Coordinate> coordinates;

	for (std::list<House>::iterator ith = houses.begin(); ith != houses.end(); ith++){
		Coordinate location;
		CityObjects* ptr = &(*ith);
		if (getAdjecantStreet(ptr, Coordinate(0, 0)) == Coordinate(-1, -1)){
			House* houseptr = dynamic_cast<House*>(ptr);
			std::string houselocation = houseptr->getLocation().getString();
			o.print("\tERROR: House at location " + houselocation + " doesn't have a street linked to it.\n");
			integrity = false;
		}
		for (int x = 0; x < 2; x++){
			for (int y = 0; y < 2; y++){
				location = ith->getLocation();
				location.setX(location.getX()+x);
				location.setY(location.getY()-y);
				if (matrix.getObject(location.getX(), location.getY()) != &(*ith)){
					std::string houselocation = ith->getLocation().getString();
					o.print("\tERROR: House at location " + houselocation + " is supposed to be at " + location.getString() + " but is not.\n");
					integrity = false;
				}
				coordinates.push_back(location);
			}
		}
	}

	for (std::list<Fire_Department>::iterator itd = departments.begin(); itd != departments.end(); itd++){
		Coordinate location;
		CityObjects* ptr = &(*itd);
		if (getAdjecantStreet(ptr, Coordinate(0, 0)) == Coordinate(-1, -1)){
			Fire_Department* depptr = dynamic_cast<Fire_Department*>(ptr);
			std::string deplocation = depptr->getLocation().getString();
			o.print("\tERROR: Department at location " + deplocation + " doesn't have a street linked to it.\n");
			integrity = false;
		}
		for (int x = 0; x < 4; x++){
			for (int y = 0; y < 4; y++){
				location = itd->getLocation();
				location.setX(location.getX()+x);
				location.setY(location.getY()-y);
				if (matrix.getObject(location.getX(), location.getY()) != &(*itd)){
					std::string deplocation = itd->getLocation().getString();
					o.print("\tERROR: Department at location " + deplocation + " is supposed to be at " + location.getString() + " but is not.\n");
					integrity = false;
				}
				coordinates.push_back(location);
			}
		}
	}

	for (std::list<Store>::iterator itd = stores.begin(); itd != stores.end(); itd++){
		Coordinate location;
		CityObjects* ptr = &(*itd);
		Store* storeptr = dynamic_cast<Store*>(ptr);
		if (getAdjecantStreet(ptr, Coordinate(0, 0)) == Coordinate(-1, -1)){
			std::string storeLocation = storeptr->getLocation().getString();
			o.print("\tERROR: Store at location " + storeLocation + " doesn't have a street linked to it.\n");
			integrity = false;
		}
		for (int x = 0; x < storeptr->getWidth(); x++){
			for (int y = 0; y < storeptr->getLength(); y++){
				location = itd->getLocation();
				location.setX(location.getX()+x);
				location.setY(location.getY()-y);
				if (matrix.getObject(location.getX(), location.getY()) != &(*itd)){
					std::string storeLocation = itd->getLocation().getString();
					o.print("\tERROR: Store at location " + storeLocation + " is supposed to be at " + location.getString() + " but is not.\n");
					integrity = false;
				}
				coordinates.push_back(location);
			}
		}
	}

	for (std::list<Hospital>::iterator itd = hospitals.begin(); itd != hospitals.end(); itd++){
		Coordinate location;
		CityObjects* ptr = &(*itd);
		Hospital* hospitalPtr = dynamic_cast<Hospital*>(ptr);
		if (getAdjecantStreet(ptr, Coordinate(0, 0)) == Coordinate(-1, -1)){
			std::string hospitalLocation = hospitalPtr->getLocation().getString();
			o.print("\tERROR: Hospital at location " + hospitalLocation + " doesn't have a street linked to it.\n");
			integrity = false;
		}
		for (int x = 0; x < hospitalPtr->getWidth(); x++){
			for (int y = 0; y < hospitalPtr->getLength(); y++){
				location = itd->getLocation();
				location.setX(location.getX()+x);
				location.setY(location.getY()-y);
				if (matrix.getObject(location.getX(), location.getY()) != &(*itd)){
					std::string storeLocation = itd->getLocation().getString();
					o.print("\tERROR: Hospital at location " + storeLocation + " is supposed to be at " + location.getString() + " but is not.\n");
					integrity = false;
				}
				coordinates.push_back(location);
			}
		}
	}

	for (std::list<PoliceStation>::iterator itd = poliStats.begin(); itd != poliStats.end(); itd++){
		Coordinate location;
		CityObjects* ptr = &(*itd);
		PoliceStation* polPtr = dynamic_cast<PoliceStation*>(ptr);
		if (getAdjecantStreet(ptr, Coordinate(0, 0)) == Coordinate(-1, -1)){
			std::string polLocation = polPtr->getLocation().getString();
			o.print("\tERROR: Policestation at location " + polLocation + " doesn't have a street linked to it.\n");
			integrity = false;
		}
		for (int x = 0; x < polPtr->getWidth(); x++){
			for (int y = 0; y < polPtr->getLength(); y++){
				location = itd->getLocation();
				location.setX(location.getX()+x);
				location.setY(location.getY()-y);
				if (matrix.getObject(location.getX(), location.getY()) != &(*itd)){
					std::string polLocation = itd->getLocation().getString();
					o.print("\tERROR: Policestation at location " + polLocation + " is supposed to be at " + location.getString() + " but is not.\n");
					integrity = false;
				}
				coordinates.push_back(location);
			}
		}
	}

	for (std::list<Street>::iterator its = streets.begin(); its != streets.end(); its++){
		Coordinate location;
		Coordinate start = its->getStart();
		Coordinate end = its->getEnd();
		location = start;

		if (start.getX() == end.getX()){
			for (int y = start.getY(); y <= end.getY(); y++){
				location.setY(y);
				if (matrix.getObject(location.getX(), location.getY()) == NULL) {
					continue;
				}
				if (matrix.getObject(location.getX(), location.getY())->getType() == street){
					coordinates.push_back(location);
					Street* ptr = dynamic_cast<Street*>(matrix.getObject(location.getX(), location.getY()));
					if (its->getName() != ptr->getName()){
						o.print("\tERROR: Street " + its->getName() + " was supposed to be found on location " + location.getString() + " but is not.\n");
						integrity = false;
					}
					if(location.getX() == 0){
						if (matrix.getObject(location.getX()+1, location.getY()) == NULL) {
							continue;
						}
						if (matrix.getObject(location.getX()+1, location.getY())->getType() == street){
							o.print("\tERROR: Street " + its->getName() + " on location " + location.getString() + " was more than one wide.\n");
							integrity = false;
						}
					}
					else if (location.getX() == matrix.getColumns()-1){
						if (matrix.getObject(location.getX()-1, location.getY()) == NULL) {
							continue;
						}
						if (matrix.getObject(location.getX()-1, location.getY())->getType() == street){
							o.print("\tERROR: Street " + its->getName() + " on location " + location.getString() + " was more than one wide.\n");
							integrity = false;
						}
					}
					else {
						if (matrix.getObject(location.getX()-1, location.getY()) == NULL) {
							continue;
						}
						if (matrix.getObject(location.getX()-1, location.getY())->getType() == street){
							o.print("\tERROR: Street " + its->getName() + " on location " + location.getString() + " was more than one wide.\n");
							integrity = false;
						}
						if (matrix.getObject(location.getX()+1, location.getY()) == NULL) {
							continue;
						}
						if (matrix.getObject(location.getX()+1, location.getY())->getType() == street){
							o.print("\tERROR: Street " + its->getName() + " on location " + location.getString() + " was more than one wide.\n");
							integrity = false;
						}
					}
				}
				else if (matrix.getObject(location.getX(), location.getY())->getType() == crossroad){
					Crossroad* ptr = dynamic_cast<Crossroad*>(matrix.getObject(location.getX(), location.getY()));
					if ((its->getName() != ptr->getStreet1()) && (its->getName() != ptr->getStreet2())){
						o.print("\tERROR: Street " + its->getName() + " was supposed to be found on location " + location.getString() + " but is not. (as a crossroad)\n");
						integrity = false;
					}
				}
			}
		}

		else if (start.getY() == end.getY()){
			for (int x = start.getX(); x <= end.getX(); x++){
				location.setX(x);
				if (matrix.getObject(location.getX(), location.getY()) == NULL) {
					continue;
				}
				if (matrix.getObject(location.getX(), location.getY())->getType() == street){
					coordinates.push_back(location);
					Street* ptr = dynamic_cast<Street*>(matrix.getObject(location.getX(), location.getY()));
					if (its->getName() != ptr->getName()){
						o.print("\tERROR: Street " + its->getName() + " was supposed to be found on location " + location.getString() + " but is not.\n");
						integrity = false;
					}
					if(location.getY() == 0){
						if (matrix.getObject(location.getX(), location.getY()+1) == NULL) {
							continue;
						}
						if (matrix.getObject(location.getX(), location.getY()+1)->getType() == street){
							o.print("\tERROR: Street " + its->getName() + " on location " + location.getString() + " was more than one wide.\n");
							integrity = false;
						}
					}
					else if (location.getY() == matrix.getRows()-1){
						if (matrix.getObject(location.getX(), location.getY()-1) == NULL) {
							continue;
						}
						if (matrix.getObject(location.getX(), location.getY()-1)->getType() == street){
							o.print("\tERROR: Street " + its->getName() + " on location " + location.getString() + " was more than one wide.\n");
							integrity = false;
						}
					}
					else {
						if (matrix.getObject(location.getX(), location.getY()-1) == NULL) {
							continue;
						}
						if (matrix.getObject(location.getX(), location.getY()-1)->getType() == street){
							o.print("\tERROR: Street " + its->getName() + " on location " + location.getString() + " was more than one wide.\n");
							integrity = false;
						}
						if (matrix.getObject(location.getX(), location.getY()+1) == NULL) {
							continue;
						}
						if (matrix.getObject(location.getX(), location.getY()+1)->getType() == street){
							o.print("\tERROR: Street " + its->getName() + " on location " + location.getString() + " was more than one wide.\n");
							integrity = false;
						}
					}
				}
				else if (matrix.getObject(location.getX(), location.getY())->getType() == crossroad){
					Crossroad* ptr = dynamic_cast<Crossroad*>(matrix.getObject(location.getX(), location.getY()));
					if ((its->getName() != ptr->getStreet1()) && (its->getName() != ptr->getStreet2())){
						o.print("\tERROR: Street " + its->getName() + " was supposed to be found on location " + location.getString() + " but is not. (as a crossroad)\n");
						integrity = false;
					}
				}
			}
		}
	}

	for (std::list<Crossroad>::iterator itc = crossroads.begin(); itc != crossroads.end(); itc++){
		Coordinate location = itc->getLocation();
		if (matrix.getObject(location.getX(), location.getY()) != &(*itc)){
			o.print("\tERROR: Crossroad " + itc->getName() + " was supposed to be found on location " + location.getString() + " but is not.\n");
			integrity = false;
		}
		coordinates.push_back(location);
	}



	for (int i = 0; i < coordinates.size(); i++){
		for (int j = i+1; j < coordinates.size(); j++){
			if (coordinates[i] == coordinates[j]){
				o.print("\tERROR: Overlap found at location " + coordinates[i].getString() + ".\n");
				integrity = false;
			}
		}
	}

	if (coordinates.size() != matrix.getRows()*matrix.getColumns()){
		o.print("\tERROR: Empty spots were found in the city.\n");
		integrity = false;
	}

	for (std::list<Firetruck>::iterator itt = trucks.begin(); itt != trucks.end(); itt++){
		Fire_Department* ptr = itt->getBaseptr();
		bool found = false;
		for (std::list<Fire_Department>::iterator itd = departments.begin(); itd != departments.end(); itd++){
			if (&(*itd) == ptr){
				found = true;
			}
		}
		if (found == false){
			o.print("\tERROR: Truck with name " + itt->getName() + " was not linked to a base.\n");
			integrity = false;
		}
	}
	return integrity;
}