Building& Building::step(int nRidersToAdd) { //****************************************************************************** // the rider phase, you should see stranded riders randomly placed on floors, * // and there should be no up-riders on the top floor and no down-riders on the * // bottom floor, and elevators should remain idle. * //****************************************************************************** for (int i = 0; i < nRidersToAdd; i++) { int max = floors.size(); // max number of floors int from = 0; int to = 0; getDifferentInts(max, from, to); // create a rider with randomly selected from- and to-floors (*(floors[from])).addNewRider(Rider(*(floors[to]))); // tell the from-floor to add this rider } // increment timer once per second this->simTime++; //*************************************************************************************** // the elevator phase, the elevators should (a) stop to pickup riders on the floors on * // their way to another destination, and (b) go to floors with waiting riders after * // they reach an idle state. * //*************************************************************************************** // ELEVATOR ACTIONS [3] for (int i = 0; i < getElevatorCount(); i++) { if (!elevators[i]->isDoorOpen()) // if elevator door is closed (move up or down) [3] { // if not near enough to destination to reach it in this time step, continue moving [3] if (!elevators[i]->isNearDestination()) { if (elevators[i]->isDirectionUp()) // if elevator direction is up, move up [3] { elevators[i]->moveUp(); } else // otherwise, move down [3] { elevators[i]->moveDown(); } } else // otherwise it's near enough to destination to reach it in this time step... [4] { elevators[i]->moveToDestinationFloor(); // tell elevator to move to its destination floor [4] elevators[i]->openDoor(); // tell elevator to open its door [4] elevators[i]->removeRidersForDestinationFloor(); // tell elevator to remove riders for its destination floor // get a non-const pointer to the elevator's destination floor (using const_cast) [5] Floor* elvDestinationFl = const_cast<Floor*>(&elevators[i]->getDestination()); // if elevator is empty, choose a direction based on longest-waiting rider (the one with the smallest ID) on the floor: [5] if (elevators[i]->getRiderCount() == 0) { if (elvDestinationFl->isPreferredDirectionUp()) // if the floor's chosen direction is up [5] { elevators[i]->setDirectionUp(); // tell elevator to set its direction to up [5] } else // otherwise [5] { elevators[i]->setDirectionDown(); // tell elevator to set its direction to down [5] } } int n = elevators[i]->getAvailableSpace(); // if there is space in the elevator after letting off riders, board new ones [6] if (n > 0) { if (elevators[i]->isDirectionUp()) // if elevator direction is up, board up-riders (if any)... [6] { elevators[i]->addRiders(elvDestinationFl->removeUpRiders(n)); } else { elevators[i]->addRiders(elvDestinationFl->removeDownRiders(n)); // otherwise, board down-riders (if any) [6] } } // reassess elevator destination based on its riders [8] elevators[i]->setDestinationBasedOnRiders(); } } // End if door is closed else // otherwise if door is open (then it already let off riders, or is in its initial state) [7] { if (elevators[i]->getRiderCount() > 0) // if elevator has any riders [7] { elevators[i]->closeDoor(); // tell elevator to close its door [7] } else // otherwise [9] { elevators[i]->setIdle(); // tell elevator to go idle [9] } } } // End elevator loop //********************************************************************************************************* // the floor phase, the elevators should choose a direction, but do not move. * //********************************************************************************************************* // FLOOR ACTIONS [2] for (int i = 0; i < getFloorCount(); i++) { if (floors[i]->hasRidersWaiting()) // check each floor (for waiting riders) [2] { if(!floors[i]->hasRidersWaiting()) // if there are no riders waiting on this floor, continue with next floor [2] { continue; } for (int j = 0; j < getElevatorCount(); j++) // look at each elevator to see if it needs to be sent here [2] { // get elevator's relative location (negative if elevator is below floor) [2] int relativeLocation = elevators[j]->getLocation() - floors[i]->getLocation(); if (elevators[j]->isIdle()) // if this elevator's idle... [2] { if (relativeLocation > 0) // if elevator is above the floor, set elevator direction to down [2] { elevators[j]->setDirectionDown(); } else // otherwise if it's below, set elevator direction to up [2] { elevators[j]->setDirectionUp(); // tell elevator to close its door [2] } elevators[j]->setDestination(floors[i]); // set elevator's destination to this floor [2] elevators[j]->closeDoor(); // tell elevator to close its door [2] } // end idle // else if there are riders on this floor waiting to go up, and the elevator is going up... [10] else if (floors[i]->hasUpRiders() && elevators[j]->isDirectionUp()) { // get distance from elevator's destination floor to this floor (positive if elevator destination is above this floor) [10] int distFromElvDestination = elevators[j]->getDestination().getLocation() - floors[i]->getLocation(); // if elevator is below floor and elevator destination is above this floor [10] if (elevators[j]->getLocation() < floors[i]->getLocation() && distFromElvDestination > 0) { // set elevator's destination to this floor [10] elevators[j]->setDestination(floors[i]); } } // else if there are riders on this floor waiting to go down, and the elevator is going down... [10] else if (floors[i]->hasDownRiders() && elevators[j]->isDirectionDown()) { // get distance from elevator's destination floor to this floor (positive if elevator destination is above this floor) [10] double distFromElvDestination = elevators[j]->getDestination().getLocation() - floors[i]->getLocation(); // if elevator is above floor and elevator destination is below this floor [10] if (elevators[j]->getLocation() > floors[i]->getLocation() && distFromElvDestination < 0) { // set elevator's destination to this floor [10] elevators[j]->setDestination(floors[i]); } } } // End elevator loop } // End riders waiting } // End floor loop return *this; } // end Building::step
Building& Building::step(int numRiders) // moves elevators and riders each second. numRiders = riders to randomly add to floors { int count = 0; // screen counter for pausing. //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ //@@@@@@@@@@@@@@@@@@@@@@ Riders @@@@@@@@@@@@@@@@@@@@@@@@@@@ //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ for (int i=0; i < numRiders; i++) // add riders on random floors { int randomStartFloor = rand() % buildingFloors.size(); // random number from 0 to buildingFloors max int randomDestinationFloor = rand() % buildingFloors.size(); // random number from 0 to buildingFloors max if (randomStartFloor == 0) // Basement case - no down { randomDestinationFloor = rand() % (buildingFloors.size()-1) + 1; // random number from 1 to (buildingFloors max -1) } else if (randomStartFloor == buildingFloors.size()) // Top case - no up { randomDestinationFloor = rand() % (buildingFloors.size()-1); // random number from 0 to (buildingFloors max -1) } else { while (randomDestinationFloor == randomStartFloor) // make sure starting != destination floor { randomDestinationFloor = rand() % buildingFloors.size(); } } buildingFloors[randomStartFloor]->addNewRider(Rider(*buildingFloors[randomDestinationFloor])); // add rider on rand floor w/ rand dest. } // end for (adding riders to random floors) //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ //@@@@@@@@@@@@@@@@@@@@@@ Elevator @@@@@@@@@@@@@@@@@@@@@@@@@@@ //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ for (int i = 0; i<buildingElevators.size(); i++) // checking all elevators one by one [3] { if (buildingElevators[i]->isDoorOpen() == false) // if doors are closed [3] { if (!buildingElevators[i]->isNearDestination()) // executed if elevator is not near destination [3] { if (buildingElevators[i]->isDirectionUp()) buildingElevators[i]->moveUp(); // move up [3] else if (buildingElevators[i]->isDirectionDown()) buildingElevators[i]->moveDown(); // move down [3] } // end 'if not near destination' loop else if (buildingElevators[i]->isNearDestination()) // if elevator is near to destination [4] { buildingElevators[i]->moveToDestinationFloor(); // [4] buildingElevators[i]->openDoor(); // [4] { buildingElevators[i]->removeRidersForDestinationFloor();// remove riders; ignore returned vector of removed riders [4] } // "{}" & cout are temp Floor* elevatorsCurrentFloor = const_cast<Floor*>(&(buildingElevators[i]->getDestination())); // [5] if (buildingElevators[i]->hasRiders() == false) // if elevator is empty [5] { if (elevatorsCurrentFloor->isPreferredDirectionUp() == true) // if floor direction = up [5] { buildingElevators[i]->setDirectionUp(); } else // if floor direction != up [5] { buildingElevators[i]->setDirectionDown(); } } // end if elevator = empty if (buildingElevators[i]->getAvailableSpace()!=0) // if space in elevator, board riders [6] { if (buildingElevators[i]->isDirectionUp() == true) // if the elevator is going up { if (elevatorsCurrentFloor->hasUpRiders() == true) // if there are up riders on the floor { buildingElevators[i]->addRiders(elevatorsCurrentFloor->removeUpRiders(buildingElevators[i]->getAvailableSpace())); // board up-riders [6] } // end if (has up riders) } // end if elevator direction == up else // for down elevators { if (elevatorsCurrentFloor->hasDownRiders() == true) // if there are down riders on the floor { buildingElevators[i]->addRiders(elevatorsCurrentFloor->removeDownRiders(buildingElevators[i]->getAvailableSpace()));// board down-riders [6] } // end if (has down riders) } // end else (for down riders) } // end if space in elevator buildingElevators[i]->setDestinationBasedOnRiders(); //reassess elevator's destination based on riders [8] } // end if 'near destination' loop } // end 'if elevator doors are closed' loop else // "isDoorOpen() == true" (it already let off riders, or is in its initial state) [7] { if (buildingElevators[i]->hasRiders() == true) // has riders [7] { buildingElevators[i]->closeDoor(); // close door [7] } // end if has riders else // if no riders { buildingElevators[i]->setIdle(); // go idle [7] } } // else (for already let off riders or is in initial state. } // end for loop checking all elevators //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ //@@@@@@@@@@@@@@@@@@@@@@ Floor @@@@@@@@@@@@@@@@@@@@@@@@@@@ //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ for (int i = 0; i<buildingFloors.size(); i++) // check each floor (for waiting riders) [2] { if (buildingFloors[i]->hasRidersWaiting() == true) // if there are no riders waiting on this floor, continue with next floor [2] { for (int j = 0; j<buildingElevators.size(); j++) // look at each elevator to see if it needs to be sent here [2] { int relativeLocation = ((buildingElevators[j]->getLocation()) - (buildingFloors[i]->getLocation())); // get elevator's relative location (negative if elevator is below floor) [2] if (buildingElevators[j]->isIdle() == true) // if this elevator's idle... [2] { if (relativeLocation > 0) // if elevator is above the floor... [2] { buildingElevators[j]->setDirectionDown(); // ... set elevator direction to down [2] } // end if (elevator > floor) else if (relativeLocation < 0) // otherwise if it's below... [2] { buildingElevators[j]->setDirectionUp(); // ...set elevator direction to up [2] } buildingElevators[j]->setDestination(buildingFloors[i]); // set elevator's destination to this floor [2] buildingElevators[j]->closeDoor(); // tell elevator to close its door [2] } // end if elevator.isIdle() else if (buildingFloors[i]->hasUpRiders() == true && buildingElevators[j]->isDirectionUp() == true) //there are riders on this floor waiting to go up, and the elevator is going up... [10] { // distance from elevator's destination floor to this floor (positive if elevator destination is above this floor) [10] int elevatorDistance = ( (buildingElevators[j]->getDestination().getLocation()) - (buildingFloors[i]->getLocation()) ); if ( (relativeLocation < 0) && (elevatorDistance > 0)) //elevator is below floor and elevator destination is above this floor [10] { buildingElevators[j]->setDestination(buildingFloors[i]); //set elevator's destination to this floor [10] } // end if (elevator is moving up past this floor) } // end else (riders on floor going up AND elevator going up) else if ( (buildingFloors[i]->hasDownRiders() == true) && (buildingElevators[j]->isDirectionDown() == true)) //there are riders on this floor waiting to go down, and the elevator is going down... [10] { // distance from elevator's destination floor to this floor (negative if elevator destination is below this floor) [10] int elevatorDistance = ( (buildingElevators[j]->getDestination().getLocation()) - (buildingFloors[i]->getLocation()) ); if ( (relativeLocation > 0) && (elevatorDistance < 0)) //elevator is above floor and elevator destination is below this floor [10] { buildingElevators[j]->setDestination(buildingFloors[i]); //set elevator's destination to this floor [10] } // end if (elevator is moving down past this floor) } // end else (riders on floor going down AND elevator going down) } // end for loop checking all elevators } // end if Floor has riders waiting loop } // end for loop for checking all Floors simulationTime ++; return *this; // should return a reference to itself. }
Building& Building::step(int max) { for(int i = 0; i < max; i++) { int topFloor = getFloorCount(); int firstFloor; // starting floor int int lastFloor; // ending floor int getDifferentInts(topFloor, firstFloor, lastFloor); // call getDifferentInts creates 2 random ints that are not equal to one another floors[firstFloor]->addNewRider(Rider(*floors[lastFloor])); // rider loop } // ELEVATOR ACTIONS [3] for(int i = 0; i < getElevatorCount(); i++){// tell each elevator to perform an action [3] if(getElevator(i).isDoorOpen() == false){ // if elevator door is closed if(getElevator(i).isNearDestination() == false){ // if not near enough to destination to reach it in this time step, continue moving [3] if(getElevator(i).isDirectionUp() == true){ // if elevator direction is up, move up [3] elevators[i]->moveUp(); } else{ // otherwise, move down [3] elevators[i]->moveDown(); } } else{ // otherwise it's near enough to destination to reach it in this time step... [4] elevators[i]->moveToDestinationFloor(); // tell elevator to move to its destination floor [4] elevators[i]->openDoor(); // tell elevator to open its door [4] elevators[i]->removeRidersForDestinationFloor(); // tell elevator to remove riders for its destination floor -- ignore returned vector of removed riders [4] // get a non-const pointer to the elevator's destination floor (using const_cast) [5] Floor* ncp; const_cast<const Floor*&>(ncp) = &getElevator(i).getDestination(); if(getElevator(i).getRiderCount() == 0){ // if elevator is empty //choose a direction based on longest-waiting rider (the one with the smallest ID) on the floor: [5] if(ncp->isPreferredDirectionUp() == true) // if the floor's chosen direction is up [5] elevators[i]->setDirectionUp(); // tell elevator to set its direction to up [5] else // otherwise [5] elevators[i]->setDirectionDown(); // tell elevator to set its direction to down [5] } if(getElevator(i).getAvailableSpace() > 0 && elevators[i]->isDoorOpen() == true){ // if there is space in the elevator after letting off riders // board new ones [6] if(getElevator(i).isDirectionUp() == true){ // if elevator direction is up, board up-riders (if any)... [6] elevators[i]->addRiders(ncp->removeUpRiders(elevators[i]->getAvailableSpace())); } else{ // otherwise, board down-riders (if any) [6] elevators[i]->addRiders(ncp->removeDownRiders(elevators[i]->getAvailableSpace())); } } elevators[i]->setDestinationBasedOnRiders(); // reassess elevator destination based on its riders [8] } } else{ // otherwise (then it already let off riders, or is in its initial state) [7] if(getElevator(i).hasRiders() == true){ // if elevator has any riders elevators[i]->closeDoor(); // tell elevator to close its door [7] } else{ elevators[i]->setIdle(); } } } // FLOOR ACTIONS [2] // check each floor (for waiting riders) [2] for(int i = 0; i < getFloorCount();i++){ if(getFloor(i).hasRidersWaiting() == false){ // if there are no riders waiting on this floor, continue with next floor [2] continue; } for(int j = 0; j < getElevatorCount(); j++){ int currentLoc = elevators[j]->getLocation() - floors[i]->getLocation(); // get elevator's relative location if(getElevator(j).isIdle() == true){ // look at each elevator to see if it needs to be sent here [2] if(currentLoc >= 0){ // elevator is above if positive elevators[j]->setDirectionDown(); // set elevator direction to down [2] elevators[j]->setDestination(floors[i]);// set elevator's destination to this floor [2] elevators[j]->closeDoor(); // tell elevator to close its door [2] } else{ elevators[j]->setDirectionUp();// set elevator direction to up [2] elevators[j]->setDestination(floors[i]); // set elevator's destination to this floor [2] elevators[j]->closeDoor(); // tell elevator to close its door [2] } } // else if there are riders on this floor waiting to go up, and the elevator is going up... [10] else if(getFloor(i).hasUpRiders() == true && getElevator(j).isDirectionUp() == true){ // get distance from elevator's destination floor to this floor (positive if elevator destination is above this floor) [10] double DistancetoFloor = getElevator(j).getDestination().getLocation() - getFloor(i).getLocation(); if (DistancetoFloor > 0 && currentLoc < 0){ // if elevator is below floor and elevator destination is above this floor [10] elevators[j]->setDestination(&(getFloor(i))); // set elevator's destination to this floor [10] } } else if(getFloor(i).hasUpRiders() == true && getElevator(j).isDirectionUp() == true){ // get distance from elevator's destination floor to this floor (negative if elevator destination is below this floor) [10] double DistancetoFloor = getElevator(j).getDestination().getLocation() - getFloor(i).getLocation(); if (DistancetoFloor < 0 && currentLoc > 0){ // if elevator is above floor and elevator destination is below this floor [10] elevators[j]->setDestination(&(getFloor(i))); // set elevator's destination to this floor [10] } } } } simulationTimer++; return *this; }