-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
63 lines (53 loc) · 2.55 KB
/
solution.js
File metadata and controls
63 lines (53 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{
init: function(elevators, floors) {
for (var floornumber = 0; floornumber < floors.length;floornumber++){
floors[floornumber].on("up_button_pressed", function() {
var elevator = elevators[0];
var queue = elevator.destinationQueue.length;
for (var i = 0 ; i < elevators.length; i++){
//looking for most suitable elevators (least busy)
if (isNaN(elevators[i].destinationQueue.length) || elevators[i].destinationQueue.length < queue){
elevator = elevators[i];
queue = elevators[i].destinationQueue.length;
}
}
elevator.goToFloor(this.floorNum());
});
floors[floornumber].on("down_button_pressed", function() {
var elevator = elevators[0];
var queue = elevator.destinationQueue.length;
for (var i = 0 ; i < elevators.length; i++){
//looking for the most suitable elevator (least busy)
if (isNaN(elevators[i].destinationQueue.length) ||elevators[i].destinationQueue.length < queue){
elevator = elevators[i];
queue = elevators[i].destinationQueue.length;
}
}
elevator.goToFloor(this.floorNum());
});
}
for (var elevatornumber = 0; elevatornumber < elevators.length;elevatornumber++){
//append the destination floor
elevators[elevatornumber].on("floor_button_pressed", function(floorNum) {
if (this.destinationQueue.indexOf(floorNum)<0)
this.goToFloor(floorNum);
});
//stop when passing by
elevators[elevatornumber].on("passing_floor", function(floorNum, direction) {
if (this.destinationQueue.indexOf(floorNum)>0)
this.goToFloor(floorNum, true)
});
//making sure there are not any unused stop
elevators[elevatornumber].on("stopped_at_floor", function(floorNum) {
for(var i = this.destinationQueue.length - 1; i >= 0; i--) {
if(this.destinationQueue[i] === floorNum) {
this.destinationQueue.splice(i, 1);
}
}
})
}
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here //
}
}