Skip to content

Instantly share code, notes, and snippets.

@francois
Created August 16, 2015 18:35
Show Gist options
  • Save francois/b4447e41ccd91b8ad56c to your computer and use it in GitHub Desktop.
Save francois/b4447e41ccd91b8ad56c to your computer and use it in GitHub Desktop.
These represent successive solutions to the [Elevator Saga](http://play.elevatorsaga.com/)
{
init: function(elevators, floors) {
var waitingFloors = [];
floors.forEach(function(floor) {
floor.on("up_button_pressed", function() {
waitingFloors.push(floor.floorNum());
});
floor.on("down_button_pressed", function() {
waitingFloors.push(floor.floorNum());
});
});
elevators.forEach(function(elevator) {
elevator.on("idle", function() {
if (elevator.getPressedFloors().length === 0) {
if (waitingFloors.length === 0) {
elevator.goToFloor(0);
} else {
var targetFloor = waitingFloors[0];
var newWaitingFloors = [];
waitingFloors.forEach(function(fl) { if(fl === targetFloor) {} else { newWaitingFloors.push(fl); } })
waitingFloors = newWaitingFloors;
elevator.goToFloor(targetFloor);
}
} else {
elevator.goToFloor(elevator.getPressedFloors()[0]);
}
});
});
},
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