First create a c header file called elev.h looking like this
#include "elev_app.h"
#include "msoftcon.h"
#include <iostream>
#include <iomanip>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
using namespace std;
enum direction { UP,DOWN, STOP };
//loading/unloading time (ticks)
const int LOAD_TIME = 3;
const int SPACING = 7;
const int BUF_LENGTH = 80;
class building; //forward declaration
class elevator
{
private:
building* ptrBuilding;
const int car_number;
int current_floor;
int old_floor;
direction current_dir;
bool destination[NUM_FLOORS];
int loading_timer;
int unloading_timer;
public:
elevator(building*,int);
void car_tick1();
void car_tick2();
void car_display();
void dests_display() const;
void decide();
void move();
void get_destinations();
int get_floor() const;
direction get_direction() const;
};
class building
{
private:
elevator* car_list[NUM_CARS];
int num_cars; //cars created so far
bool floor_request[2][NUM_FLOORS]; //False=UP True=Down
public:
building();
~building();
void master_tick(); //send tick to all cars
int get_cars_floor(const int) const;
direction get_cars_dir(const int) const;
bool get_floor_req(const int,const int);
void set_floor_req(const int,const int,const bool);
void record_floor_reqs(); //get floor request
void show_floor_reqs() const; //show fllor request
};
After you have created the elev.h now create elev.cpp the main executable file.#include "elev.h"
building::building()
{
//string for floors
char ustring[BUF_LENGTH];
//intialize graphics
init_graphics();
//clear screen
clear_screen();
num_cars = 0;
for(int k=0; k<NUM_CARS; k++)
{
car_list[k] = new elevator(this, num_cars);
num_cars++;
}
for(int j=0;mj<NUM_FLOORS; j++)
{
set_cursor_pos(3,NUM_FLOORS-j); //put floor number
itoa(j+1, ustring, 10);
cout << setw(3) << ustring;
floor_request[UP][j] = false; //no floor request yet
floor_request[DOWN][j] = false;
}
}
//end constructor
//start destructor
building::-building()
{
for(int k=0; k<NUM_CARS; k++)
delete car_list[k];
}
//master time tick
void building::master_tick()
{
int j;
show_floor_reqs();
for(j=0; j<NUM_CARS; j++)
car_list[j]->car_tick1();
for(j=0; j<NUM_CARS; j++)
car_list[j]->car_tick2();
}
//display floor request
void building::show_floor_reqs() const
{
for(int j=0; j<NUM_FLOORS; j++)
{
set_cursor_pos(SPACING, NUM_FLOORS-j);
if(floor_request[UP][j]==true)
cout << "\x1E"; //up arrow
else
cout << " ";
set_cursor_pos(SPACING+3, NUM_FLOORS-j);
if(floor_request[DOWN][j]==true)
cout << "\1xF";
else
cout << " ";
}
}
//get requets from riders outside the cars
void building::record_floor_reqs()
{
char ch = "x";
char ustring[BUF_LENGTH];
int iFloor;
char chDirection; //'u' or 'd' for up and down
set_cursor_pos(1,22);
cout << "Press [ENTER] to call an elevator:";
if(!kbhit()) // wait for keypress
return;
cin.ignore(10, "\n");
if(ch=="\x1B") //if escape key end the program
exit(0);
set_cursor_pos(1,22); clear_line(); //clear old text
set_cursor_pos(1,22); //bottom of screen
cout << "Enter the floor you're on:";
cin.get(ustring,BUF_LENGTH);
cin.ignore(10, "\n");
iFloor = atoi(ustring); //convert to integer
cout << "Ente the direction you want to go"(U or D):";
cin.get(chDirection); //(avoid multiple lines feeds)
cin.ignore(10,"\n");
if(chDirection=='u' || chDirection=='U')
floor_request[UP][iFloor-1] = true; //up floor request
if(cgDirection=='d' || chDirection=='D')
floor_request[DOWN][iFloor-1]=true; //down floor request
set_cursor_pos(1,22);
set_cursor_pos(1,23);
set_cursor_pos(1,24);
}
//see if there is a specific request
bool building::get_floor_req(const int dir,const int floor) const //check error on compiling here
{
return floor_request[dir][floor];
}
//set specific floor request
bool building::set_floor_req(const int dir, const int floor, const bool updown)
{
floor_request[dir][floor]=updown;
}
//---------------------------------------------
find where the carrier is in this case car
int building::get_cars_floor(const int carNo)const
{
return car_list[carNo]->get_floor();
}
//find which direction the car is moving
direction building::get_cars_dir(const int carNo) const
{
return car_list[carNo]->get_direction();
}
// function definitions for class elevator begins here
elevator::elevator(building* ptrB. int nc) :
ptrBuilding(ptrB),car_number(nc)
{
current_floor = 0; //start at 0
old_floor = 0;
current_dir = STOP;
for(int j=0; j<NUM_FLOOR; j++)
destination[j] = false;
loading_timer = 0; //no loading yet
unloading_timer = 0;
} //end constructor
//get current floor
int elevator::get_floor() const
{
return current_floor;
}
//get current direction
direction elevator::get_direction() const
{
return current_dir;
}
//tick for each car
void elevator::car_tick1()
{
car_display();
dests_display();
if(loading_timer)
__loading_timer;
if(unloading_timer)
__unloading_timer;
decide();
}
//All cars must decide before any of them move
void elevator::car_tick2()
{
move();
}
//display elevator image
void elevator::car_display()
{
set_cursor_pos(SPACING+(car_number+)*SPACING, NUM_FLOORS-old_floor);
cout << " "; //erase old position
set_cursor_pos(SPACING-1(car_number+)*SPACING, NUM_FLOORS-current_floor);
switch(loading_timer)
{
case 3:
cout << "\x01\xDB \xDB "; //draw a car with open door
break;
case 2:
cout << " \xDB \x01\xDB";
get_destinations();
break;
case 1:
cout << "\xDB\xDB\xDB "; //draw a car with closed door
break;
case 0;
cout << "\xDB\xDB\xDB"; //closed door
break;
}
set_cursor_pos(SPACING+(car_number+1)*SPACING,NUM_FLOORS-current_floor);
switch(unloading_timer)
{
case 3:
cout << "\xDB\x01\xDB";
break;
case 2:
cout << "\xDB\xDB\x01";
break;
case 1:
cout << "\xDB\xDB\xDB";
break;
case 0:
cout << "\xDB\xDB\xDB";
break;
}
//display destinations
void elevator::dests_display() const
{
for(int j=0; j<NUM_FLOORS; j++)
{
set_cursor_pos(SPACING-2+(car_number+1)*SPACING,NUM_FLOORS-j);
if(destination[j] == true)
cout << "\xFE'; //small box
else
cout << "";
}
}
//Decide what to do
void elevator::decide()
{
int j;
bool destins_above, destins_below; //destinations
bool requests_above, requests_below; //requests
int nearest_higher_req = 0;
int nearest_lower_req = 0;
bool car_between_up,car_between_dn;
bool car_opposite_up, car_opposite_dn;
int ofloor;
direction odir;
//Ensure we don't go too high or too low
if((current_floor==NUM_FLOORS-1 && current_dir==UP) || (current_floor==0 && current_dir==DOWN)
current_dir = STOP;
// if there a destination on this floor ,unload pasengers
if(destination[current_floor]==true)
{
destination[current_floor] = false;
if(!unloading_timer)
unloading_timer = LOAD_TIME;
return;
}
//check if there is an UP floor request on this floor
if((ptrBuilding->get_floor_req(UP, current_floor) && current_dir !=DOWN))
{
current_dir = UP;
ptrBuilding->set_floor_req(current_dir,current_floor,false);
//load
if(!loading_timer)
loading_time = LOAD_TIME;
return;
}
//check if there is an DOWN floor request on this floor
if((ptrBuilding->get_floor_req(DOWN, current_floor) && current_dir !=UP))
{
current_dir = DOWN;
ptrBuilding->set_floor_req(current_dir,current_floor,false);
//load
if(!loading_timer)
loading_time = LOAD_TIME;
return;
}
//check if there are other destinations or requests
destins_above = destins_below = false;
requests_above = requests_below = false;
for(j=current_floor+1; j<NUM_FLOORS; j++)
{
if(destination[j])
destins_above = true;
if(ptrBuilding->get_floor_req(UP,j) || ptrBuilding->get_floor_req(DOWN,j))
{
requests_above = true;
if(!nearest_higher_req)
nearest_higher_req - j;
}
}
//check fllors below
for(j=current_floor-1; j=>0; j--)
{
if(destination[j])
destins_below = true;
if(ptrBuilding->get_floor_req(UP,j) || ptrBuilding->get_floor_req(DOWN,j)
{
requests_below = true;
if(!nearest_lower_req)
nearest_lower_req = j;
}
}
//if no request or destinations DOWN or Up ,STOP the system
if(!destins_above && !requests_above && !destins_below && !requests_below)
{
current_dir = STOP;
return;
}
//Go toward destination if stopped or already moving
if(destins_above && (current_dir==STOP || current_dir==UP))
{
current_dir = UP;
return;
}
if(destins_below && (current_dir==STOP || current_dir==DN))
{
current_dir = DOWN;
return;
}
//Find if there cars going in the same direction between us and the nearest floor
car_between_up = car_between_down = false;
car_opposite_up = car_opposite_DOWN = false;
//check the car
for(j=0; j<NUM_CARS; j++)
{
if(j !=car_number)
{
ofloor = ptrBuilding->get_car_floors(j);
odir = ptrBuilding->get_cars_dir(j);
//if it going up and there is arequest above us
if((odir==UP || odir==STOP) && requests_above)
// if it above us and thebelow the nearest request
if((ofloor>current_floor && ofloor <= nearest higher req) || (ofloor==current_floor && j<car_number))
car_between_dn = true;
//if it going and there is a request above us
if((odir==UP || odir==STOP) && requests_below)
//it a below request and closer to it than we are
if(nearest_lower_req>=ofloor && nearest_low_req - ofloor<current_floor - nearest_lower_req)
car_opposite_up = true;
//if it going DOWN and there is arequest above us
if((odir==DOWN || odir==STOP) && requests_above)
// if it above us and thebelow the nearest request
if(ofloor >=nearest_higher_req && ofloor - nearest_higher_req<nearest_higher_req - current_floor)
car_opposite_down = true;
}
}
//if we are going down or stopped and there is a FR
//No other cars going down between us and the FR
if(current_dir==DOWN || current_dir==STOP) && requests_below && !car_between_down && !car_opposite_up)
{
current_dir = DOWN;
return;
}
//if nothing else happening stop
current_dir = STOP;
}
//....end decide() finally artificiall intelligence
//if loading or unloading
void elevator::move()
{
if(loading_timer || unloading_timer) //don't move
return;
if(current_dir==UP) //if it going up,go up
current_floor++;
}
//end move
//stop ,get destination
void elevator::get_destinations()
{
char ustring[BUF_LENGTH];
int dest_floor'
set_cursor_pos(1,22); clear_line();
set_cursor_pos(1,22)
cout << "Car" << (car_number+1) << "has stopped at floor" << (current_floor+1) << "\n Enter the destination floors (0 when finished)";
for(int j=1; j<NUM_FLOORS; j++)
{
set_cursor_pos(1,24);
cout << "Destination" << j << ":";
cin.get(ustring,BUF_LENGTH);
cin.ignore(10,"\n");
dest_floor = atoi(ustring);
set_cursor_pos(1,24);
if(dest_floor==0)
{
set_cursor_pos(1,22); clear_line();
set_cursor_pos(1,23); clear_line();
set_cursor_pos(1,24); clear_line();
return;
}
--dest_floor;
if(dest_floor==current_floor)
{ --j; continue; }
if(j==1 && current_dir==STOP)
current dir = (dest floor <current floor) ? DOWN : UP;
destination[dest_floor] = true;
dests_display();
}
}
//This the end of the code
Comments
Post a Comment