Skip to content

Instantly share code, notes, and snippets.

@zkendall
Last active February 8, 2023 14:12
Show Gist options
  • Save zkendall/a6633f256a454ebd46ddb6d0ad8fd4b6 to your computer and use it in GitHub Desktop.
Save zkendall/a6633f256a454ebd46ddb6d0ad8fd4b6 to your computer and use it in GitHub Desktop.
Elevator Saga - The elevator programming game - My Solutions
// My Reference Docs
// http://play.elevatorsaga.com/#challenge=5
// http://play.elevatorsaga.com/documentation.html#docs
// https://www.codecademy.com/articles/glossary-javascript
// Passes through level 4
// Sometimes passes 6
// Also passes: 8, 9, 16
{
init: function(elevators, floors) {
log = function() { return console.log.apply(console, arguments); };
function remove(arr, item) {
for(var i = arr.length; i--;) {
if(arr[i] === item) {
arr.splice(i, 1);
}
}
}
function updateIndicator(elevator) {
// Always "stopped"? Need to determine destination from destination queue?
if(elevator.destinationDirection() == "up") {
elevator.goingDownIndicator(false).goingUpIndicator(true);
} else if(elevator.destinationDirection() == "down") {
elevator.goingDownIndicator(true).goingUpIndicator(false);
} else {
// If stopped, will go anywhere.
elevator.goingDownIndicator(true).goingUpIndicator(true);
}
}
function leastBusy() {
var least = elevators[0];
for (var i = elevators.length - 1; i >= 0; i--) {
if(elevators[i].destinationDirection() == "stopped") {
least = elevators[i];
break;
}
var ll = least.destinationQueue ? 0 : least.destinationQueue.length;
var itL = elevators[i].destinationQueue ? 0 : elevators[i].destinationQueue.length
if(ll > itL) {
least = elevators[i].destinationQueue
}
}
return least;
}
// ELEVATOR EVENTS
elevators.forEach(function(elevator) {
elevator.on("idle", function() {
log("elevator-"+elevators.indexOf(elevator)+".idle()");
updateIndicator(elevator);
});
elevator.on("floor_button_pressed", function(floorNum) {
log("elevator-"+elevators.indexOf(elevator)+".floor_button_pressed()")
this.goToFloor(floorNum);
log("elevator-"+elevators.indexOf(elevator)+".destinationQueue="+this.destinationQueue);
});
elevator.on("passing_floor", function(floorNum, direction) {
var floor = floors[floorNum];
});
elevator.on("stopped_at_floor", function(floorNum) {
var queue = this.destinationQueue;
remove(queue, floorNum)
updateIndicator(elevator);
});
});
// FLOOR EVENTS
floors.forEach(function(floor) {
floor.on("up_button_pressed", function() {
log("floor-" + this.floorNum() + ".up_button_pressed()")
var elevator = leastBusy(elevators);
var queue = elevator.destinationQueue;
elevator.goToFloor(this.floorNum())
});
floor.on("down_button_pressed", function() {
log("floor-" + this.floorNum() + ".down_button_pressed()")
var elevator = leastBusy(elevators);
var queue = elevator.destinationQueue;
elevator.goToFloor(this.floorNum())
});
});
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
@audio-engineer
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment