Subversion Repositories programming

Rev

Rev 321 | Rev 329 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 321 Rev 328
Line 4... Line 4...
4
Controller::Controller (int num_floors, int num_elevators) : num_floors (num_floors), num_elevators (num_elevators)
4
Controller::Controller (int num_floors, int num_elevators) : num_floors (num_floors), num_elevators (num_elevators)
5
{
5
{
6
    assert (num_floors > 2);
6
    assert (num_floors > 2);
7
    assert (num_elevators > 0);
7
    assert (num_elevators > 0);
8
 
8
 
9
    elevators = vector<Elevator> (num_elevators, Elevator (num_floors));
9
    elevators = std::vector<Elevator> (num_elevators, Elevator (num_floors));
10
}
10
}
11
 
11
 
12
Controller::~Controller ()
12
Controller::~Controller ()
13
{
13
{
14
}
14
}
15
 
15
 
16
void Controller::request_elevator (int on_floor)
16
void Controller::request_elevator (int on_floor)
17
{
17
{
-
 
18
    assert (on_floor <= num_floors);
-
 
19
    assert (on_floor > 0);
-
 
20
 
-
 
21
    float distance = INT_MAX;
-
 
22
    int elevator_number;
-
 
23
 
-
 
24
    // check that there is not already an elevator requested
-
 
25
    // to this floor. If there is already one, ignore this
-
 
26
    // request.
-
 
27
    if (floor_already_requested (on_floor))
-
 
28
        return;
-
 
29
 
-
 
30
    // find elevator that is closest, AND NOT moving away
-
 
31
    // "push" it's button
-
 
32
    elevator_number = find_closest_elevator (on_floor);
-
 
33
    elevators[elevator_number].push_button (on_floor);
-
 
34
}
-
 
35
 
-
 
36
/**
-
 
37
 * Check if there is an elevator already in the queue to stop
-
 
38
 * at the floor given as a parameter.
-
 
39
 */
-
 
40
bool Controller::floor_already_requested (int on_floor)
-
 
41
{
-
 
42
    int i;
-
 
43
 
-
 
44
    for (i=0; i<num_elevators; i++)
-
 
45
        if (elevators[i].button_is_pushed (on_floor))
-
 
46
            return true;
-
 
47
 
-
 
48
    return false;
-
 
49
}
-
 
50
 
-
 
51
/**
-
 
52
 * Return the number of the closest elevator to the
-
 
53
 * floor given as a parameter.
-
 
54
 *
-
 
55
 * We only want to choose elevators that are close and
-
 
56
 * heading in the right direction, or the closest elevator
-
 
57
 * if it is sitting idle.
-
 
58
 */
-
 
59
int Controller::find_closest_elevator (int to_floor)
-
 
60
{
-
 
61
    int i;
-
 
62
    float distance = INT_MAX;
-
 
63
    float temp_distance;
-
 
64
    int answer;
-
 
65
 
-
 
66
    float cur_floor;
-
 
67
    int direction;
-
 
68
 
-
 
69
    for (i=0; i<num_elevators; i++)
-
 
70
    {
-
 
71
        cur_floor = elevators[i].get_current_floor ();
-
 
72
        direction = elevators[i].get_direction ();
-
 
73
        temp_distance = fabsf (cur_floor - to_floor);
-
 
74
 
-
 
75
        // Check that this elevator is closer
-
 
76
        if (temp_distance < distance)
-
 
77
        {
-
 
78
            // If we are below the floor we want to go to and we
-
 
79
            // are moving up, then this is an ok choice
-
 
80
            if (cur_floor < to_floor && direction == MOVE_UP)
-
 
81
            {
-
 
82
                answer = i;
-
 
83
                distance = temp_distance;
-
 
84
            }
-
 
85
            // If we are above the floor we want to go to and we
-
 
86
            // are moving down, then this is an ok choice
-
 
87
            else if (cur_floor > to_floor && direction == MOVE_DOWN)
-
 
88
            {
-
 
89
                answer = i;
-
 
90
                distance = temp_distance;
-
 
91
            }
-
 
92
            // If we are closest, but idle, then this is an ok choice
-
 
93
            else
-
 
94
            {
-
 
95
                answer = i;
-
 
96
                distance = temp_distance;
-
 
97
            }
-
 
98
        }
-
 
99
    }
-
 
100
 
-
 
101
    return answer;
18
}
102
}
19
 
103