// Building.h
const int yposFloorNo[5] = { 21, 17, 13, 9, 5 };
#include "elevator.h"
#include "elevatorbutton.h"
#include "interface.h"
class Building : public ElevatorButton
{
public:
/* default constructor is used to display the building and all interfaces */
Building();
/* this algorithm will decide which elevator moves by accepting */
/* three arguments: position of first and second elevator and */
/* position of the destination */
int DecideElevator(int, int, int);
/* this member function will build two elevators and display them on screen */
void BuildElevator ();
/* this member function is the main executor of Elevator Simulation */
void ElevatorRun (Elevator, Elevator );
private:
int liftchoose;
};
Building :: Building ()
{
textbackground(4);
clrscr();
textattr(15 | 4*16);
gotoxy(2,4); cprintf("ÚÄÄÄÄ ÄÄÄÄÄÄÄÄÄÄÄÄÄ ÄÄÄÄ¿");
gotoxy(2,5); cprintf("³ ³");
gotoxy(2,6); cprintf("³ 5 ³");
gotoxy(2,7); cprintf("³ ³");
gotoxy(2,8); cprintf("³ÄÄ ÄÄÄÄÄÄÄÄÄ Äij");
gotoxy(2,9); cprintf("³ ³");
gotoxy(2,10); cprintf("³ 4 ³");
gotoxy(2,11); cprintf("³ ³");
gotoxy(2,12); cprintf("³ÄÄ ÄÄÄÄÄÄÄÄÄ Äij");
gotoxy(2,13); cprintf("³ ³");
gotoxy(2,14); cprintf("³ 3 ³");
gotoxy(2,15); cprintf("³ ³");
gotoxy(2,16); cprintf("³ÄÄ ÄÄÄÄÄÄÄÄÄ Äij");
gotoxy(2,17); cprintf("³ ³");
gotoxy(2,18); cprintf("³ 2 ³");
gotoxy(2,19); cprintf("³ ³");
gotoxy(2,20); cprintf("³ÄÄ ÄÄÄÄÄÄÄÄÄ Äij");
gotoxy(2,21); cprintf("³ ³");
gotoxy(2,22); cprintf("³ 1 ³");
gotoxy(2,23); cprintf("³ ³");
gotoxy(2,24); cprintf("ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ");
for(int i=1; i<26; ++i ) { gotoxy(45,i); cprintf("³"); }
DisplayIt();
}
void Building :: BuildElevator ()
{
Elevator One (5,21); // build elevator One
Elevator Two(19,21); // build elevator Two
ButtonCall(); // diplay Call menu
ElevatorRun(One, Two);
}
void Building :: ElevatorRun ( Elevator One, Elevator Two )
{
int no;
One.floorNo = 1;
Two.floorNo = 1;
while(1)
{
gotoxy(66,9); cout<< One.floorNo;
gotoxy(72,9); cout<< Two.floorNo;
no = ActivateButton();
liftchoose = DecideElevator( One.y, Two.y, yposFloorNo[no - 1] );
if (liftchoose == 1 ) // first elevator
{
One.movewhere ( yposFloorNo[no - 1] );
One.floorNo = no;
gotoxy(66,9); cout<< One.floorNo;
One.Enter(66);
no = DestinationButton ( yposFloorNo[no - 1] );
One.movewhere ( yposFloorNo[no - 1] );
One.floorNo = no;
gotoxy(66,9); cout<< One.floorNo;
One.Exit(66);
}
else // second elevator
{
Two.movewhere ( yposFloorNo[no - 1] );
Two.floorNo = no;
gotoxy(72,9); cout<< Two.floorNo;
Two.Enter(72);
no = DestinationButton ( yposFloorNo[no - 1] );
Two.movewhere ( yposFloorNo[no - 1] );
Two.floorNo = no;
gotoxy(72,9); cout<< Two.floorNo;
Two.Exit(72);
}
}
}
int Building :: DecideElevator (int y1, int y2, int ycall )
{
if ( abs(ycall - y1) < abs(ycall - y2) ) return 1; // first elevator
else return 2; // second elevator
}