Last active
July 8, 2020 06:45
-
-
Save AlexanderFadeev/7b8edf6440d0d5e28f573dce9d121aac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
init: function(elevators, floors) { | |
let tasks = []; | |
for (let index in floors) { | |
let floor = floors[index]; | |
floor.on("up_button_pressed", () => { | |
tasks.push({ | |
floor: floor.floorNum(), | |
dir: "up", | |
}); | |
}); | |
floor.on("down_button_pressed", () => { | |
tasks.push({ | |
floor: floor.floorNum(), | |
dir: "down", | |
}); | |
}) | |
} | |
for (let index in elevators) { | |
let elevator = elevators[index]; | |
elevator.dir = "up"; | |
elevator.pickup = function() { | |
if (tasks.length === 0) { | |
return; | |
} | |
elevator.goToFloor(tasks[0].floor); | |
tasks.splice(0, 1); | |
}; | |
elevator.on("idle", function() { | |
if (elevator.getPressedFloors().length == 0) { | |
elevator.pickup(); | |
return; | |
} | |
if (elevator.dir == "up") { | |
let next = 9999; | |
for (let floor of elevator.getPressedFloors()) { | |
if (floor < elevator.currentFloor()) { | |
continue; | |
} | |
next = Math.min(next, floor); | |
} | |
elevator.goToFloor(next); | |
return; | |
} else { | |
let next = -1; | |
for (let floor of elevator.getPressedFloors()) { | |
if (floor > elevator.currentFloor()) { | |
continue; | |
} | |
next = Math.max(next, floor); | |
} | |
elevator.goToFloor(next); | |
return; | |
} | |
}); | |
} | |
}, | |
update: function(dt, elevators, floors) { | |
// We normally don't need to do anything here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stuck at challenge 12