Last active
July 9, 2025 18:37
-
-
Save danielvcorreia/fef746111ca1e811ca19968aef44bb70 to your computer and use it in GitHub Desktop.
A bad implementation of The elevator programming game that works while struggling on some levels.
This file contains hidden or 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) { | |
| const max_destinations = 2 | |
| const max_loadFactor = 0.5 | |
| for (let i = 0; i < elevators.length; i++) { | |
| elevators[i].on("idle", function() { | |
| for (let j = 0; j < floors.length; j++) { | |
| floors[j].on("down_button_pressed", function() { | |
| // check if there is any other elevator that is going to this floor and has a lower load factor | |
| for (let k = 0; k < elevators.length; k++) { | |
| if (elevators[i] != elevators[k]) { | |
| if (elevators[k].destinationQueue.includes(j) && elevators[k].loadFactor() < max_loadFactor) { | |
| // if so, return and do not assign this elevator | |
| return; | |
| } | |
| } | |
| } | |
| if (elevators[i].loadFactor() < max_loadFactor && elevators[i].destinationQueue.length < max_destinations) { | |
| // check if there is another elevator that is stopped and has no load | |
| for (let k = 0; k < elevators.length; k++) { | |
| if (elevators[k].destinationDirection == "stopped" && elevators[k].loadFactor() == 0) { | |
| elevators[k].goToFloor(floors[j].floorNum()); | |
| return; | |
| } | |
| } | |
| elevators[i].goToFloor(floors[j].floorNum()); | |
| } | |
| }) | |
| floors[j].on("up_button_pressed", function() { | |
| // check if there is any other elevator that is going to this floor and has a lower load factor | |
| for (let k = 0; k < elevators.length; k++) { | |
| if (elevators[i] != elevators[k]) { | |
| if (elevators[k].destinationQueue.includes(j) && elevators[k].loadFactor() < max_loadFactor) { | |
| // if so, return and do not assign this elevator | |
| return; | |
| } | |
| } | |
| } | |
| if (elevators[i].loadFactor() < max_loadFactor && elevators[i].destinationQueue.length < max_destinations) { | |
| // check if there is another elevator that is stopped and has no load | |
| for (let k = 0; k < elevators.length; k++) { | |
| if (elevators[k].destinationDirection == "stopped" && elevators[k].loadFactor() == 0) { | |
| elevators[k].goToFloor(floors[j].floorNum()); | |
| return; | |
| } | |
| } | |
| elevators[i].goToFloor(floors[j].floorNum()); | |
| } | |
| }) | |
| } | |
| }); | |
| elevators[i].on("passing_floor", function(floorNum, direction) { | |
| // if the floor is in the elevator's pressed floors list | |
| if (elevators[i].getPressedFloors().includes(floorNum)) { | |
| // move that floor to the front of the destination queue | |
| const index = elevators[i].destinationQueue.indexOf(floorNum); | |
| const t = elevators[i].destinationQueue.splice(index, 1); | |
| elevators[i].destinationQueue.unshift(t[0]); | |
| elevators[i].checkDestinationQueue(); | |
| } | |
| // if the floor is in the elevator's destination queue and the elevator's load factor is less than the maximum | |
| if (elevators[i].destinationQueue.includes(floorNum) && elevators[i].loadFactor() < max_loadFactor) { | |
| // move that floor to the front of the destination queue | |
| const index = elevators[i].destinationQueue.indexOf(floorNum); | |
| const t = elevators[i].destinationQueue.splice(index, 1); | |
| elevators[i].destinationQueue.unshift(t[0]); | |
| elevators[i].checkDestinationQueue(); | |
| } | |
| }); | |
| elevators[i].on("floor_button_pressed", function(floorNum) { | |
| elevators[i].goToFloor(floorNum); | |
| }); | |
| } | |
| }, | |
| update: function(dt, elevators, floors) { | |
| // We normally don't need to do anything here | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note there are two hyperparameters that need to be changed depending on the challenge.